Completed
Push — fetcher_factories ( 10a56f...fd93ea )
by David
13:44
created

SplashUrlNodeTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 196
Duplicated Lines 43.88 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 5
Bugs 0 Features 0
Metric Value
dl 86
loc 196
c 5
b 0
f 0
wmc 14
lcom 2
cbo 4
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A testUnsupportedWildcard() 0 7 1
A testDoubleMethod() 9 9 1
A testDoubleWildcardMethod() 9 9 1
A testDoubleParameter() 0 9 1
A testSameUrls() 10 10 1
A testDoubleWildcardMethodWithHttpMethod() 9 9 1
A testAddUrl() 13 13 1
A testTrailingSlashUrl() 12 12 1
A testRootUrl() 12 12 1
A testGlobalUrlCatchGet() 12 12 1
A testMultiUrls() 0 16 1
A testParametersUrls() 0 13 1
B testWildcardUrls() 0 25 1
A testFallbackToWilcard() 0 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Mouf\Mvc\Splash\Store;
4
5
use Mouf\Mvc\Splash\Services\SplashRoute;
6
use Mouf\Mvc\Splash\Utils\SplashException;
7
use Zend\Diactoros\ServerRequest;
8
9
/**
10
 * A SplashUrlNode is a datastructure optimised to navigate all possible URLs known to the application.
11
 * A SplashUrlNode represents all possible routes starting at the current position (just after a / in a URL).
12
 *
13
 * @author David Negrier
14
 */
