Vous êtes connecté en tant que anonymous Se Deconnecter
Browse code

Application modulaire fonctionnelle !

Emmanuel ROY authored on 12/08/2019 15:10:25
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,337 +0,0 @@
1
-<?php declare(strict_types=1);
2
-/**
3
- * Part of Windwalker project Test files.  @codingStandardsIgnoreStart
4
- *
5
- * @copyright  Copyright (C) 2019 SMS Taiwan, Inc.
6
- * @license    GNU General Public License version 2 or later; see LICENSE
7
- */
8
-
9
-namespace Windwalker\Structure\Test;
10
-
11
-use Windwalker\Structure\Structure;
12
-use Windwalker\Structure\StructureHelper;
13
-use Windwalker\Structure\Test\Stubs\StubDumpable;
14
-
15
-/**
16
- * Test class of StructureHelper
17
- *
18
- * @since 2.1
19
- */
20
-class StructureHelperTest extends \PHPUnit\Framework\TestCase
21
-{
22
-    /**
23
-     * Sets up the fixture, for example, opens a network connection.
24
-     * This method is called before a test is executed.
25
-     *
26
-     * @return void
27
-     */
28
-    protected function setUp(): void
29
-    {
30
-    }
31
-
32
-    /**
33
-     * Tears down the fixture, for example, closes a network connection.
34
-     * This method is called after a test is executed.
35
-     *
36
-     * @return void
37
-     */
38
-    protected function tearDown(): void
39
-    {
40
-    }
41
-
42
-    /**
43
-     * Method to test isAssociativeArray().
44
-     *
45
-     * @return void
46
-     *
47
-     * @covers \Windwalker\Structure\StructureHelper::isAssociativeArray
48
-     */
49
-    public function testIsAssociativeArray()
50
-    {
51
-        $this->assertFalse(StructureHelper::isAssociativeArray(['a', 'b']));
52
-
53
-        $this->assertTrue(StructureHelper::isAssociativeArray([1, 2, 'a' => 'b', 'c', 'd']));
54
-    }
55
-
56
-    /**
57
-     * Method to test toObject().
58
-     *
59
-     * @return void
60
-     *
61
-     * @covers \Windwalker\Structure\StructureHelper::toObject
62
-     */
63
-    public function testToObject()
64
-    {
65
-        $data = StructureHelper::toObject(['foo' => 'bar']);
66
-
67
-        $this->assertIsObject($data);
68
-
69
-        $this->assertEquals('bar', $data->foo);
70
-
71
-        $data = StructureHelper::toObject(['foo' => 'bar'], 'ArrayObject');
72
-
73
-        $this->assertInstanceOf('ArrayObject', $data);
74
-
75
-        $data = StructureHelper::toObject(['foo' => ['bar' => 'baz']]);
76
-
77
-        $this->assertEquals('baz', $data->foo->bar);
78
-    }
79
-
80
-    /**
81
-     * Method to test getByPath().
82
-     *
83
-     * @return void
84
-     *
85
-     * @covers \Windwalker\Structure\StructureHelper::getByPath
86
-     */
87
-    public function testGetByPath()
88
-    {
89
-        $data = [
90
-            'flower' => 'sakura',
91
-            'olive' => 'peace',
92
-            'pos1' => [
93
-                'sunflower' => 'love',
94
-            ],
95
-            'pos2' => [
96
-                'cornflower' => 'elegant',
97
-            ],
98
-            'array' => [
99
-                'A',
100
-                'B',
101
-                'C',
102
-            ],
103
-        ];
104
-
105
-        $this->assertEquals('sakura', StructureHelper::getByPath($data, 'flower'));
106
-        $this->assertEquals('love', StructureHelper::getByPath($data, 'pos1.sunflower'));
107
-        $this->assertEquals('love', StructureHelper::getByPath($data, 'pos1/sunflower', '/'));
108
-        $this->assertEquals($data['array'], StructureHelper::getByPath($data, 'array'));
109
-        $this->assertNull(StructureHelper::getByPath($data, 'not.exists'));
110
-    }
111
-
112
-    /**
113
-     * Method to test getByPath().
114
-     *
115
-     * @return void
116
-     *
117
-     * @covers \Windwalker\Structure\StructureHelper::getByPath
118
-     */
119
-    public function testGetByPathWithObject()
120
-    {
121
-        $data = [
122
-            'flower' => 'sakura',
123
-            'olive' => 'peace',
124
-            'pos1' => (object) [
125
-                'sunflower' => 'love',
126
-            ],
127
-            'pos2' => new Structure(
128
-                [
129
-                    'cornflower' => 'elegant',
130
-                ]
131
-            ),
132
-            'array' => [
133
-                'A',
134
-                'B',
135
-                'C',
136
-            ],
137
-        ];
138
-
139
-        $this->assertEquals('sakura', StructureHelper::getByPath($data, 'flower'));
140
-        $this->assertEquals('love', StructureHelper::getByPath($data, 'pos1.sunflower'));
141
-        $this->assertEquals('elegant', StructureHelper::getByPath($data, 'pos2.cornflower'));
142
-        $this->assertEquals(null, StructureHelper::getByPath($data, 'pos2.data'));
143
-    }
144
-
145
-    /**
146
-     * Method to test setByPath().
147
-     *
148
-     * @return void
149
-     *
150
-     * @covers \Windwalker\Structure\StructureHelper::setByPath
151
-     */
152
-    public function testSetByPath()
153
-    {
154
-        $data = [];
155
-
156
-        // One level
157
-        $return = StructureHelper::setByPath($data, 'flower', 'sakura');
158
-
159
-        $this->assertEquals('sakura', $data['flower']);
160
-        $this->assertTrue($return);
161
-
162
-        // Multi-level
163
-        StructureHelper::setByPath($data, 'foo.bar', 'test');
164
-
165
-        $this->assertEquals('test', $data['foo']['bar']);
166
-
167
-        // Separator
168
-        StructureHelper::setByPath($data, 'foo/bar', 'play', '/');
169
-
170
-        $this->assertEquals('play', $data['foo']['bar']);
171
-
172
-        // False
173
-        $return = StructureHelper::setByPath($data, '', 'goo');
174
-
175
-        $this->assertFalse($return);
176
-
177
-        // Fix path
178
-        StructureHelper::setByPath($data, 'double..separators', 'value');
179
-
180
-        $this->assertEquals('value', $data['double']['separators']);
181
-    }
182
-
183
-    /**
184
-     * testRemoveByPath
185
-     *
186
-     * @return  void
187
-     */
188
-    public function testRemoveByPath()
189
-    {
190
-        $data = [
191
-            'foo' => [
192
-                'bar' => '123',
193
-            ],
194
-        ];
195
-
196
-        StructureHelper::removeByPath($data, 'foo.bar');
197
-
198
-        $this->assertFalse(array_key_exists('bar', $data['foo']));
199
-
200
-        $data = [
201
-            'foo' => [
202
-                'bar' => '123',
203
-            ],
204
-        ];
205
-
206
-        StructureHelper::removeByPath($data, 'foo');
207
-
208
-        $this->assertFalse(array_key_exists('foo', $data));
209
-
210
-        $data = [
211
-            'foo' => [
212
-                'bar' => '123',
213
-            ],
214
-        ];
215
-
216
-        StructureHelper::removeByPath($data, 'foo.yoo');
217
-
218
-        $this->assertEquals('123', $data['foo']['bar']);
219
-    }
220
-
221
-    /**
222
-     * Method to test getPathNodes().
223
-     *
224
-     * @return void
225
-     *
226
-     * @covers \Windwalker\Structure\StructureHelper::getPathNodes
227
-     */
228
-    public function testGetPathNodes()
229
-    {
230
-        $this->assertEquals(['a', 'b', 'c'], StructureHelper::getPathNodes('a..b.c'));
231
-        $this->assertEquals(['a', 'b', 'c'], StructureHelper::getPathNodes('a//b/c', '/'));
232
-    }
233
-
234
-    /**
235
-     * testFlatten
236
-     *
237
-     * @return  void
238
-     *
239
-     * @covers  \Windwalker\Structure\StructureHelper::flatten
240
-     * @since   2.0
241
-     */
242
-    public function testFlatten()
243
-    {
244
-        $array = [
245
-            'flower' => 'sakura',
246
-            'olive' => 'peace',
247
-            'pos1' => [
248
-                'sunflower' => 'love',
249
-            ],
250
-            'pos2' => [
251
-                'cornflower' => 'elegant',
252
-            ],
253
-        ];
254
-
255
-        $flatted = StructureHelper::flatten($array);
256
-
257
-        $this->assertEquals($flatted['pos1.sunflower'], 'love');
258
-
259
-        $flatted = StructureHelper::flatten($array, '/');
260
-
261
-        $this->assertEquals($flatted['pos1/sunflower'], 'love');
262
-    }
263
-
264
-    /**
265
-     * Data provider for object inputs
266
-     *
267
-     * @return  array
268
-     *
269
-     * @since   2.0
270
-     */
271
-    public function seedTestToArray()
272
-    {
273
-        return [
274
-            'string' => [
275
-                'foo',
276
-                false,
277
-                ['foo'],
278
-            ],
279
-            'array' => [
280
-                ['foo'],
281
-                false,
282
-                ['foo'],
283
-            ],
284
-            'array_recursive' => [
285
-                [
286
-                    'foo' => [
287
-                        (object) ['bar' => 'bar'],
288
-                        (object) ['baz' => 'baz'],
289
-                    ],
290
-                ],
291
-                true,
292
-                [
293
-                    'foo' => [
294
-                        ['bar' => 'bar'],
295
-                        ['baz' => 'baz'],
296
-                    ],
297
-                ],
298
-            ],
299
-            'iterator' => [
300
-                ['foo' => new \ArrayIterator(['bar' => 'baz'])],
301
-                true,
302
-                ['foo' => ['bar' => 'baz']],
303
-            ],
304
-        ];
305
-    }
306
-
307
-    /**
308
-     * testToArray
309
-     *
310
-     * @param $input
311
-     * @param $recursive
312
-     * @param $expect
313
-     *
314
-     * @return  void
315
-     *
316
-     * @dataProvider  seedTestToArray
317
-     * @covers        \Windwalker\Utilities\ArrayHelper::toArray
318
-     */
319
-    public function testToArray($input, $recursive, $expect)
320
-    {
321
-        $this->assertEquals($expect, StructureHelper::toArray($input, $recursive));
322
-    }
323
-
324
-    public function testDumpObjectValue()
325
-    {
326
-        $data = new StubDumpable(new StubDumpable());
327
-
328
-        $dumped = StructureHelper::dumpObjectValues($data);
329
-
330
-        $this->assertEquals('foo', $dumped['foo']);
331
-        $this->assertEquals('bar', $dumped['bar']);
332
-        $this->assertNull($dumped['data']['self']);
333
-        $this->assertEquals(StructureHelper::dumpObjectValues(new StubDumpable()), $dumped['data']['new']);
334
-        $this->assertEquals(['sakura', 'rose'], $dumped['data']['flower']);
335
-        $this->assertEquals(['wind' => 'walker'], $dumped['iterator']);
336
-    }
337
-}
Browse code

