testCreateBreadCrumbWithEmptyRequestAndEmptyMenu()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SumoCoders\FrameworkCoreBundle\Tests\BreadCrumb;
4
5
use Knp\Menu\FactoryInterface;
6
use Knp\Menu\MenuItem;
7
use SumoCoders\FrameworkCoreBundle\BreadCrumb\BreadCrumbBuilder;
8
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\RequestStack;
11
12
class BreadCrumbBuilderTest extends \PHPUnit_Framework_TestCase
13
{
14
    /**
15
     * @var BreadCrumbBuilder
16
     */
17
    protected $breadCrumbBuilder;
18
19
    /**
20
     * @inherit
21
     */
22
    protected function tearDown()
23
    {
24
        $this->breadCrumbBuilder = null;
25
    }
26
27
    /**
28
     * @return \PHPUnit_Framework_MockObject_MockObject
29
     */
30
    protected function getEventDispatcher()
31
    {
32
        return $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
33
    }
34
35
    /**
36
     * @return \PHPUnit_Framework_MockObject_MockObject
37
     */
38 View Code Duplication
    protected function getFactory($item)
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...
39
    {
40
        /** @var \PHPUnit_Framework_MockObject_MockBuilder $factory */
41
        $factory = $this->getMockBuilder(FactoryInterface::class)->getMock();
42
        $factory->method('createItem')
43
            ->will(
44
                $this->returnValue(
45
                    $item
46
                )
47
            );
48
49
        return $factory;
50
    }
51
52
    protected function createSimpleBreadCrumb()
53
    {
54
        $item = new MenuItem(
55
            'root',
56
            $this->getMockBuilder(FactoryInterface::class)->getMock()
57
        );
58
        $factory = $this->getFactory($item);
59
60
        $this->breadCrumbBuilder = new BreadCrumbBuilder(
61
            $factory,
62
            $this->getEventDispatcher()
63
        );
64
    }
65
66 View Code Duplication
    public function testCreateBreadCrumbWithEmptyRequestAndEmptyMenu()
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...
67
    {
68
        $this->createSimpleBreadCrumb();
69
70
        $requestStack = $this->getMockBuilder(RequestStack::class)
71
            ->getMock();
72
        $requestStack->method('getCurrentRequest')
73
            ->willReturn(
74
                $this->getMockBuilder(Request::class)->getMock()
75
            );
76
77
        $breadCrumb = $this->breadCrumbBuilder->createBreadCrumb($requestStack);
78
79
        $this->assertTrue($breadCrumb->hasChildren());
80
        $this->assertEquals(1, count($breadCrumb->getChildren()));
81
    }
82
83 View Code Duplication
    public function testIfLastItemDoesNotHaveAnUri()
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...
84
    {
85
        $this->createSimpleBreadCrumb();
86
        $requestStack = $this->getMockBuilder(RequestStack::class)
87
            ->getMock();
88
        $requestStack->method('getCurrentRequest')
89
            ->willReturn(
90
                $this->getMockBuilder(Request::class)->getMock()
91
            );
92
93
        $breadCrumb = $this->breadCrumbBuilder->createBreadCrumb($requestStack);
94
95
        $lastChild = $breadCrumb->getLastChild();
96
        $this->assertNull($lastChild->getUri());
97
    }
98
99 View Code Duplication
    public function testIfBreadCrumbIsEmptyWhenDontExtraFromTheRequestIsEnabled()
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...
100
    {
101
        $this->createSimpleBreadCrumb();
102
        $this->breadCrumbBuilder->dontExtractFromTheRequest();
103
104
        $requestStack = $this->getMockBuilder(RequestStack::class)
105
            ->getMock();
106
        $requestStack->method('getCurrentRequest')
107
            ->willReturn(
108
                $this->getMockBuilder(Request::class)->getMock()
109
            );
110
111
        $breadCrumb = $this->breadCrumbBuilder->createBreadCrumb($requestStack);
112
113
        $this->assertFalse($breadCrumb->hasChildren());
114
    }
115
116
    public function testIfSimpleItemIsAdded()
117
    {
118
        $this->createSimpleBreadCrumb();
119
        $requestStack = $this->getMockBuilder(RequestStack::class)
120
            ->getMock();
121
        $requestStack->method('getCurrentRequest')
122
            ->willReturn(
123
                $this->getMockBuilder(Request::class)->getMock()
124
            );
125
126
127
        $this->breadCrumbBuilder->dontExtractFromTheRequest();
128
        $this->breadCrumbBuilder->addSimpleItem('first', 'http://www.example.org');
129
        $this->breadCrumbBuilder->addSimpleItem('last', 'http://www.example.org');
130
131
        $breadCrumb = $this->breadCrumbBuilder->createBreadCrumb($requestStack);
132
133
        $this->assertEquals(2, count($breadCrumb->getChildren()));
134
135
        $this->assertEquals('first', $breadCrumb->getChild('first')->getLabel());
136
        $this->assertEquals('http://www.example.org', $breadCrumb->getChild('first')->getUri());
137
        $this->assertEquals('last', $breadCrumb->getLastChild()->getLabel());
138
        $this->assertNull($breadCrumb->getLastChild()->getUri());
139
    }
140
141
    public function testIfBreadCrumbHasOnlyHomeWhenItemsIsSetWithEmptyArray()
142
    {
143
        $this->createSimpleBreadCrumb();
144
145
        $this->breadCrumbBuilder->addSimpleItem('first', 'http://www.example.org');
146
        $this->breadCrumbBuilder->overwriteItems([]);
147
148
        $requestStack = $this->getMockBuilder(RequestStack::class)
149
            ->getMock();
150
        $requestStack->method('getCurrentRequest')
151
            ->willReturn(
152
                $this->getMockBuilder(Request::class)->getMock()
153
            );
154
155
        $breadCrumb = $this->breadCrumbBuilder->createBreadCrumb($requestStack);
156
157
        $this->assertEquals(1, count($breadCrumb->getChildren()));
158
    }
159
160
    public function testIfItemIsAdded()
161
    {
162
        $this->createSimpleBreadCrumb();
163
        $requestStack = $this->getMockBuilder(RequestStack::class)
164
            ->getMock();
165
        $requestStack->method('getCurrentRequest')
166
            ->willReturn(
167
                $this->getMockBuilder(Request::class)->getMock()
168
            );
169
170
171
        $first = new MenuItem('first', $this->getFactory(null));
172
        $first->setUri('http://www.example.org');
173
        $this->breadCrumbBuilder->addItem($first);
174
175
        $last = new MenuItem('last', $this->getFactory(null));
176
        $this->breadCrumbBuilder->addItem($last);
177
178
        $this->breadCrumbBuilder->dontExtractFromTheRequest();
179
        $breadCrumb = $this->breadCrumbBuilder->createBreadCrumb($requestStack);
180
181
        $this->assertEquals(2, count($breadCrumb->getChildren()));
182
183
        $this->assertEquals('first', $breadCrumb->getChild('first')->getLabel());
184
        $this->assertEquals('http://www.example.org', $breadCrumb->getChild('first')->getUri());
185
        $this->assertEquals('last', $breadCrumb->getLastChild()->getLabel());
186
        $this->assertNull($breadCrumb->getLastChild()->getUri());
187
    }
188
}
189