1 | <?php |
||
14 | * @coversDefaultClass \Chadicus\Psr\Http\ServerMiddleware\Middleware |
||
15 | * @covers ::__construct |
||
16 | */ |
||
17 | final class MiddlewareTest extends \PHPUnit\Framework\TestCase |
||
18 | { |
||
19 | /** |
||
20 | * Verify basic behavior of __invoke(). |
||
21 | * |
||
22 | * @test |
||
23 | * @covers ::__invoke |
||
24 | * |
||
25 | * @return void |
||
26 | */ |
||
27 | public function invoke() |
||
64 | |||
65 | /** |
||
66 | * Verify behavior of __invoke() when AuthenticationException is thrown. |
||
67 | * |
||
68 | * @test |
||
69 | * @covers ::__invoke |
||
70 | * |
||
71 | * @return void |
||
72 | * |
||
73 | * @throws \Exception Thrown if the $next callable passed to the middleware is called. |
||
74 | */ |
||
75 | public function invokeExceptionThrown() |
||
76 | { |
||
77 | $privateKey = md5(microtime(true)); |
||
78 | $publicKey = md5(microtime()); |
||
79 | $nonce = rand(); |
||
80 | $time = time(); |
||
81 | $signature = md5("{$privateKey}{$nonce}{$time}{$publicKey}"); |
||
82 | |||
83 | $token = new Token($publicKey, $signature, $nonce, $time); |
||
84 | |||
85 | $provider = $this->getMockBuilder('\\Chadicus\\Psr\\Http\\ServerMiddleware\\KeyProviderInterface')->getMock(); |
||
86 | $provider->method('findPrivateKey')->willReturn($privateKey); |
||
87 | |||
88 | $extractor = $this->getMockBuilder('\\Chadicus\\Psr\\Http\\ServerMiddleware\\TokenExtractorInterface')->getMock(); |
||
89 | $extractor->method('extract')->willReturn($token); |
||
90 | |||
91 | $exception = new AuthenticationException(400, 'Bad Request'); |
||
92 | |||
93 | $validator = $this->getMockBuilder('\\Chadicus\\Psr\\Http\\ServerMiddleware\\TokenValidatorInterface')->getMock(); |
||
94 | $validator->method('validate')->will($this->throwException($exception)); |
||
95 | |||
96 | $container = new ArrayObject(); |
||
97 | |||
98 | $middleware = new Middleware($provider, $extractor, $validator, $container); |
||
99 | |||
100 | $headers = [ |
||
101 | 'X-Hmac-Auth' => ["{$publicKey}:{$signature}:{$nonce}:{$time}"], |
||
102 | ]; |
||
103 | |||
104 | $next = function ($request, $response) { |
||
105 | throw new \Exception('This should not have been called!!'); |
||
106 | }; |
||
107 | |||
108 | $psr7Request = new ServerRequest([], [], 'http://localhost', 'GET', 'php://input', $headers); |
||
109 | |||
110 | $response = $middleware($psr7Request, new Response(), $next); |
||
111 | |||
112 | $this->assertFalse(isset($container['privateKey'])); |
||
113 | |||
114 | $this->assertSame(400, $response->getStatusCode()); |
||
115 | $this->assertSame('Bad Request', $response->getReasonPhrase()); |
||
118 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.