17
17
18
18
class IniFileLoaderTest extends \PHPUnit_Framework_TestCase
19
19
{
20
- protected static $ fixturesPath ;
21
-
22
20
protected $ container ;
23
21
protected $ loader ;
24
22
25
- public static function setUpBeforeClass ()
26
- {
27
- self ::$ fixturesPath = realpath (__DIR__ .'/../Fixtures/ ' );
28
- }
29
-
30
23
protected function setUp ()
31
24
{
32
25
$ 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 ' ));
34
27
}
35
28
36
29
public function testIniFileCanBeLoaded ()
@@ -39,6 +32,68 @@ public function testIniFileCanBeLoaded()
39
32
$ this ->assertEquals (array ('foo ' => 'bar ' , 'bar ' => '%foo% ' ), $ this ->container ->getParameterBag ()->all (), '->load() takes a single file name as its first argument ' );
40
33
}
41
34
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
+
42
97
/**
43
98
* @expectedException \InvalidArgumentException
44
99
* @expectedExceptionMessage The file "foo.ini" does not exist (in:
@@ -49,14 +104,23 @@ public function testExceptionIsRaisedWhenIniFileDoesNotExist()
49
104
}
50
105
51
106
/**
52
- * @expectedException \InvalidArgumentException
107
+ * @expectedException \Symfony\Component\DependencyInjection\Exception\ InvalidArgumentException
53
108
* @expectedExceptionMessage The "nonvalid.ini" file is not valid.
54
109
*/
55
110
public function testExceptionIsRaisedWhenIniFileCannotBeParsed ()
56
111
{
57
112
@$ this ->loader ->load ('nonvalid.ini ' );
58
113
}
59
114
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
+
60
124
public function testSupports ()
61
125
{
62
126
$ loader = new IniFileLoader (new ContainerBuilder (), new FileLocator ());
0 commit comments