Completed
Pull Request — master (#325)
by Paul
09:55
created

WidgetMapManagerTest::newWidget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
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'])];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$sortedWidget was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sortedWidget = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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