Passed
Pull Request — master (#219)
by Dmitriy
11:24 queued 09:25
created

testDispatchCallsMiddlewareFullStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 15
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 23
rs 9.7666
1
<?php
2
3
namespace Yiisoft\Yii\Web\Tests;
4
5
use Nyholm\Psr7\Response;
6
use Nyholm\Psr7\ServerRequest;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Container\ContainerInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Yiisoft\Yii\Web\Emitter\SapiEmitter;
13
use Yiisoft\Yii\Web\MiddlewareDispatcher;
14
15
class MiddlewareDispatcherTest extends TestCase
16
{
17
    private MiddlewareDispatcher $middlewareDispatcher;
18
    private ContainerInterface $containerMock;
19
    private RequestHandlerInterface $fallbackHandlerMock;
20
21
    /**
22
     * @var MiddlewareInterface[]
23
     */
24
    private array $middlewareMocks;
0 ignored issues
show
introduced by
The private property $middlewareMocks is not used, and could be removed.
Loading history...
25
26
    public function setUp(): void
27
    {
28
        parent::setUp();
29
        $this->containerMock = $this->createMock(ContainerInterface::class);
30
        $this->fallbackHandlerMock = $this->createMock(RequestHandlerInterface::class);
31
        $this->middlewareDispatcher = new MiddlewareDispatcher($this->containerMock, $this->fallbackHandlerMock);
32
    }
33
34
    public function testAddThrowsInvalidArgumentExceptionWhenMiddlewareIsNotOfCorrectType(): void
35
    {
36
        $this->expectException(\InvalidArgumentException::class);
37
        $exampleInput = new SapiEmitter();
38
39
        $this->middlewareDispatcher->addMiddleware($exampleInput);
0 ignored issues
show
Bug introduced by
$exampleInput of type Yiisoft\Yii\Web\Emitter\SapiEmitter is incompatible with the type Psr\Http\Server\MiddlewareInterface|callable expected by parameter $middleware of Yiisoft\Yii\Web\Middlewa...atcher::addMiddleware(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $this->middlewareDispatcher->addMiddleware(/** @scrutinizer ignore-type */ $exampleInput);
Loading history...
40
    }
41
42
    /**
43
     * @doesNotPerformAssertions
44
     */
45
    public function testAddAddsCallableToMiddlewareArrayWithoutThrowingException(): void
46
    {
47
        $callable = static function () {
48
            echo 'example function for testing purposes';
49
        };
50
        $this->middlewareDispatcher->addMiddleware($callable);
51
    }
52
53
    /**
54
     * @doesNotPerformAssertions
55
     */
56
    public function testAddAddsMiddlewareInterfaceToMiddlewareArrayWithoutThrowingException(): void
57
    {
58
        $middleware = $this->createMock(MiddlewareInterface::class);
59
        $this->middlewareDispatcher->addMiddleware($middleware);
60
    }
61
62
    public function testDispatchCallsMiddlewareFullStack(): void
63
    {
64
        $request = new ServerRequest('GET', '/');
65
        $this->fallbackHandlerMock
66
            ->expects($this->never())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on Psr\Http\Server\RequestHandlerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
            ->/** @scrutinizer ignore-call */ 
67
              expects($this->never())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
67
            ->method('handle')
68
            ->with($request);
69
70
        $middleware1 = static function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
71
            $request = $request->withAttribute('middleware', 'middleware1');
72
            return $handler->handle($request);
73
        };
74
        $middleware2 = static function (ServerRequestInterface $request) {
75
            return new Response(200, [], null, '1.1', implode($request->getAttributes()));
76
        };
77
78
        $this->middlewareDispatcher->addMiddleware($middleware2)->addMiddleware($middleware1);
79
        $response = $this->middlewareDispatcher->dispatch($request);
80
81
        $this->assertSame(200, $response->getStatusCode());
82
        $this->assertSame('middleware1', $response->getReasonPhrase());
83
        // ensure that dispatcher could be called multiple times
84
        $this->middlewareDispatcher->dispatch($request);
85
    }
86
}
87