PlaceTest::testValidInstructions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.36
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * (c) 2018 Douglas Reith.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Reith\ToyRobot\Console;
11
12
use PHPUnit\Framework\TestCase;
13
use Psr\Log\LoggerInterface;
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\Console\Tester\CommandTester;
16
use Reith\ToyRobot\Infrastructure\Bus\CommandBus;
17
18
class PlaceTest extends TestCase
19
{
20
    public function testValidInstructions()
21
    {
22
        $application = new Application();
23
        $application->add(new Place());
24
        $command = $application->find('place');
25
        $commandTester = new CommandTester($command);
26
27
        $mockBusHelper = self::createMock(BusHelper::class);
28
        $mockBusHelper->method('getName')
29
            ->willReturn('bus');
30
31
        $application->getHelperSet()->set($mockBusHelper);
0 ignored issues
show
Documentation introduced by
$mockBusHelper is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...Helper\HelperInterface>.

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
        $result = $commandTester->execute([
34
            'command' => $command->getName(),
35
            'X,Y,F' => null,
36
        ]);
37
38
        // Successful exit
39
        self::assertEquals(0, $result);
40
41
        $result = $commandTester->execute([
42
            'command' => $command->getName(),
43
            'X,Y,F' => '0,1',
44
        ]);
45
46
        self::assertEquals(0, $result);
47
48
        $result = $commandTester->execute([
49
            'command' => $command->getName(),
50
            'X,Y,F' => '3,2,S',
51
        ]);
52
53
        self::assertEquals(0, $result);
54
    }
55
56
    /**
57
     * @param string $invalidInstruction
58
     * @dataProvider getInvalidInstructions
59
     * @expectedException \RuntimeException
60
     */
61
    public function testInvalidInstructions(string $invalidInstruction)
62
    {
63
        $application = new Application();
64
        $application->add(new Place());
65
        $command = $application->find('place');
66
        $commandTester = new CommandTester($command);
67
        $mockLogger = self::createMock(LoggerInterface::class);
68
        $busHelper = new BusHelper(new CommandBus($mockLogger), new CommandBus($mockLogger));
0 ignored issues
show
Documentation introduced by
$mockLogger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Log\LoggerInterface>|null.

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...
Unused Code introduced by
The call to BusHelper::__construct() has too many arguments starting with new \Reith\ToyRobot\Infr...CommandBus($mockLogger).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
69
        $application->getHelperSet()->set($busHelper);
70
71
        $commandTester->execute([
72
            'command' => $command->getName(),
73
            'X,Y,F' => $invalidInstruction,
74
        ]);
75
    }
76
77
    public static function getInvalidInstructions(): array
78
    {
79
        return [
80
            ['5,1'], // out of bounds
81
            ['bar'], // nonsense
82
            ['3,2,T'], // not a direction
83
            ['3,2,South'], // direction is a single letter
84
            ['2, 2'], // spaces are not allowed
85
            ['E,1,1'], // wrong order
86
        ];
87
    }
88
}
89