MigrationsTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. <?php
  2. /**
  3. * Copyright (c) 2016 Thomas Müller <thomas.mueller@tmit.eu>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\DB;
  9. use Doctrine\DBAL\Platforms\OraclePlatform;
  10. use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
  11. use Doctrine\DBAL\Schema\Column;
  12. use Doctrine\DBAL\Schema\ForeignKeyConstraint;
  13. use Doctrine\DBAL\Schema\Index;
  14. use Doctrine\DBAL\Schema\Schema;
  15. use Doctrine\DBAL\Schema\SchemaException;
  16. use Doctrine\DBAL\Schema\Sequence;
  17. use Doctrine\DBAL\Schema\Table;
  18. use Doctrine\DBAL\Types\Type;
  19. use OC\DB\Connection;
  20. use OC\DB\MigrationService;
  21. use OC\DB\SchemaWrapper;
  22. use OCP\IDBConnection;
  23. use OCP\Migration\IMigrationStep;
  24. /**
  25. * Class MigrationsTest
  26. *
  27. * @package Test\DB
  28. */
  29. class MigrationsTest extends \Test\TestCase {
  30. /** @var MigrationService | \PHPUnit\Framework\MockObject\MockObject */
  31. private $migrationService;
  32. /** @var \PHPUnit\Framework\MockObject\MockObject | IDBConnection $db */
  33. private $db;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->db = $this->createMock(Connection::class);
  37. $this->db->expects($this->any())->method('getPrefix')->willReturn('test_oc_');
  38. $this->migrationService = new MigrationService('testing', $this->db);
  39. }
  40. public function testGetters() {
  41. $this->assertEquals('testing', $this->migrationService->getApp());
  42. $this->assertEquals(\OC::$SERVERROOT . '/apps/testing/lib/Migration', $this->migrationService->getMigrationsDirectory());
  43. $this->assertEquals('OCA\Testing\Migration', $this->migrationService->getMigrationsNamespace());
  44. $this->assertEquals('test_oc_migrations', $this->migrationService->getMigrationsTableName());
  45. }
  46. public function testCore() {
  47. $this->migrationService = new MigrationService('core', $this->db);
  48. $this->assertEquals('core', $this->migrationService->getApp());
  49. $this->assertEquals(\OC::$SERVERROOT . '/core/Migrations', $this->migrationService->getMigrationsDirectory());
  50. $this->assertEquals('OC\Core\Migrations', $this->migrationService->getMigrationsNamespace());
  51. $this->assertEquals('test_oc_migrations', $this->migrationService->getMigrationsTableName());
  52. }
  53. public function testExecuteUnknownStep() {
  54. $this->expectException(\InvalidArgumentException::class);
  55. $this->expectExceptionMessage('Version 20170130180000 is unknown.');
  56. $this->migrationService->executeStep('20170130180000');
  57. }
  58. public function testUnknownApp() {
  59. $this->expectException(\Exception::class);
  60. $this->expectExceptionMessage('App not found');
  61. $migrationService = new MigrationService('unknown-bloody-app', $this->db);
  62. }
  63. public function testExecuteStepWithUnknownClass() {
  64. $this->expectException(\Exception::class);
  65. $this->expectExceptionMessage('Migration step \'X\' is unknown');
  66. $this->migrationService = $this->getMockBuilder(MigrationService::class)
  67. ->setMethods(['findMigrations'])
  68. ->setConstructorArgs(['testing', $this->db])
  69. ->getMock();
  70. $this->migrationService->expects($this->any())->method('findMigrations')->willReturn(
  71. ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A']
  72. );
  73. $this->migrationService->executeStep('20170130180000');
  74. }
  75. public function testExecuteStepWithSchemaChange() {
  76. $schema = $this->createMock(Schema::class);
  77. $this->db->expects($this->any())
  78. ->method('createSchema')
  79. ->willReturn($schema);
  80. $this->db->expects($this->once())
  81. ->method('migrateToSchema');
  82. $wrappedSchema = $this->createMock(Schema::class);
  83. $wrappedSchema->expects($this->once())
  84. ->method('getTables')
  85. ->willReturn([]);
  86. $wrappedSchema->expects($this->once())
  87. ->method('getSequences')
  88. ->willReturn([]);
  89. $schemaResult = $this->createMock(SchemaWrapper::class);
  90. $schemaResult->expects($this->once())
  91. ->method('getWrappedSchema')
  92. ->willReturn($wrappedSchema);
  93. $step = $this->createMock(IMigrationStep::class);
  94. $step->expects($this->once())
  95. ->method('preSchemaChange');
  96. $step->expects($this->once())
  97. ->method('changeSchema')
  98. ->willReturn($schemaResult);
  99. $step->expects($this->once())
  100. ->method('postSchemaChange');
  101. $this->migrationService = $this->getMockBuilder(MigrationService::class)
  102. ->setMethods(['createInstance'])
  103. ->setConstructorArgs(['testing', $this->db])
  104. ->getMock();
  105. $this->migrationService->expects($this->any())
  106. ->method('createInstance')
  107. ->with('20170130180000')
  108. ->willReturn($step);
  109. $this->migrationService->executeStep('20170130180000');
  110. }
  111. public function testExecuteStepWithoutSchemaChange() {
  112. $schema = $this->createMock(Schema::class);
  113. $this->db->expects($this->any())
  114. ->method('createSchema')
  115. ->willReturn($schema);
  116. $this->db->expects($this->never())
  117. ->method('migrateToSchema');
  118. $step = $this->createMock(IMigrationStep::class);
  119. $step->expects($this->once())
  120. ->method('preSchemaChange');
  121. $step->expects($this->once())
  122. ->method('changeSchema')
  123. ->willReturn(null);
  124. $step->expects($this->once())
  125. ->method('postSchemaChange');
  126. $this->migrationService = $this->getMockBuilder(MigrationService::class)
  127. ->setMethods(['createInstance'])
  128. ->setConstructorArgs(['testing', $this->db])
  129. ->getMock();
  130. $this->migrationService->expects($this->any())
  131. ->method('createInstance')
  132. ->with('20170130180000')
  133. ->willReturn($step);
  134. $this->migrationService->executeStep('20170130180000');
  135. }
  136. public function dataGetMigration() {
  137. return [
  138. ['current', '20170130180001'],
  139. ['prev', '20170130180000'],
  140. ['next', '20170130180002'],
  141. ['latest', '20170130180003'],
  142. ];
  143. }
  144. /**
  145. * @dataProvider dataGetMigration
  146. * @param string $alias
  147. * @param string $expected
  148. */
  149. public function testGetMigration($alias, $expected) {
  150. $this->migrationService = $this->getMockBuilder(MigrationService::class)
  151. ->setMethods(['getMigratedVersions', 'findMigrations'])
  152. ->setConstructorArgs(['testing', $this->db])
  153. ->getMock();
  154. $this->migrationService->expects($this->any())->method('getMigratedVersions')->willReturn(
  155. ['20170130180000', '20170130180001']
  156. );
  157. $this->migrationService->expects($this->any())->method('findMigrations')->willReturn(
  158. ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A']
  159. );
  160. $this->assertEquals(
  161. ['20170130180000', '20170130180001', '20170130180002', '20170130180003'],
  162. $this->migrationService->getAvailableVersions());
  163. $migration = $this->migrationService->getMigration($alias);
  164. $this->assertEquals($expected, $migration);
  165. }
  166. public function testMigrate() {
  167. $this->migrationService = $this->getMockBuilder(MigrationService::class)
  168. ->setMethods(['getMigratedVersions', 'findMigrations', 'executeStep'])
  169. ->setConstructorArgs(['testing', $this->db])
  170. ->getMock();
  171. $this->migrationService->expects($this->any())->method('getMigratedVersions')->willReturn(
  172. ['20170130180000', '20170130180001']
  173. );
  174. $this->migrationService->expects($this->any())->method('findMigrations')->willReturn(
  175. ['20170130180000' => 'X', '20170130180001' => 'Y', '20170130180002' => 'Z', '20170130180003' => 'A']
  176. );
  177. $this->assertEquals(
  178. ['20170130180000', '20170130180001', '20170130180002', '20170130180003'],
  179. $this->migrationService->getAvailableVersions());
  180. $this->migrationService->expects($this->exactly(2))->method('executeStep')
  181. ->withConsecutive(['20170130180002'], ['20170130180003']);
  182. $this->migrationService->migrate();
  183. }
  184. public function testEnsureOracleConstraintsValid() {
  185. $column = $this->createMock(Column::class);
  186. $column->expects($this->once())
  187. ->method('getName')
  188. ->willReturn(\str_repeat('a', 30));
  189. $index = $this->createMock(Index::class);
  190. $index->expects($this->once())
  191. ->method('getName')
  192. ->willReturn(\str_repeat('a', 30));
  193. $foreignKey = $this->createMock(ForeignKeyConstraint::class);
  194. $foreignKey->expects($this->once())
  195. ->method('getName')
  196. ->willReturn(\str_repeat('a', 30));
  197. $table = $this->createMock(Table::class);
  198. $table->expects($this->atLeastOnce())
  199. ->method('getName')
  200. ->willReturn(\str_repeat('a', 30));
  201. $sequence = $this->createMock(Sequence::class);
  202. $sequence->expects($this->atLeastOnce())
  203. ->method('getName')
  204. ->willReturn(\str_repeat('a', 30));
  205. $primaryKey = $this->createMock(Index::class);
  206. $primaryKey->expects($this->once())
  207. ->method('getName')
  208. ->willReturn(\str_repeat('a', 30));
  209. $table->expects($this->once())
  210. ->method('getColumns')
  211. ->willReturn([$column]);
  212. $table->expects($this->once())
  213. ->method('getIndexes')
  214. ->willReturn([$index]);
  215. $table->expects($this->once())
  216. ->method('getForeignKeys')
  217. ->willReturn([$foreignKey]);
  218. $table->expects($this->once())
  219. ->method('getPrimaryKey')
  220. ->willReturn($primaryKey);
  221. $schema = $this->createMock(Schema::class);
  222. $schema->expects($this->once())
  223. ->method('getTables')
  224. ->willReturn([$table]);
  225. $schema->expects($this->once())
  226. ->method('getSequences')
  227. ->willReturn([$sequence]);
  228. $sourceSchema = $this->createMock(Schema::class);
  229. $sourceSchema->expects($this->any())
  230. ->method('getTable')
  231. ->willThrowException(new SchemaException());
  232. $sourceSchema->expects($this->any())
  233. ->method('hasSequence')
  234. ->willReturn(false);
  235. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  236. }
  237. public function testEnsureOracleConstraintsValidWithPrimaryKey() {
  238. $index = $this->createMock(Index::class);
  239. $index->expects($this->any())
  240. ->method('getName')
  241. ->willReturn(\str_repeat('a', 30));
  242. $table = $this->createMock(Table::class);
  243. $table->expects($this->any())
  244. ->method('getName')
  245. ->willReturn(\str_repeat('a', 26));
  246. $table->expects($this->once())
  247. ->method('getColumns')
  248. ->willReturn([]);
  249. $table->expects($this->once())
  250. ->method('getIndexes')
  251. ->willReturn([]);
  252. $table->expects($this->once())
  253. ->method('getForeignKeys')
  254. ->willReturn([]);
  255. $table->expects($this->once())
  256. ->method('getPrimaryKey')
  257. ->willReturn($index);
  258. $schema = $this->createMock(Schema::class);
  259. $schema->expects($this->once())
  260. ->method('getTables')
  261. ->willReturn([$table]);
  262. $schema->expects($this->once())
  263. ->method('getSequences')
  264. ->willReturn([]);
  265. $sourceSchema = $this->createMock(Schema::class);
  266. $sourceSchema->expects($this->any())
  267. ->method('getTable')
  268. ->willThrowException(new SchemaException());
  269. $sourceSchema->expects($this->any())
  270. ->method('hasSequence')
  271. ->willReturn(false);
  272. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  273. }
  274. public function testEnsureOracleConstraintsValidWithPrimaryKeyDefault() {
  275. $defaultName = 'PRIMARY';
  276. if ($this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) {
  277. $defaultName = \str_repeat('a', 26) . '_' . \str_repeat('b', 30) . '_seq';
  278. } elseif ($this->db->getDatabasePlatform() instanceof OraclePlatform) {
  279. $defaultName = \str_repeat('a', 26) . '_seq';
  280. }
  281. $index = $this->createMock(Index::class);
  282. $index->expects($this->any())
  283. ->method('getName')
  284. ->willReturn($defaultName);
  285. $index->expects($this->any())
  286. ->method('getColumns')
  287. ->willReturn([\str_repeat('b', 30)]);
  288. $table = $this->createMock(Table::class);
  289. $table->expects($this->any())
  290. ->method('getName')
  291. ->willReturn(\str_repeat('a', 25));
  292. $table->expects($this->once())
  293. ->method('getColumns')
  294. ->willReturn([]);
  295. $table->expects($this->once())
  296. ->method('getIndexes')
  297. ->willReturn([]);
  298. $table->expects($this->once())
  299. ->method('getForeignKeys')
  300. ->willReturn([]);
  301. $table->expects($this->once())
  302. ->method('getPrimaryKey')
  303. ->willReturn($index);
  304. $schema = $this->createMock(Schema::class);
  305. $schema->expects($this->once())
  306. ->method('getTables')
  307. ->willReturn([$table]);
  308. $schema->expects($this->once())
  309. ->method('getSequences')
  310. ->willReturn([]);
  311. $sourceSchema = $this->createMock(Schema::class);
  312. $sourceSchema->expects($this->any())
  313. ->method('getTable')
  314. ->willThrowException(new SchemaException());
  315. $sourceSchema->expects($this->any())
  316. ->method('hasSequence')
  317. ->willReturn(false);
  318. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  319. }
  320. public function testEnsureOracleConstraintsTooLongTableName() {
  321. $this->expectException(\InvalidArgumentException::class);
  322. $table = $this->createMock(Table::class);
  323. $table->expects($this->any())
  324. ->method('getName')
  325. ->willReturn(\str_repeat('a', 31));
  326. $schema = $this->createMock(Schema::class);
  327. $schema->expects($this->once())
  328. ->method('getTables')
  329. ->willReturn([$table]);
  330. $sourceSchema = $this->createMock(Schema::class);
  331. $sourceSchema->expects($this->any())
  332. ->method('getTable')
  333. ->willThrowException(new SchemaException());
  334. $sourceSchema->expects($this->any())
  335. ->method('hasSequence')
  336. ->willReturn(false);
  337. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  338. }
  339. public function testEnsureOracleConstraintsTooLongPrimaryWithDefault() {
  340. $this->expectException(\InvalidArgumentException::class);
  341. $defaultName = 'PRIMARY';
  342. if ($this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) {
  343. $defaultName = \str_repeat('a', 27) . '_' . \str_repeat('b', 30) . '_seq';
  344. } elseif ($this->db->getDatabasePlatform() instanceof OraclePlatform) {
  345. $defaultName = \str_repeat('a', 27) . '_seq';
  346. }
  347. $index = $this->createMock(Index::class);
  348. $index->expects($this->any())
  349. ->method('getName')
  350. ->willReturn($defaultName);
  351. $index->expects($this->any())
  352. ->method('getColumns')
  353. ->willReturn([\str_repeat('b', 30)]);
  354. $table = $this->createMock(Table::class);
  355. $table->expects($this->any())
  356. ->method('getName')
  357. ->willReturn(\str_repeat('a', 27));
  358. $table->expects($this->once())
  359. ->method('getColumns')
  360. ->willReturn([]);
  361. $table->expects($this->once())
  362. ->method('getIndexes')
  363. ->willReturn([]);
  364. $table->expects($this->once())
  365. ->method('getForeignKeys')
  366. ->willReturn([]);
  367. $table->expects($this->once())
  368. ->method('getPrimaryKey')
  369. ->willReturn($index);
  370. $schema = $this->createMock(Schema::class);
  371. $schema->expects($this->once())
  372. ->method('getTables')
  373. ->willReturn([$table]);
  374. $sourceSchema = $this->createMock(Schema::class);
  375. $sourceSchema->expects($this->any())
  376. ->method('getTable')
  377. ->willThrowException(new SchemaException());
  378. $sourceSchema->expects($this->any())
  379. ->method('hasSequence')
  380. ->willReturn(false);
  381. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  382. }
  383. public function testEnsureOracleConstraintsTooLongPrimaryWithName() {
  384. $this->expectException(\InvalidArgumentException::class);
  385. $index = $this->createMock(Index::class);
  386. $index->expects($this->any())
  387. ->method('getName')
  388. ->willReturn(\str_repeat('a', 31));
  389. $table = $this->createMock(Table::class);
  390. $table->expects($this->any())
  391. ->method('getName')
  392. ->willReturn(\str_repeat('a', 26));
  393. $table->expects($this->once())
  394. ->method('getColumns')
  395. ->willReturn([]);
  396. $table->expects($this->once())
  397. ->method('getIndexes')
  398. ->willReturn([]);
  399. $table->expects($this->once())
  400. ->method('getForeignKeys')
  401. ->willReturn([]);
  402. $table->expects($this->once())
  403. ->method('getPrimaryKey')
  404. ->willReturn($index);
  405. $schema = $this->createMock(Schema::class);
  406. $schema->expects($this->once())
  407. ->method('getTables')
  408. ->willReturn([$table]);
  409. $sourceSchema = $this->createMock(Schema::class);
  410. $sourceSchema->expects($this->any())
  411. ->method('getTable')
  412. ->willThrowException(new SchemaException());
  413. $sourceSchema->expects($this->any())
  414. ->method('hasSequence')
  415. ->willReturn(false);
  416. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  417. }
  418. public function testEnsureOracleConstraintsTooLongColumnName() {
  419. $this->expectException(\InvalidArgumentException::class);
  420. $column = $this->createMock(Column::class);
  421. $column->expects($this->any())
  422. ->method('getName')
  423. ->willReturn(\str_repeat('a', 31));
  424. $table = $this->createMock(Table::class);
  425. $table->expects($this->any())
  426. ->method('getName')
  427. ->willReturn(\str_repeat('a', 30));
  428. $table->expects($this->once())
  429. ->method('getColumns')
  430. ->willReturn([$column]);
  431. $schema = $this->createMock(Schema::class);
  432. $schema->expects($this->once())
  433. ->method('getTables')
  434. ->willReturn([$table]);
  435. $sourceSchema = $this->createMock(Schema::class);
  436. $sourceSchema->expects($this->any())
  437. ->method('getTable')
  438. ->willThrowException(new SchemaException());
  439. $sourceSchema->expects($this->any())
  440. ->method('hasSequence')
  441. ->willReturn(false);
  442. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  443. }
  444. public function testEnsureOracleConstraintsTooLongIndexName() {
  445. $this->expectException(\InvalidArgumentException::class);
  446. $index = $this->createMock(Index::class);
  447. $index->expects($this->any())
  448. ->method('getName')
  449. ->willReturn(\str_repeat('a', 31));
  450. $table = $this->createMock(Table::class);
  451. $table->expects($this->any())
  452. ->method('getName')
  453. ->willReturn(\str_repeat('a', 30));
  454. $table->expects($this->once())
  455. ->method('getColumns')
  456. ->willReturn([]);
  457. $table->expects($this->once())
  458. ->method('getIndexes')
  459. ->willReturn([$index]);
  460. $schema = $this->createMock(Schema::class);
  461. $schema->expects($this->once())
  462. ->method('getTables')
  463. ->willReturn([$table]);
  464. $sourceSchema = $this->createMock(Schema::class);
  465. $sourceSchema->expects($this->any())
  466. ->method('getTable')
  467. ->willThrowException(new SchemaException());
  468. $sourceSchema->expects($this->any())
  469. ->method('hasSequence')
  470. ->willReturn(false);
  471. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  472. }
  473. public function testEnsureOracleConstraintsTooLongForeignKeyName() {
  474. $this->expectException(\InvalidArgumentException::class);
  475. $foreignKey = $this->createMock(ForeignKeyConstraint::class);
  476. $foreignKey->expects($this->any())
  477. ->method('getName')
  478. ->willReturn(\str_repeat('a', 31));
  479. $table = $this->createMock(Table::class);
  480. $table->expects($this->any())
  481. ->method('getName')
  482. ->willReturn(\str_repeat('a', 30));
  483. $table->expects($this->once())
  484. ->method('getColumns')
  485. ->willReturn([]);
  486. $table->expects($this->once())
  487. ->method('getIndexes')
  488. ->willReturn([]);
  489. $table->expects($this->once())
  490. ->method('getForeignKeys')
  491. ->willReturn([$foreignKey]);
  492. $schema = $this->createMock(Schema::class);
  493. $schema->expects($this->once())
  494. ->method('getTables')
  495. ->willReturn([$table]);
  496. $sourceSchema = $this->createMock(Schema::class);
  497. $sourceSchema->expects($this->any())
  498. ->method('getTable')
  499. ->willThrowException(new SchemaException());
  500. $sourceSchema->expects($this->any())
  501. ->method('hasSequence')
  502. ->willReturn(false);
  503. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  504. }
  505. public function testEnsureOracleConstraintsNoPrimaryKey() {
  506. $this->markTestSkipped('Test disabled for now due to multiple reasons, see https://github.com/nextcloud/server/pull/31580#issuecomment-1069182234 for details.');
  507. $this->expectException(\InvalidArgumentException::class);
  508. $table = $this->createMock(Table::class);
  509. $table->expects($this->atLeastOnce())
  510. ->method('getName')
  511. ->willReturn(\str_repeat('a', 30));
  512. $table->expects($this->once())
  513. ->method('getColumns')
  514. ->willReturn([]);
  515. $table->expects($this->once())
  516. ->method('getIndexes')
  517. ->willReturn([]);
  518. $table->expects($this->once())
  519. ->method('getForeignKeys')
  520. ->willReturn([]);
  521. $table->expects($this->once())
  522. ->method('getPrimaryKey')
  523. ->willReturn(null);
  524. $schema = $this->createMock(Schema::class);
  525. $schema->expects($this->once())
  526. ->method('getTables')
  527. ->willReturn([$table]);
  528. $schema->expects($this->once())
  529. ->method('getSequences')
  530. ->willReturn([]);
  531. $sourceSchema = $this->createMock(Schema::class);
  532. $sourceSchema->expects($this->any())
  533. ->method('getTable')
  534. ->willThrowException(new SchemaException());
  535. $sourceSchema->expects($this->any())
  536. ->method('hasSequence')
  537. ->willReturn(false);
  538. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  539. }
  540. public function testEnsureOracleConstraintsTooLongSequenceName() {
  541. $this->expectException(\InvalidArgumentException::class);
  542. $sequence = $this->createMock(Sequence::class);
  543. $sequence->expects($this->any())
  544. ->method('getName')
  545. ->willReturn(\str_repeat('a', 31));
  546. $schema = $this->createMock(Schema::class);
  547. $schema->expects($this->once())
  548. ->method('getTables')
  549. ->willReturn([]);
  550. $schema->expects($this->once())
  551. ->method('getSequences')
  552. ->willReturn([$sequence]);
  553. $sourceSchema = $this->createMock(Schema::class);
  554. $sourceSchema->expects($this->any())
  555. ->method('getTable')
  556. ->willThrowException(new SchemaException());
  557. $sourceSchema->expects($this->any())
  558. ->method('hasSequence')
  559. ->willReturn(false);
  560. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  561. }
  562. public function testEnsureOracleConstraintsBooleanNotNull() {
  563. $this->expectException(\InvalidArgumentException::class);
  564. $column = $this->createMock(Column::class);
  565. $column->expects($this->any())
  566. ->method('getName')
  567. ->willReturn('aaaa');
  568. $column->expects($this->any())
  569. ->method('getType')
  570. ->willReturn(Type::getType('boolean'));
  571. $column->expects($this->any())
  572. ->method('getNotnull')
  573. ->willReturn(true);
  574. $table = $this->createMock(Table::class);
  575. $table->expects($this->any())
  576. ->method('getName')
  577. ->willReturn(\str_repeat('a', 30));
  578. $table->expects($this->once())
  579. ->method('getColumns')
  580. ->willReturn([$column]);
  581. $schema = $this->createMock(Schema::class);
  582. $schema->expects($this->once())
  583. ->method('getTables')
  584. ->willReturn([$table]);
  585. $sourceSchema = $this->createMock(Schema::class);
  586. $sourceSchema->expects($this->any())
  587. ->method('getTable')
  588. ->willThrowException(new SchemaException());
  589. $sourceSchema->expects($this->any())
  590. ->method('hasSequence')
  591. ->willReturn(false);
  592. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  593. }
  594. public function testEnsureOracleConstraintsStringLength4000() {
  595. $this->expectException(\InvalidArgumentException::class);
  596. $column = $this->createMock(Column::class);
  597. $column->expects($this->any())
  598. ->method('getName')
  599. ->willReturn('aaaa');
  600. $column->expects($this->any())
  601. ->method('getType')
  602. ->willReturn(Type::getType('string'));
  603. $column->expects($this->any())
  604. ->method('getLength')
  605. ->willReturn(4001);
  606. $table = $this->createMock(Table::class);
  607. $table->expects($this->any())
  608. ->method('getName')
  609. ->willReturn(\str_repeat('a', 30));
  610. $table->expects($this->once())
  611. ->method('getColumns')
  612. ->willReturn([$column]);
  613. $schema = $this->createMock(Schema::class);
  614. $schema->expects($this->once())
  615. ->method('getTables')
  616. ->willReturn([$table]);
  617. $sourceSchema = $this->createMock(Schema::class);
  618. $sourceSchema->expects($this->any())
  619. ->method('getTable')
  620. ->willThrowException(new SchemaException());
  621. $sourceSchema->expects($this->any())
  622. ->method('hasSequence')
  623. ->willReturn(false);
  624. self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
  625. }
  626. }