-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRouterTest.php
More file actions
88 lines (67 loc) · 3.02 KB
/
Copy pathRouterTest.php
File metadata and controls
88 lines (67 loc) · 3.02 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
<?php
declare(strict_types=1);
use Infra\Enums\HttpMethod;
use Infra\Http\Request;
use Infra\Http\Router;
use Tests\Doubles\MockRequestHandler;
describe('Request Handling', function () {
beforeEach(function () {
$this->handler = new MockRequestHandler;
});
it('should handle a request and return a response from the correct handler', function () {
$request = new Request('/found', HttpMethod::GET, []);
$successHandler = new MockRequestHandler(200, 'Success');
$router = new Router($request);
$router->get('/found', $successHandler);
$response = $router->handleRequest();
expect($response->getStatus())->toBe(200)
->and($response->getBody())->toBe('Success');
});
it('should return a 404 response when no route matches the request URI', function () {
$request = new Request('/not-found', HttpMethod::GET, []);
$router = new Router($request);
$response = $router->handleRequest();
expect($response->getStatus())->toBe(404);
});
it('should return a 404 response when the path matches but the HTTP method does not', function () {
$request = new Request('/test', HttpMethod::POST, []);
$router = new Router($request);
$router->get('/test', $this->handler);
$response = $router->handleRequest();
expect($response->getStatus())->toBe(404);
});
it('should inject route parameters into the request object', function () {
$request = new Request('/users/123', HttpMethod::GET, []);
$router = new Router($request);
$router->get('/users/{id}', $this->handler);
$router->handleRequest();
expect($request)->not->toBeNull()
->and($request->getParam('id'))->toBe(123)
->and($request->getParams())->toBe(['id' => 123]);
});
});
describe('Route Registration', function () {
it('should correctly register and handle routes for all HTTP methods', function (HttpMethod $method, string $routerMethod) {
$request = new Request('/test', $method, []);
$router = new Router($request);
$handler = new MockRequestHandler(201, 'Created');
$router->{$routerMethod}('/test', $handler);
$response = $router->handleRequest();
expect($response->getStatus())->toBe(201)
->and($response->getBody())->toBe('Created');
})->with([
'GET' => [HttpMethod::GET, 'get'],
'POST' => [HttpMethod::POST, 'post'],
'PUT' => [HttpMethod::PUT, 'put'],
'PATCH' => [HttpMethod::PATCH, 'patch'],
'DELETE' => [HttpMethod::DELETE, 'delete'],
]);
it('should handle a redirect and return a redirect response', function () {
$request = new Request('/old-path', HttpMethod::GET, []);
$router = new Router($request);
$router->redirect('/old-path', '/new-path', 301);
$response = $router->handleRequest();
expect($response->getStatus())->toBe(301)
->and($response->getHeaders()['Location'])->toBe('/new-path');
});
});