1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vectorface\SnappyRouterTests\Handler; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Vectorface\SnappyRouter\Handler\PatternMatchHandler; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A test for the PatternMatchHandler class. |
10
|
|
|
* @copyright Copyright (c) 2014, VectorFace, Inc. |
11
|
|
|
* @author Dan Bruce <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class PatternMatchHandlerTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Demonstrates how to use the PatternMatchHandler class. |
17
|
|
|
* @test |
18
|
|
|
*/ |
19
|
|
|
public function synopsis() |
20
|
|
|
{ |
21
|
|
|
$config = array( |
22
|
|
|
'routes' => array( |
23
|
|
|
'/user/{name}/{id:[0-9]+}' => array( |
24
|
|
|
'get' => function ($routeParams) { |
25
|
|
|
return print_r($routeParams, true); |
26
|
|
|
} |
27
|
|
|
), |
28
|
|
|
'/anotherRoute' => function () { |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
) |
32
|
|
|
); |
33
|
|
|
$handler = new PatternMatchHandler($config); |
34
|
|
|
$this->assertTrue($handler->isAppropriate('/user/asdf/1234', array(), array(), 'GET')); |
35
|
|
|
$expected = print_r(array('name' => 'asdf', 'id' => 1234), true); |
36
|
|
|
$this->assertEquals($expected, $handler->performRoute()); |
37
|
|
|
|
38
|
|
|
// not a matching pattern |
39
|
|
|
$this->assertFalse($handler->isAppropriate('/user/1234', array(), array(), 'GET')); |
40
|
|
|
|
41
|
|
|
// matching pattern but invalid HTTP verb |
42
|
|
|
$this->assertFalse($handler->isAppropriate('/user/asdf/1234', array(), array(), 'POST')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Tests that the cached route handler works as well. |
47
|
|
|
*/ |
48
|
|
|
public function testCachedRouteHandler() |
49
|
|
|
{ |
50
|
|
|
$cacheFile = __DIR__.'/routes.cache'; |
51
|
|
|
if (file_exists($cacheFile)) { |
52
|
|
|
unlink($cacheFile); |
53
|
|
|
} |
54
|
|
|
$config = array( |
55
|
|
|
'routes' => array( |
56
|
|
|
'/user/{name}/{id:[0-9]+}' => array( |
57
|
|
|
'get' => function ($routeParams) { |
58
|
|
|
return print_r($routeParams, true); |
59
|
|
|
} |
60
|
|
|
), |
61
|
|
|
'/anotherRoute' => function () { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
), |
65
|
|
|
'routeCache' => array( |
66
|
|
|
'cacheFile' => $cacheFile |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$handler = new PatternMatchHandler($config); |
71
|
|
|
$this->assertTrue($handler->isAppropriate('/user/asdf/1234', array(), array(), 'GET')); |
72
|
|
|
$this->assertNotEmpty(file_get_contents($cacheFile)); |
73
|
|
|
unlink($cacheFile); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Tests that the getRequest() method returns null. |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function testGetRequest() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$config = array( |
82
|
|
|
'routes' => array( |
83
|
|
|
'/testRoute' => function () { |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
) |
87
|
|
|
); |
88
|
|
|
$handler = new PatternMatchHandler($config); |
89
|
|
|
$this->assertTrue($handler->isAppropriate('/testRoute', array(), array(), 'GET')); |
90
|
|
|
$this->assertNull($handler->getRequest()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.