RobotTest::robotCanMove()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
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\Domain\Robot;
11
12
use MathPHP\LinearAlgebra\Vector;
13
use PHPUnit\Framework\TestCase;
14
use Reith\ToyRobot\Domain\Space\Table;
15
16
class RobotTest extends TestCase
17
{
18
    public static function testCreatingARobot()
19
    {
20
        // This is an example of a 5x5 table
21
        $table = Table::create(5);
22
23
        // To create (or reset) a robot it must be placed in a space.
24
        // Default position is 0,0 (for 2 dimensions)
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
25
        $robot = $table->placeRobot();
26
27
        self::assertInstanceOf(Robot::class, $robot);
28
    }
29
30
    /**
31
     * @test
32
     */
33
    public static function robotCanMove()
34
    {
35
        $table = Table::create(5);
36
        $robot = $table->placeRobot();
37
38
        // From origin, 0,0, robot can move northward
39
        $position = $robot->moveNorthward()->getPosition();
40
41
        // Now it should be at 0,1
42
        self::assertEquals([0, 1], $position->getVector());
43
44
        $position = $robot->moveSouthward()->getPosition();
45
46
        // Now it should be back at origin
47
        self::assertEquals([0, 0], $position->getVector());
48
49
        // Let's get it dancing
50
        $position = $robot
51
            ->moveEastward()
52
            ->moveEastward()
53
            ->moveNorthward()
54
            ->moveNorthward()
55
            ->moveWestward()
56
            ->moveNorthward()
57
            ->getPosition()
58
        ;
59
60
        self::assertEquals([1, 3], $position->getVector());
61
62
        self::assertSame('1,3,N', $robot->getReportAsString());
63
    }
64
65
    /**
66
     * @test
67
     * @expectedException \Assert\AssertionFailedException
68
     */
69
    public static function robotCannotStartWithABadDirection()
70
    {
71
        $table = Table::create(3);
0 ignored issues
show
Unused Code introduced by
$table is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
72
73
        Robot::create(
74
            Table::create(4),
75
            new Vector([0, 1]),
76
            'U'
77
        );
78
    }
79
80
    /**
81
     * @test
82
     * @expectedException \Reith\ToyRobot\Domain\Space\Exception\BoundaryTestException
83
     */
84
    public static function robotCannotFallOffTheSouthSide()
85
    {
86
        $table = Table::create(5);
87
        $robot = $table->placeRobot();
88
89
        $robot->moveSouthward();
90
    }
91
92
    /**
93
     * @test
94
     * @expectedException \Reith\ToyRobot\Domain\Space\Exception\BoundaryTestException
95
     */
96
    public static function robotCannotFallOffTheWestSide()
97
    {
98
        $table = Table::create(5);
99
        $robot = $table->placeRobot();
100
101
        $robot->moveWestward();
102
    }
103
104
    /**
105
     * @test
106
     * @expectedException \Reith\ToyRobot\Domain\Space\Exception\BoundaryTestException
107
     */
108
    public static function robotCannotFallOffTheNorthSide()
109
    {
110
        // Smaller 3 x 3 table
111
        $table = Table::create(3);
112
        $robot = $table->placeRobot();
113
114
        $robot
115
            ->moveNorthward()
116
            ->moveNorthward()
117
            ->moveNorthward()
118
        ;
119
    }
120
}
121