736f75a112c87de1bd3836aad37a1d1796c01b1b
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Loader / IniFileLoaderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Tests\Loader;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16 use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
17 use Symfony\Component\Config\FileLocator;
18
19 class IniFileLoaderTest extends TestCase
20 {
21     protected $container;
22     protected $loader;
23
24     protected function setUp()
25     {
26         $this->container = new ContainerBuilder();
27         $this->loader = new IniFileLoader($this->container, new FileLocator(realpath(__DIR__.'/../Fixtures/').'/ini'));
28     }
29
30     public function testIniFileCanBeLoaded()
31     {
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');
34     }
35
36     /**
37      * @dataProvider getTypeConversions
38      */
39     public function testTypeConversions($key, $value, $supported)
40     {
41         $this->loader->load('types.ini');
42         $parameters = $this->container->getParameterBag()->all();
43         $this->assertSame($value, $parameters[$key], '->load() converts values to PHP types');
44     }
45
46     /**
47      * @dataProvider getTypeConversions
48      * @requires PHP 5.6.1
49      * This test illustrates where our conversions differs from INI_SCANNER_TYPED introduced in PHP 5.6.1
50      */
51     public function testTypeConversionsWithNativePhp($key, $value, $supported)
52     {
53         if (defined('HHVM_VERSION_ID')) {
54             return $this->markTestSkipped();
55         }
56
57         if (!$supported) {
58             $this->markTestSkipped(sprintf('Converting the value "%s" to "%s" is not supported by the IniFileLoader.', $key, $value));
59         }
60
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');
64     }
65
66     public function getTypeConversions()
67     {
68         return array(
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),
85             array('1', 1, true),
86             array('0', 0, 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),
95         );
96     }
97
98     /**
99      * @expectedException        \InvalidArgumentException
100      * @expectedExceptionMessage The file "foo.ini" does not exist (in:
101      */
102     public function testExceptionIsRaisedWhenIniFileDoesNotExist()
103     {
104         $this->loader->load('foo.ini');
105     }
106
107     /**
108      * @expectedException        \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
109      * @expectedExceptionMessage The "nonvalid.ini" file is not valid.
110      */
111     public function testExceptionIsRaisedWhenIniFileCannotBeParsed()
112     {
113         @$this->loader->load('nonvalid.ini');
114     }
115
116     /**
117      * @expectedException        \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
118      * @expectedExceptionMessage The "almostvalid.ini" file is not valid.
119      */
120     public function testExceptionIsRaisedWhenIniFileIsAlmostValid()
121     {
122         @$this->loader->load('almostvalid.ini');
123     }
124
125     public function testSupports()
126     {
127         $loader = new IniFileLoader(new ContainerBuilder(), new FileLocator());
128
129         $this->assertTrue($loader->supports('foo.ini'), '->supports() returns true if the resource is loadable');
130         $this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
131     }
132 }