-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathApplicationTest.php
More file actions
477 lines (365 loc) · 14.2 KB
/
Copy pathApplicationTest.php
File metadata and controls
477 lines (365 loc) · 14.2 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
namespace Bow\Tests\Application;
use Bow\Application\Application;
use Bow\Application\Exception\ApplicationException;
use Bow\Container\Capsule;
use Bow\Http\Exception\BadRequestException;
use Bow\Http\Exception\HttpException;
use Bow\Http\Request;
use Bow\Http\Response;
use Bow\Router\Exception\RouterException;
use Bow\Router\Route;
use Bow\Testing\KernelTesting;
use Bow\Tests\Config\TestingConfiguration;
use Mockery;
class ApplicationTest extends \PHPUnit\Framework\TestCase
{
public static function setUpBeforeClass(): void
{
$config = TestingConfiguration::getConfig();
$config->boot();
}
public function setUp(): void
{
Mockery::mock();
}
public function tearDown(): void
{
Mockery::close();
}
/**
* Create a basic request mock
*/
private function createRequestMock(string $method = 'GET', string $path = '/'): Request
{
$request = Mockery::mock(Request::class);
$request->allows()->method()->andReturns($method);
$request->allows()->capture()->andReturns(null);
$request->allows()->path()->andReturns($path);
$request->allows()->get("_method")->andReturns("");
$request->allows()->domain()->andReturns("localhost");
// run() asks whether the JSON body failed to decode; null = it did not.
$request->allows()->getInvalidJsonPayload()->andReturns(null);
return $request;
}
/**
* Create a basic response mock
*/
private function createResponseMock(int $expectedStatus = 200): Response
{
$response = Mockery::mock(Response::class);
$response->allows()->withHeader('X-Powered-By', 'Bow Framework');
$response->allows()->status($expectedStatus);
$response->allows()->send(Mockery::any(), Mockery::any())->andReturn('');
// sendResponse() now reads the status already held by the response
// instead of overwriting it with a hardcoded 200.
$response->allows()->getCode()->andReturns($expectedStatus);
return $response;
}
/**
* Create a basic config mock
*/
private function createConfigMock(bool $isCli = false): KernelTesting
{
$config = Mockery::mock(KernelTesting::class);
$config->allows([
"offsetGet" => ["root" => "", "auto_csrf" => false],
"offsetExists" => true,
"boot" => $config,
"isCli" => $isCli
]);
return $config;
}
public function test_instance_of_application()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = Application::make($request, $response);
$app->bind(TestingConfiguration::getConfig());
$this->assertInstanceOf(Application::class, $app);
$this->assertInstanceOf(Application::class, app('app'));
$this->assertInstanceOf(Capsule::class, $app->getContainer());
}
public function test_one_time_application_boot()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = Application::make($request, $response);
$app->bind(TestingConfiguration::getConfig());
$this->assertInstanceOf(Application::class, $app);
$this->assertInstanceOf(Application::class, app('app'));
$this->assertInstanceOf(Capsule::class, $app->getContainer());
}
public function test_application_singleton_pattern()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app1 = Application::make($request, $response);
$app2 = Application::make($request, $response);
$this->assertSame($app1, $app2);
}
public function test_get_router_returns_router_instance()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = new Application($request, $response);
$router = $app->getRouter();
$this->assertInstanceOf(\Bow\Router\Router::class, $router);
}
public function test_is_running_on_cli()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = new Application($request, $response);
$isCli = $app->isRunningOnCli();
$this->assertIsBool($isCli);
$this->assertEquals(php_sapi_name() == 'cli', $isCli);
}
public function test_disable_powered_by_mention()
{
$request = $this->createRequestMock();
$response = Mockery::mock(Response::class);
// Should NOT call withHeader for X-Powered-By
$response->shouldNotReceive('withHeader')->with('X-Powered-By', Mockery::any());
$response->allows()->status(200);
$response->allows()->send(Mockery::any(), Mockery::any())->andReturn('');
$response->allows()->getCode()->andReturns(200);
$config = $this->createConfigMock();
$app = new Application($request, $response);
$app->disablePoweredByMention();
$app->bind($config);
$app->getRouter()->get('/', function () {
return 'test';
});
$app->run();
$this->assertTrue(true); // If we get here without Mockery exception, test passes
}
public function test_send_application_with_404_status()
{
$this->expectException(RouterException::class);
$this->expectExceptionMessage('Route "/non-existent-path" not found');
$request = $this->createRequestMock('GET', '/non-existent-path');
$response = $this->createResponseMock(404);
$config = $this->createConfigMock();
$app = new Application($request, $response);
$app->bind($config);
$app->run();
}
/**
* @throws BadRequestException
* @throws \ReflectionException
* @throws RouterException
*/
public function test_send_application_with_matched_route()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$config = $this->createConfigMock();
$app = new Application($request, $response);
$app->bind($config);
$app->getRouter()->get('/', function () {
return "work";
});
$this->assertTrue($app->run());
}
public function test_send_application_with_no_matched_route()
{
$this->expectException(RouterException::class);
$request = $this->createRequestMock('GET', '/name');
$response = $this->createResponseMock(404);
$config = $this->createConfigMock();
$app = new Application($request, $response);
$app->bind($config);
$app->getRouter()->get('/', function () {
return "not work";
});
$this->assertFalse($app->run());
}
public function test_post_request_routing()
{
$request = $this->createRequestMock('POST', '/users');
$response = $this->createResponseMock();
$config = $this->createConfigMock();
$app = new Application($request, $response);
$app->bind($config);
$app->getRouter()->post('/users', function () {
return ['created' => true];
});
$this->assertTrue($app->run());
}
public function test_put_request_routing()
{
$request = $this->createRequestMock('PUT', '/users/1');
$response = $this->createResponseMock();
$config = $this->createConfigMock();
$app = new Application($request, $response);
$app->bind($config);
$app->getRouter()->put('/users/1', function () {
return ['updated' => true];
});
$this->assertTrue($app->run());
}
public function test_delete_request_routing()
{
$request = $this->createRequestMock('DELETE', '/users/1');
$response = $this->createResponseMock();
$config = $this->createConfigMock();
$app = new Application($request, $response);
$app->bind($config);
$app->getRouter()->delete('/users/1', function () {
return ['deleted' => true];
});
$this->assertTrue($app->run());
}
public function test_patch_request_routing()
{
$request = $this->createRequestMock('PATCH', '/users/1');
$response = $this->createResponseMock();
$config = $this->createConfigMock();
$app = new Application($request, $response);
$app->bind($config);
$app->getRouter()->patch('/users/1', function () {
return ['patched' => true];
});
$this->assertTrue($app->run());
}
public function test_any_request_routing()
{
$request = $this->createRequestMock('GET', '/api/endpoint');
$response = $this->createResponseMock();
$config = $this->createConfigMock();
$app = new Application($request, $response);
$app->bind($config);
$app->getRouter()->any('/api/endpoint', function () {
return 'any method works';
});
$this->assertTrue($app->run());
}
public function test_application_with_cli_mode()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$config = $this->createConfigMock(true);
$app = new Application($request, $response);
$app->bind($config);
// In CLI mode, run() should return true immediately
$this->assertTrue($app->run());
}
public function test_abort_method_throws_http_exception()
{
$this->expectException(HttpException::class);
$this->expectExceptionMessage('Not Found');
$request = $this->createRequestMock();
$response = Mockery::mock(Response::class);
$response->allows()->status(Mockery::any());
$response->allows()->withHeader(Mockery::any(), Mockery::any());
$app = new Application($request, $response);
$app->abort(404, 'Not Found');
}
public function test_abort_method_with_headers()
{
$this->expectException(HttpException::class);
$request = $this->createRequestMock();
$response = Mockery::mock(Response::class);
$response->allows()->status(Mockery::any());
$response->shouldReceive('withHeader')->with('X-Custom-Header', 'value')->once();
$app = new Application($request, $response);
$app->abort(403, 'Forbidden', ['X-Custom-Header' => 'value']);
}
public function test_container_method_returns_capsule()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = new Application($request, $response);
$this->assertInstanceOf(Capsule::class, $app->container());
}
public function test_container_method_resolves_binding()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = new Application($request, $response);
$app->container('test', fn() => 'test-value');
$this->assertEquals('test-value', $app->container('test'));
}
public function test_container_method_throws_exception_on_invalid_callable()
{
$this->expectException(\TypeError::class);
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = new Application($request, $response);
$app->container('test', 'not-callable');
}
public function test_rest_method_creates_resource_routes()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = new Application($request, $response);
$result = $app->rest('/api/users', 'UserController');
$this->assertInstanceOf(Application::class, $result);
}
public function test_rest_method_with_array_configuration()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = new Application($request, $response);
$result = $app->rest('/api/posts', [
'controller' => 'PostController',
'ignores' => ['destroy']
]);
$this->assertInstanceOf(Application::class, $result);
}
public function test_rest_method_throws_exception_on_missing_controller()
{
$this->expectException(ApplicationException::class);
$this->expectExceptionMessage('[REST] No defined controller!');
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = new Application($request, $response);
$app->rest('/api/users', ['ignores' => ['destroy']]);
}
public function test_magic_call_delegates_to_router()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = new Application($request, $response);
// Test that we can call router methods via __call
$route = $app->get('/test', function () {
return 'test';
});
$this->assertInstanceOf(Route::class, $route);
}
public function test_magic_call_throws_exception_on_invalid_method()
{
$this->expectException(ApplicationException::class);
$this->expectExceptionMessage('Method [nonExistentMethod] does not exist in Application.');
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = new Application($request, $response);
$app->nonExistentMethod();
}
public function test_send_method_executes_run()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$config = $this->createConfigMock();
$app = new Application($request, $response);
$app->bind($config);
$app->getRouter()->get('/', function () {
return 'sent';
});
// send() method should execute without throwing
ob_start();
$app->send();
$output = ob_get_clean();
$this->assertTrue(true); // If we reach here, send() worked
}
public function test_invoke_with_params_returns_capsule()
{
$request = $this->createRequestMock();
$response = $this->createResponseMock();
$app = new Application($request, $response);
// With any number of params, __invoke returns capsule based on count($params) > 0
$result = $app('test');
$this->assertInstanceOf(Capsule::class, $result);
}
}