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