1+ <?php
2+
3+ namespace pconfig ;
4+
5+ use Exception ;
6+ use PHPUnit \Framework \TestCase ;
7+
8+ class PConfigTest extends TestCase
9+ {
10+ private $ basePath = __DIR__ . '/data/ ' ;
11+
12+ public function testJSON ()
13+ {
14+ $ this ->testConfig ('json ' );
15+ }
16+
17+ public function testINI ()
18+ {
19+ $ this ->testConfig ('ini ' );
20+ }
21+
22+ public function testPHP ()
23+ {
24+ $ this ->testConfig ('php ' );
25+ }
26+
27+ public function testYAML ()
28+ {
29+ $ this ->testConfig ('yaml ' );
30+ }
31+
32+ private function testConfig ($ type )
33+ {
34+ $ config = null ;
35+ try {
36+ $ config = new PConfig ($ this ->basePath . 'config. ' . $ type );
37+ } catch (Exception $ e ) {
38+ $ this ->fail ($ e ->getMessage ());
39+ }
40+ $ this ->assertNotNull ($ config );
41+
42+ $ this ->assertArrayHasKey ('language ' , $ config );
43+ $ this ->assertArrayHasKey ("php " , $ config ->get ('language ' ));
44+ $ this ->assertArrayHasKey ("php " , $ config ['language ' ]);
45+ $ this ->assertArrayHasKey ("type " , $ config ['language ' ]['php ' ]);
46+ $ this ->assertCount (3 , $ config ['language ' ]['php ' ]['type ' ]);
47+ $ this ->assertContains ('object ' , $ config ['language ' ]['php ' ]['type ' ]);
48+
49+ try {
50+ $ config = new PConfig ([
51+ 'file ' => $ this ->basePath . 'config. ' . $ type ,
52+ ]);
53+ } catch (Exception $ e ) {
54+ $ this ->fail ($ e ->getMessage ());
55+ }
56+ $ this ->assertNotNull ($ config );
57+
58+ $ this ->assertArrayHasKey ('language ' , $ config );
59+ $ this ->assertArrayHasKey ("php " , $ config ->get ('language ' ));
60+ $ this ->assertArrayHasKey ("php " , $ config ['language ' ]);
61+ $ this ->assertArrayHasKey ("type " , $ config ['language ' ]['php ' ]);
62+ $ this ->assertCount (3 , $ config ['language ' ]['php ' ]['type ' ]);
63+ $ this ->assertContains ('object ' , $ config ['language ' ]['php ' ]['type ' ]);
64+
65+ $ this ->assertTrue ($ config ->set ('hello.world ' , true ));
66+
67+ $ this ->assertArrayHasKey ('hello ' , $ config );
68+ $ this ->assertArrayHasKey ('world ' , $ config ->get ('hello ' ));
69+ $ this ->assertArrayHasKey ('world ' , $ config ['hello ' ]);
70+
71+ $ this ->assertTrue ($ config ->exists ('hello ' ));
72+ $ this ->assertTrue (isset ($ config ['hello ' ]));
73+ $ this ->assertTrue ($ config ->exists ('hello.world ' ));
74+ $ this ->assertTrue (isset ($ config ['hello.world ' ]));
75+ $ this ->assertTrue (isset ($ config ['hello ' ]['world ' ]));
76+
77+ $ this ->assertNotEquals ('true ' , $ config ->get ('hello.world ' ));
78+ $ this ->assertNotEquals ('true ' , $ config ['hello.world ' ]);
79+ $ this ->assertNotEquals ('true ' , $ config ['hello ' ]['world ' ]);
80+
81+ $ this ->assertEquals (true , $ config ->get ('hello.world ' ));
82+ $ this ->assertEquals (true , $ config ['hello.world ' ]);
83+ $ this ->assertEquals (true , $ config ['hello ' ]['world ' ]);
84+
85+ $ this ->assertFalse ($ config ->delete ('hello.world1 ' ));
86+ $ this ->assertTrue ($ config ->delete ('hello.world ' ));
87+
88+ unset($ config ['hello.world ' ]);
89+
90+ $ this ->assertFalse ($ config ->exists ('hello.world ' ));
91+ $ this ->assertFalse (isset ($ config ['hello.world ' ]));
92+ $ this ->assertNull ($ config ['hello.world ' ]);
93+
94+ $ config ->set ('array.arr ' , [
95+ 1 , 2 , 3 , 4
96+ ]);
97+
98+ $ this ->assertCount (4 , $ config ->get ('array.arr ' ));
99+ $ this ->assertCount (4 , $ config ['array.arr ' ]);
100+ $ this ->assertEquals (1 , $ config ['array.arr.0 ' ]);
101+ $ this ->assertEquals (4 , $ config ->get ('array.arr.3 ' ));
102+
103+ $ config ->setConfig ([
104+ PConfig::CONFIG_KEY_EXTRACT_SEPARATOR => '@ '
105+ ]);
106+
107+ $ this ->assertTrue ($ config ->set ('my@config ' , 'myconfig ' ));
108+ $ this ->assertArrayHasKey ('config ' , $ config ['my ' ]);
109+ $ this ->assertEquals ('myconfig ' , $ config ['my@config ' ]);
110+ $ this ->assertEquals ('myconfig ' , $ config ['my ' ]['config ' ]);
111+
112+ $ this ->assertEquals ("hello " , $ config ->get ('hello.php ' , 'hello ' ));
113+
114+ $ config ->setFile ($ this ->basePath . 'config_new. ' . $ type );
115+
116+ try {
117+ $ this ->assertTrue ($ config ->save ());
118+ } catch (Exception $ e ) {
119+ $ this ->fail ($ e ->getMessage ());
120+ }
121+ }
122+
123+ }
0 commit comments