WarehouseTreeTest::testPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 9
rs 10
c 4
b 1
f 2
1
<?php
2
3
use GGGGino\WarehousePath\Breadcrumb\BreadthFirstBreadcrumb;
4
use GGGGino\WarehousePath\Entity\Corridor;
5
use GGGGino\WarehousePath\Entity\Location;
6
use GGGGino\WarehousePath\Entity\Place;
7
use GGGGino\WarehousePath\Entity\Wall;
8
use GGGGino\WarehousePath\Parser\TreeParser;
9
use GGGGino\WarehousePath\PlacesCollector;
10
use GGGGino\WarehousePath\Warehouse;
11
use PHPUnit\Framework\TestCase;
12
13
final class WarehouseTreeTest extends TestCase
14
{
15
    /**
16
     * @var Warehouse
17
     */
18
    protected $warehouse;
19
20
    /**
21
     * Simple warehouse structure
22
     * Lx = Location
23
     * Wx = Wall
24
     * Cx = Corridor
25
     *
26
     *  L1|W1|L6|L7
27
     *  L2|W2|L5|L8
28
     *  L3|C1|L4
29
     */
30
    protected function setUp()
31
    {
32
        $placesCollector = new PlacesCollector();
33
        $placesCollector->addBasePlaceTypes();
34
35
        /** @var Location $locationType */
36
        $locationType = $placesCollector->getPlaceTypeByClass(Location::class);
37
        /** @var Corridor $locationType */
38
        $corridorType = $placesCollector->getPlaceTypeByClass(Corridor::class);
39
        /** @var Wall $locationType */
40
        $wallType = $placesCollector->getPlaceTypeByClass(Wall::class);
41
42
        $loc1 = new Place($locationType, 'L1');
43
        $loc2 = new Place($locationType, 'L2');
44
        $loc3 = new Place($locationType, 'L3');
45
        $loc4 = new Place($locationType, 'L4');
46
        $loc5 = new Place($locationType, 'L5');
47
        $loc6 = new Place($locationType, 'L6');
48
        $loc7 = new Place($locationType, 'L7');
49
        $loc8 = new Place($locationType, 'L8');
50
        $corr1 = new Place($corridorType, 'C1');
51
        $wall1 = new Place($wallType, 'W1');
52
        $wall2 = new Place($wallType, 'W2');
53
54
        $loc1->setRightRef($wall1);
55
        $loc1->setBottomRef($loc2);
56
57
        $loc2->setRightRef($wall2);
58
        $loc2->setBottomRef($loc3);
59
60
        $loc3->setRightRef($corr1);
61
62
        $wall1->setBottomRef($wall2);
63
        $wall1->setRightRef($loc6);
64
65
        $wall2->setBottomRef($corr1);
66
        $wall2->setRightRef($loc5);
67
68
        $corr1->setRightRef($loc4);
69
        $loc6->setBottomRef($loc5);
70
        $loc6->setRightRef($loc7);
71
        $loc5->setBottomRef($loc4);
72
        $loc5->setRightRef($loc8);
73
74
        $loc7->setBottomRef($loc8);
75
76
        $arrayPlaces = array($loc1, $loc2, $loc3, $loc4, $loc5, $loc6, $loc7, $loc8, $corr1, $wall1, $wall2);
77
78
        $wm = new TreeParser($arrayPlaces, $placesCollector);
79
        $placesCollector->setPlaces($wm->parse());
80
81
        $breadcrumbBuilder = new BreadthFirstBreadcrumb($placesCollector);
82
83
        $this->warehouse = new Warehouse($placesCollector, $wm, $breadcrumbBuilder);
84
    }
85
86
    /**
87
     * Test instance
88
     */
89
    public function testConstructor(): void
90
    {
91
        $this->assertInstanceOf(Warehouse::class, $this->warehouse);
92
    }
93
94
    public function testPath(): void
95
    {
96
        /** @var Place $start */
97
        $start = $this->warehouse->getPlacesCollector()->getPlaces()[0];
98
        /** @var Place $end */
99
        $end = $this->warehouse->getPlacesCollector()->getPlaces()[4];
100
101
        $this->warehouse->getPath($start, $end);
102
        $this->assertEquals(7, $end->getCurrentWeight());
103
    }
104
105
    public function testPath2(): void
106
    {
107
        /** @var Place $start */
108
        $start = $this->warehouse->getPlacesCollector()->getPlaces()[0];
109
        /** @var Place $end */
110
        $end = $this->warehouse->getPlacesCollector()->getPlaces()[6];
111
112
        $this->warehouse->getPath($start, $end);
113
        $this->assertEquals(9, $end->getCurrentWeight());
114
    }
115
116
    public function testPrintMatrix(): void
117
    {
118
        /** @var Place[][] $matrix */
119
        $matrix = $this->warehouse->createMatrix();
120
        $this->assertEquals(3, count($matrix));
121
        $this->assertEquals(4, count($matrix[0]));
122
    }
123
}