Completed
Push — master ( 8184b0...a4b4a5 )
by Douglas
02:13
created

toyrobotTest::testRobotReport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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
use PHPUnit\Framework\TestCase;
11
use Psr\Log\LoggerInterface;
12
use Symfony\Component\Console\Application;
13
use Symfony\Component\Console\Tester\ApplicationTester;
14
use Reith\ToyRobot\Domain\Space\Table;
15
16
// Persistence
17
use Reith\ToyRobot\Infrastructure\Persistence\InMemoryRobotStore;
18
use Reith\ToyRobot\Infrastructure\Persistence\RobotRepository;
19
20
// Console tasks
21
use Reith\ToyRobot\Console\Place;
22
use Reith\ToyRobot\Console\Report;
23
use Reith\ToyRobot\Console\BusHelper;
24
25
// Buses
26
use Reith\ToyRobot\Infrastructure\Bus\CommandBus;
27
use Reith\ToyRobot\Infrastructure\Bus\QueryBus;
28
29
// Command and query handlers
30
use Reith\ToyRobot\CommandHandler\RobotPlacer;
31
use Reith\ToyRobot\QueryHandler\RobotReporter;
32
33
class toyrobotTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
34
{
35
    private $testApp;
36
37
    protected function setUp()
38
    {
39
        $mockLogger = self::createMock(LoggerInterface::class);
40
41
        $store = InMemoryRobotStore::getStore($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...
42
43
        // Pass to the repository
44
        $repository = new RobotRepository($store);
45
        $table = Table::create(5);
46
47
        // Create the command and query handlers
48
        $robotPlacer = new RobotPlacer($table, $repository, $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>.

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...
49
        $robotReporter = new RobotReporter($repository, $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>.

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...
50
51
        // Now the buses
52
        $commandBus = (new CommandBus())
53
            ->registerHandler($robotPlacer)
54
        ;
55
56
        $queryBus = (new QueryBus())
57
            ->registerHandler($robotReporter)
58
        ;
59
60
        // Set up the console
61
        $application = new Application();
62
        $application->setAutoExit(false);
63
        $application->addCommands([new Place(), new Report()]);
64
65
        $busHelper = (new BusHelper())
66
            ->setCommandBus($commandBus)
67
            ->setQueryBus($queryBus)
68
        ;
69
70
        $application->getHelperSet()->set($busHelper);
71
72
        $this->testApp = new ApplicationTester($application);
73
    }
74
75
    public function testToyrobotApp()
76
    {
77
        $this->testApp->run([]);
78
79
        self::assertContains(
80
            'Place the robot on the table',
81
            $this->testApp->getDisplay()
82
        );
83
84
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
85
    }
86
87
    public function testPlaceRobot()
88
    {
89
        $this->testApp->run(['place']);
90
91
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
92
    }
93
94
    public function testRobotReport()
95
    {
96
        $this->testApp->run(['report']);
97
98
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
99
    }
100
101
    public function testPlacingAndReporting()
102
    {
103
        $instruction = '2,2,S';
104
105
        $this->testApp->run(['command' => 'place', 'X,Y,F' => '2,2,S']);
106
107
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
108
109
        $this->testApp->run(['report']);
110
111
        self::assertContains($instruction, $this->testApp->getDisplay());
112
113
        // 
114
        // An incorrect placement won't move the robot, it'll report
115
        // the same position
116
        //
117
        $this->testApp->run(['command' => 'place', 'X,Y,F' => '12,4,W']);
118
119
        self::assertNotEquals(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
120
121
        $this->testApp->run(['report']);
122
123
        self::assertContains($instruction, $this->testApp->getDisplay());
124
    }
125
}
126