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); |
|
|
|
|
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)); |
|
|
|
|
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
|
|
|
|
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: