diff options
author | Morris Jobke <hey@morrisjobke.de> | 2017-07-05 17:32:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-05 17:32:40 +0200 |
commit | 60398b919be713ddb1813956061fee638c54317a (patch) | |
tree | 483d8f03b2a2b2b11cfebefbf0c3eb3c4b408d8f /tests | |
parent | ca565644b36930b63bb996268e848460843f0433 (diff) | |
parent | 163e8774a40bf35a650b41f1af415f89397511a5 (diff) | |
download | nextcloud-server-60398b919be713ddb1813956061fee638c54317a.tar.gz nextcloud-server-60398b919be713ddb1813956061fee638c54317a.zip |
Merge pull request #5231 from nextcloud/migrations
Migrations
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/App/CodeChecker/CodeCheckerTest.php | 6 | ||||
-rw-r--r-- | tests/lib/App/CodeChecker/DeprecationCheckTest.php | 6 | ||||
-rw-r--r-- | tests/lib/App/CodeChecker/NodeVisitorTest.php | 3 | ||||
-rw-r--r-- | tests/lib/App/CodeChecker/StrongComparisonCheckTest.php | 6 | ||||
-rw-r--r-- | tests/lib/DB/MigrationsTest.php | 210 | ||||
-rw-r--r-- | tests/lib/InstallerTest.php | 6 |
6 files changed, 230 insertions, 7 deletions
diff --git a/tests/lib/App/CodeChecker/CodeCheckerTest.php b/tests/lib/App/CodeChecker/CodeCheckerTest.php index cdbb7c17da5..bb121eccc44 100644 --- a/tests/lib/App/CodeChecker/CodeCheckerTest.php +++ b/tests/lib/App/CodeChecker/CodeCheckerTest.php @@ -23,7 +23,8 @@ class CodeCheckerTest extends TestCase { */ public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { $checker = new CodeChecker( - new PrivateCheck(new EmptyCheck()) + new PrivateCheck(new EmptyCheck()), + false ); $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); @@ -49,7 +50,8 @@ class CodeCheckerTest extends TestCase { */ public function testPassValidUsage($fileToVerify) { $checker = new CodeChecker( - new PrivateCheck(new EmptyCheck()) + new PrivateCheck(new EmptyCheck()), + false ); $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); diff --git a/tests/lib/App/CodeChecker/DeprecationCheckTest.php b/tests/lib/App/CodeChecker/DeprecationCheckTest.php index ee69f075a61..1a16e7e4920 100644 --- a/tests/lib/App/CodeChecker/DeprecationCheckTest.php +++ b/tests/lib/App/CodeChecker/DeprecationCheckTest.php @@ -23,7 +23,8 @@ class DeprecationCheckTest extends TestCase { */ public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { $checker = new CodeChecker( - new DeprecationCheck(new EmptyCheck()) + new DeprecationCheck(new EmptyCheck()), + false ); $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); @@ -48,7 +49,8 @@ class DeprecationCheckTest extends TestCase { */ public function testPassValidUsage($fileToVerify) { $checker = new CodeChecker( - new DeprecationCheck(new EmptyCheck()) + new DeprecationCheck(new EmptyCheck()), + false ); $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); diff --git a/tests/lib/App/CodeChecker/NodeVisitorTest.php b/tests/lib/App/CodeChecker/NodeVisitorTest.php index ca0b6e9ccc1..d1b1f643677 100644 --- a/tests/lib/App/CodeChecker/NodeVisitorTest.php +++ b/tests/lib/App/CodeChecker/NodeVisitorTest.php @@ -59,7 +59,8 @@ class NodeVisitorTest extends TestCase { */ public function testMethodsToCheck($expectedErrors, $fileToVerify) { $checker = new CodeChecker( - new TestList(new EmptyCheck()) + new TestList(new EmptyCheck()), + false ); $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); diff --git a/tests/lib/App/CodeChecker/StrongComparisonCheckTest.php b/tests/lib/App/CodeChecker/StrongComparisonCheckTest.php index c73eae286ab..6d3d2f50efd 100644 --- a/tests/lib/App/CodeChecker/StrongComparisonCheckTest.php +++ b/tests/lib/App/CodeChecker/StrongComparisonCheckTest.php @@ -23,7 +23,8 @@ class StrongComparisonCheckTest extends TestCase { */ public function testFindInvalidUsage($expectedErrorToken, $expectedErrorCode, $fileToVerify) { $checker = new CodeChecker( - new StrongComparisonCheck(new EmptyCheck()) + new StrongComparisonCheck(new EmptyCheck()), + false ); $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); @@ -45,7 +46,8 @@ class StrongComparisonCheckTest extends TestCase { */ public function testPassValidUsage($fileToVerify) { $checker = new CodeChecker( - new StrongComparisonCheck(new EmptyCheck()) + new StrongComparisonCheck(new EmptyCheck()), + false ); $errors = $checker->analyseFile(\OC::$SERVERROOT . "/tests/data/app/code-checker/$fileToVerify"); diff --git a/tests/lib/DB/MigrationsTest.php b/tests/lib/DB/MigrationsTest.php new file mode 100644 index 00000000000..9c06fe4cec5 --- /dev/null +++ b/tests/lib/DB/MigrationsTest.php @@ -0,0 +1,210 @@ +<?php + +/** + * Copyright (c) 2016 Thomas Müller <thomas.mueller@tmit.eu> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + + +namespace Test\DB; + +use Doctrine\DBAL\Schema\Schema; +use OC\DB\Connection; +use OC\DB\MigrationService; +use OC\DB\SchemaWrapper; +use OCP\IDBConnection; +use OCP\Migration\IMigrationStep; +use OCP\Migration\ISchemaMigration; +use OCP\Migration\ISqlMigration; + +/** + * Class MigrationsTest + * + * @package Test\DB + */ +class MigrationsTest extends \Test\TestCase { + + /** @var MigrationService | \PHPUnit_Framework_MockObject_MockObject */ + private $migrationService; + /** @var \PHPUnit_Framework_MockObject_MockObject | IDBConnection $db */ + private $db; + + public function setUp() { + parent::setUp(); + + $this->db = $this->createMock(Connection::class); + $this->db->expects($this->any())->method('getPrefix')->willReturn('test_oc_'); + $this->migrationService = new MigrationService('testing', $this->db); + } + + public function testGetters() { + $this->assertEquals('testing', $this->migrationService->getApp()); + $this->assertEquals(\OC::$SERVERROOT . '/apps/testing/lib/Migration', $this->migrationService->getMigrationsDirectory()); + $this->assertEquals('OCA\Testing\Migration', $this->migrationService->getMigrationsNamespace()); + $this->assertEquals('test_oc_migrations', $this->migrationService->getMigrationsTableName()); + } + + public function testCore() { + $this->migrationService = new MigrationService('core', $this->db); + + $this->assertEquals('core', $this->migrationService->getApp()); + $this->assertEquals(\OC::$SERVERROOT . '/core/Migrations', $this->migrationService->getMigrationsDirectory()); + $this->assertEquals('OC\Core\Migrations', $this->migrationService->getMigrationsNamespace()); + $this->assertEquals('test_oc_migrations', $this->migrationService->getMigrationsTableName()); + } + + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Version 20170130180000 is unknown. + */ + public function testExecuteUnknownStep() { + $this->migrationService->executeStep('20170130180000'); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage App not found + */ + public function testUnknownApp() { + $migrationService = new MigrationService('unknown-bloody-app', $this->db); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Migration step 'X' is unknown + */ + public function testExecuteStepWithUnknownClass() { + $this->migrationService = $this->getMockBuilder(MigrationService::class) + ->setMethods(['findMigrations']) + ->setConstructorArgs(['testing', $this->db]) + ->getMock(); + $this->migrationService->expects($this->any())->method('findMigrations')->willReturn( + ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A'] + ); + $this->migrationService->executeStep('20170130180000'); + } + + public function testExecuteStepWithSchemaChange() { + + $schema = $this->createMock(Schema::class); + $this->db->expects($this->any()) + ->method('createSchema') + ->willReturn($schema); + + $this->db->expects($this->once()) + ->method('migrateToSchema'); + + $schemaResult = $this->createMock(SchemaWrapper::class); + $schemaResult->expects($this->once()) + ->method('getWrappedSchema') + ->willReturn($this->createMock(Schema::class)); + + $step = $this->createMock(IMigrationStep::class); + $step->expects($this->at(0)) + ->method('preSchemaChange'); + $step->expects($this->at(1)) + ->method('changeSchema') + ->willReturn($schemaResult); + $step->expects($this->at(2)) + ->method('postSchemaChange'); + + $this->migrationService = $this->getMockBuilder(MigrationService::class) + ->setMethods(['createInstance']) + ->setConstructorArgs(['testing', $this->db]) + ->getMock(); + + $this->migrationService->expects($this->any()) + ->method('createInstance') + ->with('20170130180000') + ->willReturn($step); + $this->migrationService->executeStep('20170130180000'); + } + + public function testExecuteStepWithoutSchemaChange() { + + $schema = $this->createMock(Schema::class); + $this->db->expects($this->any()) + ->method('createSchema') + ->willReturn($schema); + + $this->db->expects($this->never()) + ->method('migrateToSchema'); + + $step = $this->createMock(IMigrationStep::class); + $step->expects($this->at(0)) + ->method('preSchemaChange'); + $step->expects($this->at(1)) + ->method('changeSchema') + ->willReturn(null); + $step->expects($this->at(2)) + ->method('postSchemaChange'); + + $this->migrationService = $this->getMockBuilder(MigrationService::class) + ->setMethods(['createInstance']) + ->setConstructorArgs(['testing', $this->db]) + ->getMock(); + + $this->migrationService->expects($this->any()) + ->method('createInstance') + ->with('20170130180000') + ->willReturn($step); + $this->migrationService->executeStep('20170130180000'); + } + + public function dataGetMigration() { + return [ + ['current', '20170130180001'], + ['prev', '20170130180000'], + ['next', '20170130180002'], + ['latest', '20170130180003'], + ]; + } + + /** + * @dataProvider dataGetMigration + * @param string $alias + * @param string $expected + */ + public function testGetMigration($alias, $expected) { + $this->migrationService = $this->getMockBuilder(MigrationService::class) + ->setMethods(['getMigratedVersions', 'findMigrations']) + ->setConstructorArgs(['testing', $this->db]) + ->getMock(); + $this->migrationService->expects($this->any())->method('getMigratedVersions')->willReturn( + ['20170130180000', '20170130180001'] + ); + $this->migrationService->expects($this->any())->method('findMigrations')->willReturn( + ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A'] + ); + + $this->assertEquals( + ['20170130180000', '20170130180001', '20170130180002', '20170130180003'], + $this->migrationService->getAvailableVersions()); + + $migration = $this->migrationService->getMigration($alias); + $this->assertEquals($expected, $migration); + } + + public function testMigrate() { + $this->migrationService = $this->getMockBuilder(MigrationService::class) + ->setMethods(['getMigratedVersions', 'findMigrations', 'executeStep']) + ->setConstructorArgs(['testing', $this->db]) + ->getMock(); + $this->migrationService->expects($this->any())->method('getMigratedVersions')->willReturn( + ['20170130180000', '20170130180001'] + ); + $this->migrationService->expects($this->any())->method('findMigrations')->willReturn( + ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A'] + ); + + $this->assertEquals( + ['20170130180000', '20170130180001', '20170130180002', '20170130180003'], + $this->migrationService->getAvailableVersions()); + + $this->migrationService->expects($this->exactly(2))->method('executeStep') + ->withConsecutive(['20170130180002'], ['20170130180003']); + $this->migrationService->migrate(); + } +} diff --git a/tests/lib/InstallerTest.php b/tests/lib/InstallerTest.php index a31c8826bd9..107b9dcb41f 100644 --- a/tests/lib/InstallerTest.php +++ b/tests/lib/InstallerTest.php @@ -19,6 +19,12 @@ use OCP\IConfig; use OCP\ILogger; use OCP\ITempManager; +/** + * Class InstallerTest + * + * @package Test + * @group DB + */ class InstallerTest extends TestCase { private static $appid = 'testapp'; |