BrokerTest::testCannotHandleWithoutMiddleware()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Northwoods\Broker;
5
6
use Eloquent\Phony\Mock\Handle\InstanceHandle;
7
use Eloquent\Phony\Phpunit\Phony;
8
use OutOfBoundsException;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Http\Message\ResponseInterface;
11
use Psr\Http\Message\ServerRequestInterface;
12
use Psr\Http\Server\MiddlewareInterface;
13
use Psr\Http\Server\RequestHandlerInterface;
14
15
class BrokerTest extends TestCase
16
{
17
    /** @var Broker */
18
    private $broker;
19
20
    public function setUp(): void
21
    {
22
        $this->broker = new Broker();
23
    }
24
25 View Code Duplication
    public function testCanAppendMiddleware(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
26
    {
27
        // Mock
28
        $mw1 = $this->mockMiddleware();
29
        $mw2 = $this->mockMiddleware();
30
31
        $this->broker->append($mw1->get(), $mw2->get());
32
33
        // Execute
34
        $response = $this->broker->process(
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
            $this->mockRequest()->get(),
0 ignored issues
show
Documentation introduced by
$this->mockRequest()->get() is of type object<Eloquent\Phony\Mock\Mock>, but the function expects a object<Psr\Http\Message\ServerRequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
            $this->mockHandler()->get()
0 ignored issues
show
Documentation introduced by
$this->mockHandler()->get() is of type object<Eloquent\Phony\Mock\Mock>, but the function expects a object<Psr\Http\Server\RequestHandlerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
        );
38
39
        // Verify
40
        Phony::inOrder(
41
            $mw1->process->once()->called(),
42
            $mw2->process->once()->called()
43
        );
44
    }
45
46 View Code Duplication
    public function testCanPrependMiddleware(): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
47
    {
48
        // Mock
49
        $mw1 = $this->mockMiddleware();
50
        $mw2 = $this->mockMiddleware();
51
52
        $this->broker->append($mw1->get());
53
        $this->broker->prepend($mw2->get());
54
55
        // Execute
56
        $response = $this->broker->process(
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
            $this->mockRequest()->get(),
0 ignored issues
show
Documentation introduced by
$this->mockRequest()->get() is of type object<Eloquent\Phony\Mock\Mock>, but the function expects a object<Psr\Http\Message\ServerRequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
            $this->mockHandler()->get()
0 ignored issues
show
Documentation introduced by
$this->mockHandler()->get() is of type object<Eloquent\Phony\Mock\Mock>, but the function expects a object<Psr\Http\Server\RequestHandlerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
59
        );
60
61
        // Verify
62
        Phony::inOrder(
63
            $mw2->process->once()->called(),
64
            $mw1->process->once()->called()
65
        );
66
    }
67
68
    public function testCannotHandleWithoutMiddleware(): void
69
    {
70
        // Expect
71
        $this->expectException(OutOfBoundsException::class);
72
73
        // Execute
74
        $this->broker->handle(
75
            $this->mockRequest()->get()
0 ignored issues
show
Documentation introduced by
$this->mockRequest()->get() is of type object<Eloquent\Phony\Mock\Mock>, but the function expects a object<Psr\Http\Message\ServerRequestInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
        );
77
    }
78
79
    private function mockHandler(): InstanceHandle
80
    {
81
        $handler = Phony::mock(RequestHandlerInterface::class);
82
        $handler->handle->returns($this->mockResponse());
83
84
        return $handler;
85
    }
86
87
    private function mockMiddleware(): InstanceHandle
88
    {
89
        $middleware = Phony::mock(MiddlewareInterface::class);
90
91
        $middleware->process->does(function ($request, $handler) {
92
            return $handler->handle($request);
93
        });
94
95
        return $middleware;
96
    }
97
98
    private function mockRequest(): InstanceHandle
99
    {
100
        return Phony::mock(ServerRequestInterface::class);
101
    }
102
103
    private function mockResponse(): InstanceHandle
104
    {
105
        return Phony::mock(ResponseInterface::class);
106
    }
107
}
108