|
1
|
|
|
<?php |
|
2
|
|
|
namespace FwlibTest\Html\ListView; |
|
3
|
|
|
|
|
4
|
|
|
use Fwlib\Html\ListView\AbstractRetriever; |
|
5
|
|
|
use Fwlib\Html\ListView\FitMode; |
|
6
|
|
|
use Fwlib\Html\ListView\Fitter; |
|
7
|
|
|
use Fwlib\Html\ListView\ListDto; |
|
8
|
|
|
use Fwlib\Html\ListView\ListView; |
|
9
|
|
|
use Fwlib\Html\ListView\Renderer; |
|
10
|
|
|
use Fwlib\Html\ListView\RendererInterface; |
|
11
|
|
|
use Fwlib\Html\ListView\RequestInterface; |
|
12
|
|
|
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase; |
|
13
|
|
|
use PHPUnit_Framework_MockObject_MockObject as MockObject; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @copyright Copyright 2015 Fwolf |
|
17
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html LGPL-3.0+ |
|
18
|
|
|
*/ |
|
19
|
|
|
class ListViewTest extends PHPUnitTestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @param string[] $methods |
|
23
|
|
|
* @return MockObject|ListView |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function buildMock(array $methods = null) |
|
26
|
|
|
{ |
|
27
|
|
|
$mock = $this->getMock(ListView::class, $methods); |
|
28
|
|
|
|
|
29
|
|
|
return $mock; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
public function testAccessors() |
|
34
|
|
|
{ |
|
35
|
|
|
$listView = $this->buildMock(); |
|
36
|
|
|
|
|
37
|
|
|
$this->assertInstanceOf( |
|
38
|
|
|
RendererInterface::class, |
|
39
|
|
|
$this->reflectionCall($listView, 'getRenderer') |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
$this->assertInstanceOf( |
|
43
|
|
|
RequestInterface::class, |
|
44
|
|
|
$this->reflectionCall($listView, 'getRequest') |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
/** @var MockObject|Fitter $fitter */ |
|
48
|
|
|
$fitter = $this->getMock(Fitter::class, []); |
|
49
|
|
|
$listView->setFitter($fitter); |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$listView->setRowCount(42); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
public function testDecorateRows() |
|
56
|
|
|
{ |
|
57
|
|
|
$listView = $this->buildMock(); |
|
58
|
|
|
|
|
59
|
|
|
$listDto = new ListDto; |
|
60
|
|
|
$this->reflectionCall($listView, 'decorateRows', [$listDto]); |
|
61
|
|
|
$this->assertEmpty($listDto->getBody()); |
|
62
|
|
|
|
|
63
|
|
|
// No decorator |
|
64
|
|
|
$listDto->setBody([3 => ['a', 'b']]) |
|
|
|
|
|
|
65
|
|
|
->setRowCount(1); |
|
66
|
|
|
$this->reflectionCall($listView, 'decorateRows', [$listDto]); |
|
67
|
|
|
$this->assertEqualArray([3 => ['a', 'b']], $listDto->getBody()); |
|
68
|
|
|
|
|
69
|
|
|
$listView->setRowDecorator(function ($row) { |
|
|
|
|
|
|
70
|
|
|
foreach ($row as &$val) { |
|
71
|
|
|
$val = strtoupper($val); |
|
72
|
|
|
} |
|
73
|
|
|
unset($val); |
|
74
|
|
|
|
|
75
|
|
|
return $row; |
|
76
|
|
|
}); |
|
77
|
|
|
$this->reflectionCall($listView, 'decorateRows', [$listDto]); |
|
78
|
|
|
$this->assertEqualArray([3 => ['A', 'B']], $listDto->getBody()); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
public function testFitHeadAndBody() |
|
83
|
|
|
{ |
|
84
|
|
|
$listView = $this->buildMock(); |
|
85
|
|
|
|
|
86
|
|
|
$listView->setConfig('fitMode', FitMode::TO_TITLE); |
|
|
|
|
|
|
87
|
|
|
$listView->setConfig('fitEmptyFiller', '-'); |
|
88
|
|
|
|
|
89
|
|
|
$listView->setHead(['F' => 'Foo', 'B' => 'Bar']); |
|
|
|
|
|
|
90
|
|
|
$listView->setBody([['F' => 'foo']]); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
$listDto = $this->reflectionCall($listView, 'getListDto'); |
|
93
|
|
|
$this->reflectionCall($listView, 'fitHeadAndBody', [$listDto]); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertEqualArray( |
|
96
|
|
|
[['F' => 'foo', 'B' => '-']], |
|
97
|
|
|
$listDto->getBody() |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
View Code Duplication |
public function testGetFilledListDto() |
|
|
|
|
|
|
103
|
|
|
{ |
|
104
|
|
|
$listView = $this->buildMock(); |
|
105
|
|
|
|
|
106
|
|
|
/** @var MockObject|AbstractRetriever $retriever */ |
|
107
|
|
|
$retriever = $this->getMock( |
|
108
|
|
|
AbstractRetriever::class, |
|
109
|
|
|
['getListBody', 'getRowCount'] |
|
110
|
|
|
); |
|
111
|
|
|
$retriever->expects($this->once()) |
|
|
|
|
|
|
112
|
|
|
->method('getRowCount') |
|
113
|
|
|
->willReturn(42); |
|
114
|
|
|
$listView->setRetriever($retriever); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
$listDto = $this->reflectionCall($listView, 'getFilledListDto'); |
|
117
|
|
|
$this->assertEquals(42, $listDto->getRowCount()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
public function testGetHtml() |
|
122
|
|
|
{ |
|
123
|
|
|
$listView = $this->buildMock( |
|
124
|
|
|
['fitHeadAndBody', 'decorateRows', 'render'] |
|
125
|
|
|
); |
|
126
|
|
|
$listView->expects($this->once()) |
|
|
|
|
|
|
127
|
|
|
->method('fitHeadAndBody'); |
|
128
|
|
|
$listView->expects($this->once()) |
|
129
|
|
|
->method('decorateRows'); |
|
130
|
|
|
$listView->expects($this->once()) |
|
131
|
|
|
->method('render'); |
|
132
|
|
|
|
|
133
|
|
|
$listView->getHtml(); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
public function testRender() |
|
138
|
|
|
{ |
|
139
|
|
|
/** @var MockObject|Renderer $renderer */ |
|
140
|
|
|
$renderer = $this->getMock( |
|
141
|
|
|
Renderer::class, |
|
142
|
|
|
['setConfigInstance', 'setListDto', 'getHtml'] |
|
143
|
|
|
); |
|
144
|
|
|
$renderer->expects($this->once()) |
|
|
|
|
|
|
145
|
|
|
->method('setConfigInstance') |
|
146
|
|
|
->willReturnSelf(); |
|
147
|
|
|
$renderer->expects($this->once()) |
|
148
|
|
|
->method('setListDto') |
|
149
|
|
|
->willReturnSelf(); |
|
150
|
|
|
$renderer->expects($this->once()) |
|
151
|
|
|
->method('getHtml'); |
|
152
|
|
|
|
|
153
|
|
|
$listView = $this->buildMock(); |
|
154
|
|
|
$listView->setRenderer($renderer); |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
$this->reflectionCall($listView, 'render', [new ListDto()]); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
public function testSetBody() |
|
161
|
|
|
{ |
|
162
|
|
|
$listView = $this->buildMock(); |
|
163
|
|
|
|
|
164
|
|
|
$listView->setBody([['key' => 'foo'], ['key' => 'bar']], true); |
|
|
|
|
|
|
165
|
|
|
/** @var ListDto $listDto */ |
|
166
|
|
|
$listDto = $this->reflectionCall($listView, 'getListDto'); |
|
167
|
|
|
$this->assertEquals(2, $listDto->getRowCount()); |
|
168
|
|
|
|
|
169
|
|
|
$listView->reset(); |
|
|
|
|
|
|
170
|
|
|
$listDto = $this->reflectionCall($listView, 'getListDto'); |
|
171
|
|
|
$this->assertEquals( |
|
172
|
|
|
ListView::ROW_COUNT_NOT_SET, |
|
173
|
|
|
$listDto->getRowCount() |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: