1717
1818class IniFileLoaderTest extends \PHPUnit_Framework_TestCase
1919{
20- protected static $ fixturesPath ;
21-
2220 protected $ container ;
2321 protected $ loader ;
2422
25- public static function setUpBeforeClass ()
26- {
27- self ::$ fixturesPath = realpath (__DIR__ .'/../Fixtures/ ' );
28- }
29-
3023 protected function setUp ()
3124 {
3225 $ this ->container = new ContainerBuilder ();
33- $ this ->loader = new IniFileLoader ($ this ->container , new FileLocator (self :: $ fixturesPath .'/ini ' ));
26+ $ this ->loader = new IniFileLoader ($ this ->container , new FileLocator (realpath ( __DIR__ . ' /../Fixtures/ ' ) .'/ini ' ));
3427 }
3528
3629 public function testIniFileCanBeLoaded ()
@@ -39,6 +32,68 @@ public function testIniFileCanBeLoaded()
3932 $ this ->assertEquals (array ('foo ' => 'bar ' , 'bar ' => '%foo% ' ), $ this ->container ->getParameterBag ()->all (), '->load() takes a single file name as its first argument ' );
4033 }
4134
35+ /**
36+ * @dataProvider getTypeConversions
37+ */
38+ public function testTypeConversions ($ key , $ value , $ supported )
39+ {
40+ $ this ->loader ->load ('types.ini ' );
41+ $ parameters = $ this ->container ->getParameterBag ()->all ();
42+ $ this ->assertSame ($ value , $ parameters [$ key ], '->load() converts values to PHP types ' );
43+ }
44+
45+ /**
46+ * @dataProvider getTypeConversions
47+ * @requires PHP 5.6.1
48+ * This test illustrates where our conversions differs from INI_SCANNER_TYPED introduced in PHP 5.6.1
49+ */
50+ public function testTypeConversionsWithNativePhp ($ key , $ value , $ supported )
51+ {
52+ if (defined ('HHVM_VERSION_ID ' )) {
53+ return $ this ->markTestSkipped ();
54+ }
55+
56+ if (!$ supported ) {
57+ return ;
58+ }
59+
60+ $ this ->loader ->load ('types.ini ' );
61+ $ expected = parse_ini_file (__DIR__ .'/../Fixtures/ini/types.ini ' , true , INI_SCANNER_TYPED );
62+ $ this ->assertSame ($ value , $ expected ['parameters ' ][$ key ], '->load() converts values to PHP types ' );
63+ }
64+
65+ public function getTypeConversions ()
66+ {
67+ return array (
68+ array ('true_comment ' , true , true ),
69+ array ('true ' , true , true ),
70+ array ('false ' , false , true ),
71+ array ('on ' , true , true ),
72+ array ('off ' , false , true ),
73+ array ('yes ' , true , true ),
74+ array ('no ' , false , true ),
75+ array ('none ' , false , true ),
76+ array ('null ' , null , true ),
77+ array ('constant ' , PHP_VERSION , true ),
78+ array ('12 ' , 12 , true ),
79+ array ('12_string ' , '12 ' , true ),
80+ array ('12_comment ' , 12 , true ),
81+ array ('12_string_comment ' , '12 ' , true ),
82+ array ('12_string_comment_again ' , '12 ' , true ),
83+ array ('-12 ' , -12 , true ),
84+ array ('1 ' , 1 , true ),
85+ array ('0 ' , 0 , true ),
86+ array ('0b0110 ' , bindec ('0b0110 ' ), false ), // not supported by INI_SCANNER_TYPED
87+ array ('11112222333344445555 ' , '1111,2222,3333,4444,5555 ' , true ),
88+ array ('0777 ' , 0777 , false ), // not supported by INI_SCANNER_TYPED
89+ array ('255 ' , 0xFF , false ), // not supported by INI_SCANNER_TYPED
90+ array ('100.0 ' , 1e2 , false ), // not supported by INI_SCANNER_TYPED
91+ array ('-120.0 ' , -1.2E2 , false ), // not supported by INI_SCANNER_TYPED
92+ array ('-10100.1 ' , -10100.1 , false ), // not supported by INI_SCANNER_TYPED
93+ array ('-10,100.1 ' , '-10,100.1 ' , true ),
94+ );
95+ }
96+
4297 /**
4398 * @expectedException \InvalidArgumentException
4499 * @expectedExceptionMessage The file "foo.ini" does not exist (in:
@@ -49,14 +104,23 @@ public function testExceptionIsRaisedWhenIniFileDoesNotExist()
49104 }
50105
51106 /**
52- * @expectedException \InvalidArgumentException
107+ * @expectedException \Symfony\Component\DependencyInjection\Exception\ InvalidArgumentException
53108 * @expectedExceptionMessage The "nonvalid.ini" file is not valid.
54109 */
55110 public function testExceptionIsRaisedWhenIniFileCannotBeParsed ()
56111 {
57112 @$ this ->loader ->load ('nonvalid.ini ' );
58113 }
59114
115+ /**
116+ * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
117+ * @expectedExceptionMessage The "almostvalid.ini" file is not valid.
118+ */
119+ public function testExceptionIsRaisedWhenIniFileIsAlmostValid ()
120+ {
121+ @$ this ->loader ->load ('almostvalid.ini ' );
122+ }
123+
60124 public function testSupports ()
61125 {
62126 $ loader = new IniFileLoader (new ContainerBuilder (), new FileLocator ());
0 commit comments