|
1
|
|
|
<?php |
|
2
|
|
|
namespace PSFS\test\base; |
|
3
|
|
|
|
|
4
|
|
|
use PSFS\Dispatcher; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class DispatcherTest |
|
8
|
|
|
* @package PSFS\test\base |
|
9
|
|
|
*/ |
|
10
|
|
|
class DispatcherTest extends \PHPUnit_Framework_TestCase { |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Método que devuelve una instancia del Dspatcher |
|
14
|
|
|
* @param PHPUnit_Framework_MockObject_MockObject $config |
|
15
|
|
|
* @param PHPUnit_Framework_MockObject_MockObject $router |
|
16
|
|
|
* @param PHPUnit_Framework_MockObject_MockObject $security |
|
17
|
|
|
* @return Dispatcher |
|
18
|
|
|
*/ |
|
19
|
|
|
private function getInstance($config = null, $router = null, $security = null) { |
|
20
|
|
|
$dispatcher = Dispatcher::getInstance(); |
|
21
|
|
|
if (null !== $config) { |
|
22
|
|
|
$dispatcher->config = $config; |
|
23
|
|
|
} |
|
24
|
|
|
if (null !== $router) { |
|
25
|
|
|
$dispatcher->router = $router; |
|
26
|
|
|
} |
|
27
|
|
|
if (null !== $security) { |
|
28
|
|
|
$dispatcher->security = $security; |
|
29
|
|
|
} |
|
30
|
|
|
return $dispatcher; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Método que crea un objeto Mock seteado a debug |
|
35
|
|
|
* @param boolean $configured |
|
36
|
|
|
* @param boolean $debug |
|
37
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
|
38
|
|
|
*/ |
|
39
|
|
|
private function mockConfiguredDebugConfig($configured = true, $debug = true) { |
|
40
|
|
|
$config = $this->getMock("\\PSFS\\base\\config\\Config"); |
|
41
|
|
|
$config->expects($this->any())->method("isConfigured")->will($this->returnValue($configured)); |
|
42
|
|
|
$config->expects($this->any())->method("getDebugMode")->will($this->returnValue($debug)); |
|
43
|
|
|
return $config; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Método que mockea la clase Router |
|
48
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
|
49
|
|
|
*/ |
|
50
|
|
|
private function mockDebugRouter() { |
|
51
|
|
|
return $this->getMock("\\PSFS\\base\\Router"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Método que mockea la clase Router |
|
56
|
|
|
* @return \PHPUnit_Framework_MockObject_MockObject |
|
57
|
|
|
*/ |
|
58
|
|
|
private function mockDebugSecurity() { |
|
59
|
|
|
return $this->getMock("\\PSFS\\base\\Security"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testConstructor() { |
|
63
|
|
|
$dispatcher = $this->getInstance(); |
|
64
|
|
|
|
|
65
|
|
|
$this->assertNotNull($dispatcher); |
|
66
|
|
|
$this->assertTrue($dispatcher instanceof Dispatcher); |
|
67
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function testMem() { |
|
71
|
|
|
$dispatcher = $this->getInstance(); |
|
72
|
|
|
$this->assertNotNull($dispatcher->getMem("Bytes")); |
|
73
|
|
|
$this->assertNotNull($dispatcher->getMem("KBytes")); |
|
74
|
|
|
$this->assertNotNull($dispatcher->getMem("MBytes")); |
|
75
|
|
|
$this->assertNotNull($dispatcher->getMem()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testTS() { |
|
79
|
|
|
$dispatcher = $this->getInstance(); |
|
80
|
|
|
$ts = $dispatcher->getTs(); |
|
81
|
|
|
$this->assertNotNull($ts); |
|
82
|
|
|
usleep(200); |
|
83
|
|
|
$this->assertGreaterThan($ts, $dispatcher->getTs()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @expectedException \ErrorException |
|
88
|
|
|
*/ |
|
89
|
|
|
public function testNotice() { |
|
90
|
|
|
$this->getInstance($this->mockConfiguredDebugConfig()); |
|
91
|
|
|
$test = array(); |
|
92
|
|
|
//This throws notice because position 0 in array not exists |
|
93
|
|
|
$test = $test[0] === true; |
|
94
|
|
|
unset($test); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @expectedException \ErrorException |
|
99
|
|
|
*/ |
|
100
|
|
|
public function testWarning() { |
|
101
|
|
|
$this->getInstance($this->mockConfiguredDebugConfig()); |
|
102
|
|
|
//This throws a warning because file 'test.txt' not exists |
|
103
|
|
|
file_get_contents(__DIR__ . "test.txt"); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testNormalExecution() { |
|
107
|
|
|
$router = $this->mockDebugRouter(); |
|
108
|
|
|
$router->expects($this->any())->method("execute")->willReturn("OK"); |
|
109
|
|
|
|
|
110
|
|
|
$dispatcher = $this->getInstance($this->mockConfiguredDebugConfig(), $router); |
|
111
|
|
|
$response = $dispatcher->run(); |
|
112
|
|
|
$this->assertNotNull($response); |
|
113
|
|
|
$this->assertEquals("OK", $response); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @expectedException \PSFS\base\exception\ConfigException |
|
118
|
|
|
* @expectedExceptionMessage CONFIG |
|
119
|
|
|
*/ |
|
120
|
|
|
public function testNotConfigured() { |
|
121
|
|
|
$config = $this->mockConfiguredDebugConfig(false); |
|
122
|
|
|
$router = $this->mockDebugRouter(); |
|
123
|
|
|
$router->expects($this->any())->method("httpNotFound")->willThrowException(new \PSFS\base\exception\ConfigException("CONFIG")); |
|
124
|
|
|
$router->expects($this->any())->method("getAdmin")->willThrowException(new \PSFS\base\exception\ConfigException("CONFIG")); |
|
125
|
|
|
$dispatcher = $this->getInstance($config, $router); |
|
126
|
|
|
|
|
127
|
|
|
$dispatcher->run(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @expectedException \PSFS\base\exception\SecurityException |
|
132
|
|
|
* @expectedExceptionMessage NOT AUTHORIZED |
|
133
|
|
|
*/ |
|
134
|
|
|
public function testNotAuthorized() { |
|
135
|
|
|
$config = $this->mockConfiguredDebugConfig(); |
|
136
|
|
|
$router = $this->mockDebugRouter(); |
|
137
|
|
|
$router->expects($this->any())->method("execute")->willThrowException(new \PSFS\base\exception\SecurityException("NOT AUTHORIZED")); |
|
138
|
|
|
$security = $this->mockDebugSecurity(); |
|
139
|
|
|
$security->expects($this->any())->method("notAuthorized")->willThrowException(new \PSFS\base\exception\SecurityException("NOT AUTHORIZED")); |
|
140
|
|
|
$dispatcher = $this->getInstance($config, $router, $security); |
|
141
|
|
|
|
|
142
|
|
|
$dispatcher->run(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @expectedException \PSFS\base\exception\RouterException |
|
147
|
|
|
* @expectedExceptionMessage NOT FOUND |
|
148
|
|
|
*/ |
|
149
|
|
|
public function testNotFound() { |
|
150
|
|
|
$config = $this->mockConfiguredDebugConfig(); |
|
151
|
|
|
$router = $this->mockDebugRouter(); |
|
152
|
|
|
$router->expects($this->any())->method("execute")->willThrowException(new \PSFS\base\exception\RouterException("NOT FOUND")); |
|
153
|
|
|
$router->expects($this->any())->method("httpNotFound")->willThrowException(new \PSFS\base\exception\RouterException("NOT FOUND")); |
|
154
|
|
|
$dispatcher = $this->getInstance($config, $router); |
|
155
|
|
|
|
|
156
|
|
|
$dispatcher->run(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @expectedException \Exception |
|
161
|
|
|
* @expectedExceptionMessage CATCH EXCEPTION |
|
162
|
|
|
*/ |
|
163
|
|
|
public function testCatchException() { |
|
164
|
|
|
$router = $this->mockDebugRouter(); |
|
165
|
|
|
$router->expects($this->any())->method("execute")->willThrowException(new \Exception("CATCH EXCEPTION")); |
|
166
|
|
|
$router->expects($this->any())->method("httpNotFound")->willThrowException(new \Exception("CATCH EXCEPTION")); |
|
167
|
|
|
$dispatcher = $this->getInstance($this->mockConfiguredDebugConfig(), $router); |
|
168
|
|
|
$dispatcher->run(); |
|
169
|
|
|
} |
|
170
|
|
|
} |