ConfigServiceProviderTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /*
  3. * This file is part of ConfigServiceProvider.
  4. *
  5. * (c) Igor Wiedler <igor@wiedler.ch>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. use Silex\Application;
  11. use Igorw\Silex\ConfigServiceProvider;
  12. /**
  13. * @author Igor Wiedler <igor@wiedler.ch>
  14. * @author Jérôme Macias <jerome.macias@gmail.com>
  15. */
  16. class ConfigServiceProviderTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @dataProvider provideFilenames
  20. */
  21. public function testRegisterWithoutReplacement($filename)
  22. {
  23. $app = new Application();
  24. $app->register(new ConfigServiceProvider($filename));
  25. $this->assertSame(true, $app['debug']);
  26. $this->assertSame('%data%', $app['data']);
  27. }
  28. /**
  29. * @dataProvider provideFilenames
  30. */
  31. public function testRegisterWithReplacement($filename)
  32. {
  33. $app = new Application();
  34. $app->register(new ConfigServiceProvider($filename, array(
  35. 'data' => 'test-replacement'
  36. )));
  37. $this->assertSame(true, $app['debug']);
  38. $this->assertSame('test-replacement', $app['data']);
  39. }
  40. /**
  41. * @dataProvider provideEmptyFilenames
  42. */
  43. public function testEmptyConfigs($filename)
  44. {
  45. $readConfigMethod = new \ReflectionMethod('Igorw\Silex\ConfigServiceProvider', 'readConfig');
  46. $readConfigMethod->setAccessible(true);
  47. $this->assertEquals(
  48. array(),
  49. $readConfigMethod->invoke(new ConfigServiceProvider($filename))
  50. );
  51. }
  52. /**
  53. * @dataProvider provideReplacementFilenames
  54. */
  55. public function testInFileReplacements($filename)
  56. {
  57. $app = new Application();
  58. $app->register(new ConfigServiceProvider($filename));
  59. $this->assertSame('/var/www', $app['%path%']);
  60. $this->assertSame('/var/www/web/images', $app['path.images']);
  61. $this->assertSame('/var/www/upload', $app['path.upload']);
  62. $this->assertSame('http://example.com', $app['%url%']);
  63. $this->assertSame('http://example.com/images', $app['url.images']);
  64. }
  65. /**
  66. * Currently not tested via testMergeConfigs as TOML seems to have problems
  67. * to create 'db.options' keys
  68. */
  69. public function testTomlMergeConfigs()
  70. {
  71. $app = new Application();
  72. $filenameBase = __DIR__."/Fixtures/config_base.toml";
  73. $filenameExtended = __DIR__."/Fixtures/config_extend.toml";
  74. $app->register(new ConfigServiceProvider($filenameBase));
  75. $app->register(new ConfigServiceProvider($filenameExtended));
  76. $this->assertSame('pdo_mysql', $app['db']['driver']);
  77. $this->assertSame('utf8', $app['db']['charset']);
  78. $this->assertSame('127.0.0.1', $app['db']['host']);
  79. $this->assertSame('mydatabase', $app['db']['dbname']);
  80. $this->assertSame('root', $app['db']['user']);
  81. $this->assertSame('', $app['db']['password']);
  82. $this->assertSame('123', $app['myproject']['param1']);
  83. $this->assertSame('456', $app['myproject']['param2']);
  84. $this->assertSame('456', $app['myproject']['param3']);
  85. $this->assertSame(array(4, 5, 6), $app['myproject']['param4']);
  86. $this->assertSame('456', $app['myproject']['param5']);
  87. $this->assertSame(array(1, 2, 3, 4), $app['keys']['set']);
  88. }
  89. /**
  90. * @dataProvider provideMergeFilenames
  91. */
  92. public function testMergeConfigs($filenameBase, $filenameExtended)
  93. {
  94. $app = new Application();
  95. $app->register(new ConfigServiceProvider($filenameBase));
  96. $app->register(new ConfigServiceProvider($filenameExtended));
  97. $this->assertSame('pdo_mysql', $app['db.options']['driver']);
  98. $this->assertSame('utf8', $app['db.options']['charset']);
  99. $this->assertSame('127.0.0.1', $app['db.options']['host']);
  100. $this->assertSame('mydatabase', $app['db.options']['dbname']);
  101. $this->assertSame('root', $app['db.options']['user']);
  102. $this->assertSame(null, $app['db.options']['password']);
  103. $this->assertSame('123', $app['myproject.test']['param1']);
  104. $this->assertSame('456', $app['myproject.test']['param2']);
  105. $this->assertSame('123', $app['myproject.test']['param3']['param2A']);
  106. $this->assertSame('456', $app['myproject.test']['param3']['param2B']);
  107. $this->assertSame('456', $app['myproject.test']['param3']['param2C']);
  108. $this->assertSame(array(4, 5, 6), $app['myproject.test']['param4']);
  109. $this->assertSame('456', $app['myproject.test']['param5']);
  110. $this->assertSame(array(1,2,3,4), $app['test.noparent.key']['test']);
  111. }
  112. /**
  113. * @test
  114. * @expectedException RuntimeException
  115. * @expectedExceptionMessage Invalid JSON provided "Syntax error" in
  116. */
  117. public function invalidJsonShouldThrowException()
  118. {
  119. $app = new Application();
  120. $app->register(new ConfigServiceProvider(__DIR__."/Fixtures/broken.json"));
  121. }
  122. /**
  123. * @test
  124. * @expectedException Symfony\Component\Yaml\Exception\ParseException
  125. */
  126. public function invalidYamlShouldThrowException()
  127. {
  128. $app = new Application();
  129. $app->register(new ConfigServiceProvider(__DIR__."/Fixtures/broken.yml"));
  130. }
  131. /**
  132. * @test
  133. * @expectedException \Exception
  134. */
  135. public function invalidTomlShouldThrowException()
  136. {
  137. $app = new Application();
  138. $app->register(new ConfigServiceProvider(__DIR__."/Fixtures/broken.toml"));
  139. }
  140. public function provideFilenames()
  141. {
  142. return array(
  143. array(__DIR__."/Fixtures/config.php"),
  144. array(__DIR__."/Fixtures/config.json"),
  145. array(__DIR__."/Fixtures/config.yml"),
  146. array(__DIR__."/Fixtures/config.toml"),
  147. );
  148. }
  149. public function provideReplacementFilenames()
  150. {
  151. return array(
  152. array(__DIR__."/Fixtures/config_replacement.php"),
  153. array(__DIR__."/Fixtures/config_replacement.json"),
  154. array(__DIR__."/Fixtures/config_replacement.yml"),
  155. array(__DIR__."/Fixtures/config_replacement.toml"),
  156. );
  157. }
  158. public function provideEmptyFilenames()
  159. {
  160. return array(
  161. array(__DIR__."/Fixtures/config_empty.php"),
  162. array(__DIR__."/Fixtures/config_empty.json"),
  163. array(__DIR__."/Fixtures/config_empty.yml"),
  164. array(__DIR__."/Fixtures/config_empty.toml"),
  165. );
  166. }
  167. public function provideMergeFilenames()
  168. {
  169. return array(
  170. array(__DIR__."/Fixtures/config_base.php", __DIR__."/Fixtures/config_extend.php"),
  171. array(__DIR__."/Fixtures/config_base.json", __DIR__."/Fixtures/config_extend.json"),
  172. array(__DIR__."/Fixtures/config_base.yml", __DIR__."/Fixtures/config_extend.yml"),
  173. );
  174. }
  175. }