Passed
Pull Request — master (#219)
by Dmitriy
02:04
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
    public function setUp(): void
22
    {
23
        parent::setUp();
24
        $this->containerMock = $this->createMock(ContainerInterface::class);
25
        $this->fallbackHandlerMock = $this->createMock(RequestHandlerInterface::class);
26
        $this->middlewareDispatcher = new MiddlewareDispatcher($this->containerMock, $this->fallbackHandlerMock);
27
    }
28
29
    public function testAddThrowsInvalidArgumentExceptionWhenMiddlewareIsNotOfCorrectType(): void
30
    {
31
        $this->expectException(\InvalidArgumentException::class);
32
        $exampleInput = new SapiEmitter();
33
34
        $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

34
        $this->middlewareDispatcher->addMiddleware(/** @scrutinizer ignore-type */ $exampleInput);
Loading history...
35
    }
36
37
    /**
38
     * @doesNotPerformAssertions
39
     */
40
    public function testAddAddsCallableToMiddlewareArrayWithoutThrowingException(): void
41
    {
42
        $callable = static function () {
43
            echo 'example function for testing purposes';
44
        };
45
        $this->middlewareDispatcher->addMiddleware($callable);
46
    }
47
48
    /**
49
     * @doesNotPerformAssertions
50
     */
51
    public function testAddAddsMiddlewareInterfaceToMiddlewareArrayWithoutThrowingException(): void
52
    {
53
        $middleware = $this->createMock(MiddlewareInterface::class);
54
        $this->middlewareDispatcher->addMiddleware($middleware);
55
    }
56
57
    public function testDispatchCallsMiddlewareFullStack(): void
58
    {
59
        $request = new ServerRequest('GET', '/');
60
        $this->fallbackHandlerMock
61
            ->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

61
            ->/** @scrutinizer ignore-call */ 
62
              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...
62
            ->method('handle')
63
            ->with($request);
64
65
        $middleware1 = static function (ServerRequestInterface $request, RequestHandlerInterface $handler) {
66
            $request = $request->withAttribute('middleware', 'middleware1');
67
            return $handler->handle($request);
68
        };
69
        $middleware2 = static function (ServerRequestInterface $request) {
70
            return new Response(200, [], null, '1.1', implode($request->getAttributes()));
71
        };
72
73
        $this->middlewareDispatcher->addMiddleware($middleware2)->addMiddleware($middleware1);
74
        $response = $this->middlewareDispatcher->dispatch($request);
75
76
        $this->assertSame(200, $response->getStatusCode());
77
        $this->assertSame('middleware1', $response->getReasonPhrase());
78
        // ensure that dispatcher could be called multiple times
79
        $this->middlewareDispatcher->dispatch($request);
80
    }
81
}
82