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 Symfony\Component\Console\Application; |
14
|
|
|
use Symfony\Component\Console\Tester\CommandTester; |
15
|
|
|
use Reith\ToyRobot\Infrastructure\Bus\CommandBus; |
16
|
|
|
|
17
|
|
|
class PlaceTest extends TestCase |
18
|
|
|
{ |
19
|
|
|
public static function testValidInstructions() |
20
|
|
|
{ |
21
|
|
|
$application = new Application(); |
22
|
|
|
$application->add(new Place()); |
23
|
|
|
$command = $application->find('place'); |
24
|
|
|
$commandTester = new CommandTester($command); |
25
|
|
|
$busHelper = new BusHelper(new CommandBus([]), new CommandBus([])); |
26
|
|
|
$application->getHelperSet()->set($busHelper); |
27
|
|
|
|
28
|
|
|
$result = $commandTester->execute([ |
29
|
|
|
'command' => $command->getName(), |
30
|
|
|
'X,Y,F' => null, |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
// Successful exit |
34
|
|
|
self::assertEquals(0, $result); |
35
|
|
|
|
36
|
|
|
$result = $commandTester->execute([ |
37
|
|
|
'command' => $command->getName(), |
38
|
|
|
'X,Y,F' => '0,1', |
39
|
|
|
]); |
40
|
|
|
|
41
|
|
|
self::assertEquals(0, $result); |
42
|
|
|
|
43
|
|
|
$result = $commandTester->execute([ |
44
|
|
|
'command' => $command->getName(), |
45
|
|
|
'X,Y,F' => '3,2,S', |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
self::assertEquals(0, $result); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $invalidInstruction |
53
|
|
|
* @dataProvider getInvalidInstructions |
54
|
|
|
* @expectedException \RuntimeException |
55
|
|
|
*/ |
56
|
|
|
public static function testInvalidInstructions(string $invalidInstruction) |
57
|
|
|
{ |
58
|
|
|
$application = new Application(); |
59
|
|
|
$application->add(new Place()); |
60
|
|
|
$command = $application->find('place'); |
61
|
|
|
$commandTester = new CommandTester($command); |
62
|
|
|
$busHelper = new BusHelper(new CommandBus([]), new CommandBus([])); |
63
|
|
|
$application->getHelperSet()->set($busHelper); |
64
|
|
|
|
65
|
|
|
$result = $commandTester->execute([ |
|
|
|
|
66
|
|
|
'command' => $command->getName(), |
67
|
|
|
'X,Y,F' => $invalidInstruction, |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function getInvalidInstructions(): array |
72
|
|
|
{ |
73
|
|
|
return [ |
74
|
|
|
['5,1'], // out of bounds |
75
|
|
|
['bar'], // nonsense |
76
|
|
|
['3,2,T'], // not a direction |
77
|
|
|
['3,2,South'], // direction is a single letter |
78
|
|
|
['2, 2'], // spaces are not allowed |
79
|
|
|
['E,1,1'], // wrong order |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.