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