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 testGeneratorInference() |
37
|
|
|
{ |
38
|
|
|
$route = 'random_route_id'; |
39
|
|
|
$generator = new CartesianDestinationGenerator(); |
40
|
|
|
|
41
|
|
|
$destinations = $generator->generate( |
42
|
|
|
$route, |
43
|
|
|
['source_arg' => new CollectionSource(new ArrayCollection([1, 2, 3]))], |
44
|
|
|
['filler_1' => $this->createCloneFiller(['source_arg'])] |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
self::assertCount(3, $destinations); |
48
|
|
|
foreach ($destinations as $destination) { |
49
|
|
|
self::assertSame($route, $destination->getRoute()); |
50
|
|
|
self::assertNotNull($destination->resolve('source_arg')); |
51
|
|
|
self::assertNotNull($destination->resolve('filler_1')); |
52
|
|
|
self::assertSame($destination->resolve('source_arg'), $destination->resolve('filler_1')); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testCartesianTransform() |
57
|
|
|
{ |
58
|
|
|
$route = 'random_route_id'; |
59
|
|
|
$generator = new CartesianDestinationGenerator(); |
60
|
|
|
|
61
|
|
|
$destinations = $generator->generate( |
62
|
|
|
$route, |
63
|
|
|
[ |
64
|
|
|
'source_arg' => new CollectionSource(new ArrayCollection([1, 2, 3])), |
65
|
|
|
'source_arg_2' => new CollectionSource(new ArrayCollection([1, 2, 3])), |
66
|
|
|
] |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
self::assertCount(9, $destinations); |
70
|
|
|
foreach ($destinations as $destination) { |
71
|
|
|
self::assertSame($route, $destination->getRoute()); |
72
|
|
|
self::assertNotNull($destination->resolve('source_arg')); |
73
|
|
|
self::assertNotNull($destination->resolve('source_arg_2')); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testFilteredCartesianTransform() |
78
|
|
|
{ |
79
|
|
|
$route = 'random_route_id'; |
80
|
|
|
$generator = new CartesianDestinationGenerator(); |
81
|
|
|
|
82
|
|
|
$source1 = new CollectionSource(new ArrayCollection([1, 2, 3])); |
83
|
|
|
$source2 = new CollectionSource(new ArrayCollection([1, 2, 3])); |
84
|
|
|
|
85
|
|
|
$source1->withCondition(new ExactCondition(1)); |
86
|
|
|
$source2->withCondition(new ExactCondition(3)); |
87
|
|
|
|
88
|
|
|
$destinations = $generator->generate( |
89
|
|
|
$route, |
90
|
|
|
[ |
91
|
|
|
'source_arg' => $source1, |
92
|
|
|
'source_arg_2' => $source2, |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
self::assertCount(1, $destinations); |
97
|
|
|
$destination = array_shift($destinations); |
98
|
|
|
self::assertSame($route, $destination->getRoute()); |
99
|
|
|
self::assertSame(1, $destination->resolve('source_arg')); |
100
|
|
|
self::assertSame(3, $destination->resolve('source_arg_2')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function createCloneFiller(array $dependencies) |
104
|
|
|
{ |
105
|
|
|
return new CallbackFiller( |
106
|
|
|
$dependencies, |
107
|
|
|
function ($items) { |
108
|
|
|
return array_shift($items); |
109
|
|
|
} |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|