ValidatorMiddlewareTest::testExecute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace League\Tactician\Bundle\Tests\Middleware;
4
5
use League\Tactician\Bundle\Middleware\InvalidCommandException;
6
use League\Tactician\Bundle\Middleware\ValidatorMiddleware;
7
use League\Tactician\Bundle\Tests\Fake\FakeCommand;
8
use Mockery\MockInterface;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\Validator\ConstraintViolationList;
11
use Symfony\Component\Validator\Validator\ValidatorInterface;
12
13
class ValidatorMiddlewareTest extends TestCase
14
{
15
    /**
16
     * @var ValidatorInterface | MockInterface
17
     */
18
    protected $validator;
19
20
    /**
21
     * @var ValidatorMiddleware
22
     */
23
    protected $middleware;
24
25
    protected function setUp(): void
26
    {
27
        parent::setUp();
28
29
        $this->validator = \Mockery::mock('Symfony\Component\Validator\Validator\ValidatorInterface');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Mockery::mock('Symfony\...r\\ValidatorInterface') of type object<Mockery\LegacyMockInterface> is incompatible with the declared type object<Symfony\Component...tor\ValidatorInterface> of property $validator.

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...
30
31
        $this->middleware = new ValidatorMiddleware($this->validator);
0 ignored issues
show
Documentation introduced by
$this->validator is of type object<Mockery\LegacyMockInterface>, but the function expects a object<Symfony\Component...tor\ValidatorInterface>.

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...
32
    }
33
34
    public function testExecute()
35
    {
36
        $list = new ConstraintViolationList([\Mockery::mock('Symfony\Component\Validator\ConstraintViolation')]);
0 ignored issues
show
Documentation introduced by
array(\Mockery::mock('Sy...\ConstraintViolation')) is of type array<integer,object<Moc...LegacyMockInterface>"}>, but the function expects a array<integer,object<Sym...intViolationInterface>>.

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
        $this->validator->shouldReceive('validate')->once()->andReturn($list);
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

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...
39
40
        try {
41
42
            $this->middleware->execute(new FakeCommand(), function () {
43
            });
44
45
        } catch (InvalidCommandException $e) {
46
            $this->assertEquals($list, $e->getViolations());
47
            $this->assertEquals(new FakeCommand(), $e->getCommand());
48
        }
49
    }
50
51
    public function testExecuteWithoutViolations()
52
    {
53
        $list = new ConstraintViolationList([]);
54
55
        $this->validator->shouldReceive('validate')->once()->andReturn($list);
0 ignored issues
show
Bug introduced by
The method shouldReceive() does not seem to exist on object<Symfony\Component...tor\ValidatorInterface>.

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...
56
57
        $this->middleware->execute(new FakeCommand(), function () {
58
        });
59
    }
60
}
61