Skip to content

Commit 2ba1f68

Browse files
author
Mikaël Capelle
committed
Update navbar tests.
1 parent ac1510b commit 2ba1f68

File tree

1 file changed

+282
-12
lines changed

1 file changed

+282
-12
lines changed

tests/TestCase/View/Helper/BootstrapNavbarHelperTest.php

Lines changed: 282 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,299 @@
99
class BootstrapNavbarHelperTest extends TestCase {
1010

1111
/**
12-
* Setup
12+
* Instance of the BootstrapNavbarHelper.
1313
*
14-
* @return void
14+
* @var BootstrapNavbarHelper
1515
*/
16-
public function setUp() {
17-
parent::setUp();
18-
$this->View = new View();
19-
$this->Navbar = new BootstrapNavbarHelper($this->View);
20-
}
16+
public $navbar;
2117

2218
/**
23-
* Tear Down
19+
* Setup
2420
*
2521
* @return void
2622
*/
27-
public function tearDown() {
28-
parent::tearDown();
29-
unset($this->Navbar);
30-
unset($this->View);
23+
public function setUp() {
24+
parent::setUp();
25+
$view = new View();
26+
$this->navbar = new BootstrapNavbarHelper($view);
3127
}
3228

3329
public function testCreate() {
30+
// Test default:
31+
$result = $this->navbar->create(null);
32+
$expected = [
33+
['nav' => [
34+
'class' => 'navbar navbar-default'
35+
]],
36+
['div' => [
37+
'class' => 'container'
38+
]],
39+
['div' => [
40+
'class' => 'navbar-header'
41+
]],
42+
'button' => [
43+
'type' => 'button',
44+
'class' => 'navbar-toggle collapsed',
45+
'data-toggle' => 'collapse',
46+
'data-target' => '.navbar-collapse',
47+
'aria-expanded' => 'false'
48+
],
49+
['span' => ['class' => 'sr-only']], __('Toggle navigation'), '/span',
50+
['span' => ['class' => 'icon-bar']], '/span',
51+
['span' => ['class' => 'icon-bar']], '/span',
52+
['span' => ['class' => 'icon-bar']], '/span',
53+
'/button',
54+
'/div',
55+
['div' => [
56+
'class' => 'collapse navbar-collapse'
57+
]]
58+
];
59+
$this->assertHtml($expected, $result);
60+
61+
// Test non responsive:
62+
$result = $this->navbar->create(null, ['responsive' => false]);
63+
$expected = [
64+
['nav' => [
65+
'class' => 'navbar navbar-default'
66+
]],
67+
['div' => [
68+
'class' => 'container'
69+
]]
70+
];
71+
$this->assertHtml($expected, $result);
72+
73+
// Test brand and non responsive:
74+
$result = $this->navbar->create('Brandname', ['responsive' => false]);
75+
$expected = [
76+
['nav' => [
77+
'class' => 'navbar navbar-default'
78+
]],
79+
['div' => [
80+
'class' => 'container'
81+
]],
82+
['div' => [
83+
'class' => 'navbar-header'
84+
]],
85+
['a' => [
86+
'class' => 'navbar-brand',
87+
'href' => '/',
88+
]], 'Brandname', '/a',
89+
'/div',
90+
];
91+
$this->assertHtml($expected, $result);
92+
93+
// Test brand and responsive:
94+
$result = $this->navbar->create('Brandname');
95+
$expected = [
96+
['nav' => [
97+
'class' => 'navbar navbar-default'
98+
]],
99+
['div' => [
100+
'class' => 'container'
101+
]],
102+
['div' => [
103+
'class' => 'navbar-header'
104+
]],
105+
'button' => [
106+
'type' => 'button',
107+
'class' => 'navbar-toggle collapsed',
108+
'data-toggle' => 'collapse',
109+
'data-target' => '.navbar-collapse',
110+
'aria-expanded' => 'false'
111+
],
112+
['span' => ['class' => 'sr-only']], __('Toggle navigation'), '/span',
113+
['span' => ['class' => 'icon-bar']], '/span',
114+
['span' => ['class' => 'icon-bar']], '/span',
115+
['span' => ['class' => 'icon-bar']], '/span',
116+
'/button',
117+
['a' => [
118+
'class' => 'navbar-brand',
119+
'href' => '/',
120+
]], 'Brandname', '/a',
121+
'/div',
122+
['div' => [
123+
'class' => 'collapse navbar-collapse'
124+
]]
125+
];
126+
$this->assertHtml($expected, $result);
127+
128+
// Test fluid
129+
$result = $this->navbar->create(null, ['fluid' => true, 'responsive' => false]);
130+
$expected = [
131+
['nav' => [
132+
'class' => 'navbar navbar-default'
133+
]],
134+
['div' => [
135+
'class' => 'container-fluid'
136+
]]
137+
];
138+
$this->assertHtml($expected, $result);
139+
140+
// Test inverted
141+
$result = $this->navbar->create(null, ['inverse' => true, 'responsive' => false]);
142+
$expected = [
143+
['nav' => [
144+
'class' => 'navbar navbar-inverse'
145+
]],
146+
['div' => [
147+
'class' => 'container'
148+
]]
149+
];
150+
$this->assertHtml($expected, $result);
151+
152+
// Test static
153+
$result = $this->navbar->create(null, ['static' => true, 'responsive' => false]);
154+
$expected = [
155+
['nav' => [
156+
'class' => 'navbar navbar-default navbar-static-top'
157+
]],
158+
['div' => [
159+
'class' => 'container'
160+
]]
161+
];
162+
$this->assertHtml($expected, $result);
163+
164+
// Test fixed top
165+
$result = $this->navbar->create(null, ['fixed' => 'top', 'responsive' => false]);
166+
$expected = [
167+
['nav' => [
168+
'class' => 'navbar navbar-default navbar-fixed-top'
169+
]],
170+
['div' => [
171+
'class' => 'container'
172+
]]
173+
];
174+
$this->assertHtml($expected, $result);
175+
176+
// Test fixed bottom
177+
$result = $this->navbar->create(null, ['fixed' => 'bottom', 'responsive' => false]);
178+
$expected = [
179+
['nav' => [
180+
'class' => 'navbar navbar-default navbar-fixed-bottom'
181+
]],
182+
['div' => [
183+
'class' => 'container'
184+
]]
185+
];
186+
$this->assertHtml($expected, $result);
187+
}
188+
189+
public function testEnd() {
190+
// Test standard end (responsive)
191+
$this->navbar->create(null);
192+
$result = $this->navbar->end();
193+
$expected = ['/div', '/div', '/nav'];
194+
$this->assertHtml($expected, $result);
195+
196+
// Test non-responsive end
197+
$this->navbar->create(null, ['responsive' => false]);
198+
$result = $this->navbar->end();
199+
$expected = ['/div', '/nav'];
200+
$this->assertHtml($expected, $result);
201+
}
202+
203+
public function testButton() {
204+
$result = $this->navbar->button('Click Me!');
205+
$expected = [
206+
['button' => ['class' => 'navbar-btn btn btn-default', 'type' => 'button']],
207+
'Click Me!', '/button'];
208+
$this->assertHtml($expected, $result);
209+
210+
$result = $this->navbar->button('Click Me!', ['class' => 'my-class', 'href' => '/']);
211+
$expected = [
212+
['button' => ['class' => 'my-class navbar-btn btn btn-default',
213+
'href' => '/', 'type' => 'button']],
214+
'Click Me!', '/button'];
215+
$this->assertHtml($expected, $result);
216+
}
217+
218+
public function testText() {
219+
// Normal test
220+
$result = $this->navbar->text('Some text');
221+
$expected = [
222+
['p' => ['class' => 'navbar-text']],
223+
'Some text',
224+
'/p'
225+
];
226+
$this->assertHtml($expected, $result);
227+
228+
// Custom tag test
229+
$result = $this->navbar->text('Some text', ['tag' => 'span']);
230+
$expected = [
231+
['span' => ['class' => 'navbar-text']],
232+
'Some text',
233+
'/span'
234+
];
235+
$this->assertHtml($expected, $result);
236+
237+
// Custom options
238+
$result = $this->navbar->text('Some text', ['class' => 'my-class']);
239+
$expected = [
240+
['p' => ['class' => 'my-class navbar-text']],
241+
'Some text',
242+
'/p'
243+
];
244+
$this->assertHtml($expected, $result);
245+
246+
// Link automatic wrapping
247+
$result = $this->navbar->text('Some text with a <a href="/">link</a>.');
248+
$expected = [
249+
['p' => ['class' => 'navbar-text']],
250+
'Some text with a <a href="/" class="navbar-link">link</a>.',
251+
'/p'
252+
];
253+
$this->assertHtml($expected, $result);
254+
/*
255+
$result = $this->navbar->text(
256+
'Some text with a <a href="/" class="my-class">link</a>.');
257+
$expected = [
258+
['p' => ['class' => 'navbar-text']],
259+
'Some text with a <a href="/" class="my-class navbar-link">link</a>.',
260+
'/p'
261+
];
262+
$this->assertHtml($expected, $result); */
263+
}
264+
265+
public function testMenu() {
266+
// TODO: Add test for this...
267+
$this->navbar->autoActiveLink = false;
268+
// Basic test:
269+
$this->navbar->create(null);
270+
$result = $this->navbar->beginMenu(['class' => 'my-menu']);
271+
$result .= $this->navbar->link('Link', '/', ['class' => 'active']);
272+
$result .= $this->navbar->link('Blog', ['controller' => 'pages', 'action' => 'test']);
273+
$result .= $this->navbar->beginMenu('Dropdown');
274+
$result .= $this->navbar->link('Action');
275+
$result .= $this->navbar->link('Another action');
276+
$result .= $this->navbar->link('Something else here');
277+
$result .= $this->navbar->divider();
278+
$result .= $this->navbar->link('Another action');
279+
$result .= $this->navbar->endMenu();
280+
$result .= $this->navbar->endMenu();
281+
$expected = [
282+
['ul' => ['class' => 'my-menu nav navbar-nav']],
283+
['li' => ['class' => 'active']],
284+
['a' => ['href' => '/']], 'Link', '/a', '/li',
285+
['li' => []],
286+
['a' => ['href' => '/pages/test']], 'Blog', '/a', '/li',
287+
['li' => ['class' => 'dropdown']],
288+
['a' => ['href' => '#', 'class' => 'dropdown-toggle', 'data-toggle' => 'dropdown',
289+
'role' => 'button', 'aria-haspopup' => 'true',
290+
'aria-expanded' => 'false']],
291+
'Dropdown<span class="caret"></span>', '/a',
292+
['ul' => ['class' => 'dropdown-menu']],
293+
['li' => []], ['a' => ['href' => '/']], 'Action', '/a', '/li',
294+
['li' => []], ['a' => ['href' => '/']], 'Another action', '/a', '/li',
295+
['li' => []], ['a' => ['href' => '/']], 'Something else here', '/a', '/li',
296+
['li' => ['role' => 'separator', 'class' => 'divider']], '/li',
297+
['li' => []], ['a' => ['href' => '/']], 'Another action', '/a', '/li',
298+
'/ul',
299+
'/li',
300+
'/ul'
301+
];
302+
$this->assertHtml($expected, $result);
34303

304+
// TODO: Add more tests...
35305
}
36306

37307
};

0 commit comments

Comments
 (0)