forked from yiisoft/yii2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListViewTest.php
266 lines (240 loc) · 7.8 KB
/
ListViewTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\framework\widgets;
use yii\data\ArrayDataProvider;
use yii\data\DataProviderInterface;
use yii\widgets\ListView;
use yiiunit\TestCase;
/**
* @group widgets
*/
class ListViewTest extends TestCase
{
protected function setUp()
{
parent::setUp();
$this->mockApplication();
}
public function testEmptyListShown()
{
ob_start();
$this->getListView([
'dataProvider' => new ArrayDataProvider(['allModels' => []]),
'emptyText' => 'Nothing at all',
])->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE('<div id="w0" class="list-view"><div class="empty">Nothing at all</div></div>', $out);
}
public function testEmpty()
{
ob_start();
$this->getListView([
'dataProvider' => new ArrayDataProvider(['allModels' => []]),
'emptyText' => false,
])->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE('<div id="w0" class="list-view"></div>', $out);
}
public function testEmptyListNotShown()
{
ob_start();
$this->getListView([
'dataProvider' => new ArrayDataProvider(['allModels' => []]),
'showOnEmpty' => true,
])->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE(<<<'HTML'
<div id="w0" class="list-view">
</div>
HTML
, $out);
}
/**
* @param array $options
* @return ListView
*/
private function getListView($options = [])
{
return new ListView(array_merge([
'id' => 'w0',
'dataProvider' => $this->getDataProvider(),
], $options));
}
/**
* @return DataProviderInterface
*/
private function getDataProvider()
{
return new ArrayDataProvider([
'allModels' => [
['id' => 1, 'login' => 'silverfire'],
['id' => 2, 'login' => 'samdark'],
['id' => 3, 'login' => 'cebe'],
],
]);
}
public function testSimplyListView()
{
ob_start();
$this->getListView()->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE(<<<'HTML'
<div id="w0" class="list-view"><div class="summary">Showing <b>1-3</b> of <b>3</b> items.</div>
<div data-key="0">0</div>
<div data-key="1">1</div>
<div data-key="2">2</div>
</div>
HTML
, $out);
}
public function testWidgetOptions()
{
ob_start();
$this->getListView(['options' => ['class' => 'test-passed'], 'separator' => ''])->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE(<<<'HTML'
<div id="w0" class="test-passed"><div class="summary">Showing <b>1-3</b> of <b>3</b> items.</div>
<div data-key="0">0</div><div data-key="1">1</div><div data-key="2">2</div>
</div>
HTML
, $out);
}
public function itemViewOptions()
{
return [
[
null,
'<div id="w0" class="list-view"><div class="summary">Showing <b>1-3</b> of <b>3</b> items.</div>
<div data-key="0">0</div>
<div data-key="1">1</div>
<div data-key="2">2</div>
</div>',
],
[
function ($model, $key, $index, $widget) {
return "Item #{$index}: {$model['login']} - Widget: " . $widget->className();
},
'<div id="w0" class="list-view"><div class="summary">Showing <b>1-3</b> of <b>3</b> items.</div>
<div data-key="0">Item #0: silverfire - Widget: yii\widgets\ListView</div>
<div data-key="1">Item #1: samdark - Widget: yii\widgets\ListView</div>
<div data-key="2">Item #2: cebe - Widget: yii\widgets\ListView</div>
</div>',
],
[
'@yiiunit/data/views/widgets/ListView/item',
'<div id="w0" class="list-view"><div class="summary">Showing <b>1-3</b> of <b>3</b> items.</div>
<div data-key="0">Item #0: silverfire - Widget: yii\widgets\ListView</div>
<div data-key="1">Item #1: samdark - Widget: yii\widgets\ListView</div>
<div data-key="2">Item #2: cebe - Widget: yii\widgets\ListView</div>
</div>',
],
];
}
/**
* @dataProvider itemViewOptions
* @param mixed $itemView
* @param string $expected
*/
public function testItemViewOptions($itemView, $expected)
{
ob_start();
$this->getListView(['itemView' => $itemView])->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE($expected, $out);
}
public function itemOptions()
{
return [
[
null,
'<div id="w0" class="list-view"><div class="summary">Showing <b>1-3</b> of <b>3</b> items.</div>
<div data-key="0">0</div>
<div data-key="1">1</div>
<div data-key="2">2</div>
</div>',
],
[
function ($model, $key, $index, $widget) {
return [
'tag' => 'span',
'data' => [
'test' => 'passed',
'key' => $key,
'index' => $index,
'id' => $model['id'],
],
];
},
'<div id="w0" class="list-view"><div class="summary">Showing <b>1-3</b> of <b>3</b> items.</div>
<span data-test="passed" data-key="0" data-index="0" data-id="1" data-key="0">0</span>
<span data-test="passed" data-key="1" data-index="1" data-id="2" data-key="1">1</span>
<span data-test="passed" data-key="2" data-index="2" data-id="3" data-key="2">2</span>
</div>',
],
];
}
/**
* @dataProvider itemOptions
* @param mixed $itemOptions
* @param string $expected
*/
public function testItemOptions($itemOptions, $expected)
{
ob_start();
$this->getListView(['itemOptions' => $itemOptions])->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE($expected, $out);
}
public function testBeforeAndAfterItem()
{
$before = function ($model, $key, $index, $widget) {
$widget = get_class($widget);
return "<!-- before: {$model['id']}, key: $key, index: $index, widget: $widget -->";
};
$after = function ($model, $key, $index, $widget) {
if ($model['id'] === 1) {
return null;
}
$widget = get_class($widget);
return "<!-- after: {$model['id']}, key: $key, index: $index, widget: $widget -->";
};
ob_start();
$this->getListView([
'beforeItem' => $before,
'afterItem' => $after,
])->run();
$out = ob_get_clean();
$this->assertEqualsWithoutLE(<<<HTML
<div id="w0" class="list-view"><div class="summary">Showing <b>1-3</b> of <b>3</b> items.</div>
<!-- before: 1, key: 0, index: 0, widget: yii\widgets\ListView -->
<div data-key="0">0</div>
<!-- before: 2, key: 1, index: 1, widget: yii\widgets\ListView -->
<div data-key="1">1</div>
<!-- after: 2, key: 1, index: 1, widget: yii\widgets\ListView -->
<!-- before: 3, key: 2, index: 2, widget: yii\widgets\ListView -->
<div data-key="2">2</div>
<!-- after: 3, key: 2, index: 2, widget: yii\widgets\ListView -->
</div>
HTML
, $out
);
}
/**
* @see https://github.com/yiisoft/yii2/pull/14596
*/
public function testShouldTriggerInitEvent()
{
$initTriggered = false;
$this->getListView([
'on init' => function () use (&$initTriggered) {
$initTriggered = true;
},
'dataProvider' => new ArrayDataProvider(['allModels' => []]),
]);
$this->assertTrue($initTriggered);
}
}