Passed
Pull Request — master (#306)
by Tobias
02:05
created

CallbackMiddlewareTest::testCallback()   B

Complexity

Conditions 2
Paths 1

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 1
nop 0
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Buzz\Test\Unit\Middleware;
6
7
use Buzz\Exception\InvalidArgumentException;
8
use Buzz\Middleware\CallbackMiddleware;
9
use Nyholm\Psr7\Request;
10
use Nyholm\Psr7\Response;
11
use PHPUnit\Framework\TestCase;
12
13
class CallbackMiddlewareTest extends TestCase
14
{
15
    public function testCallback()
16
    {
17
        $requestIn = new Request('GET', '/');
18
        $responseIn = new Response(200);
19
20
        $responseOut = new Response(201);
21
        $requestOut = new Request('POST', '/');
22
23
        $middleware = new CallbackMiddleware(function () use ($requestIn, $responseIn, $requestOut, $responseOut) {
24
            $calls[] = $args = func_get_args();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$calls was never initialized. Although not strictly required by PHP, it is generally a good practice to add $calls = array(); before regardless.
Loading history...
25
            $this->assertEquals($requestIn, $args[0]);
26
27
            if (2 === count($args)) {
28
                $this->assertEquals($responseIn, $args[1]);
29
30
                return $responseOut;
31
            }
32
33
            return $requestOut;
34
        });
35
36
        $firstRequest = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $firstRequest is dead and can be removed.
Loading history...
37
        $this->assertEquals($requestOut, $middleware->handleRequest($requestIn, function ($request) {
38
            return $request;
39
        }));
40
41
        $secondRequest = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $secondRequest is dead and can be removed.
Loading history...
42
        $secondResponse = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $secondResponse is dead and can be removed.
Loading history...
43
        $this->assertEquals($responseOut, $middleware->handleResponse($requestIn, $responseIn, function ($request, $response) {
44
            return $response;
45
        }));
46
    }
47
48
    public function testInvalidCallback()
49
    {
50
        $this->expectException(InvalidArgumentException::class);
51
52
        new CallbackMiddleware([1, 2, 3]);
53
    }
54
}
55