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