Passed
Push — master ( 928c25...8579cb )
by Gerrit
02:16
created

tests/unit/Arguments/ArgumentCallTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Copyright (C) 2017  Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\SymfonyGenerics\Tests\Unit\Arguments;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Arguments\ArgumentCall;
15
use Addiks\SymfonyGenerics\Arguments\Argument;
16
use InvalidArgumentException;
17
use stdClass;
18
use Addiks\SymfonyGenerics\Services\ArgumentCompilerInterface;
19
use ReflectionMethod;
20
21
final class ArgumentCallTest extends TestCase
22
{
23
24
    /**
25
     * @test
26
     */
27
    public function shouldResolveCallArgument()
28
    {
29
        /** @var Argument $callee */
30
        $callee = $this->createMock(Argument::class);
31
        $callee->method("resolve")->willReturn($this);
32
33
        /** @var Argument $argumentA */
34
        $argumentA = $this->createMock(Argument::class);
35
        $argumentA->method("resolve")->willReturn("some-foo");
36
37
        /** @var Argument $argumentB */
38
        $argumentB = $this->createMock(Argument::class);
39
        $argumentB->method("resolve")->willReturn(31415);
40
41
        /** @var ArgumentCompilerInterface $argumentCompiler */
42
        $argumentCompiler = $this->createMock(ArgumentCompilerInterface::class);
43
        $argumentCompiler->expects($this->any())->method('buildCallArguments')->with(
44
            $this->equalTo(new ReflectionMethod(__CLASS__, 'someMethod')),
45
            $this->equalTo(["some-foo", 31415])
46
        )->willReturn(["some-foo", 31415]);
47
48
        $subject = new ArgumentCall(
49
            $argumentCompiler,
50
            $callee,
51
            "someMethod",
52
            [
53
                $argumentA,
54
                $argumentB
55
            ]
56
        );
57
58
        /** @var mixed $actualResult */
59
        $actualResult = $subject->resolve();
60
61
        $this->assertEquals("some-result", $actualResult);
62
    }
63
64
    public function someMethod(string $foo, int $bar): string
65
    {
66
        $this->assertEquals("some-foo", $foo);
67
        $this->assertEquals(31415, $bar);
68
        return "some-result";
69
    }
70
71
    /**
72
     * @test
73
     */
74 View Code Duplication
    public function shouldRejectNonObjectCallee()
75
    {
76
        $this->expectException(InvalidArgumentException::class);
77
78
        /** @var Argument $callee */
79
        $callee = $this->createMock(Argument::class);
80
        $callee->method("resolve")->willReturn("non-object");
81
82
        $subject = new ArgumentCall(
83
            $this->createMock(ArgumentCompilerInterface::class),
0 ignored issues
show
$this->createMock(\Addik...mpilerInterface::class) is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Addiks\SymfonyGen...umentCompilerInterface>.

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...
84
            $callee,
85
            "someMethod",
86
            []
87
        );
88
89
        $subject->resolve();
90
    }
91
92
}
93