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