1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bankiru\Seo\Tests\Unit; |
4
|
|
|
|
5
|
|
|
use Bankiru\Seo\Destination\CartesianDestinationGenerator; |
6
|
|
|
use Bankiru\Seo\Exception\DestinationException; |
7
|
|
|
use Bankiru\Seo\Integration\Local\CallbackFiller; |
8
|
|
|
use Bankiru\Seo\Integration\Local\CollectionSource; |
9
|
|
|
use Bankiru\Seo\Integration\Local\ExactCondition; |
10
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
11
|
|
|
|
12
|
|
|
class DestinationGeneratorTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
public function testGeneratorCircularityException() |
15
|
|
|
{ |
16
|
|
|
$generator = new CartesianDestinationGenerator(); |
17
|
|
|
try { |
18
|
|
|
$generator->generate( |
19
|
|
|
'random_route_id', |
20
|
|
|
[ |
21
|
|
|
'source_arg' => new CollectionSource(new ArrayCollection([1, 2, 3])), |
22
|
|
|
], |
23
|
|
|
[ |
24
|
|
|
'filler_1' => $this->createCloneFiller(['filler_2']), |
25
|
|
|
'filler_2' => $this->createCloneFiller(['filler_3']), |
26
|
|
|
'filler_3' => $this->createCloneFiller(['filler_1']), |
27
|
|
|
] |
28
|
|
|
); |
29
|
|
|
} catch (DestinationException $exception) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
self::fail('Circular dependency was not detected'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testGeneratorMissingDependencyException() |
37
|
|
|
{ |
38
|
|
|
$generator = new CartesianDestinationGenerator(); |
39
|
|
|
try { |
40
|
|
|
$generator->generate( |
41
|
|
|
'random_route_id', |
42
|
|
|
[ |
43
|
|
|
'source_arg' => new CollectionSource(new ArrayCollection([1, 2, 3])), |
44
|
|
|
], |
45
|
|
|
[ |
46
|
|
|
'filler_1' => $this->createCloneFiller(['source_arg']), |
47
|
|
|
'filler_2' => $this->createCloneFiller(['source_arg_2']), |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
} catch (DestinationException $exception) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
self::fail('Dependency miss was not detected'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testGeneratorInference() |
58
|
|
|
{ |
59
|
|
|
$route = 'random_route_id'; |
60
|
|
|
$generator = new CartesianDestinationGenerator(); |
61
|
|
|
|
62
|
|
|
$destinations = $generator->generate( |
63
|
|
|
$route, |
64
|
|
|
['source_arg' => new CollectionSource(new ArrayCollection([1, 2, 3]))], |
65
|
|
|
['filler_1' => $this->createCloneFiller(['source_arg'])] |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
self::assertCount(3, $destinations); |
69
|
|
|
foreach ($destinations as $destination) { |
70
|
|
|
self::assertSame($route, $destination->getRoute()); |
71
|
|
|
self::assertNotNull($destination->resolve('source_arg')); |
72
|
|
|
self::assertNotNull($destination->resolve('filler_1')); |
73
|
|
|
self::assertSame($destination->resolve('source_arg'), $destination->resolve('filler_1')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
self::assertEquals( |
77
|
|
|
3, |
78
|
|
|
$generator->count(['source_arg' => new CollectionSource(new ArrayCollection([1, 2, 3]))]) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testCartesianTransform() |
83
|
|
|
{ |
84
|
|
|
$route = 'random_route_id'; |
85
|
|
|
$generator = new CartesianDestinationGenerator(); |
86
|
|
|
|
87
|
|
|
$destinations = $generator->generate( |
88
|
|
|
$route, |
89
|
|
|
[ |
90
|
|
|
'source_arg' => new CollectionSource(new ArrayCollection([1, 2, 3])), |
91
|
|
|
'source_arg_2' => new CollectionSource(new ArrayCollection([1, 2, 3])), |
92
|
|
|
] |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
self::assertCount(9, $destinations); |
96
|
|
|
foreach ($destinations as $destination) { |
97
|
|
|
self::assertSame($route, $destination->getRoute()); |
98
|
|
|
self::assertNotNull($destination->resolve('source_arg')); |
99
|
|
|
self::assertNotNull($destination->resolve('source_arg_2')); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function testFilteredCartesianTransform() |
104
|
|
|
{ |
105
|
|
|
$route = 'random_route_id'; |
106
|
|
|
$generator = new CartesianDestinationGenerator(); |
107
|
|
|
|
108
|
|
|
$source1 = new CollectionSource(new ArrayCollection([1, 2, 3])); |
109
|
|
|
$source2 = new CollectionSource(new ArrayCollection([1, 2, 3])); |
110
|
|
|
|
111
|
|
|
$source1->withCondition(new ExactCondition(1)); |
112
|
|
|
$source2->withCondition(new ExactCondition(3)); |
113
|
|
|
|
114
|
|
|
$destinations = $generator->generate( |
115
|
|
|
$route, |
116
|
|
|
[ |
117
|
|
|
'source_arg' => $source1, |
118
|
|
|
'source_arg_2' => $source2, |
119
|
|
|
] |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
self::assertCount(1, $destinations); |
123
|
|
|
$destination = array_shift($destinations); |
124
|
|
|
self::assertSame($route, $destination->getRoute()); |
125
|
|
|
self::assertSame(1, $destination->resolve('source_arg')); |
126
|
|
|
self::assertSame(3, $destination->resolve('source_arg_2')); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function createCloneFiller(array $dependencies) |
130
|
|
|
{ |
131
|
|
|
return new CallbackFiller( |
132
|
|
|
$dependencies, |
133
|
|
|
function ($items) { |
134
|
|
|
return array_shift($items); |
135
|
|
|
} |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|