1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Gui\Tests; |
3
|
|
|
|
4
|
|
|
use Mezon\Gui\ListBuilder\ListBuilder; |
5
|
|
|
|
6
|
|
|
class ListBuilderUnitTest extends ListBuilderTestsBase |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Method runs string assertions |
11
|
|
|
* |
12
|
|
|
* @param array $asserts |
13
|
|
|
* asserts |
14
|
|
|
* @param string $content |
15
|
|
|
* content to assert |
16
|
|
|
*/ |
17
|
|
|
protected function runAssertions(array $asserts, string $content): void |
18
|
|
|
{ |
19
|
|
|
foreach ($asserts as $assert) { |
20
|
|
|
$this->assertStringContainsString($assert, $content); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Testing constructor |
26
|
|
|
*/ |
27
|
|
|
public function testConstructorValid(): void |
28
|
|
|
{ |
29
|
|
|
// setup and test body |
30
|
|
|
$listBuilder = new ListBuilder($this->getFields(), new FakeAdapter()); |
|
|
|
|
31
|
|
|
|
32
|
|
|
// assertions |
33
|
|
|
$this->assertIsArray($listBuilder->getFields(), 'Invalid fields list type'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Data provider for the testListingForm |
38
|
|
|
* |
39
|
|
|
* @return array test data |
40
|
|
|
*/ |
41
|
|
|
public function listingFormDataProvider(): array |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
[ |
45
|
|
|
0, |
46
|
|
|
$this->getRecords(), |
47
|
|
|
[ |
48
|
|
|
'>id<', |
49
|
|
|
'>1<', |
50
|
|
|
'>2<' |
51
|
|
|
] |
52
|
|
|
], |
53
|
|
|
[ |
54
|
|
|
1, |
55
|
|
|
$this->getRecords(), |
56
|
|
|
[ |
57
|
|
|
'>id<', |
58
|
|
|
'>1<', |
59
|
|
|
'>2<', |
60
|
|
|
'/create-endpoint/' |
61
|
|
|
] |
62
|
|
|
], |
63
|
|
|
[ |
64
|
|
|
0, |
65
|
|
|
[], |
66
|
|
|
[ |
67
|
|
|
'class="no-items-title"', |
68
|
|
|
'../create/' |
69
|
|
|
] |
70
|
|
|
] |
71
|
|
|
]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Testing listing form |
76
|
|
|
* |
77
|
|
|
* @param int $createButton |
78
|
|
|
* do we need to show create button |
79
|
|
|
* @param array $records |
80
|
|
|
* list of records to be displayed |
81
|
|
|
* @param array $asserts |
82
|
|
|
* asserts |
83
|
|
|
* @dataProvider listingFormDataProvider |
84
|
|
|
*/ |
85
|
|
|
public function testListingForm(int $createButton, array $records, array $asserts): void |
86
|
|
|
{ |
87
|
|
|
// setup |
88
|
|
|
$_GET['create-page-endpoint'] = $createButton ? '/create-endpoint/' : null; |
89
|
|
|
$_GET['create-button'] = $createButton; |
90
|
|
|
$listBuilder = new ListBuilder($this->getFields(), new FakeAdapter($records)); |
|
|
|
|
91
|
|
|
|
92
|
|
|
// test body |
93
|
|
|
$content = $listBuilder->listingForm(); |
94
|
|
|
|
95
|
|
|
// assertions |
96
|
|
|
$this->runAssertions($asserts, $content); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Data provider for the testSimpleListingForm |
101
|
|
|
* |
102
|
|
|
* @return array test data |
103
|
|
|
*/ |
104
|
|
|
public function simpleListingFormDataProvider(): array |
105
|
|
|
{ |
106
|
|
|
return [ |
107
|
|
|
[ |
108
|
|
|
[], |
109
|
|
|
[ |
110
|
|
|
'class="no-items-title"' |
111
|
|
|
] |
112
|
|
|
] |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Testing listing form |
118
|
|
|
* |
119
|
|
|
* @param array $records |
120
|
|
|
* records to display |
121
|
|
|
* @param array $asserts |
122
|
|
|
* asserts |
123
|
|
|
* @dataProvider simpleListingFormDataProvider |
124
|
|
|
*/ |
125
|
|
|
public function testSimpleListingForm(array $records, array $asserts): void |
126
|
|
|
{ |
127
|
|
|
// setup |
128
|
|
|
$_GET['update-button'] = 1; |
129
|
|
|
$_GET['create-button'] = 1; |
130
|
|
|
$listBuilder = new ListBuilder($this->getFields(), new FakeAdapter($records)); |
|
|
|
|
131
|
|
|
|
132
|
|
|
// test body |
133
|
|
|
$content = $listBuilder->simpleListingForm(); |
134
|
|
|
|
135
|
|
|
// assertions |
136
|
|
|
$this->runAssertions($asserts, $content); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Testing data provider |
141
|
|
|
* |
142
|
|
|
* @return array testing data |
143
|
|
|
*/ |
144
|
|
|
public function customActionsDataProvider(): array |
145
|
|
|
{ |
146
|
|
|
$setup = function (): object { |
147
|
|
|
// setup method |
148
|
|
|
$listBuilder = new ListBuilder($this->getFields(), new FakeAdapter($this->getRecords())); |
|
|
|
|
149
|
|
|
|
150
|
|
|
$listBuilder->setCustomActions('!{id}!'); |
151
|
|
|
|
152
|
|
|
return $listBuilder; |
153
|
|
|
}; |
154
|
|
|
|
155
|
|
|
$assert = function ($result): void { |
156
|
|
|
// asserting method |
157
|
|
|
$this->assertStringNotContainsString('!1!', $result); |
158
|
|
|
$this->assertStringNotContainsString('!2!', $result); |
159
|
|
|
}; |
160
|
|
|
|
161
|
|
|
return [ |
162
|
|
|
// #0, simpleListingForm |
163
|
|
|
[ |
164
|
|
|
$setup, |
165
|
|
|
$assert, |
166
|
|
|
'simpleListingForm' |
167
|
|
|
], |
168
|
|
|
// #1, listingForm |
169
|
|
|
[ |
170
|
|
|
$setup, |
171
|
|
|
function ($result): void { |
172
|
|
|
// asserting method |
173
|
|
|
$this->assertStringContainsString('!1!', $result); |
174
|
|
|
$this->assertStringContainsString('!2!', $result); |
175
|
|
|
}, |
176
|
|
|
'listingForm' |
177
|
|
|
], |
178
|
|
|
// #2, listingForm, no custom buttons |
179
|
|
|
[ |
180
|
|
|
function (): object { |
181
|
|
|
// setup method |
182
|
|
|
return new ListBuilder($this->getFields(), new FakeAdapter($this->getRecords())); |
|
|
|
|
183
|
|
|
}, |
184
|
|
|
$assert, |
185
|
|
|
'listingForm' |
186
|
|
|
], |
187
|
|
|
// #3, listingForm, no custom buttons |
188
|
|
|
[ |
189
|
|
|
function (): object { |
190
|
|
|
// setup method |
191
|
|
|
return new ListBuilder([ |
|
|
|
|
192
|
|
|
'id' => [ |
193
|
|
|
'title' => 'Some id field' |
194
|
|
|
] |
195
|
|
|
], new FakeAdapter($this->getRecords())); |
196
|
|
|
}, |
197
|
|
|
$assert, |
198
|
|
|
'listingForm' |
199
|
|
|
], |
200
|
|
|
// #4, listingForm, no custom buttons |
201
|
|
|
[ |
202
|
|
|
function (): object { |
203
|
|
|
// setup method |
204
|
|
|
return new ListBuilder([ |
|
|
|
|
205
|
|
|
'id' => [ |
206
|
|
|
'title' => 'Some id field' |
207
|
|
|
] |
208
|
|
|
], new FakeAdapter($this->getRecords())); |
209
|
|
|
}, |
210
|
|
|
function (string $result) use ($assert) { |
211
|
|
|
$assert($result); |
212
|
|
|
|
213
|
|
|
$this->assertStringContainsString('Some id field', $result); |
214
|
|
|
$this->assertStringContainsString('>1<', $result); |
215
|
|
|
$this->assertStringContainsString('>2<', $result); |
216
|
|
|
}, |
217
|
|
|
'listingForm' |
218
|
|
|
], |
219
|
|
|
// #5, simpleListingForm, no custom buttons |
220
|
|
|
[ |
221
|
|
|
function (): object { |
222
|
|
|
// setup method |
223
|
|
|
return new ListBuilder([ |
|
|
|
|
224
|
|
|
'title' => [ |
225
|
|
|
'title' => 'Title field' |
226
|
|
|
] |
227
|
|
|
], new FakeAdapter($this->getRecords())); |
228
|
|
|
}, |
229
|
|
|
function (string $result) use ($assert) { |
230
|
|
|
$assert($result); |
231
|
|
|
|
232
|
|
|
$this->assertStringContainsString('Title field', $result); |
233
|
|
|
}, |
234
|
|
|
'simpleListingForm' |
235
|
|
|
], |
236
|
|
|
// #6, listingForm, default buttons |
237
|
|
|
[ |
238
|
|
|
function (): object { |
239
|
|
|
// setup method |
240
|
|
|
$_GET['update-button'] = 1; |
241
|
|
|
$_GET['create-button'] = 1; |
242
|
|
|
return new ListBuilder($this->getFields(), new FakeAdapter($this->getRecords())); |
|
|
|
|
243
|
|
|
}, |
244
|
|
|
function (string $result) use ($assert) { |
245
|
|
|
$assert($result); |
246
|
|
|
|
247
|
|
|
$this->assertStringContainsString('>id<', $result); |
248
|
|
|
$this->assertStringContainsString('>1<', $result); |
249
|
|
|
$this->assertStringContainsString('>2<', $result); |
250
|
|
|
}, |
251
|
|
|
'simpleListingForm' |
252
|
|
|
] |
253
|
|
|
]; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Testing method |
258
|
|
|
* |
259
|
|
|
* @param callable $setup |
260
|
|
|
* setup method |
261
|
|
|
* @param callable $assertions |
262
|
|
|
* assertions method |
263
|
|
|
* @paran string $method method to be called |
264
|
|
|
* @dataProvider customActionsDataProvider |
265
|
|
|
*/ |
266
|
|
|
public function testCustomActions(callable $setup, callable $assertions, string $method): void |
267
|
|
|
{ |
268
|
|
|
// setup |
269
|
|
|
$obj = $setup(); |
270
|
|
|
|
271
|
|
|
// test body |
272
|
|
|
$result = $obj->$method(); |
273
|
|
|
|
274
|
|
|
// assertions |
275
|
|
|
$assertions($result); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|