Completed
Push — master ( 854a6f...c7bd33 )
by Chad
12s
created

MiddlewareTest::invokeExceptionThrown()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 0
1
<?php
2
3
namespace ChadicusTest\Psr\Http\ServerMiddleware;
4
5
use ArrayObject;
6
use Chadicus\Psr\Http\ServerMiddleware\AuthenticationException;
7
use Chadicus\Psr\Http\ServerMiddleware\Middleware;
8
use Chadicus\Psr\Http\ServerMiddleware\Token;
9
use Chadicus\Psr\Http\ServerMiddleware;
10
use Zend\Diactoros\Response;
11
use Zend\Diactoros\ServerRequest;
12
13
/**
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()
28
    {
29
        $privateKey = md5(microtime(true));
30
        $publicKey = md5(microtime());
31
        $nonce = rand();
32
        $time = time();
33
        $signature = md5("{$privateKey}{$nonce}{$time}{$publicKey}");
34
35
        $token = new Token($publicKey, $signature, $nonce, $time);
36
37
        $provider = $this->getMockBuilder('\\Chadicus\\Psr\\Http\\ServerMiddleware\\KeyProviderInterface')->getMock();
38
        $provider->method('findPrivateKey')->willReturn($privateKey);
39
40
        $extractor = $this->getMockBuilder('\\Chadicus\\Psr\\Http\\ServerMiddleware\\TokenExtractorInterface')->getMock();
41
        $extractor->method('extract')->willReturn($token);
42
43
        $validator = $this->getMockBuilder('\\Chadicus\\Psr\\Http\\ServerMiddleware\\TokenValidatorInterface')->getMock();
44
        $validator->method('validate')->willReturn(true);
45
46
        $container = new ArrayObject();
47
48
        $middleware = new Middleware($provider, $extractor, $validator, $container);
49
50
        $headers = [
51
            'X-Hmac-Auth' => ["{$publicKey}:{$signature}:{$nonce}:{$time}"],
52
        ];
53
54
        $next = function ($request, $response) {
55
            return $response;
56
        };
57
58
        $psr7Request = new ServerRequest([], [], 'http://localhost', 'GET', 'php://input', $headers);
59
60
        $middleware($psr7Request, new Response(), $next);
61
62
        $this->assertSame($privateKey, $container['privateKey']);
63
    }
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) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $response is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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());
116
    }
117
}
118