Passed
Push — master ( a65783...53adb5 )
by Povilas
02:44
created

RowViewTest   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 57
dl 0
loc 142
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Povs\ListerBundle\View;
4
5
use Doctrine\ORM\QueryBuilder;
6
use PHPUnit\Framework\MockObject\MockObject;
7
use PHPUnit\Framework\TestCase;
8
use Povs\ListerBundle\Mapper\ListField;
9
use Povs\ListerBundle\Service\ValueAccessor;
10
11
/**
12
 * @author Povilas Margaiatis <[email protected]>
13
 */
14
class RowViewTest extends TestCase
15
{
16
    public function testInitBody(): RowView
17
    {
18
        return $this->getRowView('body', ['foo' => 'bar', 'list_identifier' => '100']);
19
    }
20
21
    public function testInitHeader(): RowView
22
    {
23
        return $this->getRowView('header', null);
24
    }
25
26
    /**
27
     * @depends testInitBody
28
     * @param RowView $rowView
29
     */
30
    public function testGetGetFields(RowView $rowView): void
31
    {
32
        $this->assertCount(1, $rowView->getFields());
33
        $this->assertInstanceOf(FieldView::class, $rowView->getFields()[0]);
34
    }
35
36
    /**
37
     * @depends testInitBody
38
     * @param RowView $rowView
39
     */
40
    public function testGetValueBody(RowView $rowView): void
41
    {
42
        $this->assertEquals(['id' => 'field'], $rowView->getValue());
43
    }
44
45
    /**
46
     * @depends testInitBody
47
     * @param RowView $rowView
48
     */
49
    public function testGetIdBody(RowView $rowView): void
50
    {
51
        $this->assertEquals(100, $rowView->getId());
52
    }
53
54
    /**
55
     * @depends testInitBody
56
     * @param RowView $rowView
57
     */
58
    public function testGetLabeledValueBody(RowView $rowView): void
59
    {
60
        $this->assertEquals(['header' => 'field'], $rowView->getLabeledValue());
61
    }
62
63
    /**
64
     * @depends testInitHeader
65
     * @param RowView $rowView
66
     */
67
    public function testGetLabeledValueHeader(RowView $rowView): void
68
    {
69
        $this->assertEquals(['header' => 'header'], $rowView->getLabeledValue());
70
    }
71
72
    /**
73
     * @depends testInitHeader
74
     * @param RowView $rowView
75
     */
76
    public function testGetValueHeader(RowView $rowView): void
77
    {
78
        $this->assertEquals(['id' => 'header'], $rowView->getValue());
79
    }
80
81
    /**
82
     * @depends testInitHeader
83
     * @param RowView $rowView
84
     */
85
    public function testGetIdHeader(RowView $rowView): void
86
    {
87
        $this->assertNull($rowView->getId());
88
    }
89
90
    public function testGetList(): void
91
    {
92
        $listViewMock = $this->createMock(ListView::class);
93
        $view = $this->getRowView('body', ['list_identifier' => '100'], $listViewMock);
94
95
        $this->assertEquals($listViewMock, $view->getList());
96
    }
97
98
    /**
99
     * @param string     $type
100
     * @param array|null $data
101
     * @param MockObject $listViewMock
102
     *
103
     * @return RowView
104
     */
105
    private function getRowView(string $type, ?array $data, ?MockObject $listViewMock = null): RowView
106
    {
107
        $valueAccessor = $this->createMock(ValueAccessor::class);
108
        $listViewMock = $listViewMock ?: $this->createMock(ListView::class);
109
        $fieldViewMock = $this->createMock(FieldView::class);
110
        $listViewMock->expects($this->once())
111
            ->method('getFieldViews')
112
            ->willReturn([$fieldViewMock]);
113
        $listFieldMock = $this->createMock(ListField::class);
114
        $listFieldMock->expects($this->once())
115
            ->method('getId')
116
            ->willReturn('id');
117
        $fieldViewMock->method('getListField')
118
            ->willReturn($listFieldMock);
119
120
        if ($type === 'body') {
121
            $queryBuilderMock = $this->createMock(QueryBuilder::class);
122
            $valueAccessor->expects($this->once())
123
                ->method('getFieldValue')
124
                ->with($fieldViewMock, ['normalized' => 'data'])
125
                ->willReturn('field');
126
            $valueAccessor->expects($this->once())
127
                ->method('normalizeData')
128
                ->with($data, $queryBuilderMock)
129
                ->willReturn(['normalized' => 'data']);
130
            $fieldViewMock->method('getValue')
131
                ->willReturn('field');
132
        } else {
133
            $queryBuilderMock = null;
134
            $valueAccessor->expects($this->once())
135
                ->method('getHeaderValue')
136
                ->with($fieldViewMock)
137
                ->willReturn('header');
138
            $listFieldMock->expects($this->once())
139
                ->method('setOption')
140
                ->with('label', 'header');
141
            $fieldViewMock->method('getValue')
142
                ->willReturn('header');
143
        }
144
145
        $fieldViewMock->method('getLabel')
146
            ->willReturn('header');
147
148
        $fieldViewMock->expects($this->once())
149
            ->method('init')
150
            ->with($this->isInstanceOf(RowView::class), $type === 'body' ? 'field' : 'header');
151
152
        $rowView = new RowView($valueAccessor, $queryBuilderMock);
153
        $rowView->init($listViewMock, $type, $data);
154
155
        return $rowView;
156
    }
157
}
158