PageRenderTime 21ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/robmorgan/phinx/tests/Phinx/Migration/ManagerTest.php

https://gitlab.com/vannh/portal_training
PHP | 230 lines | 176 code | 32 blank | 22 comment | 1 complexity | 62c3db94df27f94f3c93781c2ffdb3f6 MD5 | raw file
  1. <?php
  2. namespace Test\Phinx\Migration;
  3. use Symfony\Component\Console\Output\StreamOutput;
  4. use Phinx\Config\Config;
  5. use Phinx\Db\Adapter\MysqlAdapter;
  6. use Phinx\Migration\Manager;
  7. use Phinx\Migration\Manager\Environment;
  8. class ManagerTest extends \PHPUnit_Framework_TestCase
  9. {
  10. /**
  11. * @var Manager
  12. */
  13. private $manager;
  14. protected function setUp()
  15. {
  16. $config = new Config($this->getConfigArray());
  17. $output = new StreamOutput(fopen('php://memory', 'a', false));
  18. $this->manager = new Manager($config, $output);
  19. }
  20. protected function tearDown()
  21. {
  22. $this->manager = null;
  23. }
  24. private function getCorrectedPath($path)
  25. {
  26. return str_replace('/', DIRECTORY_SEPARATOR, $path);
  27. }
  28. /**
  29. * Returns a sample configuration array for use with the unit tests.
  30. *
  31. * @return array
  32. */
  33. public function getConfigArray()
  34. {
  35. return array(
  36. 'paths' => array(
  37. 'migrations' => $this->getCorrectedPath(__DIR__ . '/_files/migrations'),
  38. ),
  39. 'environments' => array(
  40. 'default_migration_table' => 'phinxlog',
  41. 'default_database' => 'production',
  42. 'production' => array(
  43. 'adapter' => 'mysql',
  44. 'host' => TESTS_PHINX_DB_ADAPTER_MYSQL_HOST,
  45. 'name' => TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE,
  46. 'user' => TESTS_PHINX_DB_ADAPTER_MYSQL_USERNAME,
  47. 'pass' => TESTS_PHINX_DB_ADAPTER_MYSQL_PASSWORD,
  48. 'port' => TESTS_PHINX_DB_ADAPTER_MYSQL_PORT
  49. )
  50. )
  51. );
  52. }
  53. public function testInstantiation()
  54. {
  55. $this->assertTrue($this->manager->getOutput() instanceof StreamOutput);
  56. }
  57. public function testPrintStatusMethod()
  58. {
  59. // stub environment
  60. $envStub = $this->getMock('\Phinx\Migration\Manager\Environment', array(), array('mockenv', array()));
  61. $envStub->expects($this->once())
  62. ->method('getVersions')
  63. ->will($this->returnValue(array('20120111235330', '20120116183504')));
  64. $this->manager->setEnvironments(array('mockenv' => $envStub));
  65. $this->manager->printStatus('mockenv');
  66. rewind($this->manager->getOutput()->getStream());
  67. $outputStr = stream_get_contents($this->manager->getOutput()->getStream());
  68. $this->assertRegExp('/up 20120111235330 TestMigration/', $outputStr);
  69. $this->assertRegExp('/up 20120116183504 TestMigration2/', $outputStr);
  70. }
  71. public function testPrintStatusMethodWithNoMigrations()
  72. {
  73. // stub environment
  74. $envStub = $this->getMock('\Phinx\Migration\Manager\Environment', array(), array('mockenv', array()));
  75. // override the migrations directory to an empty one
  76. $configArray = $this->getConfigArray();
  77. $configArray['paths']['migrations'] = $this->getCorrectedPath(__DIR__ . '/_files/nomigrations');
  78. $config = new Config($configArray);
  79. $this->manager->setConfig($config);
  80. $this->manager->setEnvironments(array('mockenv' => $envStub));
  81. $this->manager->printStatus('mockenv');
  82. rewind($this->manager->getOutput()->getStream());
  83. $outputStr = stream_get_contents($this->manager->getOutput()->getStream());
  84. $this->assertRegExp('/There are no available migrations. Try creating one using the create command./', $outputStr);
  85. }
  86. public function testPrintStatusMethodWithMissingMigrations()
  87. {
  88. // stub environment
  89. $envStub = $this->getMock('\Phinx\Migration\Manager\Environment', array(), array('mockenv', array()));
  90. $envStub->expects($this->once())
  91. ->method('getVersions')
  92. ->will($this->returnValue(array('20120103083300', '20120815145812')));
  93. $this->manager->setEnvironments(array('mockenv' => $envStub));
  94. $this->manager->printStatus('mockenv');
  95. rewind($this->manager->getOutput()->getStream());
  96. $outputStr = stream_get_contents($this->manager->getOutput()->getStream());
  97. $this->assertRegExp('/up 20120103083300 \*\* MISSING \*\*/', $outputStr);
  98. $this->assertRegExp('/up 20120815145812 \*\* MISSING \*\*/', $outputStr);
  99. }
  100. public function testGetMigrationsWithDuplicateMigrationVersions()
  101. {
  102. $this->setExpectedException(
  103. 'InvalidArgumentException',
  104. 'Duplicate migration - "' . $this->getCorrectedPath(__DIR__ . '/_files/duplicateversions/20120111235330_duplicate_migration_2.php') . '" has the same version as "20120111235330"'
  105. );
  106. $config = new Config(array('paths' => array('migrations' => $this->getCorrectedPath(__DIR__ . '/_files/duplicateversions'))));
  107. $output = new StreamOutput(fopen('php://memory', 'a', false));
  108. $manager = new Manager($config, $output);
  109. $manager->getMigrations();
  110. }
  111. public function testGetMigrationsWithDuplicateMigrationNames()
  112. {
  113. $this->setExpectedException(
  114. 'InvalidArgumentException',
  115. 'Migration "20120111235331_duplicate_migration_name.php" has the same name as "20120111235330_duplicate_migration_name.php"'
  116. );
  117. $config = new Config(array('paths' => array('migrations' => $this->getCorrectedPath(__DIR__ . '/_files/duplicatenames'))));
  118. $output = new StreamOutput(fopen('php://memory', 'a', false));
  119. $manager = new Manager($config, $output);
  120. $manager->getMigrations();
  121. }
  122. public function testGetMigrationsWithInvalidMigrationClassName()
  123. {
  124. $this->setExpectedException(
  125. 'InvalidArgumentException',
  126. 'Could not find class "InvalidClass" in file "' . $this->getCorrectedPath(__DIR__ . '/_files/invalidclassname/20120111235330_invalid_class.php') . '"'
  127. );
  128. $config = new Config(array('paths' => array('migrations' => $this->getCorrectedPath(__DIR__ . '/_files/invalidclassname'))));
  129. $output = new StreamOutput(fopen('php://memory', 'a', false));
  130. $manager = new Manager($config, $output);
  131. $manager->getMigrations();
  132. }
  133. public function testGetMigrationsWithClassThatDoesntExtendAbstractMigration()
  134. {
  135. $this->setExpectedException(
  136. 'InvalidArgumentException',
  137. 'The class "InvalidSuperClass" in file "' . $this->getCorrectedPath(__DIR__ . '/_files/invalidsuperclass/20120111235330_invalid_super_class.php') . '" must extend \Phinx\Migration\AbstractMigration'
  138. );
  139. $config = new Config(array('paths' => array('migrations' => $this->getCorrectedPath(__DIR__ . '/_files/invalidsuperclass'))));
  140. $output = new StreamOutput(fopen('php://memory', 'a', false));
  141. $manager = new Manager($config, $output);
  142. $manager->getMigrations();
  143. }
  144. public function testGettingAValidEnvironment()
  145. {
  146. $this->assertTrue($this->manager->getEnvironment('production') instanceof Environment);
  147. }
  148. public function testGettingOutputObject()
  149. {
  150. $migrations = $this->manager->getMigrations();
  151. $outputObject = $this->manager->getOutput();
  152. $this->assertInstanceOf('\Symfony\Component\Console\Output\OutputInterface', $outputObject);
  153. foreach ($migrations as $migration) {
  154. $this->assertEquals($outputObject, $migration->getOutput());
  155. }
  156. }
  157. /**
  158. * @expectedException \InvalidArgumentException
  159. * @expectedExceptionMessage The environment "invalidenv" does not exist
  160. */
  161. public function testGettingAnInvalidEnvironment()
  162. {
  163. $this->manager->getEnvironment('invalidenv');
  164. }
  165. public function testReversibleMigrationsWorkAsExpected()
  166. {
  167. if (!TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED) {
  168. $this->markTestSkipped('Mysql tests disabled. See TESTS_PHINX_DB_ADAPTER_MYSQL_ENABLED constant.');
  169. }
  170. $configArray = $this->getConfigArray();
  171. $adapter = $this->manager->getEnvironment('production')->getAdapter();
  172. // override the migrations directory to use the reversible migrations
  173. $configArray['paths']['migrations'] = $this->getCorrectedPath(__DIR__ . '/_files/reversiblemigrations');
  174. $config = new Config($configArray);
  175. // ensure the database is empty
  176. $adapter->dropDatabase(TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE);
  177. $adapter->createDatabase(TESTS_PHINX_DB_ADAPTER_MYSQL_DATABASE);
  178. $adapter->disconnect();
  179. // migrate to the latest version
  180. $this->manager->setConfig($config);
  181. $this->manager->migrate('production');
  182. // ensure up migrations worked
  183. $this->assertFalse($adapter->hasTable('info'));
  184. $this->assertTrue($adapter->hasTable('statuses'));
  185. $this->assertTrue($adapter->hasTable('users'));
  186. $this->assertTrue($adapter->hasTable('user_logins'));
  187. $this->assertTrue($adapter->hasColumn('users', 'biography'));
  188. $this->assertTrue($adapter->hasForeignKey('user_logins', array('user_id')));
  189. // revert all changes to the first
  190. $this->manager->rollback('production', '20121213232502');
  191. // ensure reversed migrations worked
  192. $this->assertTrue($adapter->hasTable('info'));
  193. $this->assertFalse($adapter->hasTable('statuses'));
  194. $this->assertFalse($adapter->hasTable('user_logins'));
  195. $this->assertTrue($adapter->hasColumn('users', 'bio'));
  196. $this->assertFalse($adapter->hasForeignKey('user_logins', array('user_id')));
  197. }
  198. }