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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.