Passed
Pull Request — master (#257)
by Wilmer
15:17
created

WebActionsCallerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
c 1
b 0
f 0
dl 0
loc 60
rs 10
wmc 5
1
<?php
2
3
namespace Yiisoft\Yii\Web\Middleware;
4
5
use Nyholm\Psr7\Response;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Container\ContainerInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\RequestHandlerInterface;
11
use Yiisoft\Di\Container;
12
13
class WebActionsCallerTest extends TestCase
14
{
15
    /** @var ServerRequestInterface  */
16
    private $request;
17
18
    /** @var RequestHandlerInterface  */
19
    private $handler;
20
21
    /** @var ContainerInterface  */
22
    private $container;
23
24
    protected function setUp(): void
25
    {
26
        $this->request = $this->createMock(ServerRequestInterface::class);
27
        $this->handler = $this->createMock(RequestHandlerInterface::class);
28
        $this->container = new Container([self::class => $this]);
29
    }
30
31
    public function testProcess(): void
32
    {
33
        $this->request
34
            ->method('getAttribute')
35
            ->with($this->equalTo('action'))
36
            ->willReturn('process');
37
38
        $response = (new WebActionsCaller(self::class, $this->container))->process($this->request, $this->handler);
39
        $this->assertEquals(204, $response->getStatusCode());
40
    }
41
42
    public function testExceptionOnNullAction(): void
43
    {
44
        $this->request
45
            ->method('getAttribute')
46
            ->with($this->equalTo('action'))
47
            ->willReturn(null);
48
49
        $this->expectException(\RuntimeException::class);
50
        (new WebActionsCaller(self::class, $this->container))->process($this->request, $this->handler);
51
    }
52
53
    public function testHandlerInvocation(): void
54
    {
55
        $this->request
56
            ->method('getAttribute')
57
            ->with($this->equalTo('action'))
58
            ->willReturn('notExistant');
59
60
        $this->handler
61
            ->expects($this->once())
62
            ->method('handle');
63
64
        (new WebActionsCaller(self::class, $this->container))->process($this->request, $this->handler);
65
    }
66
67
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
68
    {
69
        $this->assertSame($this->request, $request);
70
        $this->assertSame($this->handler, $handler);
71
72
        return new Response(204);
73
    }
74
}
75