initial commit

Emmanuel ROY authored on 09/08/2019 08:39:02
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,337 @@
1
+<?php declare(strict_types=1);
2
+/**
3
+ * Part of Windwalker project Test files.  @codingStandardsIgnoreStart
4
+ *
5
+ * @copyright  Copyright (C) 2019 SMS Taiwan, Inc.
6
+ * @license    GNU General Public License version 2 or later; see LICENSE
7
+ */
8
+
9
+namespace Windwalker\Structure\Test;
10
+
11
+use Windwalker\Structure\Structure;
12
+use Windwalker\Structure\StructureHelper;
13
+use Windwalker\Structure\Test\Stubs\StubDumpable;
14
+
15
+/**
16
+ * Test class of StructureHelper
17
+ *
18
+ * @since 2.1
19
+ */
20
+class StructureHelperTest extends \PHPUnit\Framework\TestCase
21
+{
22
+    /**
23
+     * Sets up the fixture, for example, opens a network connection.
24
+     * This method is called before a test is executed.
25
+     *
26
+     * @return void
27
+     */
28
+    protected function setUp(): void
29
+    {
30
+    }
31
+
32
+    /**
33
+     * Tears down the fixture, for example, closes a network connection.
34
+     * This method is called after a test is executed.
35
+     *
36
+     * @return void
37
+     */
38
+    protected function tearDown(): void
39
+    {
40
+    }
41
+
42
+    /**
43
+     * Method to test isAssociativeArray().
44
+     *
45
+     * @return void
46
+     *
47
+     * @covers \Windwalker\Structure\StructureHelper::isAssociativeArray
48
+     */
49
+    public function testIsAssociativeArray()
50
+    {
51
+        $this->assertFalse(StructureHelper::isAssociativeArray(['a', 'b']));
52
+
53
+        $this->assertTrue(StructureHelper::isAssociativeArray([1, 2, 'a' => 'b', 'c', 'd']));
54
+    }
55
+
56
+    /**
57
+     * Method to test toObject().
58
+     *
59
+     * @return void
60
+     *
61
+     * @covers \Windwalker\Structure\StructureHelper::toObject
62
+     */
63
+    public function testToObject()
64
+    {
65
+        $data = StructureHelper::toObject(['foo' => 'bar']);
66
+
67
+        $this->assertIsObject($data);
68
+
69
+        $this->assertEquals('bar', $data->foo);
70
+
71
+        $data = StructureHelper::toObject(['foo' => 'bar'], 'ArrayObject');
72
+
73
+        $this->assertInstanceOf('ArrayObject', $data);
74
+
75
+        $data = StructureHelper::toObject(['foo' => ['bar' => 'baz']]);
76
+
77
+        $this->assertEquals('baz', $data->foo->bar);
78
+    }
79
+
80
+    /**
81
+     * Method to test getByPath().
82
+     *
83
+     * @return void
84
+     *
85
+     * @covers \Windwalker\Structure\StructureHelper::getByPath
86
+     */
87
+    public function testGetByPath()
88
+    {
89
+        $data = [
90
+            'flower' => 'sakura',
91
+            'olive' => 'peace',
92
+            'pos1' => [
93
+                'sunflower' => 'love',
94
+            ],
95
+            'pos2' => [
96
+                'cornflower' => 'elegant',
97
+            ],
98
+            'array' => [
99
+                'A',
100
+                'B',
101
+                'C',
102
+            ],
103
+        ];
104
+
105
+        $this->assertEquals('sakura', StructureHelper::getByPath($data, 'flower'));
106
+        $this->assertEquals('love', StructureHelper::getByPath($data, 'pos1.sunflower'));
107
+        $this->assertEquals('love', StructureHelper::getByPath($data, 'pos1/sunflower', '/'));
108
+        $this->assertEquals($data['array'], StructureHelper::getByPath($data, 'array'));
109
+        $this->assertNull(StructureHelper::getByPath($data, 'not.exists'));
110
+    }
111
+
112
+    /**
113
+     * Method to test getByPath().
114
+     *
115
+     * @return void
116
+     *
117
+     * @covers \Windwalker\Structure\StructureHelper::getByPath
118
+     */
119
+    public function testGetByPathWithObject()
120
+    {
121
+        $data = [
122
+            'flower' => 'sakura',
123
+            'olive' => 'peace',
124
+            'pos1' => (object) [
125
+                'sunflower' => 'love',
126
+            ],
127
+            'pos2' => new Structure(
128
+                [
129
+                    'cornflower' => 'elegant',
130
+                ]
131
+            ),
132
+            'array' => [
133
+                'A',
134
+                'B',
135
+                'C',
136
+            ],
137
+        ];
138
+
139
+        $this->assertEquals('sakura', StructureHelper::getByPath($data, 'flower'));
140
+        $this->assertEquals('love', StructureHelper::getByPath($data, 'pos1.sunflower'));
141
+        $this->assertEquals('elegant', StructureHelper::getByPath($data, 'pos2.cornflower'));
142
+        $this->assertEquals(null, StructureHelper::getByPath($data, 'pos2.data'));
143
+    }
144
+
145
+    /**
146
+     * Method to test setByPath().
147
+     *
148
+     * @return void
149
+     *
150
+     * @covers \Windwalker\Structure\StructureHelper::setByPath
151
+     */
152
+    public function testSetByPath()
153
+    {
154
+        $data = [];
155
+
156
+        // One level
157
+        $return = StructureHelper::setByPath($data, 'flower', 'sakura');
158
+
159
+        $this->assertEquals('sakura', $data['flower']);
160
+        $this->assertTrue($return);
161
+
162
+        // Multi-level
163
+        StructureHelper::setByPath($data, 'foo.bar', 'test');
164
+
165
+        $this->assertEquals('test', $data['foo']['bar']);
166
+
167
+        // Separator
168
+        StructureHelper::setByPath($data, 'foo/bar', 'play', '/');
169
+
170
+        $this->assertEquals('play', $data['foo']['bar']);
171
+
172
+        // False
173
+        $return = StructureHelper::setByPath($data, '', 'goo');
174
+
175
+        $this->assertFalse($return);
176
+
177
+        // Fix path
178
+        StructureHelper::setByPath($data, 'double..separators', 'value');
179
+
180
+        $this->assertEquals('value', $data['double']['separators']);
181
+    }
182
+
183
+    /**
184
+     * testRemoveByPath
185
+     *
186
+     * @return  void
187
+     */
188
+    public function testRemoveByPath()
189
+    {
190
+        $data = [
191
+            'foo' => [
192
+                'bar' => '123',
193
+            ],
194
+        ];
195
+
196
+        StructureHelper::removeByPath($data, 'foo.bar');
197
+
198
+        $this->assertFalse(array_key_exists('bar', $data['foo']));
199
+
200
+        $data = [
201
+            'foo' => [
202
+                'bar' => '123',
203
+            ],
204
+        ];
205
+
206
+        StructureHelper::removeByPath($data, 'foo');
207
+
208
+        $this->assertFalse(array_key_exists('foo', $data));
209
+
210
+        $data = [
211
+            'foo' => [
212
+                'bar' => '123',
213
+            ],
214
+        ];
215
+
216
+        StructureHelper::removeByPath($data, 'foo.yoo');
217
+
218
+        $this->assertEquals('123', $data['foo']['bar']);
219
+    }
220
+
221
+    /**
222
+     * Method to test getPathNodes().
223
+     *
224
+     * @return void
225
+     *
226
+     * @covers \Windwalker\Structure\StructureHelper::getPathNodes
227
+     */
228
+    public function testGetPathNodes()
229
+    {
230
+        $this->assertEquals(['a', 'b', 'c'], StructureHelper::getPathNodes('a..b.c'));
231
+        $this->assertEquals(['a', 'b', 'c'], StructureHelper::getPathNodes('a//b/c', '/'));
232
+    }
233
+
234
+    /**
235
+     * testFlatten
236
+     *
237
+     * @return  void
238
+     *
239
+     * @covers  \Windwalker\Structure\StructureHelper::flatten
240
+     * @since   2.0
241
+     */
242
+    public function testFlatten()
243
+    {
244
+        $array = [
245
+            'flower' => 'sakura',
246
+            'olive' => 'peace',
247
+            'pos1' => [
248
+                'sunflower' => 'love',
249
+            ],
250
+            'pos2' => [
251
+                'cornflower' => 'elegant',
252
+            ],
253
+        ];
254
+
255
+        $flatted = StructureHelper::flatten($array);
256
+
257
+        $this->assertEquals($flatted['pos1.sunflower'], 'love');
258
+
259
+        $flatted = StructureHelper::flatten($array, '/');
260
+
261
+        $this->assertEquals($flatted['pos1/sunflower'], 'love');
262
+    }
263
+
264
+    /**
265
+     * Data provider for object inputs
266
+     *
267
+     * @return  array
268
+     *
269
+     * @since   2.0
270
+     */
271
+    public function seedTestToArray()
272
+    {
273
+        return [
274
+            'string' => [
275
+                'foo',
276
+                false,
277
+                ['foo'],
278
+            ],
279
+            'array' => [
280
+                ['foo'],
281
+                false,
282
+                ['foo'],
283
+            ],
284
+            'array_recursive' => [
285
+                [
286
+                    'foo' => [
287
+                        (object) ['bar' => 'bar'],
288
+                        (object) ['baz' => 'baz'],
289
+                    ],
290
+                ],
291
+                true,
292
+                [
293
+                    'foo' => [
294
+                        ['bar' => 'bar'],
295
+                        ['baz' => 'baz'],
296
+                    ],
297
+                ],
298
+            ],
299
+            'iterator' => [
300
+                ['foo' => new \ArrayIterator(['bar' => 'baz'])],
301
+                true,
302
+                ['foo' => ['bar' => 'baz']],
303
+            ],
304
+        ];
305
+    }
306
+
307
+    /**
308
+     * testToArray
309
+     *
310
+     * @param $input
311
+     * @param $recursive
312
+     * @param $expect
313
+     *
314
+     * @return  void
315
+     *
316
+     * @dataProvider  seedTestToArray
317
+     * @covers        \Windwalker\Utilities\ArrayHelper::toArray
318
+     */
319
+    public function testToArray($input, $recursive, $expect)
320
+    {
321
+        $this->assertEquals($expect, StructureHelper::toArray($input, $recursive));
322
+    }
323
+
324
+    public function testDumpObjectValue()
325
+    {
326
+        $data = new StubDumpable(new StubDumpable());
327
+
328
+        $dumped = StructureHelper::dumpObjectValues($data);
329
+
330
+        $this->assertEquals('foo', $dumped['foo']);
331
+        $this->assertEquals('bar', $dumped['bar']);
332
+        $this->assertNull($dumped['data']['self']);
333
+        $this->assertEquals(StructureHelper::dumpObjectValues(new StubDumpable()), $dumped['data']['new']);
334
+        $this->assertEquals(['sakura', 'rose'], $dumped['data']['flower']);
335
+        $this->assertEquals(['wind' => 'walker'], $dumped['iterator']);
336
+    }
337
+}