ListBuilderUnitTest::commonBehaviourDataProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 124
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 68
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 124
rs 8.6981

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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