/vendor/robmorgan/phinx/tests/Phinx/Config/ConfigTest.php

https://gitlab.com/vannh/portal_training · PHP · 207 lines · 151 code · 23 blank · 33 comment · 0 complexity · 679936ac179962943d8614bafb22c7ff MD5 · raw file

  1. <?php
  2. namespace Test\Phinx\Config;
  3. class ConfigTest extends \PHPUnit_Framework_TestCase
  4. {
  5. /**
  6. * Returns a sample configuration array for use with the unit tests.
  7. *
  8. * @return array
  9. */
  10. public function getConfigArray()
  11. {
  12. return array(
  13. 'default' => array(
  14. 'paths' => array(
  15. 'migrations' => '%%PHINX_CONFIG_PATH%%/testmigrations2',
  16. 'schema' => '%%PHINX_CONFIG_PATH%%/testmigrations2/schema.sql',
  17. )
  18. ),
  19. 'environments' => array(
  20. 'default_migration_table' => 'phinxlog',
  21. 'default_database' => 'testing',
  22. 'testing' => array(
  23. 'adapter' => 'sqllite',
  24. 'path' => '%%PHINX_CONFIG_PATH%%/testdb/test.db'
  25. ),
  26. 'production' => array(
  27. 'adapter' => 'mysql'
  28. )
  29. )
  30. );
  31. }
  32. public function testGetEnvironmentsMethod()
  33. {
  34. $config = new \Phinx\Config\Config($this->getConfigArray());
  35. $this->assertEquals(2, sizeof($config->getEnvironments()));
  36. $this->assertArrayHasKey('testing', $config->getEnvironments());
  37. $this->assertArrayHasKey('production', $config->getEnvironments());
  38. }
  39. public function testGetEnvironmentMethod()
  40. {
  41. $config = new \Phinx\Config\Config($this->getConfigArray());
  42. $db = $config->getEnvironment('testing');
  43. $this->assertEquals('sqllite', $db['adapter']);
  44. }
  45. public function testHasEnvironmentMethod()
  46. {
  47. $configArray = $this->getConfigArray();
  48. $config = new \Phinx\Config\Config($configArray);
  49. $this->assertTrue($config->hasEnvironment('testing'));
  50. $this->assertFalse($config->hasEnvironment('fakeenvironment'));
  51. }
  52. public function testGetDefaultEnvironmentMethod()
  53. {
  54. $path = __DIR__ . '/_files';
  55. // test with the config array
  56. $configArray = $this->getConfigArray();
  57. $config = new \Phinx\Config\Config($configArray);
  58. $this->assertEquals('testing', $config->getDefaultEnvironment());
  59. // test using a Yaml file without the 'default_database' key.
  60. // (it should default to the first one).
  61. $config = \Phinx\Config\Config::fromYaml($path . '/no_default_database_key.yml');
  62. $this->assertEquals('production', $config->getDefaultEnvironment());
  63. // test using environment variable PHINX_ENVIRONMENT
  64. // (it should return the configuration specified in the environment)
  65. putenv('PHINX_ENVIRONMENT=externally-specified-environment');
  66. $config = \Phinx\Config\Config::fromYaml($path . '/no_default_database_key.yml');
  67. $this->assertEquals('externally-specified-environment', $config->getDefaultEnvironment());
  68. putenv('PHINX_ENVIRONMENT=');
  69. }
  70. /**
  71. * @expectedException \RuntimeException
  72. */
  73. public function testGetDefaultEnvironmentWithAnEmptyYamlFile()
  74. {
  75. // test using a Yaml file with no key or entries
  76. $path = __DIR__ . '/_files';
  77. $config = \Phinx\Config\Config::fromYaml($path . '/empty.yml');
  78. $config->getDefaultEnvironment();
  79. }
  80. /**
  81. * @expectedException \RuntimeException
  82. * @expectedExceptionMessage The environment configuration for 'staging' is missing
  83. */
  84. public function testGetDefaultEnvironmentWithAMissingEnvironmentEntry()
  85. {
  86. // test using a Yaml file with a 'default_database' key, but without a
  87. // corresponding entry
  88. $path = __DIR__ . '/_files';
  89. $config = \Phinx\Config\Config::fromYaml($path . '/missing_environment_entry.yml');
  90. $config->getDefaultEnvironment();
  91. }
  92. public function testFromPHPMethod()
  93. {
  94. $path = __DIR__ . '/_files';
  95. $config = \Phinx\Config\Config::fromPhp($path . '/valid_config.php');
  96. $this->assertEquals('dev', $config->getDefaultEnvironment());
  97. }
  98. /**
  99. * @expectedException \RuntimeException
  100. */
  101. public function testFromPHPMethodWithoutArray()
  102. {
  103. $path = __DIR__ . '/_files';
  104. $config = \Phinx\Config\Config::fromPhp($path . '/config_without_array.php');
  105. $this->assertEquals('dev', $config->getDefaultEnvironment());
  106. }
  107. public function testFromJSONMethod()
  108. {
  109. $path = __DIR__ . '/_files';
  110. $config = \Phinx\Config\Config::fromJson($path . '/valid_config.json');
  111. $this->assertEquals('dev', $config->getDefaultEnvironment());
  112. }
  113. /**
  114. * @expectedException \RuntimeException
  115. */
  116. public function testFromJSONMethodWithoutJSON()
  117. {
  118. $path = __DIR__ . '/_files';
  119. $config = \Phinx\Config\Config::fromPhp($path . '/empty.json');
  120. $this->assertEquals('dev', $config->getDefaultEnvironment());
  121. }
  122. /**
  123. * @expectedException UnexpectedValueException
  124. */
  125. public function testGetMigrationPathThrowsExceptionForNoPath()
  126. {
  127. $config = new \Phinx\Config\Config(array());
  128. $config->getMigrationPath();
  129. }
  130. public function testArrayAccessMethods()
  131. {
  132. $config = new \Phinx\Config\Config(array());
  133. $config['foo'] = 'bar';
  134. $this->assertEquals('bar', $config['foo']);
  135. $this->assertTrue(isset($config['foo']));
  136. unset($config['foo']);
  137. $this->assertFalse(isset($config['foo']));
  138. }
  139. /**
  140. * @expectedException \InvalidArgumentException
  141. * @expectedExceptionMessage Identifier "foo" is not defined.
  142. */
  143. public function testUndefinedArrayAccess()
  144. {
  145. $config = new \Phinx\Config\Config(array());
  146. $config['foo'];
  147. }
  148. public function testConfigReplacesTokensWithEnvVariables()
  149. {
  150. $_SERVER['PHINX_DBHOST'] = 'localhost';
  151. $_SERVER['PHINX_DBNAME'] = 'productionapp';
  152. $_SERVER['PHINX_DBUSER'] = 'root';
  153. $_SERVER['PHINX_DBPASS'] = 'ds6xhj1';
  154. $_SERVER['PHINX_DBPORT'] = '1234';
  155. $path = __DIR__ . '/_files';
  156. $config = \Phinx\Config\Config::fromYaml($path . '/external_variables.yml');
  157. $env = $config->getEnvironment($config->getDefaultEnvironment());
  158. $this->assertEquals('localhost', $env['host']);
  159. $this->assertEquals('productionapp', $env['name']);
  160. $this->assertEquals('root', $env['user']);
  161. $this->assertEquals('ds6xhj1', $env['pass']);
  162. $this->assertEquals('1234', $env['port']);
  163. }
  164. public function testGetMigrationBaseClassNameGetsDefaultBaseClass()
  165. {
  166. $config = new \Phinx\Config\Config(array());
  167. $this->assertEquals('AbstractMigration', $config->getMigrationBaseClassName());
  168. }
  169. public function testGetMigrationBaseClassNameGetsDefaultBaseClassWithNamespace()
  170. {
  171. $config = new \Phinx\Config\Config(array());
  172. $this->assertEquals('Phinx\Migration\AbstractMigration', $config->getMigrationBaseClassName(false));
  173. }
  174. public function testGetMigrationBaseClassNameGetsAlternativeBaseClass()
  175. {
  176. $config = new \Phinx\Config\Config(array('migration_base_class' => 'Phinx\Migration\AlternativeAbstractMigration'));
  177. $this->assertEquals('AlternativeAbstractMigration', $config->getMigrationBaseClassName());
  178. }
  179. public function testGetMigrationBaseClassNameGetsAlternativeBaseClassWithNamespace()
  180. {
  181. $config = new \Phinx\Config\Config(array('migration_base_class' => 'Phinx\Migration\AlternativeAbstractMigration'));
  182. $this->assertEquals('Phinx\Migration\AlternativeAbstractMigration', $config->getMigrationBaseClassName(false));
  183. }
  184. }