BodyTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 79
c 1
b 0
f 0
dl 0
loc 154
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testSetEntitiesWithEmptyEntities() 0 7 1
A testSetEntitiesWithPropertyGetters() 0 20 1
A testGetExtendedNodesWithActions() 0 18 1
A testSetEntitiesWithCallableGetters() 0 20 1
A testGetExtendedNodesWithoutActions() 0 16 1
A testSetEntitiesWithoutGetters() 0 17 1
A createEntity() 0 12 1
A testSetEntitiesWithMixedGetters() 0 20 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Grid\Component;
6
7
use AbterPhp\Framework\Grid\Row\Row;
8
use AbterPhp\Framework\TestDouble\Domain\Entity\FooBarStub;
9
use PHPUnit\Framework\MockObject\MockObject;
10
use PHPUnit\Framework\TestCase;
11
12
class BodyTest extends TestCase
13
{
14
    public function testSetEntitiesWithEmptyEntities(): void
15
    {
16
        $sut = new Body([], null);
17
18
        $sut->setEntities([]);
19
20
        $this->assertCount(0, $sut->getNodes());
21
    }
22
23
    public function testSetEntitiesWithoutGetters(): void
24
    {
25
        $entities   = [];
26
        $entities[] = $this->createEntity('foo', 1);
27
        $entities[] = $this->createEntity('bar', 2);
28
29
        $sut = new Body([], null);
30
31
        $sut->setEntities($entities);
32
33
        $this->assertCount(2, $sut->getNodes());
34
35
        assert($sut[0] instanceof Row);
36
        assert($sut[1] instanceof Row);
37
38
        $this->assertCount(0, $sut[0]->getCells());
39
        $this->assertCount(0, $sut[1]->getCells());
40
    }
41
42
    public function testSetEntitiesWithPropertyGetters(): void
43
    {
44
        $entities   = [];
45
        $entities[] = $this->createEntity('foo', 1);
46
        $entities[] = $this->createEntity('bar', 2);
47
48
        $getters = [
49
            'foo' => 'getFoo',
50
            'bar' => 'getBar',
51
        ];
52
53
        $sut = new Body($getters, null);
54
55
        $sut->setEntities($entities);
56
57
        $this->assertCount(2, $sut->getNodes());
58
        $this->assertInstanceOf(Row::class, $sut[0]);
59
        $this->assertInstanceOf(Row::class, $sut[1]);
60
        $this->assertCount(2, $sut[0]->getCells());
61
        $this->assertCount(2, $sut[1]->getCells());
62
    }
63
64
    public function testSetEntitiesWithCallableGetters(): void
65
    {
66
        $entities   = [];
67
        $entities[] = $this->createEntity('foo', 1);
68
        $entities[] = $this->createEntity('bar', 2);
69
70
        $getters = [
71
            'foo' => fn ($entity) => $entity->getFoo(),
72
            'bar' => [$entities[0], 'getBar'],
73
        ];
74
75
        $sut = new Body($getters, null);
76
77
        $sut->setEntities($entities);
78
79
        $this->assertCount(2, $sut->getNodes());
80
        $this->assertInstanceOf(Row::class, $sut[0]);
81
        $this->assertInstanceOf(Row::class, $sut[1]);
82
        $this->assertCount(2, $sut[0]->getCells());
83
        $this->assertCount(2, $sut[1]->getCells());
84
    }
85
86
    public function testSetEntitiesWithMixedGetters(): void
87
    {
88
        $entities   = [];
89
        $entities[] = $this->createEntity('foo', 1);
90
        $entities[] = $this->createEntity('bar', 2);
91
92
        $getters = [
93
            'foo' => 'getFoo',
94
            'bar' => [$entities[0], 'getBar'],
95
        ];
96
97
        $sut = new Body($getters, null);
98
99
        $sut->setEntities($entities);
100
101
        $this->assertCount(2, $sut->getNodes());
102
        $this->assertInstanceOf(Row::class, $sut[0]);
103
        $this->assertInstanceOf(Row::class, $sut[1]);
104
        $this->assertCount(2, $sut[0]->getCells());
105
        $this->assertCount(2, $sut[1]->getCells());
106
    }
107
108
    /**
109
     * @param string $string
110
     * @param int    $entityId
111
     * @param string $foo
112
     * @param string $bar
113
     *
114
     * @return FooBarStub|MockObject
115
     */
116
    private function createEntity(string $string, int $entityId, string $foo = '', string $bar = '')
117
    {
118
        /** @var FooBarStub|MockObject $entity */
119
        $entity = $this->createMock(FooBarStub::class);
120
121
        $entity->expects($this->any())->method('__toString')->willReturn($string);
122
        $entity->expects($this->any())->method('getId')->willReturn($entityId);
123
        $entity->expects($this->any())->method('setId');
124
        $entity->expects($this->any())->method('getFoo')->willReturn($foo);
125
        $entity->expects($this->any())->method('getBar')->willReturn($bar);
126
127
        return $entity;
128
    }
129
130
    public function testGetExtendedNodesWithoutActions(): void
131
    {
132
        $entities   = [];
133
        $entities[] = $this->createEntity('foo', 1);
134
        $entities[] = $this->createEntity('bar', 2);
135
136
        $getters = [
137
            'foo' => 'getFoo',
138
            'bar' => [$entities[0], 'getBar'],
139
        ];
140
141
        $sut = new Body($getters, null);
142
143
        $nodes = $sut->getExtendedNodes();
144
145
        $this->assertSame([], $nodes);
146
    }
147
148
    public function testGetExtendedNodesWithActions(): void
149
    {
150
        $actions = new Actions();
151
152
        $entities   = [];
153
        $entities[] = $this->createEntity('foo', 1);
154
        $entities[] = $this->createEntity('bar', 2);
155
156
        $getters = [
157
            'foo' => 'getFoo',
158
            'bar' => [$entities[0], 'getBar'],
159
        ];
160
161
        $sut = new Body($getters, $actions);
162
163
        $nodes = $sut->getExtendedNodes();
164
165
        $this->assertSame([$actions], $nodes);
166
    }
167
}
168