IniFileLoaderTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\DependencyInjection\Tests\Loader;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  14. use Symfony\Component\Config\FileLocator;
  15. class IniFileLoaderTest extends TestCase
  16. {
  17. protected $container;
  18. protected $loader;
  19. protected function setUp()
  20. {
  21. $this->container = new ContainerBuilder();
  22. $this->loader = new IniFileLoader($this->container, new FileLocator(realpath(__DIR__.'/../Fixtures/').'/ini'));
  23. }
  24. public function testIniFileCanBeLoaded()
  25. {
  26. $this->loader->load('parameters.ini');
  27. $this->assertEquals(array('foo' => 'bar', 'bar' => '%foo%'), $this->container->getParameterBag()->all(), '->load() takes a single file name as its first argument');
  28. }
  29. /**
  30. * @dataProvider getTypeConversions
  31. */
  32. public function testTypeConversions($key, $value, $supported)
  33. {
  34. $this->loader->load('types.ini');
  35. $parameters = $this->container->getParameterBag()->all();
  36. $this->assertSame($value, $parameters[$key], '->load() converts values to PHP types');
  37. }
  38. /**
  39. * @dataProvider getTypeConversions
  40. * @requires PHP 5.6.1
  41. * This test illustrates where our conversions differs from INI_SCANNER_TYPED introduced in PHP 5.6.1
  42. */
  43. public function testTypeConversionsWithNativePhp($key, $value, $supported)
  44. {
  45. if (defined('HHVM_VERSION_ID')) {
  46. $this->markTestSkipped();
  47. }
  48. if (!$supported) {
  49. $this->markTestSkipped(sprintf('Converting the value "%s" to "%s" is not supported by the IniFileLoader.', $key, $value));
  50. }
  51. $this->loader->load('types.ini');
  52. $expected = parse_ini_file(__DIR__.'/../Fixtures/ini/types.ini', true, INI_SCANNER_TYPED);
  53. $this->assertSame($value, $expected['parameters'][$key], '->load() converts values to PHP types');
  54. }
  55. public function getTypeConversions()
  56. {
  57. return array(
  58. array('true_comment', true, true),
  59. array('true', true, true),
  60. array('false', false, true),
  61. array('on', true, true),
  62. array('off', false, true),
  63. array('yes', true, true),
  64. array('no', false, true),
  65. array('none', false, true),
  66. array('null', null, true),
  67. array('constant', PHP_VERSION, true),
  68. array('12', 12, true),
  69. array('12_string', '12', true),
  70. array('12_comment', 12, true),
  71. array('12_string_comment', '12', true),
  72. array('12_string_comment_again', '12', true),
  73. array('-12', -12, true),
  74. array('1', 1, true),
  75. array('0', 0, true),
  76. array('0b0110', bindec('0b0110'), false), // not supported by INI_SCANNER_TYPED
  77. array('11112222333344445555', '1111,2222,3333,4444,5555', true),
  78. array('0777', 0777, false), // not supported by INI_SCANNER_TYPED
  79. array('255', 0xFF, false), // not supported by INI_SCANNER_TYPED
  80. array('100.0', 1e2, false), // not supported by INI_SCANNER_TYPED
  81. array('-120.0', -1.2E2, false), // not supported by INI_SCANNER_TYPED
  82. array('-10100.1', -10100.1, false), // not supported by INI_SCANNER_TYPED
  83. array('-10,100.1', '-10,100.1', true),
  84. );
  85. }
  86. /**
  87. * @expectedException \InvalidArgumentException
  88. * @expectedExceptionMessage The file "foo.ini" does not exist (in:
  89. */
  90. public function testExceptionIsRaisedWhenIniFileDoesNotExist()
  91. {
  92. $this->loader->load('foo.ini');
  93. }
  94. /**
  95. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  96. * @expectedExceptionMessage The "nonvalid.ini" file is not valid.
  97. */
  98. public function testExceptionIsRaisedWhenIniFileCannotBeParsed()
  99. {
  100. @$this->loader->load('nonvalid.ini');
  101. }
  102. /**
  103. * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
  104. * @expectedExceptionMessage The "almostvalid.ini" file is not valid.
  105. */
  106. public function testExceptionIsRaisedWhenIniFileIsAlmostValid()
  107. {
  108. @$this->loader->load('almostvalid.ini');
  109. }
  110. public function testSupports()
  111. {
  112. $loader = new IniFileLoader(new ContainerBuilder(), new FileLocator());
  113. $this->assertTrue($loader->supports('foo.ini'), '->supports() returns true if the resource is loadable');
  114. $this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
  115. $this->assertTrue($loader->supports('with_wrong_ext.yml', 'ini'), '->supports() returns true if the resource with forced type is loadable');
  116. }
  117. }