Completed
Push — master ( 39043c...2277fd )
by Yonel Ceruto
08:17 queued 31s
created

BreadcrumbsBuilderTest::getCreateFromRequestData()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 90
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
c 4
b 2
f 0
dl 0
loc 90
rs 8.5454
cc 1
eloc 62
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the BreadcrumbsBundle.
5
 *
6
 * (c) Yonel Ceruto <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Yceruto\Bundle\BreadcrumbsBundle\Tests;
13
14
use Symfony\Bundle\FrameworkBundle\Routing\Router;
15
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Routing\RequestContext;
18
use Symfony\Component\Routing\Route;
19
use Symfony\Component\Routing\RouteCollection;
20
use Yceruto\Bundle\BreadcrumbsBundle\BreadcrumbsBuilder;
21
22
class BreadcrumbsBuilderTest extends TestCase
23
{
24
    public function testCreate()
25
    {
26
        $breadcrumbs = $this->createBreadcrumbsBuilder('/', new RouteCollection())
27
            ->create();
28
29
        $this->assertInstanceOf('Yceruto\Bundle\BreadcrumbsBundle\Breadcrumbs', $breadcrumbs);
30
        $this->assertEquals(0, $breadcrumbs->count());
31
    }
32
33
    /**
34
     * @dataProvider getCreateFromRequestData
35
     */
36
    public function testCreateFromRequest($path, $result)
37
    {
38
        $routeCollection = new RouteCollection();
39
        $routeCollection->add('_index', new Route('/'));
40
        $routeCollection->add('_foo', new Route('/foo', array('breadcrumbs_label' => 'Foo')));
41
        $routeCollection->add('_foo_bar', new Route('/foo/bar'));
42
        $routeCollection->add('_bar', new Route('/bar/'));
43
        $routeCollection->add('_bar_show', new Route('/bar/{id}'));
44
        $routeCollection->add('_bar_action', new Route('/bar/{id}/{action}'));
45
46
        $breadcrumbs = $this->createBreadcrumbsBuilder($path, $routeCollection)
47
            ->createFromRequest();
48
49
        $this->assertCount(count($result['nodes']), $breadcrumbs->getNodes());
50
        $nodes = $breadcrumbs->getNodes();
51
        $node = current($nodes);
52
        foreach ($result['nodes'] as $path => $label) {
53
            $this->assertEquals($path, $node->getPath());
54
            $this->assertEquals($label, $node->getLabel());
55
            $node = next($nodes);
56
        }
57
    }
58
59
    public function getCreateFromRequestData()
60
    {
61
        return array(
62
            'index' => array(
63
                '/',
64
                array('nodes' => array('/' => 'breadcrumbs._index')),
65
            ),
66
            'foo' => array(
67
                '/foo',
68
                array(
69
                    'nodes' => array(
70
                        '/' => 'breadcrumbs._index',
71
                        '/foo' => 'Foo',
72
                    ),
73
                ),
74
            ),
75
            'foo_bar' => array(
76
                '/foo/bar',
77
                array(
78
                    'nodes' => array(
79
                        '/' => 'breadcrumbs._index',
80
                        '/foo' => 'Foo',
81
                        '/foo/bar' => 'bar',
82
                    ),
83
                ),
84
            ),
85
            'bar' => array(
86
                '/bar/',
87
                array(
88
                    'nodes' => array(
89
                        '/' => 'breadcrumbs._index',
90
                        '/bar/' => 'bar',
91
                    ),
92
                ),
93
            ),
94
            'bar_show' => array(
95
                '/bar/baz',
96
                array(
97
                    'nodes' => array(
98
                        '/' => 'breadcrumbs._index',
99
                        '/bar/' => 'bar',
100
                        '/bar/baz' => 'baz',
101
                    ),
102
                ),
103
            ),
104
            'bar_int_show' => array(
105
                '/bar/1',
106
                array(
107
                    'nodes' => array(
108
                        '/' => 'breadcrumbs._index',
109
                        '/bar/' => 'bar',
110
                        '/bar/1' => 'breadcrumbs._bar_show',
111
                    ),
112
                ),
113
            ),
114
            'bar_action' => array(
115
                '/bar/baz/edit',
116
                array(
117
                    'nodes' => array(
118
                        '/' => 'breadcrumbs._index',
119
                        '/bar/' => 'bar',
120
                        '/bar/baz' => 'baz',
121
                        '/bar/baz/edit' => 'edit',
122
                    ),
123
                ),
124
            ),
125
            'bar_int_action' => array(
126
                '/bar/1/edit',
127
                array(
128
                    'nodes' => array(
129
                        '/' => 'breadcrumbs._index',
130
                        '/bar/' => 'bar',
131
                        '/bar/1' => 'breadcrumbs._bar_show',
132
                        '/bar/1/edit' => 'edit',
133
                    ),
134
                ),
135
            ),
136
            'bar_int2_action' => array(
137
                '/bar/1/2',
138
                array(
139
                    'nodes' => array(
140
                        '/' => 'breadcrumbs._index',
141
                        '/bar/' => 'bar',
142
                        '/bar/1' => 'breadcrumbs._bar_show',
143
                        '/bar/1/2' => 'breadcrumbs._bar_action',
144
                    ),
145
                ),
146
            ),
147
        );
148
    }
149
150
    /**
151
     * Create a Breadcrumbs Builder.
152
     *
153
     * @param string          $path
154
     * @param RouteCollection $collection
155
     *
156
     * @return BreadcrumbsBuilder
157
     */
158
    private function createBreadcrumbsBuilder($path, RouteCollection $collection)
159
    {
160
        /** @var \PHPUnit_Framework_MockObject_MockObject|Router $route */
161
        $route = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Routing\Router')
162
            ->disableOriginalConstructor()
163
            ->getMock();
164
        $route->method('getRouteCollection')->willReturn($collection);
165
        $route->method('getContext')->willReturn(new RequestContext());
166
167
        $request = Request::create($path);
168
169
        if (class_exists('Symfony\Component\HttpFoundation\RequestStack')) {
170
            $requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')
171
                ->disableOriginalConstructor()
172
                ->getMock();
173
            $requestStack->method('getCurrentRequest')->willReturn($request);
174
        } else {
175
            $requestStack = null;
176
        }
177
178
        $breadcrumbsBuilder = new BreadcrumbsBuilder($route, $requestStack);
179
180
        // BC with SF 2.3
181
        if (null === $requestStack) {
182
            $breadcrumbsBuilder->setRequest($request);
183
        }
184
185
        return $breadcrumbsBuilder;
186
    }
187
}
188