|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bundle\WidgetMapBundle\Tests\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use Victoire\Bundle\CoreBundle\Entity\View; |
|
6
|
|
|
use Victoire\Bundle\PageBundle\Entity\Page; |
|
7
|
|
|
use Victoire\Bundle\WidgetBundle\Entity\Widget; |
|
8
|
|
|
use Victoire\Bundle\WidgetMapBundle\Builder\WidgetMapBuilder; |
|
9
|
|
|
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap; |
|
10
|
|
|
use Victoire\Bundle\WidgetMapBundle\Manager\WidgetMapManager; |
|
11
|
|
|
use Victoire\Widget\TextBundle\Entity\WidgetText; |
|
12
|
|
|
|
|
13
|
|
|
class WidgetMapManagerTest extends \PHPUnit_Framework_TestCase |
|
14
|
|
|
{ |
|
15
|
|
|
private $prophet; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
public function testMove() |
|
18
|
|
|
{ |
|
19
|
|
|
$builder = new WidgetMapBuilder(); |
|
20
|
|
|
$view = new Page(); |
|
21
|
|
|
$widgetMap3 = $this->newWidgetMap(3, null, null, $view, $this->newWidget(3)); |
|
22
|
|
|
$widgetMap2 = $this->newWidgetMap(2, $widgetMap3, WidgetMap::POSITION_BEFORE, $view, $this->newWidget(2)); |
|
23
|
|
|
$widgetMap1 = $this->newWidgetMap(1, $widgetMap2, WidgetMap::POSITION_AFTER, $view, $this->newWidget(1)); |
|
24
|
|
|
$widgetMap4 = $this->newWidgetMap(4, $widgetMap3, WidgetMap::POSITION_AFTER, $view, $this->newWidget(4)); |
|
25
|
|
|
|
|
26
|
|
|
$em = $this->prophet->prophesize('Doctrine\ORM\EntityManager'); |
|
27
|
|
|
$widgetMapRepo = $this->prophet->prophesize('Doctrine\ORM\EntityRepository'); |
|
28
|
|
|
|
|
29
|
|
|
$em->getRepository('VictoireWidgetMapBundle:WidgetMap')->willReturn($widgetMapRepo); |
|
30
|
|
|
|
|
31
|
|
|
$widgetMapRepo->find(1)->willReturn($widgetMap1); |
|
32
|
|
|
$widgetMapRepo->find(2)->willReturn($widgetMap2); |
|
33
|
|
|
$widgetMapRepo->find(3)->willReturn($widgetMap3); |
|
34
|
|
|
$widgetMapRepo->find(4)->willReturn($widgetMap4); |
|
35
|
|
|
|
|
36
|
|
|
$manager = new WidgetMapManager($em->reveal(), $builder); |
|
37
|
|
|
|
|
38
|
|
|
$builtWidgetMap = $builder->build($view); |
|
39
|
|
|
|
|
40
|
|
|
$order = [2, 1, 3, 4]; |
|
41
|
|
|
$i = 0; |
|
42
|
|
|
foreach ($builtWidgetMap['content'] as $widgetMap) { |
|
43
|
|
|
$this->assertEquals($order[$i++], $widgetMap->getWidget()->getId()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->moveWidgetMap($builtWidgetMap, $order, $view, $manager, $builder); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param integer[] $order |
|
51
|
|
|
* @param Page $view |
|
52
|
|
|
* @param WidgetMapManager $manager |
|
53
|
|
|
* @param WidgetMapBuilder $builder |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function moveWidgetMap($builtWidgetMap, $order, $view, $manager, $builder) |
|
56
|
|
|
{ |
|
57
|
|
|
$sortedWidget = [ |
|
58
|
|
|
'parentWidgetMap' => null, |
|
59
|
|
|
'position' => null, |
|
60
|
|
|
'slot' => 'content', |
|
61
|
|
|
'widgetMap' => null, |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
for ($i = 1; $i <= 1000; $i++) { |
|
65
|
|
|
$buildSortedWidget = function ($builtWidgetMap) use (&$order, &$buildSortedWidget, $view) { |
|
66
|
|
|
|
|
67
|
|
|
$sortedWidget['widgetMap'] = $builtWidgetMap['content'][array_rand($builtWidgetMap['content'])]; |
|
|
|
|
|
|
68
|
|
|
$availablePositions = []; |
|
69
|
|
|
$positions = [WidgetMap::POSITION_AFTER, WidgetMap::POSITION_BEFORE]; |
|
70
|
|
|
$shuffled = $builtWidgetMap['content']; |
|
71
|
|
|
shuffle($shuffled); |
|
72
|
|
|
foreach ($shuffled as $widgetMap) { |
|
73
|
|
|
if ($widgetMap->getId() !== $sortedWidget['widgetMap']->getId()) { |
|
74
|
|
|
foreach ($positions as $position) { |
|
75
|
|
|
if (!$widgetMap->hasChild($position, $view)) { |
|
76
|
|
|
$availablePositions[] = [ |
|
77
|
|
|
'parentWidgetMap' => $widgetMap, |
|
78
|
|
|
'position' => $position, |
|
79
|
|
|
]; |
|
80
|
|
|
if (array_rand([0, 1]) === 0) { |
|
81
|
|
|
break; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$randomPosition = $availablePositions[array_rand($availablePositions)]; |
|
89
|
|
|
$offset = array_search( |
|
90
|
|
|
$randomPosition['parentWidgetMap']->getWidget()->getId(), |
|
91
|
|
|
$order |
|
92
|
|
|
) + ($randomPosition['position'] == WidgetMap::POSITION_AFTER ? 1 : 0); |
|
93
|
|
|
if (!empty($order[$offset]) && $order[$offset] == $sortedWidget['widgetMap']->getId()) { |
|
94
|
|
|
return $buildSortedWidget($builtWidgetMap); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$sortedWidget = array_merge($sortedWidget, $randomPosition); |
|
98
|
|
|
|
|
99
|
|
|
$order[array_search($sortedWidget['widgetMap']->getWidget()->getId(), $order)] = null; |
|
100
|
|
|
$offset = array_search( |
|
101
|
|
|
$sortedWidget['parentWidgetMap']->getWidget()->getId(), |
|
102
|
|
|
$order |
|
103
|
|
|
) + ($sortedWidget['position'] == WidgetMap::POSITION_AFTER ? 1 : 0); |
|
104
|
|
|
array_splice($order, $offset, 0, $sortedWidget['widgetMap']->getWidget()->getId()); |
|
105
|
|
|
|
|
106
|
|
|
unset($order[array_search(null, $order)]); |
|
107
|
|
|
|
|
108
|
|
|
$order = array_values($order); |
|
109
|
|
|
$sortedWidget['widgetMap'] = $sortedWidget['widgetMap']->getId(); |
|
110
|
|
|
$sortedWidget['parentWidgetMap'] = $sortedWidget['parentWidgetMap']->getId(); |
|
111
|
|
|
|
|
112
|
|
|
return $sortedWidget; |
|
113
|
|
|
|
|
114
|
|
|
}; |
|
115
|
|
|
|
|
116
|
|
|
$sortedWidget = array_merge($sortedWidget, $buildSortedWidget($builtWidgetMap)); |
|
117
|
|
|
|
|
118
|
|
|
$manager->move($view, $sortedWidget); |
|
119
|
|
|
$newBuiltWidgetMap = $builder->build($view); |
|
120
|
|
|
|
|
121
|
|
|
$newOrder = []; |
|
122
|
|
|
foreach ($newBuiltWidgetMap['content'] as $newWidgetMap) { |
|
123
|
|
|
$newOrder[] = $newWidgetMap->getWidget()->getId(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$this->assertEquals($order, $newOrder, |
|
127
|
|
|
sprintf("move widget %s %s widget %s didn't worked at iteration %s", |
|
128
|
|
|
$sortedWidget['widgetMap'], $sortedWidget['position'], $sortedWidget['parentWidgetMap'], $i)); |
|
129
|
|
|
|
|
130
|
|
|
$builtWidgetMap = $newBuiltWidgetMap; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param integer $id |
|
136
|
|
|
* @param null|WidgetMap $parent |
|
137
|
|
|
*/ |
|
138
|
|
View Code Duplication |
protected function newWidgetMap($id, $parent, $position, View $view, Widget $widget) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
$widgetMap = new WidgetMap(); |
|
141
|
|
|
$widgetMap->setId($id); |
|
142
|
|
|
if ($parent) { |
|
143
|
|
|
$widgetMap->setParent($parent); |
|
144
|
|
|
} |
|
145
|
|
|
$widgetMap->setPosition($position); |
|
146
|
|
|
$widgetMap->setWidget($widget); |
|
147
|
|
|
$widgetMap->setSlot('content'); |
|
148
|
|
|
$widgetMap->setAction(WidgetMap::ACTION_CREATE); |
|
149
|
|
|
$view->addWidgetMap($widgetMap); |
|
150
|
|
|
|
|
151
|
|
|
return $widgetMap; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param integer $id |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function newWidget($id) |
|
158
|
|
|
{ |
|
159
|
|
|
$widget = new WidgetText(); |
|
160
|
|
|
$widget->setId($id); |
|
161
|
|
|
|
|
162
|
|
|
return $widget; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
protected function setup() |
|
166
|
|
|
{ |
|
167
|
|
|
$this->prophet = new \Prophecy\Prophet(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
protected function tearDown() |
|
171
|
|
|
{ |
|
172
|
|
|
$this->prophet->checkPredictions(); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|