MiddlewareTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 7
dl 0
loc 109
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B invoke() 0 41 1
B invokeExceptionThrown() 0 46 1
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(
41
            '\\Chadicus\\Psr\\Http\\ServerMiddleware\\TokenExtractorInterface'
42
        )->getMock();
43
        $extractor->method('extract')->willReturn($token);
44
45
        $validator = $this->getMockBuilder(
46
            '\\Chadicus\\Psr\\Http\\ServerMiddleware\\TokenValidatorInterface'
47
        )->getMock();
48
        $validator->method('validate')->willReturn(true);
49
50
        $container = new ArrayObject();
51
52
        $middleware = new Middleware($provider, $extractor, $validator, $container);
53
54
        $headers = [
55
            'X-Hmac-Auth' => ["{$publicKey}:{$signature}:{$nonce}:{$time}"],
56
        ];
57
58
        $next = function ($request, $response) {
59
            return $response;
60
        };
61
62
        $psr7Request = new ServerRequest([], [], 'http://localhost', 'GET', 'php://input', $headers);
63
64
        $middleware($psr7Request, new Response(), $next);
65
66
        $this->assertSame($privateKey, $container['privateKey']);
67
    }
68
69
    /**
70
     * Verify behavior of __invoke() when AuthenticationException is thrown.
71
     *
72
     * @test
73
     * @covers ::__invoke
74
     *
75
     * @return void
76
     *
77
     * @throws \Exception Thrown if the $next callable passed to the middleware is called.
78
     */
79
    public function invokeExceptionThrown()
80
    {
81
        $privateKey = md5(microtime(true));
82
        $publicKey = md5(microtime());
83
        $nonce = rand();
84
        $time = time();
85
        $signature = md5("{$privateKey}{$nonce}{$time}{$publicKey}");
86
87
        $token = new Token($publicKey, $signature, $nonce, $time);
88
89
        $provider = $this->getMockBuilder('\\Chadicus\\Psr\\Http\\ServerMiddleware\\KeyProviderInterface')->getMock();
90
        $provider->method('findPrivateKey')->willReturn($privateKey);
91
92
        $extractor = $this->getMockBuilder(
93
            '\\Chadicus\\Psr\\Http\\ServerMiddleware\\TokenExtractorInterface'
94
        )->getMock();
95
        $extractor->method('extract')->willReturn($token);
96
97
        $exception = new AuthenticationException(400, 'Bad Request');
98
99
        $validator = $this->getMockBuilder(
100
            '\\Chadicus\\Psr\\Http\\ServerMiddleware\\TokenValidatorInterface'
101
        )->getMock();
102
        $validator->method('validate')->will($this->throwException($exception));
103
104
        $container = new ArrayObject();
105
106
        $middleware = new Middleware($provider, $extractor, $validator, $container);
107
108
        $headers = [
109
            'X-Hmac-Auth' => ["{$publicKey}:{$signature}:{$nonce}:{$time}"],
110
        ];
111
112
        $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...
113
            throw new \Exception('This should not have been called!!');
114
        };
115
116
        $psr7Request = new ServerRequest([], [], 'http://localhost', 'GET', 'php://input', $headers);
117
118
        $response = $middleware($psr7Request, new Response(), $next);
119
120
        $this->assertFalse(isset($container['privateKey']));
121
122
        $this->assertSame(400, $response->getStatusCode());
123
        $this->assertSame('Bad Request', $response->getReasonPhrase());
124
    }
125
}
126