|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Router\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Nyholm\Psr7\Response; |
|
6
|
|
|
use Nyholm\Psr7\ServerRequest; |
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
12
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
13
|
|
|
use Yiisoft\Router\MatchingResult; |
|
14
|
|
|
use Yiisoft\Http\Method; |
|
15
|
|
|
use Yiisoft\Router\Route; |
|
16
|
|
|
|
|
17
|
|
|
final class MatchingResultTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public function testFromSucess(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$route = Route::get('/{name}'); |
|
22
|
|
|
|
|
23
|
|
|
$result = MatchingResult::fromSuccess($route, ['name' => 'Mehdi']); |
|
24
|
|
|
$this->assertTrue($result->isSuccess()); |
|
25
|
|
|
$this->assertSame(['name' => 'Mehdi'], $result->parameters()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testFromFailureOnMethodFailure(): void |
|
29
|
|
|
{ |
|
30
|
|
|
$result = MatchingResult::fromFailure([Method::GET, Method::HEAD]); |
|
31
|
|
|
|
|
32
|
|
|
$this->assertFalse($result->isSuccess()); |
|
33
|
|
|
$this->assertTrue($result->isMethodFailure()); |
|
34
|
|
|
$this->assertSame([Method::GET, Method::HEAD], $result->methods()); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testFromFailureOnNotFoundFailure(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$result = MatchingResult::fromFailure(Method::ANY); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertFalse($result->isSuccess()); |
|
42
|
|
|
$this->assertFalse($result->isMethodFailure()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function testProcessSuccess(): void |
|
46
|
|
|
{ |
|
47
|
|
|
$container = $this->createMock(ContainerInterface::class); |
|
48
|
|
|
$route = Route::post('/', null, $container)->addMiddleware($this->getMiddleware()); |
|
49
|
|
|
$result = MatchingResult::fromSuccess($route, []); |
|
50
|
|
|
$request = new ServerRequest('POST', '/'); |
|
51
|
|
|
|
|
52
|
|
|
$response = $result->process($request, $this->getRequestHandler()); |
|
53
|
|
|
$this->assertSame(201, $response->getStatusCode()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function testProcessFailure(): void |
|
57
|
|
|
{ |
|
58
|
|
|
$request = new ServerRequest('POST', '/'); |
|
59
|
|
|
|
|
60
|
|
|
$response = MatchingResult::fromFailure([Method::GET, Method::HEAD]) |
|
61
|
|
|
->process($request, $this->getRequestHandler()); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertSame(404, $response->getStatusCode()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
private function getMiddleware() |
|
67
|
|
|
{ |
|
68
|
|
|
return static function () { |
|
69
|
|
|
return new Response(201); |
|
70
|
|
|
}; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function getRequestHandler(): RequestHandlerInterface |
|
74
|
|
|
{ |
|
75
|
|
|
return new class() implements RequestHandlerInterface { |
|
76
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
|
77
|
|
|
{ |
|
78
|
|
|
return new Response(404); |
|
79
|
|
|
} |
|
80
|
|
|
}; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|