Passed
Push — master ( 39e51c...ca88cb )
by Alex
03:38
created

ListBuilderUnitTest::testConstructorValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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