|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Nimo\Tests; |
|
6
|
|
|
|
|
7
|
|
|
use Nimo\Interfaces\RequestPredictionInterface; |
|
8
|
|
|
use Nimo\Middlewares\PredictionWrapperMiddleware; |
|
9
|
|
|
use PHPUnit\Framework\Assert; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
|
|
12
|
|
|
class PredictionWrapperMiddlewareTest extends NimoTestCase |
|
13
|
|
|
{ |
|
14
|
|
|
public function testPositiveCase() |
|
15
|
|
|
{ |
|
16
|
|
|
$req = $this->getRequestMock(); |
|
17
|
|
|
$res = $this->getResponseMock(); |
|
18
|
|
|
$hdl = $this->throwHandler(); |
|
19
|
|
|
$inner = $this->assertedMiddleware($req, $hdl, $res); |
|
20
|
|
|
|
|
21
|
|
|
$prediction = new class($req) implements RequestPredictionInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use RememberConstructorParamTrait; |
|
24
|
|
|
|
|
25
|
|
|
public function isTrue(ServerRequestInterface $request): bool |
|
26
|
|
|
{ |
|
27
|
|
|
Assert::assertSame($this->params[0], $request); |
|
28
|
|
|
return true; |
|
29
|
|
|
} |
|
30
|
|
|
}; |
|
31
|
|
|
|
|
32
|
|
|
$middleware = new PredictionWrapperMiddleware($inner, $prediction); |
|
33
|
|
|
$actualResponse = $middleware->process($req, $hdl); |
|
34
|
|
|
Assert::assertSame($res, $actualResponse); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
public function testNegativeCase() |
|
39
|
|
|
{ |
|
40
|
|
|
$req = $this->getRequestMock(); |
|
41
|
|
|
$res = $this->getResponseMock(); |
|
42
|
|
|
$hdl = $this->assertedHandler($req, $res); |
|
43
|
|
|
|
|
44
|
|
|
$falsePrediction = new class($req) implements RequestPredictionInterface |
|
45
|
|
|
{ |
|
46
|
|
|
use RememberConstructorParamTrait; |
|
47
|
|
|
|
|
48
|
|
|
public function isTrue(ServerRequestInterface $request): bool |
|
49
|
|
|
{ |
|
50
|
|
|
Assert::assertSame($this->params[0], $request); |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
}; |
|
54
|
|
|
|
|
55
|
|
|
$inner = $this->throwMiddleware($req, $hdl, new \Exception('should not call inner')); |
|
56
|
|
|
$middleware = new PredictionWrapperMiddleware($inner, $falsePrediction); |
|
57
|
|
|
|
|
58
|
|
|
$actualResponse = $middleware->process($req, $hdl); |
|
59
|
|
|
Assert::assertSame($res, $actualResponse); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|