4a11dfbfc59dc7c075a17e24278d240eb77e4beb
[m6w6/merry] / tests / merry / ConfigTest.php
1 <?php
2
3 namespace merry;
4
5 require __DIR__."/../../vendor/autoload.php";
6
7 /**
8 * @covers merry\Config
9 */
10 class ConfigTest extends \PHPUnit_Framework_TestCase {
11
12 public function testBasic() {
13 $config = ["foo" => "bar", "bar" => "foo"];
14 $object = new Config($config);
15 $this->assertEquals($config, $object->toArray());
16 $this->assertEquals("bar", $object->foo);
17 $this->assertEquals("foo", $object->bar);
18 $this->assertTrue(isset($object->foo));
19 $this->assertFalse(isset($object->foobar));
20 unset($object->bar);
21 $this->assertFalse(isset($object->bar));
22 }
23
24 public function testBasicOffset() {
25 $config = ["foo" => "bar", "bar" => "foo"];
26 $object = new Config($config);
27 $this->assertEquals("bar", $object["foo"]);
28 $this->assertEquals("foo", $object["bar"]);
29 $this->assertTrue(isset($object["foo"]));
30 $this->assertFalse(isset($object["foobar"]));
31 unset($object["bar"]);
32 $this->assertFalse(isset($object["bar"]));
33 }
34
35 public function testBasicSection() {
36 $config = [
37 "primary" => [
38 "foo" => "bar",
39 "bar" => "foo",
40 ],
41 "secondary : primary" => [
42 "bar" => "foo2",
43 "baz" => "sec"
44 ],
45 "tertiary : secondary" => [
46 "bar" => "foo3",
47 "bat" => "tri"
48 ],
49 "alternative : primary" => [
50 "bar" => "fux",
51 "baz" => "alt"
52 ]
53 ];
54 $object = new Config($config, "primary");
55 $this->assertEquals($config["primary"], $object->toArray());
56
57 $object = new Config($config, "secondary");
58 $this->assertEquals($config["secondary : primary"] + $config["primary"], $object->toArray());
59
60 $object = new Config($config, "tertiary");
61 $this->assertEquals($config["tertiary : secondary"] + $config["secondary : primary"] + $config["primary"], $object->toArray());
62
63 $object = new Config($config, "alternative");
64 $this->assertEquals($config["alternative : primary"] + $config["primary"], $object->toArray());
65 }
66
67 public function testSetArray() {
68 $config = ["foo" => "bar", "arr" => [1,2,3]];
69 $object = new Config($config);
70 $object["foo"] = [$object->foo, "baz"];
71 $object["arr"][] = 4;
72
73 $this->assertEquals(["bar", "baz"], $object["foo"]->toArray());
74 $this->assertEquals([1,2,3,4], $object["arr"]->toArray());
75
76 $this->assertEquals(["foo"=>["bar","baz"], "arr"=>[1,2,3,4]], $object->toArray());
77 }
78
79 public function testApply() {
80 $config = [
81 "level1" => [
82 "level2" => [
83 "level3" => "123"
84 ],
85 "level2-1" => [
86 "level3-1" => "321"
87 ]
88 ]
89 ];
90 $object = new Config($config);
91 $this->assertEquals("123", $object->level1["level2"]->level3);
92 $reverse = function ($v){return strrev($v);};
93 $object->apply([
94 "level1" => [
95 "level2" => [
96 "level3" => $reverse
97 ],
98 "level2-1" => [
99 "level3-1" => $reverse
100 ]
101 ]
102 ]);
103 $compare = [
104 "level1" => [
105 "level2" => [
106 "level3" => "321"
107 ],
108 "level2-1" => [
109 "level3-1" => "123"
110 ]
111 ]
112 ];
113 $this->assertEquals($compare, $object->toArray());
114
115 $object->apply(function() {
116 return null;
117 });
118 $this->assertEquals(["level1" => null], $object->toArray());
119 }
120
121 public function testIterator() {
122 $config = [
123 "level1-0" => [
124 "level2-0" => "1-0.2-0",
125 "level2-1" => "1-0.2-1",
126 "level2-2" => [
127 "level3" => "1-0.2-2.3"
128 ]
129 ],
130 "level1-1" => [
131 1,2,3
132 ]
133 ];
134 $object = new Config($config);
135 $array = [];
136 foreach (new \RecursiveIteratorIterator($object) as $key => $val) {
137 $array[$key] = $val;
138 }
139 $compare = [
140 'level2-0' => '1-0.2-0',
141 'level2-1' => '1-0.2-1',
142 'level3' => '1-0.2-2.3',
143 1, 2, 3
144 ];
145 $this->assertEquals($compare, $array);
146 }
147 }