Passed
Pull Request — master (#137)
by Zhukov
03:58
created

WebActionsCallerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A example() 0 3 1
A setUp() 0 6 1
A testHandlerInvocation() 0 12 1
A testExceptionOnNullAction() 0 9 1
A testProcess() 0 9 1
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
12
class WebActionsCallerTest extends TestCase
13
{
14
    /** @var ServerRequestInterface  */
15
    private $request;
16
17
    /** @var RequestHandlerInterface  */
18
    private $handler;
19
20
    /** @var ContainerInterface  */
21
    private $container;
22
23
    protected function setUp()
24
    {
25
        $this->request = $this->createMock(ServerRequestInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Ht...equestInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Psr\Http\Message\ServerRequestInterface of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
26
        $this->handler = $this->createMock(RequestHandlerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Ht...andlerInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Psr\Http\Server\RequestHandlerInterface of property $handler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
27
        $this->container = $this->createMock(ContainerInterface::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Psr\Co...tainerInterface::class) of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Psr\Container\ContainerInterface of property $container.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
28
        $this->container->method('get')->willReturn($this);
29
    }
30
31
    public function testProcess()
32
    {
33
        $this->request
34
            ->method('getAttribute')
0 ignored issues
show
Bug introduced by
The method method() does not exist on Psr\Http\Message\ServerRequestInterface. ( Ignorable by Annotation )

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

34
            ->/** @scrutinizer ignore-call */ 
35
              method('getAttribute')

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...
35
            ->with($this->equalTo('action'))
36
            ->willReturn('example');
37
38
        $response = (new WebActionsCaller('this', $this->container))->process($this->request, $this->handler);
39
        $this->assertEquals(204, $response->getStatusCode());
40
    }
41
42
    public function testExceptionOnNullAction()
43
    {
44
        $this->request
45
            ->method('getAttribute')
46
            ->with($this->equalTo('action'))
47
            ->willReturn(null);
48
49
        $this->expectException(\RuntimeException::class);
50
        (new WebActionsCaller('this', $this->container))->process($this->request, $this->handler);
51
    }
52
53
    public function testHandlerInvocation()
54
    {
55
        $this->request
56
            ->method('getAttribute')
57
            ->with($this->equalTo('action'))
58
            ->willReturn('notExistant');
59
60
        $this->handler
61
            ->expects($this->once())
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->once())

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
64
        (new WebActionsCaller('this', $this->container))->process($this->request, $this->handler);
65
    }
66
67
    public function example(): ResponseInterface
68
    {
69
        return new Response(204);
70
    }
71
}
72