1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rad\Routing\Tests; |
4
|
|
|
|
5
|
|
|
use App\AppBundle; |
6
|
|
|
use Composer\Autoload\ClassLoader; |
7
|
|
|
use PHPUnit_Framework_TestCase; |
8
|
|
|
use Rad\Core\Bundles; |
9
|
|
|
use Rad\Routing\Router; |
10
|
|
|
use Test\TestBundle; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Config Test |
14
|
|
|
* |
15
|
|
|
* @package Rad\Configure\Tests |
16
|
|
|
*/ |
17
|
|
|
class RoutingTest extends PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Router |
21
|
|
|
*/ |
22
|
|
|
private $router; |
23
|
|
|
|
24
|
|
|
public static function setUpBeforeClass() |
25
|
|
|
{ |
26
|
|
|
$classLoader = new ClassLoader(); |
27
|
|
|
$classLoader->addPsr4('App\\', __DIR__ . '/Fixtures/src/App'); |
28
|
|
|
$classLoader->addPsr4('Test\\', __DIR__ . '/Fixtures/src/Test'); |
29
|
|
|
$classLoader->register(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
protected function setUp() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$this->router = new Router(); |
35
|
|
|
if (!defined('SRC_DIR')) { |
36
|
|
|
define('SRC_DIR', __DIR__ . '/Fixtures/src'); |
37
|
|
|
} |
38
|
|
|
if (!defined('DS')) { |
39
|
|
|
define('DS', '/'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$bundles = [ |
43
|
|
|
new AppBundle(), |
44
|
|
|
new TestBundle() |
45
|
|
|
]; |
46
|
|
|
Bundles::loadAll($bundles); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Test root URL handler behaviours |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function testRootUrlHandler() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$this->router->handle('/'); |
55
|
|
|
|
56
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'App\Action\IndexAction'); |
57
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'App\Responder\IndexResponder'); |
58
|
|
|
$this->assertEquals($this->router->getAction(), 'index'); |
59
|
|
|
$this->assertEquals($this->router->getBundle(), 'app'); |
60
|
|
|
//$this->assertEquals($this->router->getParams(), []); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Test bundle only URL handler behaviours |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function testBundleUrlHandler() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$this->router->handle('/test/'); |
69
|
|
|
|
70
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\IndexAction'); |
71
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\IndexResponder'); |
72
|
|
|
$this->assertEquals($this->router->getAction(), 'index'); |
73
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
74
|
|
|
//$this->assertEquals($this->router->getParams(), []); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Test bundle with method URL handler behaviours |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function testBundleMethodUrlHandler() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$this->router->handle('/test/method/'); |
83
|
|
|
|
84
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\Method\CliMethodAction'); |
85
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\Method\CliMethodResponder'); |
86
|
|
|
$this->assertEquals($this->router->getAction(), 'CliMethod'); |
87
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
88
|
|
|
//$this->assertEquals($this->router->getParams(), []); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test bundle and action URL handler behaviours |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function testBundleActionUrlHandler() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$this->router->handle('/test/test/'); |
97
|
|
|
|
98
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\TestAction'); |
99
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\TestResponder'); |
100
|
|
|
$this->assertEquals($this->router->getAction(), 'test'); |
101
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
102
|
|
|
//$this->assertEquals($this->router->getParams(), []); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Test bundle and action and params URL handler behaviours |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function testBundleActionParamsUrlHandler() |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$this->router->handle('/test/test/param1/param2/'); |
111
|
|
|
|
112
|
|
|
$this->assertEquals($this->router->getActionNamespace(), 'Test\Action\TestAction'); |
113
|
|
|
$this->assertEquals($this->router->getResponderNamespace(), 'Test\Responder\TestResponder'); |
114
|
|
|
$this->assertEquals($this->router->getAction(), 'test'); |
115
|
|
|
$this->assertEquals($this->router->getBundle(), 'test'); |
116
|
|
|
//$this->assertEquals($this->router->getParams(), ['param1', 'param2']); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.