1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProtonTests; |
4
|
|
|
|
5
|
|
|
use League\Container\Container; |
6
|
|
|
use League\Event\Emitter; |
7
|
|
|
use League\Route\Http\Exception\NotFoundException; |
8
|
|
|
use League\Route\RouteCollection; |
9
|
|
|
use Monolog\Logger; |
10
|
|
|
use Proton; |
11
|
|
|
use Proton\Application; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
|
15
|
|
|
class ApplicationTest extends \PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
public function testSetGet() |
18
|
|
|
{ |
19
|
|
|
$app = new Application(); |
20
|
|
|
$this->assertTrue($app->getContainer() instanceof Container); |
21
|
|
|
$this->assertTrue($app->getRouter() instanceof RouteCollection); |
22
|
|
|
$this->assertTrue($app->getEventEmitter() instanceof Emitter); |
23
|
|
|
|
24
|
|
|
$logger = $app->getLogger(); |
25
|
|
|
$this->assertTrue($logger instanceof Logger); |
26
|
|
|
$this->assertEquals($logger, $app->getLogger('default')); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testArrayAccessContainer() |
30
|
|
|
{ |
31
|
|
|
$app = new Application(); |
32
|
|
|
$app['foo'] = 'bar'; |
33
|
|
|
|
34
|
|
|
$this->assertSame('bar', $app['foo']); |
35
|
|
|
$this->assertTrue(isset($app['foo'])); |
36
|
|
|
unset($app['foo']); |
37
|
|
|
$this->assertFalse(isset($app['foo'])); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testSubscribe() |
41
|
|
|
{ |
42
|
|
|
$app = new Application(); |
43
|
|
|
|
44
|
|
|
$app->subscribe('request.received', function ($event, $request) { |
45
|
|
|
$this->assertInstanceOf('League\Event\Event', $event); |
46
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $request); |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
$reflected = new \ReflectionProperty($app, 'emitter'); |
50
|
|
|
$reflected->setAccessible(true); |
51
|
|
|
$emitter = $reflected->getValue($app); |
52
|
|
|
$this->assertTrue($emitter->hasListeners('request.received')); |
53
|
|
|
|
54
|
|
|
$foo = null; |
55
|
|
|
$app->subscribe('response.created', function ($event, $request, $response) use (&$foo) { |
|
|
|
|
56
|
|
|
$foo = 'bar'; |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
$request = Request::createFromGlobals(); |
60
|
|
|
$response = $app->handle($request); |
61
|
|
|
|
62
|
|
|
$this->assertEquals('bar', $foo); |
63
|
|
|
$this->assertEquals(404, $response->getStatusCode()); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testTerminate() |
67
|
|
|
{ |
68
|
|
|
$app = new Application(); |
69
|
|
|
|
70
|
|
|
$app->subscribe('response.sent', function ($event, $request, $response) { |
71
|
|
|
$this->assertInstanceOf('League\Event\Event', $event); |
72
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $request); |
73
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
$request = Request::createFromGlobals(); |
77
|
|
|
$response = $app->handle($request); |
78
|
|
|
|
79
|
|
|
$app->terminate($request, $response); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testHandleSuccess() |
83
|
|
|
{ |
84
|
|
|
$app = new Application(); |
85
|
|
|
|
86
|
|
|
$app->get('/', function ($request, $response) { |
87
|
|
|
$response->setContent('<h1>It works!</h1>'); |
88
|
|
|
return $response; |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
$app->post('/', function ($request, $response) { |
92
|
|
|
$response->setContent('<h1>It works!</h1>'); |
93
|
|
|
return $response; |
94
|
|
|
}); |
95
|
|
|
|
96
|
|
|
$app->put('/', function ($request, $response) { |
97
|
|
|
$response->setContent('<h1>It works!</h1>'); |
98
|
|
|
return $response; |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
$app->delete('/', function ($request, $response) { |
102
|
|
|
$response->setContent('<h1>It works!</h1>'); |
103
|
|
|
return $response; |
104
|
|
|
}); |
105
|
|
|
|
106
|
|
|
$app->patch('/', function ($request, $response) { |
107
|
|
|
$response->setContent('<h1>It works!</h1>'); |
108
|
|
|
return $response; |
109
|
|
|
}); |
110
|
|
|
|
111
|
|
|
$request = Request::createFromGlobals(); |
112
|
|
|
|
113
|
|
|
$response = $app->handle($request, 1, true); |
114
|
|
|
|
115
|
|
|
$this->assertEquals('<h1>It works!</h1>', $response->getContent()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testHandleFailThrowException() |
119
|
|
|
{ |
120
|
|
|
$app = new Application(); |
121
|
|
|
|
122
|
|
|
$request = Request::createFromGlobals(); |
123
|
|
|
|
124
|
|
|
try { |
125
|
|
|
$app->handle($request, 1, false); |
126
|
|
|
} catch (\Exception $e) { |
127
|
|
|
$this->assertTrue($e instanceof NotFoundException); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testHandleWithOtherException() |
132
|
|
|
{ |
133
|
|
|
$app = new Application(); |
134
|
|
|
$app['debug'] = true; |
135
|
|
|
|
136
|
|
|
$request = Request::createFromGlobals(); |
137
|
|
|
|
138
|
|
|
$app->subscribe('request.received', function ($event, $request, $response) { |
|
|
|
|
139
|
|
|
throw new \Exception('A test exception'); |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
$response = $app->handle($request); |
143
|
|
|
|
144
|
|
|
$this->assertEquals(500, $response->getStatusCode()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testCustomExceptionDecorator() |
148
|
|
|
{ |
149
|
|
|
$app = new Application(); |
150
|
|
|
$app['debug'] = true; |
151
|
|
|
|
152
|
|
|
$request = Request::createFromGlobals(); |
153
|
|
|
|
154
|
|
|
$app->subscribe('request.received', function ($event, $request, $response) { |
|
|
|
|
155
|
|
|
throw new \Exception('A test exception'); |
156
|
|
|
}); |
157
|
|
|
|
158
|
|
|
$app->setExceptionDecorator(function ($e) { |
|
|
|
|
159
|
|
|
$response = new Response; |
160
|
|
|
$response->setStatusCode(500); |
161
|
|
|
$response->setContent('Fail'); |
162
|
|
|
return $response; |
163
|
|
|
}); |
164
|
|
|
|
165
|
|
|
$response = $app->handle($request); |
166
|
|
|
|
167
|
|
|
$this->assertEquals(500, $response->getStatusCode()); |
168
|
|
|
$this->assertEquals('Fail', $response->getContent()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @expectedException \LogicException |
173
|
|
|
*/ |
174
|
|
|
public function testExceptionDecoratorDoesntReturnResponseObject() |
175
|
|
|
{ |
176
|
|
|
$app = new Application(); |
177
|
|
|
$app->setExceptionDecorator(function ($e) { |
|
|
|
|
178
|
|
|
return true; |
179
|
|
|
}); |
180
|
|
|
|
181
|
|
|
$request = Request::createFromGlobals(); |
182
|
|
|
|
183
|
|
|
$app->subscribe('request.received', function ($event, $request, $response) { |
|
|
|
|
184
|
|
|
throw new \Exception('A test exception'); |
185
|
|
|
}); |
186
|
|
|
|
187
|
|
|
$response = $app->handle($request); |
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function testCustomEvents() |
191
|
|
|
{ |
192
|
|
|
$app = new Application(); |
193
|
|
|
|
194
|
|
|
$time = null; |
195
|
|
|
$app->subscribe('custom.event', function ($event, $args) use (&$time) { |
196
|
|
|
$time = $args; |
197
|
|
|
}); |
198
|
|
|
|
199
|
|
|
$app->getEventEmitter()->emit('custom.event', time()); |
200
|
|
|
$this->assertTrue($time !== null); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function testRun() |
204
|
|
|
{ |
205
|
|
|
$app = new Application(); |
206
|
|
|
|
207
|
|
|
$app->get('/', function ($request, $response) { |
208
|
|
|
$response->setContent('<h1>It works!</h1>'); |
209
|
|
|
return $response; |
210
|
|
|
}); |
211
|
|
|
|
212
|
|
|
$app->subscribe('request.received', function ($event, $request) { |
213
|
|
|
$this->assertInstanceOf('League\Event\Event', $event); |
214
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $request); |
215
|
|
|
}); |
216
|
|
|
$app->subscribe('response.sent', function ($event, $request, $response) { |
217
|
|
|
$this->assertInstanceOf('League\Event\Event', $event); |
218
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $request); |
219
|
|
|
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response); |
220
|
|
|
}); |
221
|
|
|
|
222
|
|
|
ob_start(); |
223
|
|
|
$app->run(); |
224
|
|
|
ob_get_clean(); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.