Completed
Push — master ( c97ea9...aac971 )
by Alex
03:39
created

CommonListBuilderUnitTest::getFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10

1 Method

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