1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Gui\Tests; |
3
|
|
|
|
4
|
|
|
use PHPUnit\Framework\TestCase; |
5
|
|
|
use Mezon\Gui\ListBuilder\Common as CommonListBuilder; |
6
|
|
|
|
7
|
|
|
class CommonListBuilderUnitTest 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 CommonListBuilder($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
|
|
|
* Combining substrings to assert |
70
|
|
|
* |
71
|
|
|
* @param array $specificSubstrings |
72
|
|
|
* specific substrings |
73
|
|
|
* @return array total list of substrings |
74
|
|
|
*/ |
75
|
|
|
private function commonSubstring(array $specificSubstrings): array |
76
|
|
|
{ |
77
|
|
|
return array_merge([ |
78
|
|
|
'{action-message}' |
79
|
|
|
], $specificSubstrings); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Data provider for the testListingForm |
84
|
|
|
* |
85
|
|
|
* @return array test data |
86
|
|
|
*/ |
87
|
|
|
public function listingFormDataProvider(): array |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
|
|
[ |
91
|
|
|
0, |
92
|
|
|
$this->getRecords(), |
93
|
|
|
$this->commonSubstring([ |
94
|
|
|
'>id<', |
95
|
|
|
'>1<', |
96
|
|
|
'>2<' |
97
|
|
|
]) |
98
|
|
|
], |
99
|
|
|
[ |
100
|
|
|
1, |
101
|
|
|
$this->getRecords(), |
102
|
|
|
$this->commonSubstring([ |
103
|
|
|
'>id<', |
104
|
|
|
'>1<', |
105
|
|
|
'>2<', |
106
|
|
|
'/create-endpoint/' |
107
|
|
|
]) |
108
|
|
|
], |
109
|
|
|
[ |
110
|
|
|
0, |
111
|
|
|
[], |
112
|
|
|
$this->commonSubstring([ |
113
|
|
|
'class="no-items-title"', |
114
|
|
|
'../create/', |
115
|
|
|
]) |
116
|
|
|
] |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Testing listing form |
122
|
|
|
* |
123
|
|
|
* @param int $createButton |
124
|
|
|
* do we need to show create button |
125
|
|
|
* @param array $records |
126
|
|
|
* list of records to be displayed |
127
|
|
|
* @param array $asserts |
128
|
|
|
* asserts |
129
|
|
|
* @dataProvider listingFormDataProvider |
130
|
|
|
*/ |
131
|
|
|
public function testListingForm(int $createButton, array $records, array $asserts): void |
132
|
|
|
{ |
133
|
|
|
// setup |
134
|
|
|
$_GET['create-page-endpoint'] = $createButton ? '/create-endpoint/' : null; |
135
|
|
|
$_GET['create-button'] = $createButton; |
136
|
|
|
$listBuilder = new CommonListBuilder($this->getFields(), new FakeAdapter($records)); |
137
|
|
|
|
138
|
|
|
// test body |
139
|
|
|
$content = $listBuilder->listingForm(); |
140
|
|
|
|
141
|
|
|
// assertions |
142
|
|
|
$this->runAssertions($asserts, $content); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Asserting that string contains substrings |
147
|
|
|
* |
148
|
|
|
* @param array $needles |
149
|
|
|
* @param string $haystack |
150
|
|
|
*/ |
151
|
|
|
private function assertStringContainsStrings(array $needles, string $haystack): void |
152
|
|
|
{ |
153
|
|
|
foreach ($needles as $needle) { |
154
|
|
|
$this->assertStringContainsString($needle, $haystack); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Testing data provider |
160
|
|
|
* |
161
|
|
|
* @return array testing data |
162
|
|
|
*/ |
163
|
|
|
public function commonBehaviourDataProvider(): array |
164
|
|
|
{ |
165
|
|
|
$setup = function (): object { |
166
|
|
|
// setup method |
167
|
|
|
$listBuilder = new CommonListBuilder($this->getFields(), new FakeAdapter($this->getRecords())); |
168
|
|
|
|
169
|
|
|
$listBuilder->setCustomActions('!{id}!'); |
170
|
|
|
|
171
|
|
|
return $listBuilder; |
172
|
|
|
}; |
173
|
|
|
|
174
|
|
|
$assert = function ($result): void { |
175
|
|
|
// asserting method |
176
|
|
|
$this->assertStringNotContainsString('!1!', $result); |
177
|
|
|
$this->assertStringNotContainsString('!2!', $result); |
178
|
|
|
}; |
179
|
|
|
|
180
|
|
|
$headerData = [ |
181
|
|
|
'id' => [ |
182
|
|
|
'title' => 'Some id field' |
183
|
|
|
] |
184
|
|
|
]; |
185
|
|
|
|
186
|
|
|
return [ |
187
|
|
|
// #0, listingForm |
188
|
|
|
[ |
189
|
|
|
$setup, |
190
|
|
|
function ($result): void { |
191
|
|
|
// asserting method |
192
|
|
|
$this->assertStringContainsStrings([ |
193
|
|
|
'!1!', |
194
|
|
|
'!2!' |
195
|
|
|
], $result); |
196
|
|
|
} |
197
|
|
|
], |
198
|
|
|
// #1, listingForm, no custom buttons |
199
|
|
|
[ |
200
|
|
|
function (): object { |
201
|
|
|
// setup method |
202
|
|
|
return new CommonListBuilder($this->getFields(), new FakeAdapter($this->getRecords())); |
203
|
|
|
}, |
204
|
|
|
$assert |
205
|
|
|
], |
206
|
|
|
// #2, listingForm, no custom buttons |
207
|
|
|
[ |
208
|
|
|
function () use ($headerData): object { |
209
|
|
|
// setup method |
210
|
|
|
return new CommonListBuilder($headerData, new FakeAdapter($this->getRecords())); |
211
|
|
|
}, |
212
|
|
|
$assert |
213
|
|
|
], |
214
|
|
|
// #3, listingForm, no custom buttons |
215
|
|
|
[ |
216
|
|
|
function () use ($headerData): object { |
217
|
|
|
// setup method |
218
|
|
|
return new CommonListBuilder($headerData, new FakeAdapter($this->getRecords())); |
219
|
|
|
}, |
220
|
|
|
function (string $result) use ($assert) { |
221
|
|
|
$assert($result); |
222
|
|
|
|
223
|
|
|
$this->assertStringContainsStrings([ |
224
|
|
|
'Some id field', |
225
|
|
|
'>1<', |
226
|
|
|
'>2<' |
227
|
|
|
], $result); |
228
|
|
|
} |
229
|
|
|
], |
230
|
|
|
// #4, listingForm, default buttons |
231
|
|
|
[ |
232
|
|
|
function (): object { |
233
|
|
|
// setup method |
234
|
|
|
$_GET['update-button'] = 1; |
235
|
|
|
$_GET['create-button'] = 1; |
236
|
|
|
return new CommonListBuilder($this->getFields(), new FakeAdapter($this->getRecords())); |
237
|
|
|
}, |
238
|
|
|
function (string $result) use ($assert) { |
239
|
|
|
$assert($result); |
240
|
|
|
|
241
|
|
|
$this->assertStringContainsStrings([ |
242
|
|
|
'>id<', |
243
|
|
|
'>1<', |
244
|
|
|
'>2<' |
245
|
|
|
], $result); |
246
|
|
|
} |
247
|
|
|
], |
248
|
|
|
// #5, listingForm, custom title and description |
249
|
|
|
[ |
250
|
|
|
function (): object { |
251
|
|
|
// setup method |
252
|
|
|
$listBuilder = new CommonListBuilder($this->getFields(), new FakeAdapter($this->getRecords())); |
253
|
|
|
$listBuilder->listTitle = 'List Title'; |
254
|
|
|
$listBuilder->listDescription = 'List Description'; |
255
|
|
|
return $listBuilder; |
256
|
|
|
}, |
257
|
|
|
function (string $result) use ($assert) { |
258
|
|
|
$assert($result); |
259
|
|
|
|
260
|
|
|
$this->assertStringContainsStrings([ |
261
|
|
|
'>id<', |
262
|
|
|
'>1<', |
263
|
|
|
'>2<', |
264
|
|
|
'List Title', |
265
|
|
|
'List Description' |
266
|
|
|
], $result); |
267
|
|
|
} |
268
|
|
|
], |
269
|
|
|
// #6, listingForm, default title and description |
270
|
|
|
[ |
271
|
|
|
function (): object { |
272
|
|
|
// setup method |
273
|
|
|
return new CommonListBuilder($this->getFields(), new FakeAdapter($this->getRecords())); |
274
|
|
|
}, |
275
|
|
|
function (string $result) use ($assert) { |
276
|
|
|
$assert($result); |
277
|
|
|
|
278
|
|
|
$this->assertStringContainsStrings( |
279
|
|
|
[ |
280
|
|
|
'>id<', |
281
|
|
|
'>1<', |
282
|
|
|
'>2<', |
283
|
|
|
'Список записей', |
284
|
|
|
'Выберите необходимое действие' |
285
|
|
|
], |
286
|
|
|
$result); |
287
|
|
|
} |
288
|
|
|
] |
289
|
|
|
]; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Testing method |
294
|
|
|
* |
295
|
|
|
* @param callable $setup |
296
|
|
|
* setup method |
297
|
|
|
* @param callable $assertions |
298
|
|
|
* assertions method |
299
|
|
|
* @dataProvider commonBehaviourDataProvider |
300
|
|
|
*/ |
301
|
|
|
public function testCommonBehaviour(callable $setup, callable $assertions): void |
302
|
|
|
{ |
303
|
|
|
// setup |
304
|
|
|
$obj = $setup(); |
305
|
|
|
|
306
|
|
|
// test body |
307
|
|
|
$result = $obj->listingForm(); |
308
|
|
|
|
309
|
|
|
// assertions |
310
|
|
|
$assertions($result); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|