Chris@17
|
1 <?php
|
Chris@17
|
2
|
Chris@17
|
3 /*
|
Chris@17
|
4 * This file is a part of dflydev/dot-access-data.
|
Chris@17
|
5 *
|
Chris@17
|
6 * (c) Dragonfly Development Inc.
|
Chris@17
|
7 *
|
Chris@17
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@17
|
9 * file that was distributed with this source code.
|
Chris@17
|
10 */
|
Chris@17
|
11
|
Chris@17
|
12 namespace Dflydev\DotAccessData;
|
Chris@17
|
13
|
Chris@17
|
14 class UtilTest extends \PHPUnit_Framework_TestCase
|
Chris@17
|
15 {
|
Chris@17
|
16 public function testIsAssoc()
|
Chris@17
|
17 {
|
Chris@17
|
18 $this->assertTrue(Util::isAssoc(array('a' => 'A',)));
|
Chris@17
|
19 $this->assertTrue(Util::isAssoc(array()));
|
Chris@17
|
20 $this->assertFalse(Util::isAssoc(array(1 => 'One',)));
|
Chris@17
|
21 }
|
Chris@17
|
22
|
Chris@17
|
23 /**
|
Chris@17
|
24 * @dataProvider mergeAssocArrayProvider
|
Chris@17
|
25 */
|
Chris@17
|
26 public function testMergeAssocArray($message, $to, $from, $clobber, $expectedResult)
|
Chris@17
|
27 {
|
Chris@17
|
28 $result = Util::mergeAssocArray($to, $from, $clobber);
|
Chris@17
|
29 $this->assertEquals($expectedResult, $result, $message);
|
Chris@17
|
30 }
|
Chris@17
|
31
|
Chris@17
|
32 public function mergeAssocArrayProvider()
|
Chris@17
|
33 {
|
Chris@17
|
34 return array(
|
Chris@17
|
35
|
Chris@17
|
36 array(
|
Chris@17
|
37 'Clobber should replace to value with from value for strings (shallow)',
|
Chris@17
|
38 // to
|
Chris@17
|
39 array('a' => 'A'),
|
Chris@17
|
40 // from
|
Chris@17
|
41 array('a' => 'B'),
|
Chris@17
|
42 // clobber
|
Chris@17
|
43 true,
|
Chris@17
|
44 // expected result
|
Chris@17
|
45 array('a' => 'B'),
|
Chris@17
|
46 ),
|
Chris@17
|
47
|
Chris@17
|
48 array(
|
Chris@17
|
49 'Clobber should replace to value with from value for strings (deep)',
|
Chris@17
|
50 // to
|
Chris@17
|
51 array('a' => array('b' => 'B',),),
|
Chris@17
|
52 // from
|
Chris@17
|
53 array('a' => array('b' => 'C',),),
|
Chris@17
|
54 // clobber
|
Chris@17
|
55 true,
|
Chris@17
|
56 // expected result
|
Chris@17
|
57 array('a' => array('b' => 'C',),),
|
Chris@17
|
58 ),
|
Chris@17
|
59
|
Chris@17
|
60 array(
|
Chris@17
|
61 'Clobber should NOTreplace to value with from value for strings (shallow)',
|
Chris@17
|
62 // to
|
Chris@17
|
63 array('a' => 'A'),
|
Chris@17
|
64 // from
|
Chris@17
|
65 array('a' => 'B'),
|
Chris@17
|
66 // clobber
|
Chris@17
|
67 false,
|
Chris@17
|
68 // expected result
|
Chris@17
|
69 array('a' => 'A'),
|
Chris@17
|
70 ),
|
Chris@17
|
71
|
Chris@17
|
72 array(
|
Chris@17
|
73 'Clobber should NOT replace to value with from value for strings (deep)',
|
Chris@17
|
74 // to
|
Chris@17
|
75 array('a' => array('b' => 'B',),),
|
Chris@17
|
76 // from
|
Chris@17
|
77 array('a' => array('b' => 'C',),),
|
Chris@17
|
78 // clobber
|
Chris@17
|
79 false,
|
Chris@17
|
80 // expected result
|
Chris@17
|
81 array('a' => array('b' => 'B',),),
|
Chris@17
|
82 ),
|
Chris@17
|
83
|
Chris@17
|
84 array(
|
Chris@17
|
85 'Associative arrays should be combined',
|
Chris@17
|
86 // to
|
Chris@17
|
87 array('a' => array('b' => 'B',),),
|
Chris@17
|
88 // from
|
Chris@17
|
89 array('a' => array('c' => 'C',),),
|
Chris@17
|
90 // clobber
|
Chris@17
|
91 null,
|
Chris@17
|
92 // expected result
|
Chris@17
|
93 array('a' => array('b' => 'B', 'c' => 'C',),),
|
Chris@17
|
94 ),
|
Chris@17
|
95
|
Chris@17
|
96 array(
|
Chris@17
|
97 'Arrays should be replaced (with clobber enabled)',
|
Chris@17
|
98 // to
|
Chris@17
|
99 array('a' => array('b', 'c',)),
|
Chris@17
|
100 // from
|
Chris@17
|
101 array('a' => array('B', 'C',),),
|
Chris@17
|
102 // clobber
|
Chris@17
|
103 true,
|
Chris@17
|
104 // expected result
|
Chris@17
|
105 array('a' => array('B', 'C',),),
|
Chris@17
|
106 ),
|
Chris@17
|
107
|
Chris@17
|
108 array(
|
Chris@17
|
109 'Arrays should be NOT replaced (with clobber disabled)',
|
Chris@17
|
110 // to
|
Chris@17
|
111 array('a' => array('b', 'c',)),
|
Chris@17
|
112 // from
|
Chris@17
|
113 array('a' => array('B', 'C',),),
|
Chris@17
|
114 // clobber
|
Chris@17
|
115 false,
|
Chris@17
|
116 // expected result
|
Chris@17
|
117 array('a' => array('b', 'c',),),
|
Chris@17
|
118 ),
|
Chris@17
|
119 );
|
Chris@17
|
120 }
|
Chris@17
|
121 }
|