summaryrefslogtreecommitdiffstats
path: root/tests/Router/Routes/RouteBindingTest.php
blob: 8255b52a4857385f098f3526879cda6f7c997aa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php

namespace Jasny\Router\Routes;

use Jasny\Router\Routes\Glob;
use Jasny\Router\Route;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use InvalidArgumentException;

/**
 * @covers Jasny\Router\Routes\RouteBinding
 */
class RouteBindingTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Test binding simple string when getting route
     */
    public function testBindVarString()
    {
        $uri = '/foo/bar';
        $values = [$uri => ['controller' => 'value1', 'check' => 'value1']];

        $glob = new Glob($values);
        $request = $this->getServerRequest($uri);        
        $route = $glob->getRoute($request);

        $this->assertEquals($values[$uri]['check'], $route->check);
    }

    /**
     * Provide uri's and corresponding patterns for testBindVarSingleUrlPart()
     */
    public function bindVarSingleUrlPartProvider()
    {
        return [
            ['/*', '/test', ['check' => '$1'], 'test'],
            ['/', '/', ['check' => '$1|test'], 'test'],
            ['/foo/*/bar', '/foo/test/bar', ['check' => '$2'], 'test'],
            ['/foo/bar/*', '/foo/bar/test', ['check' => '$3'], 'test'],
            ['/foo/bar/*/zet/*', '/foo/bar/test1/zet/test2', ['check' => '$3', 'checkB' => '$5'], 'test1', 'test2'],
            ['/foo/bar/*/zet/*', '/foo/bar/test1/zet/test2', ['check' => '~$3~/~$5~'], 'test1/test2'],
            ['/', '/', ['check' => '$foo'], null],
            ['/', '/', ['check' => 'test', 'checkB' => null], 'test', null]
        ];
    }

    /**
     * Test binding single url part to route option
     * @dataProvider bindVarSingleUrlPartProvider
     * 
     * @param string $pattern
     * @param string $uri 
     * @param array  $options   Route options
     * @param string $check     Expected value for `check`
     * @param string $checkB    Expected value for `checkB`
     */
    public function testBindVarSingleUrlPart($pattern, $uri, $options, $check, $checkB = null)
    {
        $values = [$pattern => $options];

        $glob = new Glob($values);
        $request = $this->getServerRequest($uri);
        
        $route = $glob->getRoute($request);

        $this->assertNotNull($route, "Route not found");
        $this->assertInstanceOf(Route::class, $route);

        $this->assertEquals($check, $route->check);
        
        if (isset($checkB)) {
            $this->assertEquals($checkB, $route->checkB);
        } else {
            $this->assertObjectNotHasAttribute('checkB', $route);
        }
    }
    
    public function testBindVarWithObject()
    {
        $object = new \Exception(); // Could be anything, just not stdClass
        $glob = new Glob(['/' => ['object' => $object]]);
        
        $request = $this->getServerRequest('/');        
        $route = $glob->getRoute($request);

        $this->assertNotNull($route, "Route not found");
        $this->assertInstanceOf(Route::class, $route);
        
        $this->assertSame($object, $route->object);
    }

    public function bindVarWithSubProvider()
    {
        return [
            [['group' => ['check' => '$1']], 'array'],
            [['group' => (object)['check' => '$1']], 'object'],
            [['group' => ['sub' => (object)['check' => '$1']]], 'array', 'object'],
            [['group' => (object)['sub' => ['check' => '$1']]], 'object', 'array']
        ];
    }
    
    /**
     * @dataProvider bindVarWithSubProvider
     * 
     * @param array  $options
     * @param string $type
     * @param string $subtype
     */
    public function testBindVarWithSub(array $options, $type, $subtype = null)
    {
        $glob = new Glob(['/*' => $options]);
        
        $request = $this->getServerRequest('/test');        
        $route = $glob->getRoute($request);

        $this->assertNotNull($route, "Route not found");
        $this->assertInstanceOf(Route::class, $route);
        
        $this->assertInternalType($type, $route->group);
        
        $group = (array)$route->group;
        
        if (isset($subtype)) {
            $this->assertArrayHasKey('sub', $group);
            $this->assertInternalType($subtype, $group['sub']);
            
            $group = (array)$group['sub'];
        }
        
        $this->assertEquals($group, ['check' => 'test']);
    }
    
    
    /**
     * Provide uri's and corresponding patterns for testBindVarMultipleUrlParts()
     */
    public function bindVarMultipleUrlPartsProvider()
    {
        return [
            ['/foo', ['check' => '$1...'], false, InvalidArgumentException::class],
            ['/', ['check' => ['$1...']], false],
            ['/foo', ['check' => ['$1...']], true],
            ['/foo/bar', ['check' => ['$1...'], 'checkB' => ['$2...']],
                InvalidArgumentException::class]
        ];
    }

    /**
     * Test binding multyple url parts to route option
     * @dataProvider bindVarMultipleUrlPartsProvider
     * 
     * @param string  $uri 
     * @param array   $options     Route options
     * @param boolean $positive
     * @param string  $exception
     */
    public function testBindVarMultipleUrlParts($uri, $options, $positive, $exception = false)
    {
        if ($exception) {
            $this->expectException($exception);
        }
        
        $glob = new Glob([$uri => $options]);
        $request = $this->getServerRequest($uri);        
        $route = $glob->getRoute($request);

        if ($exception) return;

        $this->assertNotNull($route, "Route not found");
        $this->assertInstanceOf(Route::class, $route);

        $values = explode('/', trim($uri, '/'));
        
        $positive ?
            $this->assertArraysEqual($values, $route->check, "Multyple url parts are not picked correctly") :
            $this->assertEmpty($route->check, "Multyple parts element should be empty");

        if (!empty($options->checkB)) {
            array_shift($values);
            $this->assertArraysEqual($values, $route->checkB, "Secondary multyple url parts are not picked correctly");
        }
    }

    /**
     * Provide uri's and corresponding patterns for testBindVarMultipleUrlParts()
     */
    public function bindVarSuperGlobalProvider()
    {
        return [
            ['/foo', ['check' => '$_GET[check]'], 'get'],
            ['/foo', ['check' => '$_POST[check]'], 'post'],
            ['/foo', ['check' => '$_COOKIE[check]'], 'cookie']
        ];
    }

    /**
     * Test binding element of superglobal array to route option
     * @dataProvider bindVarSuperGlobalProvider
     * 
     * @param string $uri 
     * @param array $options 
     * @param string $type    ('get', 'post', 'cookie')
     */
    public function testBindVarSuperGlobal($uri, $options, $type)
    {
        $test = ['check' => 'test'];
        $glob = new Glob([$uri => $options]);
        $request = $this->getServerRequest($uri, 'GET', [$type => $test]);
        $route = $glob->getRoute($request);

        $this->assertEquals($test['check'], $route->check, "Did not obtaine value for superglobal '$type'");
    }

    /**
     * Test binding element of superglobal array to route option
     */
    public function testBindVarRequestHeader()
    {   
        $uri = '/foo/bar';
        $test = 'test_header_value';
        
        $glob = new Glob([$uri => ['check' => '$HTTP_REFERER']]);
        $request = $this->getServerRequest($uri, 'GET', [], $test);
        
        $route = $glob->getRoute($request);
        $this->assertNotNull($route, "Route not found");

        $this->assertEquals($test, $route->check);
    }    

    /**
     * Get ServerRequestInterface object
     *
     * @param string $uri
     * @param string $method  Http query method
     * @return ServerRequestInterface
     */
    public function getServerRequest($uri, $method = 'GET', $globals = [], $header = '')
    {
        $uriMock = $this->createMock(UriInterface::class);
        $uriMock->method('__toString')->willReturn("http://www.example.com" . $uri);
        $uriMock->method('getPath')->willReturn($uri);
        
        $request = $this->createMock(ServerRequestInterface::class);
        $request->method('getUri')->willReturn($uriMock);
        $request->method('getMethod')->willReturn($method);
        $request->method('getQueryParams')->willReturn(isset($globals['get']) ? $globals['get'] : []);
        $request->method('getParsedBody')->willReturn(isset($globals['post']) ? $globals['post'] : []);
        $request->method('getCookieParams')->willReturn(isset($globals['cookie']) ? $globals['cookie'] : []);
        $request->method('getHeaderLine')->willReturn($header);

        return $request;
    }

    /**
     * Assert that two 1-dimensional arrays are equal.
     * Use if array elements are scalar values, or objects with __toString() method
     *
     * @param array $array1
     * @param array $array2 
     */
    public function assertArraysEqual(array $array1, array $array2)
    {
        $this->assertEmpty(array_diff($array2, $array1), 'Missing items');
        $this->assertEmpty(array_diff($array1, $array2), 'Additional items');
    }
}