Passed
Push — html ( 4d98c2...e7bfd2 )
by Peter
03:09
created

RowTest::testGetExtendedNodes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Grid\Row;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Grid\Action\Action;
9
use AbterPhp\Framework\Grid\Cell\Cell;
10
use AbterPhp\Framework\Grid\Collection\Cells;
11
use AbterPhp\Framework\Grid\Component\Actions;
12
use AbterPhp\Framework\Html\Tag;
13
use AbterPhp\Framework\TestDouble\Domain\MockEntityFactory;
14
use PHPUnit\Framework\TestCase;
15
16
class RowTest extends TestCase
17
{
18
    public function testSetEntitySetsEntityWorksWithoutActions(): void
19
    {
20
        $sut = new Row(new Cells());
21
22
        $stubEntity = MockEntityFactory::createEntityStub($this);
23
24
        $sut->setEntity($stubEntity);
25
26
        $this->assertSame($stubEntity, $sut->getEntity());
27
    }
28
29
    public function testSetEntitySetsEntityOnAllActions(): void
30
    {
31
        $stubEntity = MockEntityFactory::createEntityStub($this);
32
33
        $actionCount = 2;
34
35
        $actions = new Actions();
36
        for ($i = 0; $i < $actionCount; $i++) {
37
            $action = $this->createMock(Action::class);
38
39
            $action->expects($this->once())->method('setEntity')->with($stubEntity);
40
41
            $actions[] = $action;
42
        }
43
44
        $sut = new Row(new Cells(), $actions);
45
46
        $sut->setEntity($stubEntity);
47
    }
48
49
    public function testRender(): void
50
    {
51
        $stubEntity = MockEntityFactory::createEntityStub($this, 'foo', null, 'id-1');
52
53
        $actionCount = 2;
54
55
        $actions = new Actions();
56
        for ($i = 0; $i < $actionCount; $i++) {
57
            $actionMock = $this->createMock(Action::class);
58
59
            $actionMock->expects($this->once())->method('setEntity')->with($stubEntity);
60
            $actionMock->expects($this->atLeastOnce())->method('__toString')->willReturn("action-$i");
61
62
            $actions[] = $actionMock;
63
        }
64
65
        $sut = new Row(new Cells(), $actions);
66
67
        $sut->setEntity($stubEntity);
68
69
        $actualResult   = (string)$sut;
70
        $repeatedResult = (string)$sut;
71
72
        $this->assertStringContainsString('action-0', $actualResult);
73
        $this->assertStringContainsString('action-1', $actualResult);
74
        $this->assertStringContainsString($actualResult, $repeatedResult);
75
    }
76
77
    public function testGetNodes(): void
78
    {
79
        $expectedResult = 0;
80
81
        $cell0 = new Cell(new Tag('abc', [], null, Html5::TAG_I), 'foo-group');
82
        $cells   = new Cells([$cell0]);
83
        $actions = new Actions();
84
85
        $sut = new Row($cells, $actions);
86
87
        $nodes = $sut->getNodes();
88
89
        $this->assertCount($expectedResult, $nodes);
90
    }
91
92
    public function testGetExtendedNodes(): void
93
    {
94
        $expectedResult = 2;
95
96
        $cell0 = new Cell(new Tag('abc', [], null, Html5::TAG_I), 'foo-group');
97
        $cells   = new Cells([$cell0]);
98
        $actions = new Actions();
99
100
        $sut = new Row($cells, $actions);
101
102
        $nodes = $sut->getExtendedNodes();
103
104
        $this->assertCount($expectedResult, $nodes);
105
    }
106
}
107