Completed
Pull Request — master (#325)
by Paul
08:28
created

WidgetMapBuilderTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 15.79 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 5
Bugs 2 Features 1
Metric Value
wmc 9
c 5
b 2
f 1
lcom 1
cbo 7
dl 15
loc 95
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testBuildPage() 0 17 2
A testBuildPageWithTemplate() 0 20 2
A testBuildPageWithTemplateTemplate() 0 22 2
A newWidgetMap() 15 15 2
A newWidget() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Bundle\WidgetMapBundle\Tests\Builder;
4
5
use Victoire\Bundle\CoreBundle\Entity\View;
6
use Victoire\Bundle\PageBundle\Entity\Page;
7
use Victoire\Bundle\TemplateBundle\Entity\Template;
8
use Victoire\Bundle\WidgetBundle\Entity\Widget;
9
use Victoire\Bundle\WidgetMapBundle\Builder\WidgetMapBuilder;
10
use Victoire\Bundle\WidgetMapBundle\Entity\WidgetMap;
11
use Victoire\Widget\TextBundle\Entity\WidgetText;
12
13
class WidgetMapBuilderTest extends \PHPUnit_Framework_TestCase
14
{
15
    public function testBuildPage()
16
    {
17
        $builder = new WidgetMapBuilder();
18
        $view = new Page();
19
        $widget3 = $this->newWidgetMap(3, null, null, $view, $this->newWidget(3));
20
        $widget2 = $this->newWidgetMap(2, $widget3, WidgetMap::POSITION_BEFORE, $view, $this->newWidget(2));
21
        $widget1 = $this->newWidgetMap(1, $widget2, WidgetMap::POSITION_AFTER, $view, $this->newWidget(1));
0 ignored issues
show
Unused Code introduced by
$widget1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
22
        $widget4 = $this->newWidgetMap(4, $widget3, WidgetMap::POSITION_AFTER, $view, $this->newWidget(4));
0 ignored issues
show
Unused Code introduced by
$widget4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
23
24
        $builtWidgetMap = $builder->build($view);
25
26
        $order = [2, 1, 3, 4];
27
        $i = 0;
28
        foreach ($builtWidgetMap['content'] as $widgetMap) {
29
            $this->assertEquals($order[$i++], $widgetMap->getWidget()->getId());
30
        }
31
    }
32
33
    public function testBuildPageWithTemplate()
34
    {
35
        $builder = new WidgetMapBuilder();
36
        $template = new Template();
37
        $view = new Page();
38
        $view->setTemplate($template);
39
40
        $widget3 = $this->newWidgetMap(3, null, null, $template, $this->newWidget(3));
41
        $widget2 = $this->newWidgetMap(2, $widget3, WidgetMap::POSITION_BEFORE, $view, $this->newWidget(2));
42
        $widget1 = $this->newWidgetMap(1, $widget2, WidgetMap::POSITION_AFTER, $view, $this->newWidget(1));
0 ignored issues
show
Unused Code introduced by
$widget1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
43
        $widget4 = $this->newWidgetMap(4, $widget3, WidgetMap::POSITION_AFTER, $view, $this->newWidget(4));
0 ignored issues
show
Unused Code introduced by
$widget4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
44
45
        $builtWidgetMap = $builder->build($view);
46
47
        $order = [2, 1, 3, 4];
48
        $i = 0;
49
        foreach ($builtWidgetMap['content'] as $widgetMap) {
50
            $this->assertEquals($order[$i++], $widgetMap->getWidget()->getId());
51
        }
52
    }
53
54
    public function testBuildPageWithTemplateTemplate()
55
    {
56
        $builder = new WidgetMapBuilder();
57
        $grandtemplate = new Template();
58
        $template = new Template();
59
        $template->setTemplate($grandtemplate);
60
        $view = new Page();
61
        $view->setTemplate($template);
62
63
        $widget3 = $this->newWidgetMap(3, null, null, $grandtemplate, $this->newWidget(3));
64
        $widget2 = $this->newWidgetMap(2, $widget3, WidgetMap::POSITION_BEFORE, $template, $this->newWidget(2));
65
        $widget1 = $this->newWidgetMap(1, $widget2, WidgetMap::POSITION_AFTER, $view, $this->newWidget(1));
0 ignored issues
show
Unused Code introduced by
$widget1 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66
        $widget4 = $this->newWidgetMap(4, $widget3, WidgetMap::POSITION_AFTER, $view, $this->newWidget(4));
0 ignored issues
show
Unused Code introduced by
$widget4 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
68
        $builtWidgetMap = $builder->build($view);
69
70
        $order = [2, 1, 3, 4];
71
        $i = 0;
72
        foreach ($builtWidgetMap['content'] as $widgetMap) {
73
            $this->assertEquals($order[$i++], $widgetMap->getWidget()->getId());
74
        }
75
    }
76
77
    /**
78
     * @param integer $id
79
     * @param null|WidgetMap $parent
80
     */
81 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...
82
    {
83
        $widgetMap = new WidgetMap();
84
        $widgetMap->setId($id);
85
        if ($parent) {
86
            $widgetMap->setParent($parent);
87
            $parent->addChild($widgetMap);
88
        }
89
        $widgetMap->setPosition($position);
90
        $widgetMap->setWidget($widget);
91
        $widgetMap->setSlot('content');
92
        $view->addWidgetMap($widgetMap);
93
94
        return $widgetMap;
95
    }
96
97
    /**
98
     * @param integer $id
99
     */
100
    protected function newWidget($id)
101
    {
102
        $widget = new WidgetText();
103
        $widget->setId($id);
104
105
        return $widget;
106
    }
107
}
108