1 | <?php |
||
13 | class ArrayHelperTest extends PHPUnit_Framework_TestCase |
||
14 | { |
||
15 | public function testGetValueFromObject() |
||
16 | { |
||
17 | $post = new Post(); |
||
18 | |||
19 | $id = ArrayHelper::getValue($post, 'id'); |
||
20 | $title = ArrayHelper::getValue($post, 'title'); |
||
21 | |||
22 | $this->assertEquals($post->id, $id); |
||
23 | $this->assertEquals($post->title, $title); |
||
24 | } |
||
25 | |||
26 | public function testRemove() |
||
27 | { |
||
28 | $array = ['name' => 'b', 'age' => 3]; |
||
29 | $name = ArrayHelper::remove($array, 'name'); |
||
30 | $this->assertEquals($name, 'b'); |
||
31 | $this->assertEquals($array, ['age' => 3]); |
||
32 | $default = ArrayHelper::remove($array, 'nonExisting', 'defaultValue'); |
||
33 | $this->assertEquals('defaultValue', $default); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * @dataProvider valueProvider |
||
38 | */ |
||
39 | public function testGetValue($key, $expected, $default = null) |
||
40 | { |
||
41 | $array = [ |
||
42 | 'name' => 'test', |
||
43 | 'date' => '31-12-2116', |
||
44 | 'post' => [ |
||
45 | 'id' => 5, |
||
46 | 'author' => [ |
||
47 | 'name' => 'Darth', |
||
48 | 'profile' => [ |
||
49 | 'title' => 'Sith', |
||
50 | ], |
||
51 | ], |
||
52 | ], |
||
53 | 'admin.firstname' => 'Obiwoan', |
||
54 | 'admin.lastname' => 'Kenovi', |
||
55 | 'admin' => [ |
||
56 | 'lastname' => 'Vader', |
||
57 | ], |
||
58 | 'version' => [ |
||
59 | '1.0' => [ |
||
60 | 'status' => 'released', |
||
61 | ], |
||
62 | ], |
||
63 | ]; |
||
64 | $this->assertEquals($expected, ArrayHelper::getValue($array, $key, $default)); |
||
65 | } |
||
66 | |||
67 | public function valueProvider() |
||
68 | { |
||
69 | return [ |
||
70 | ['name', 'test'], |
||
71 | ['noname', null], |
||
72 | ['noname', 'test', 'test'], |
||
73 | ['post.id', 5], |
||
74 | ['post.id', 5, 'test'], |
||
75 | ['nopost.id', null], |
||
76 | ['nopost.id', 'test', 'test'], |
||
77 | ['post.author.name', 'Darth'], |
||
78 | ['post.author.noname', null], |
||
79 | ['post.author.noname', 'test', 'test'], |
||
80 | ['post.author.profile.title', 'Sith'], |
||
81 | ['admin.firstname', 'Obiwoan'], |
||
82 | ['admin.firstname', 'Obiwoan', 'test'], |
||
83 | ['admin.lastname', 'Kenovi'], |
||
84 | [ |
||
85 | function ($array, $defaultValue) { |
||
86 | return $array['date'] . $defaultValue; |
||
87 | }, |
||
88 | '31-12-2116test', |
||
89 | 'test', |
||
90 | ], |
||
91 | [['version', '1.0', 'status'], 'released'], |
||
92 | [['version', '1.0', 'date'], 'defaultValue', 'defaultValue'], |
||
93 | ]; |
||
94 | } |
||
95 | } |
||
96 |