Completed
Pull Request — master (#325)
by Paul
08:12 queued 49s
created

WidgetMapManagerTest::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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('Victoire\Bundle\WidgetMapBundle\Repository\WidgetMapRepository');
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
    protected function moveWidgetMap($builtWidgetMap, $order, $view, $manager, $builder)
50
    {
51
        $sortedWidget = [
52
            'widgetMapReference' => null,
53
            'position'           => null,
54
            'slot'               => 'content',
55
            'widgetMap'          => null,
56
        ];
57
58
        for ($i = 1; $i <= 1000; $i++) {
59
            $buildSortedWidget = function ($builtWidgetMap) use (&$order, &$buildSortedWidget, $view) {
60
61
                $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...
62
                $availablePositions = [];
63
                $positions = [WidgetMap::POSITION_AFTER, WidgetMap::POSITION_BEFORE];
64
                $shuffled = $builtWidgetMap['content'];
65
                shuffle($shuffled);
66
                foreach ($shuffled as $widgetMap) {
67
                    if ($widgetMap->getId() !== $sortedWidget['widgetMap']->getId()) {
68
                        foreach ($positions as $position) {
69
                            if (!$widgetMap->hasChild($position, $view)) {
70
                                $availablePositions[] = [
71
                                    'widgetMapReference' => $widgetMap,
72
                                    'position'           => $position,
73
                                ];
74
                                if (array_rand([0, 1]) === 0) {
75
                                    break;
76
                                }
77
                            }
78
                        }
79
                    }
80
                }
81
82
                $randomPosition = $availablePositions[array_rand($availablePositions)];
83
                $offset = array_search(
84
                        $randomPosition['widgetMapReference']->getWidget()->getId(),
85
                        $order
86
                    ) + ($randomPosition['position'] == WidgetMap::POSITION_AFTER ? 1 : 0);
87
                if (!empty($order[$offset]) && $order[$offset] == $sortedWidget['widgetMap']->getId()) {
88
                    return $buildSortedWidget($builtWidgetMap);
89
                }
90
91
                $sortedWidget = array_merge($sortedWidget, $randomPosition);
92
93
                $order[array_search($sortedWidget['widgetMap']->getWidget()->getId(), $order)] = null;
94
                $offset = array_search(
95
                        $sortedWidget['widgetMapReference']->getWidget()->getId(),
96
                        $order
97
                    ) + ($sortedWidget['position'] == WidgetMap::POSITION_AFTER ? 1 : 0);
98
                array_splice($order, $offset, 0, $sortedWidget['widgetMap']->getWidget()->getId());
99
100
                unset($order[array_search(null, $order)]);
101
102
                $order = array_values($order);
103
                $sortedWidget['widgetMap'] = $sortedWidget['widgetMap']->getId();
104
                $sortedWidget['widgetMapReference'] = $sortedWidget['widgetMapReference']->getId();
105
106
                return $sortedWidget;
107
108
            };
109
110
            $sortedWidget = array_merge($sortedWidget, $buildSortedWidget($builtWidgetMap));
111
112
            $manager->move($view, $sortedWidget);
113
            $newBuiltWidgetMap = $builder->build($view);
114
115
            $newOrder = [];
116
            foreach ($newBuiltWidgetMap['content'] as $newWidgetMap) {
117
                $newOrder[] = $newWidgetMap->getWidget()->getId();
118
            }
119
120
            $this->assertEquals($order, $newOrder,
121
                sprintf("move widget %s %s widget %s didn't worked at iteration %s",
122
                    $sortedWidget['widgetMap'], $sortedWidget['position'], $sortedWidget['widgetMapReference'], $i));
123
124
            $builtWidgetMap = $newBuiltWidgetMap;
125
        }
126
    }
127
128 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...
129
    {
130
        $widgetMap = new WidgetMap();
131
        $widgetMap->setId($id);
132
        if ($parent) {
133
            $widgetMap->setParent($parent);
134
        }
135
        $widgetMap->setPosition($position);
136
        $widgetMap->setWidget($widget);
137
        $widgetMap->setSlot('content');
138
        $widgetMap->setAction(WidgetMap::ACTION_CREATE);
139
        $view->addWidgetMap($widgetMap);
140
141
        return $widgetMap;
142
    }
143
144
    protected function newWidget($id)
145
    {
146
        $widget = new WidgetText();
147
        $widget->setId($id);
148
149
        return $widget;
150
    }
151
152
    protected function setup()
153
    {
154
        $this->prophet = new \Prophecy\Prophet();
155
    }
156
157
    protected function tearDown()
158
    {
159
        $this->prophet->checkPredictions();
160
    }
161
}
162