Completed
Push — master ( 8413a9...d75c00 )
by Narcotic
10:07
created

ExtReferenceConverterTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 8.9667
c 0
b 0
f 0
cc 1
eloc 36
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
 * ExtReferenceConverterTest class file
4
 */
5
6
namespace Graviton\DocumentBundle\Tests\Service;
7
8
use Graviton\DocumentBundle\Entity\ExtReference;
9
use Graviton\DocumentBundle\Service\ExtReferenceConverter;
10
use Symfony\Component\Routing\Route;
11
use Symfony\Component\Routing\RouteCollection;
12
use Symfony\Component\Routing\RouterInterface;
13
14
/**
15
 * ExtReferenceConverter test
16
 *
17
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
18
 * @license  http://opensource.org/licenses/GPL GPL
19
 * @link     http://swisscom.ch
20
 */
21
class ExtReferenceConverterTest extends \PHPUnit_Framework_TestCase
22
{
23
    /**
24
     * @var RouterInterface|\PHPUnit_Framework_MockObject_MockObject
25
     */
26
    private $router;
27
    /**
28
     * @var RouteCollection|\PHPUnit_Framework_MockObject_MockObject
29
     */
30
    private $collection;
31
    /**
32
     * @var Route[]
33
     */
34
    private $routes;
35
36
    /**
37
     * setup type we want to test
38
     *
39
     * @return void
40
     */
41
    public function setUp()
42
    {
43
        $this->router = $this->getMockBuilder('\Symfony\Bundle\FrameworkBundle\Routing\Router')
44
            ->disableOriginalConstructor()
45
            ->setMethods(['getRouteCollection', 'generate'])
46
            ->getMock();
47
48
        $this->collection = $this->getMockBuilder('\Symfony\Bundle\FrameworkBundle\Routing\RouteCollection')
49
            ->setMethods(['all'])
50
            ->getMock();
51
52
        $this->routes = [
53
            new Route(
54
                '/core/app/{id}',
55
                [
56
                    '_controller' => 'graviton.core.controller.app:getAction',
57
                    '_format' => '~'
58
                ],
59
                [
60
                    'id' => '[a-zA-Z0-9\-_\/]+',
61
                ],
62
                [],
63
                '',
64
                [],
65
                [
66
                    'GET'
67
                ]
68
            ),
69
            new Route(
70
                '/core/app',
71
                [
72
                    '_controller' => 'graviton.core.controller.app.appAction',
73
                    '_format' => '~'
74
                ],
75
                [],
76
                [],
77
                '',
78
                [],
79
                [
80
                    'GET'
81
                ]
82
            ),
83
            new Route(
84
                '/i18n/language/{id}',
85
                [
86
                    '_controller' => 'graviton.i18n.controller.language:getAction',
87
                    '_format' => '~'
88
                ],
89
                [
90
                    'id' => '[a-zA-Z0-9\-_\/]+',
91
                ],
92
                [],
93
                '',
94
                [],
95
                [
96
                    'GET'
97
                ]
98
            ),
99
            new Route(
100
                '/hans/showcase/{id}',
101
                [
102
                    '_controller' => 'gravitondyn.showcase.controller.showcase:getAction',
103
                    '_format' => '~'
104
                ],
105
                [
106
                    'id' => '[a-zA-Z0-9\-_\/]+',
107
                ],
108
                [],
109
                '',
110
                [],
111
                [
112
                    'GET'
113
                ]
114
            ),
115
        ];
116
    }
117
118
    /**
119
     * verify that we get a mongodbref
120
     *
121
     * @dataProvider getExtReferenceProvider
122
     *
123
     * @param string       $url          extref url
124
     * @param ExtReference $extReference extref object
125
     *
126
     * @return void
127
     */
128
    public function testGetExtReference($url, ExtReference $extReference)
129
    {
130
        $this->router
131
            ->expects($this->once())
132
            ->method('getRouteCollection')
133
            ->will($this->returnValue($this->collection));
134
135
        $this->collection
136
            ->expects($this->once())
137
            ->method('all')
138
            ->will($this->returnValue($this->routes));
139
140
        $converter = new ExtReferenceConverter(
141
            $this->router,
142
            [
143
                'App' => 'graviton.core.rest.app',
144
                'Language' => 'graviton.i18n.rest.language',
145
                'ShowCase' => 'gravitondyn.showcase.rest.showcase',
146
            ]
147
        );
148
        $this->assertEquals($extReference, $converter->getExtReference($url));
149
    }
150
151
    /**
152
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string|ExtReference>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
153
     */
154
    public function getExtReferenceProvider()
155
    {
156
        return [
157
            [
158
                'http://localhost/core/app/test',
159
                ExtReference::create('App', 'test'),
160
            ],
161
            [
162
                '/core/app/test',
163
                ExtReference::create('App', 'test'),
164
            ],
165
            [
166
                'http://localhost/hans/showcase/blah',
167
                ExtReference::create('ShowCase', 'blah'),
168
            ],
169
        ];
170
    }
171
172
    /**
173
     * @dataProvider getUrlProvider
174
     *
175
     * @param ExtReference $extReference extref object
176
     * @param string       $routeId      name of route that should get loaded
177
     * @param string       $url          url we expect to result from the conversion
178
     *
179
     * @return void
180
     */
181
    public function testGetUrl(ExtReference $extReference, $routeId, $url)
182
    {
183
        $this->router
184
            ->expects($this->once())
185
            ->method('generate')
186
            ->with(
187
                $routeId,
188
                ['id' => $extReference->getId()]
189
            )
190
            ->will($this->returnValue($url));
191
192
        $converter = new ExtReferenceConverter(
193
            $this->router,
194
            [
195
                'App' => 'graviton.core.rest.app',
196
                'Language' => 'graviton.i18n.rest.language',
197
                'ShowCase' => 'gravitondyn.showcase.rest.showcase',
198
            ]
199
        );
200
        $this->assertEquals($url, $converter->getUrl($extReference));
201
    }
202
203
    /**
204
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<ExtReference|string>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
205
     */
206
    public function getUrlProvider()
207
    {
208
        return [
209
            [
210
                ExtReference::create('App', 'test'),
211
                'graviton.core.rest.app.get',
212
                'http://localhost/core/app/test',
213
            ],
214
            [
215
                ExtReference::create('Language', 'en'),
216
                'graviton.i18n.rest.language.get',
217
                'http://localhost/i18n/language/en',
218
            ],
219
        ];
220
    }
221
}
222