1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Xsolve\AssociateTests\Functional; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Xsolve\Associate\Facade; |
7
|
|
|
use Xsolve\AssociateTests\Functional\Model\Foo; |
8
|
|
|
use Xsolve\AssociateTests\Functional\Model\Bar; |
9
|
|
|
use Xsolve\AssociateTests\Functional\Model\Baz; |
10
|
|
|
|
11
|
|
|
class BasicCollectTest extends TestCase |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param array $baseObjects |
15
|
|
|
* @param array $associationPath |
16
|
|
|
* @param array $expectedAssociatedObjects |
17
|
|
|
* |
18
|
|
|
* @dataProvider provideData_basicTraversal |
19
|
|
|
*/ |
20
|
|
|
public function test_basicTraversal( |
21
|
|
|
array $baseObjects, |
22
|
|
|
array $associationPath, |
23
|
|
|
array $expectedAssociatedObjects |
24
|
|
|
) { |
25
|
|
|
$facade = new Facade(); |
26
|
|
|
$collector = $facade->getBasicCollector(); |
27
|
|
|
|
28
|
|
|
$actualAssociatedObjects = $collector->collect($baseObjects, $associationPath); |
29
|
|
|
|
30
|
|
|
$this->assertArraysAreSimilar($expectedAssociatedObjects, $actualAssociatedObjects); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
public function provideData_basicTraversal() |
37
|
|
|
{ |
38
|
|
|
$text1 = 'lorem ipsum'; |
39
|
|
|
$words1 = explode(' ', $text1); |
40
|
|
|
|
41
|
|
|
$text2 = 'dolor'; |
42
|
|
|
$words2 = explode(' ', $text2); |
43
|
|
|
|
44
|
|
|
$text3 = 'sit amet malef'; |
45
|
|
|
$words3 = explode(' ', 'sit amet malef'); |
46
|
|
|
|
47
|
|
|
$text4 = 'dolor sit'; |
48
|
|
|
$words4 = explode(' ', $text4); |
49
|
|
|
|
50
|
|
|
$foo1 = new Foo( |
51
|
|
|
$bar1 = new Bar([ |
52
|
|
|
$baz1 = new Baz($text1), |
53
|
|
|
$baz2 = new Baz($text2), |
54
|
|
|
]) |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$foo2 = new Foo( |
58
|
|
|
$bar2 = new Bar([ |
59
|
|
|
$baz1, |
60
|
|
|
$baz3 = new Baz($text3), |
61
|
|
|
$baz4 = new Baz($text4), |
62
|
|
|
]) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$foo3 = new Foo(); |
66
|
|
|
|
67
|
|
|
return [ |
68
|
|
|
[ |
69
|
|
|
[$foo1, $foo2, $foo3], |
70
|
|
|
['bar'], |
71
|
|
|
[$bar1, $bar2], |
72
|
|
|
], |
73
|
|
|
[ |
74
|
|
|
[$bar1, $bar2], |
75
|
|
|
['bazs'], |
76
|
|
|
[$baz1, $baz2, $baz3, $baz4], |
77
|
|
|
], |
78
|
|
|
[ |
79
|
|
|
[$foo1, $foo2, $foo3], |
80
|
|
|
['bar', 'bazs'], |
81
|
|
|
[$baz1, $baz2, $baz3, $baz4], |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
[$foo1, $foo2, $foo3], |
85
|
|
|
['bar', 'bazs', 'text'], |
86
|
|
|
[$text1, $text2, $text3, $text4], |
87
|
|
|
], |
88
|
|
|
[ |
89
|
|
|
[$foo1, $foo2, $foo3], |
90
|
|
|
['bar', 'bazs', 'words'], |
91
|
|
|
array_merge($words1, $words2, $words3, $words4), |
92
|
|
|
], |
93
|
|
|
[ |
94
|
|
|
[$foo1, $foo2, $foo3], |
95
|
|
|
['bar', 'bazs', 'textStats', '[wordCount]'], |
96
|
|
|
[count($words1), count($words2), count($words3), count($words4)], |
97
|
|
|
], |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $expected |
103
|
|
|
* @param array $actual |
104
|
|
|
* @param string $message |
105
|
|
|
*/ |
106
|
|
|
protected function assertArraysAreSimilar(array $expected, array $actual, string $message = '') |
107
|
|
|
{ |
108
|
|
|
$this->assertTrue( |
109
|
|
|
$this->areArraysSimilar($expected, $actual), |
110
|
|
|
$message |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $array1 |
116
|
|
|
* @param array $array2 |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
protected function areArraysSimilar(array $array1, array $array2): bool |
121
|
|
|
{ |
122
|
|
|
if (count($array1) !== count($array2)) { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$matchedKeys = []; |
127
|
|
|
foreach ($array1 as $value1) { |
128
|
|
|
foreach ($array2 as $key => $value2) { |
129
|
|
|
if ( |
130
|
|
|
!array_key_exists($key, $matchedKeys) |
131
|
|
|
&& $value2 === $value1 |
132
|
|
|
) { |
133
|
|
|
$matchedKeys[$key] = true; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return count($matchedKeys) == count($array2); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|