15
class SplashUrlNodeTest extends \PHPUnit_Framework_TestCase
16
{
17 View Code Duplication
    public function testAddUrl()
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...
18
    {
19
        $splashUrlNode = new SplashUrlNode();
20
        $callback = new SplashRoute('toto/tata', 'myController', 'myMethod', 'myTitle', 'fullComment', array('GET', 'POST'));
21
        $splashUrlNode->registerCallback($callback);
22
23
        $result = $splashUrlNode->walk('toto/tata', new ServerRequest([], [], 'toto/tata', 'GET'));
24
25
        /* @var $result SplashRoute */
26
        $this->assertInstanceOf(SplashRoute::class, $result);
27
        $this->assertEquals('myController', $result->getControllerInstanceName());
28
        $this->assertEquals('myMethod', $result->getMethodName());
29
    }
30
31 View Code Duplication
    public function testTrailingSlashUrl()
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...
32
    {
33
        $splashUrlNode = new SplashUrlNode();
34
        $callback = new SplashRoute('toto/tata/', 'myController', 'myMethod', 'myTitle', 'fullComment', array('GET', 'POST'));
35
        $splashUrlNode->registerCallback($callback);
36
37
        $result = $splashUrlNode->walk('toto/tata/', new ServerRequest([], [], 'toto/tata', 'GET'));
38
        /* @var $result SplashRoute */
39
        $this->assertInstanceOf(SplashRoute::class, $result);
40
        $this->assertEquals('myController', $result->getControllerInstanceName());
41
        $this->assertEquals('myMethod', $result->getMethodName());
42
    }
43
44 View Code Duplication
    public function testRootUrl()
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...
45
    {
46
        $splashUrlNode = new SplashUrlNode();
47
        $callback = new SplashRoute('/', 'myController', 'myMethod', 'myTitle', 'fullComment', array('GET', 'POST'));
48
        $splashUrlNode->registerCallback($callback);
49
50
        $result = $splashUrlNode->walk('/', new ServerRequest([], [], '/', 'GET'));
51
        /* @var $result SplashRoute */
52
        $this->assertInstanceOf(SplashRoute::class, $result);
53
        $this->assertEquals('myController', $result->getControllerInstanceName());
54
        $this->assertEquals('myMethod', $result->getMethodName());
55
    }
56
57 View Code Duplication
    public function testSameUrls()
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...
58
    {
59
        $splashUrlNode = new SplashUrlNode();
60
        $callback = new SplashRoute('/', 'myController', 'myMethod', 'myTitle', 'fullComment', array('GET', 'POST'));
61
        $splashUrlNode->registerCallback($callback);
62
        $callback = new SplashRoute('/', 'myController', 'myMethod', 'myTitle', 'fullComment', array('GET', 'POST'));
63
64
        $this->expectException(SplashException::class);
65
        $splashUrlNode->registerCallback($callback);
66
    }
67
68
    /**
69
     *
70
     */
71 View Code Duplication
    public function testGlobalUrlCatchGet()
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...
72
    {
73
        $splashUrlNode = new SplashUrlNode();
74
        $callback = new SplashRoute('/toto', 'myController', 'myMethod', 'myTitle', 'fullComment', array());
75
        $splashUrlNode->registerCallback($callback);
76
77
        $result = $splashUrlNode->walk('/toto', new ServerRequest([], [], '/toto', 'GET'));
78
        /* @var $result SplashRoute */
79
        $this->assertInstanceOf(SplashRoute::class, $result);
80
        $this->assertEquals('myController', $result->getControllerInstanceName());
81
        $this->assertEquals('myMethod', $result->getMethodName());
82
    }
83
84
    /**
85
     *
86
     */
87
    public function testMultiUrls()
88
    {
89
        $splashUrlNode = new SplashUrlNode();
90
        $callback = new SplashRoute('/toto', 'myControllerOk', 'myMethodOk', 'myTitle', 'fullComment', array());
91
        $splashUrlNode->registerCallback($callback);
92
        $callback = new SplashRoute('/toto/tata', 'myController', 'myMethod', 'myTitle', 'fullComment', array());
93
        $splashUrlNode->registerCallback($callback);
94
        $callback = new SplashRoute('/tata', 'myController', 'myMethod', 'myTitle', 'fullComment', array());
95
        $splashUrlNode->registerCallback($callback);
96
97
        $result = $splashUrlNode->walk('/toto', new ServerRequest([], [], '/toto', 'POST'));
98
        /* @var $result SplashRoute */
99
        $this->assertInstanceOf(SplashRoute::class, $result);
100
        $this->assertEquals('myControllerOk', $result->getControllerInstanceName());
101
        $this->assertEquals('myMethodOk', $result->getMethodName());
102
    }
103
104
    /**
105
     *
106
     */
107
    public function testParametersUrls()
108
    {
109
        $splashUrlNode = new SplashUrlNode();
110
        $callback = new SplashRoute('/toto/{var}/tata', 'myController', 'myMethod', 'myTitle', 'fullComment', []);
111
        $splashUrlNode->registerCallback($callback);
112
113
        $result = $splashUrlNode->walk('/toto/12/tata', new ServerRequest([], [], '/toto/12/tata', 'POST'));
114
        /* @var $result SplashRoute */
115
        $this->assertInstanceOf(SplashRoute::class, $result);
116
        $this->assertEquals('myController', $result->getControllerInstanceName());
117
        $this->assertEquals('myMethod', $result->getMethodName());
118
        $this->assertEquals(12, $result->getFilledParameters()['var']);
119
    }
120
121
    /**
122
     *
123
     */
124
    public function testWildcardUrls()
125
    {
126
        $splashUrlNode = new SplashUrlNode();
127
        $callback = new SplashRoute('/toto/*', 'myController', 'myMethod', 'myTitle', 'fullComment', array());
128
        $splashUrlNode->registerCallback($callback);
129
        $callback2 = new SplashRoute('/toto/*', 'myControllerPost', 'myMethodPost', 'myTitle', 'fullComment', array('POST'));
130
        $splashUrlNode->registerCallback($callback2);
131
132
        $result = $splashUrlNode->walk('/toto/tata/titi', new ServerRequest([], [], '/toto', 'GET'));
133
        /* @var $result SplashRoute */
134
        $this->assertInstanceOf(SplashRoute::class, $result);
135
        $this->assertEquals('myController', $result->getControllerInstanceName());
136
        $this->assertEquals('myMethod', $result->getMethodName());
137
138
        $result = $splashUrlNode->walk('/toto/', new ServerRequest([], [], '/toto', 'GET'));
139
        /* @var $result SplashRoute */
140
        $this->assertInstanceOf(SplashRoute::class, $result);
141
        $this->assertEquals('myController', $result->getControllerInstanceName());
142
        $this->assertEquals('myMethod', $result->getMethodName());
143
144
        // Now, let's test an URL with HTTP method set.
145
        $result = $splashUrlNode->walk('/toto/tata/titi', new ServerRequest([], [], '/toto', 'POST'));
146
        $this->assertEquals('myControllerPost', $result->getControllerInstanceName());
147
        $this->assertEquals('myMethodPost', $result->getMethodName());
148
    }
149
150
    public function testUnsupportedWildcard()
151
    {
152
        $splashUrlNode = new SplashUrlNode();
153
        $callback = new SplashRoute('toto/*/tata', 'myController', 'myMethod', 'myTitle', 'myComment');
154
        $this->expectException(SplashException::class);
155
        $splashUrlNode->registerCallback($callback);
156
    }
157
158 View Code Duplication
    public function testDoubleMethod()
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...
159
    {
160
        $splashUrlNode = new SplashUrlNode();
161
        $callback = new SplashRoute('foo/bar', 'myController', 'myMethod', 'myTitle', 'myComment');
162
        $splashUrlNode->registerCallback($callback);
163
        $callback2 = new SplashRoute('foo/bar', 'myController2', 'myMethod2', 'myTitle', 'myComment');
164
        $this->expectException(SplashException::class);
165
        $splashUrlNode->registerCallback($callback2);
166
    }
167
168 View Code Duplication
    public function testDoubleWildcardMethod()
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...
169
    {
170
        $splashUrlNode = new SplashUrlNode();
171
        $callback = new SplashRoute('foo/*', 'myController', 'myMethod', 'myTitle', 'myComment');
172
        $splashUrlNode->registerCallback($callback);
173
        $callback2 = new SplashRoute('foo/*', 'myController2', 'myMethod2', 'myTitle', 'myComment');
174
        $this->expectException(SplashException::class);
175
        $splashUrlNode->registerCallback($callback2);
176
    }
177
178 View Code Duplication
    public function testDoubleWildcardMethodWithHttpMethod()
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...
179
    {
180
        $splashUrlNode = new SplashUrlNode();
181
        $callback = new SplashRoute('foo/*', 'myController', 'myMethod', 'myTitle', 'fullComment', ['GET', 'POST']);
182
        $splashUrlNode->registerCallback($callback);
183
        $callback2 = new SplashRoute('foo/*', 'myController2', 'myMethod2', 'myTitle', 'fullComment', ['GET']);
184
        $this->expectException(SplashException::class);
185
        $splashUrlNode->registerCallback($callback2);
186
    }
187
188
    public function testDoubleParameter()
189
    {
190
        $splashUrlNode = new SplashUrlNode();
191
        $callback = new SplashRoute('foo/{var}/bar/{var}/', 'myController', 'myMethod', 'myTitle', 'myComment');
192
        $splashUrlNode->registerCallback($callback);
193
194
        $this->expectException(SplashException::class);
195
        $splashUrlNode->walk('foo/12/bar/42/', new ServerRequest([], [], '/toto', 'GET'));
196
    }
197
198
    public function testFallbackToWilcard()
199
    {
200
        $splashUrlNode = new SplashUrlNode();
201
        $callback = new SplashRoute('foo/bar/baz', 'myController', 'myMethod', 'myTitle', 'fullComment');
202
        $splashUrlNode->registerCallback($callback);
203
        $callback2 = new SplashRoute('foo/*', 'myController2', 'myMethod2', 'myTitle', 'fullComment');
204
        $splashUrlNode->registerCallback($callback2);
205
206
        $result = $splashUrlNode->walk('foo/bar', new ServerRequest([], [], '/foo/bar/biz', 'POST'));
207
        $this->assertEquals('myController2', $result->getControllerInstanceName());
208
        $this->assertEquals('myMethod2', $result->getMethodName());
209
    }
210
}
211