123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558 |
- <?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\Platforms\OraclePlatform;
- use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
- use Doctrine\DBAL\Schema\Column;
- use Doctrine\DBAL\Schema\ForeignKeyConstraint;
- use Doctrine\DBAL\Schema\Index;
- use Doctrine\DBAL\Schema\Schema;
- use Doctrine\DBAL\Schema\Sequence;
- use Doctrine\DBAL\Schema\Table;
- use OC\DB\Connection;
- use OC\DB\MigrationService;
- use OC\DB\SchemaWrapper;
- use OCP\IDBConnection;
- use OCP\Migration\IMigrationStep;
- /**
- * 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');
- $wrappedSchema = $this->createMock(Schema::class);
- // TODO re-enable once stable14 is branched of: https://github.com/nextcloud/server/issues/10518
- /*$wrappedSchema->expects($this->once())
- ->method('getTables')
- ->willReturn([]);
- $wrappedSchema->expects($this->once())
- ->method('getSequences')
- ->willReturn([]);*/
- $schemaResult = $this->createMock(SchemaWrapper::class);
- $schemaResult->expects($this->once())
- ->method('getWrappedSchema')
- ->willReturn($wrappedSchema);
- $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();
- }
- public function testEnsureOracleIdentifierLengthLimitValid() {
- $column = $this->createMock(Column::class);
- $column->expects($this->once())
- ->method('getName')
- ->willReturn(\str_repeat('a', 30));
- $index = $this->createMock(Index::class);
- $index->expects($this->once())
- ->method('getName')
- ->willReturn(\str_repeat('a', 30));
- $foreignKey = $this->createMock(ForeignKeyConstraint::class);
- $foreignKey->expects($this->once())
- ->method('getName')
- ->willReturn(\str_repeat('a', 30));
- $table = $this->createMock(Table::class);
- $table->expects($this->once())
- ->method('getName')
- ->willReturn(\str_repeat('a', 30));
- $sequence = $this->createMock(Sequence::class);
- $sequence->expects($this->once())
- ->method('getName')
- ->willReturn(\str_repeat('a', 30));
- $table->expects($this->once())
- ->method('getColumns')
- ->willReturn([$column]);
- $table->expects($this->once())
- ->method('getIndexes')
- ->willReturn([$index]);
- $table->expects($this->once())
- ->method('getForeignKeys')
- ->willReturn([$foreignKey]);
- $table->expects($this->once())
- ->method('getPrimaryKey')
- ->willReturn(null);
- $schema = $this->createMock(Schema::class);
- $schema->expects($this->once())
- ->method('getTables')
- ->willReturn([$table]);
- $schema->expects($this->once())
- ->method('getSequences')
- ->willReturn([$sequence]);
- self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
- }
- public function testEnsureOracleIdentifierLengthLimitValidWithPrimaryKey() {
- $index = $this->createMock(Index::class);
- $index->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 30));
- $table = $this->createMock(Table::class);
- $table->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 26));
- $table->expects($this->once())
- ->method('getColumns')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getIndexes')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getForeignKeys')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getPrimaryKey')
- ->willReturn($index);
- $schema = $this->createMock(Schema::class);
- $schema->expects($this->once())
- ->method('getTables')
- ->willReturn([$table]);
- $schema->expects($this->once())
- ->method('getSequences')
- ->willReturn([]);
- self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
- }
- public function testEnsureOracleIdentifierLengthLimitValidWithPrimaryKeyDefault() {
- $defaultName = 'PRIMARY';
- if ($this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) {
- $defaultName = \str_repeat('a', 26) . '_' . \str_repeat('b', 30) . '_seq';
- } else if ($this->db->getDatabasePlatform() instanceof OraclePlatform) {
- $defaultName = \str_repeat('a', 26) . '_seq';
- }
- $index = $this->createMock(Index::class);
- $index->expects($this->any())
- ->method('getName')
- ->willReturn($defaultName);
- $index->expects($this->any())
- ->method('getColumns')
- ->willReturn([\str_repeat('b', 30)]);
- $table = $this->createMock(Table::class);
- $table->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 26));
- $table->expects($this->once())
- ->method('getColumns')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getIndexes')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getForeignKeys')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getPrimaryKey')
- ->willReturn($index);
- $schema = $this->createMock(Schema::class);
- $schema->expects($this->once())
- ->method('getTables')
- ->willReturn([$table]);
- $schema->expects($this->once())
- ->method('getSequences')
- ->willReturn([]);
- self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testEnsureOracleIdentifierLengthLimitTooLongTableName() {
- $table = $this->createMock(Table::class);
- $table->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 31));
- $schema = $this->createMock(Schema::class);
- $schema->expects($this->once())
- ->method('getTables')
- ->willReturn([$table]);
- self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testEnsureOracleIdentifierLengthLimitTooLongPrimaryWithDefault() {
- $defaultName = 'PRIMARY';
- if ($this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) {
- $defaultName = \str_repeat('a', 27) . '_' . \str_repeat('b', 30) . '_seq';
- } else if ($this->db->getDatabasePlatform() instanceof OraclePlatform) {
- $defaultName = \str_repeat('a', 27) . '_seq';
- }
- $index = $this->createMock(Index::class);
- $index->expects($this->any())
- ->method('getName')
- ->willReturn($defaultName);
- $index->expects($this->any())
- ->method('getColumns')
- ->willReturn([\str_repeat('b', 30)]);
- $table = $this->createMock(Table::class);
- $table->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 27));
- $table->expects($this->once())
- ->method('getColumns')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getIndexes')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getForeignKeys')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getPrimaryKey')
- ->willReturn($index);
- $schema = $this->createMock(Schema::class);
- $schema->expects($this->once())
- ->method('getTables')
- ->willReturn([$table]);
- self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testEnsureOracleIdentifierLengthLimitTooLongPrimaryWithName() {
- $index = $this->createMock(Index::class);
- $index->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 31));
- $table = $this->createMock(Table::class);
- $table->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 26));
- $table->expects($this->once())
- ->method('getColumns')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getIndexes')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getForeignKeys')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getPrimaryKey')
- ->willReturn($index);
- $schema = $this->createMock(Schema::class);
- $schema->expects($this->once())
- ->method('getTables')
- ->willReturn([$table]);
- self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testEnsureOracleIdentifierLengthLimitTooLongColumnName() {
- $column = $this->createMock(Column::class);
- $column->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 31));
- $table = $this->createMock(Table::class);
- $table->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 30));
- $table->expects($this->once())
- ->method('getColumns')
- ->willReturn([$column]);
- $schema = $this->createMock(Schema::class);
- $schema->expects($this->once())
- ->method('getTables')
- ->willReturn([$table]);
- self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testEnsureOracleIdentifierLengthLimitTooLongIndexName() {
- $index = $this->createMock(Index::class);
- $index->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 31));
- $table = $this->createMock(Table::class);
- $table->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 30));
- $table->expects($this->once())
- ->method('getColumns')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getIndexes')
- ->willReturn([$index]);
- $schema = $this->createMock(Schema::class);
- $schema->expects($this->once())
- ->method('getTables')
- ->willReturn([$table]);
- self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testEnsureOracleIdentifierLengthLimitTooLongForeignKeyName() {
- $foreignKey = $this->createMock(ForeignKeyConstraint::class);
- $foreignKey->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 31));
- $table = $this->createMock(Table::class);
- $table->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 30));
- $table->expects($this->once())
- ->method('getColumns')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getIndexes')
- ->willReturn([]);
- $table->expects($this->once())
- ->method('getForeignKeys')
- ->willReturn([$foreignKey]);
- $schema = $this->createMock(Schema::class);
- $schema->expects($this->once())
- ->method('getTables')
- ->willReturn([$table]);
- self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testEnsureOracleIdentifierLengthLimitTooLongSequenceName() {
- $sequence = $this->createMock(Sequence::class);
- $sequence->expects($this->any())
- ->method('getName')
- ->willReturn(\str_repeat('a', 31));
- $schema = $this->createMock(Schema::class);
- $schema->expects($this->once())
- ->method('getTables')
- ->willReturn([]);
- $schema->expects($this->once())
- ->method('getSequences')
- ->willReturn([$sequence]);
- self::invokePrivate($this->migrationService, 'ensureOracleIdentifierLengthLimit', [$schema, 3]);
- }
- }
|