4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Loader;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\FileLocator;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17 use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
19 class IniFileLoaderTest extends TestCase
24 protected function setUp()
26 $this->container = new ContainerBuilder();
27 $this->loader = new IniFileLoader($this->container, new FileLocator(realpath(__DIR__.'/../Fixtures/').'/ini'));
30 public function testIniFileCanBeLoaded()
32 $this->loader->load('parameters.ini');
33 $this->assertEquals(array('foo' => 'bar', 'bar' => '%foo%'), $this->container->getParameterBag()->all(), '->load() takes a single file name as its first argument');
37 * @dataProvider getTypeConversions
39 public function testTypeConversions($key, $value, $supported)
41 $this->loader->load('types.ini');
42 $parameters = $this->container->getParameterBag()->all();
43 $this->assertSame($value, $parameters[$key], '->load() converts values to PHP types');
47 * @dataProvider getTypeConversions
49 * This test illustrates where our conversions differs from INI_SCANNER_TYPED introduced in PHP 5.6.1
51 public function testTypeConversionsWithNativePhp($key, $value, $supported)
53 if (\defined('HHVM_VERSION_ID')) {
54 $this->markTestSkipped();
58 $this->markTestSkipped(sprintf('Converting the value "%s" to "%s" is not supported by the IniFileLoader.', $key, $value));
61 $this->loader->load('types.ini');
62 $expected = parse_ini_file(__DIR__.'/../Fixtures/ini/types.ini', true, INI_SCANNER_TYPED);
63 $this->assertSame($value, $expected['parameters'][$key], '->load() converts values to PHP types');
66 public function getTypeConversions()
69 array('true_comment', true, true),
70 array('true', true, true),
71 array('false', false, true),
72 array('on', true, true),
73 array('off', false, true),
74 array('yes', true, true),
75 array('no', false, true),
76 array('none', false, true),
77 array('null', null, true),
78 array('constant', PHP_VERSION, true),
79 array('12', 12, true),
80 array('12_string', '12', true),
81 array('12_comment', 12, true),
82 array('12_string_comment', '12', true),
83 array('12_string_comment_again', '12', true),
84 array('-12', -12, true),
87 array('0b0110', bindec('0b0110'), false), // not supported by INI_SCANNER_TYPED
88 array('11112222333344445555', '1111,2222,3333,4444,5555', true),
89 array('0777', 0777, false), // not supported by INI_SCANNER_TYPED
90 array('255', 0xFF, false), // not supported by INI_SCANNER_TYPED
91 array('100.0', 1e2, false), // not supported by INI_SCANNER_TYPED
92 array('-120.0', -1.2E2, false), // not supported by INI_SCANNER_TYPED
93 array('-10100.1', -10100.1, false), // not supported by INI_SCANNER_TYPED
94 array('-10,100.1', '-10,100.1', true),
99 * @expectedException \InvalidArgumentException
100 * @expectedExceptionMessage The file "foo.ini" does not exist (in:
102 public function testExceptionIsRaisedWhenIniFileDoesNotExist()
104 $this->loader->load('foo.ini');
108 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
109 * @expectedExceptionMessage The "nonvalid.ini" file is not valid.
111 public function testExceptionIsRaisedWhenIniFileCannotBeParsed()
113 @$this->loader->load('nonvalid.ini');
117 * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
118 * @expectedExceptionMessage The "almostvalid.ini" file is not valid.
120 public function testExceptionIsRaisedWhenIniFileIsAlmostValid()
122 @$this->loader->load('almostvalid.ini');
125 public function testSupports()
127 $loader = new IniFileLoader(new ContainerBuilder(), new FileLocator());
129 $this->assertTrue($loader->supports('foo.ini'), '->supports() returns true if the resource is loadable');
130 $this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
131 $this->assertTrue($loader->supports('with_wrong_ext.yml', 'ini'), '->supports() returns true if the resource with forced type is loadable');