Passed
Push — master ( c477ed...0b5fa5 )
by Alex
02:17
created

CommonListBuilderUnitTest::testCustomActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 10
rs 10
1
<?php
2
namespace Mezon\Gui\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Gui\ListBuilder\Common as CommonListBuilder;
6
7
class CommonListBuilderUnitTest extends TestCase
8
{
9
10
    /**
11
     * Method returns list of fields
12
     *
13
     * @return array Fields algorithms object
14
     */
15
    protected function getFields(): array
16
    {
17
        return [
18
            'id',
19
            'domain_id',
20
            'title'
21
        ];
22
    }
23
24
    /**
25
     * Method runs string assertions
26
     *
27
     * @param array $asserts
28
     *            asserts
29
     * @param string $content
30
     *            content to assert
31
     */
32
    protected function runAssertions(array $asserts, string $content): void
33
    {
34
        foreach ($asserts as $assert) {
35
            $this->assertStringContainsString($assert, $content);
36
        }
37
    }
38
39
    /**
40
     * Testing constructor
41
     */
42
    public function testConstructorValid(): void
43
    {
44
        // setup and test body
45
        $listBuilder = new CommonListBuilder($this->getFields(), new FakeAdapter());
46
47
        // assertions
48
        $this->assertIsArray($listBuilder->getFields(), 'Invalid fields list type');
49
    }
50
51
    /**
52
     * Method returns testing records
53
     *
54
     * @return array testing records
55
     */
56
    private function getRecords(): array
57
    {
58
        return [
59
            [
60
                'id' => 1,
61
            ],
62
            [
63
                'id' => 2,
64
            ]
65
        ];
66
    }
67
68
    /**
69
     * Data provider for the testListingForm
70
     *
71
     * @return array test data
72
     */
73
    public function listingFormDataProvider(): array
74
    {
75
        return [
76
            [
77
                0,
78
                $this->getRecords(),
79
                [
80
                    '>id<',
81
                    '>1<',
82
                    '>2<'
83
                ]
84
            ],
85
            [
86
                1,
87
                $this->getRecords(),
88
                [
89
                    '>id<',
90
                    '>1<',
91
                    '>2<',
92
                    '/create-endpoint/'
93
                ]
94
            ],
95
            [
96
                0,
97
                [],
98
                [
99
                    'class="no-items-title"',
100
                    '../create/'
101
                ]
102
            ]
103
        ];
104
    }
105
106
    /**
107
     * Testing listing form
108
     *
109
     * @param int $createButton
110
     *            do we need to show create button
111
     * @param array $records
112
     *            list of records to be displayed
113
     * @param array $asserts
114
     *            asserts
115
     * @dataProvider listingFormDataProvider
116
     */
117
    public function testListingForm(int $createButton, array $records, array $asserts): void
118
    {
119
        // setup
120
        $_GET['create-page-endpoint'] = $createButton ? '/create-endpoint/' : null;
121
        $_GET['create-button'] = $createButton;
122
        $listBuilder = new CommonListBuilder($this->getFields(), new FakeAdapter($records));
123
124
        // test body
125
        $content = $listBuilder->listingForm();
126
127
        // assertions
128
        $this->runAssertions($asserts, $content);
129
    }
130
131
    /**
132
     * Testing data provider
133
     *
134
     * @return array testing data
135
     */
136
    public function customActionsDataProvider(): array
137
    {
138
        $setup = function (): object {
139
            // setup method
140
            $listBuilder = new CommonListBuilder($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
        return [
154
            // #0, listingForm
155
            [
156
                $setup,
157
                function ($result): void {
158
                    // asserting method
159
                    $this->assertStringContainsString('!1!', $result);
160
                    $this->assertStringContainsString('!2!', $result);
161
                }
162
            ],
163
            // #1, listingForm, no custom buttons
164
            [
165
                function (): object {
166
                    // setup method
167
                    return new CommonListBuilder($this->getFields(), new FakeAdapter($this->getRecords()));
168
                },
169
                $assert
170
            ],
171
            // #2, listingForm, no custom buttons
172
            [
173
                function (): object {
174
                    // setup method
175
                    return new CommonListBuilder([
176
                        'id' => [
177
                            'title' => 'Some id field'
178
                        ]
179
                    ], new FakeAdapter($this->getRecords()));
180
                },
181
                $assert
182
            ],
183
            // #3, listingForm, no custom buttons
184
            [
185
                function (): object {
186
                    // setup method
187
                    return new CommonListBuilder([
188
                        'id' => [
189
                            'title' => 'Some id field'
190
                        ]
191
                    ], new FakeAdapter($this->getRecords()));
192
                },
193
                function (string $result) use ($assert) {
194
                    $assert($result);
195
196
                    $this->assertStringContainsString('Some id field', $result);
197
                    $this->assertStringContainsString('>1<', $result);
198
                    $this->assertStringContainsString('>2<', $result);
199
                }
200
            ],
201
            // #4, listingForm, default buttons
202
            [
203
                function (): object {
204
                    // setup method
205
                    $_GET['update-button'] = 1;
206
                    $_GET['create-button'] = 1;
207
                    return new CommonListBuilder($this->getFields(), new FakeAdapter($this->getRecords()));
208
                },
209
                function (string $result) use ($assert) {
210
                    $assert($result);
211
212
                    $this->assertStringContainsString('>id<', $result);
213
                    $this->assertStringContainsString('>1<', $result);
214
                    $this->assertStringContainsString('>2<', $result);
215
                }
216
            ]
217
        ];
218
    }
219
220
    /**
221
     * Testing method
222
     *
223
     * @param callable $setup
224
     *            setup method
225
     * @param callable $assertions
226
     *            assertions method
227
     * @dataProvider customActionsDataProvider
228
     */
229
    public function testCustomActions(callable $setup, callable $assertions): void
230
    {
231
        // setup
232
        $obj = $setup();
233
234
        // test body
235
        $result = $obj->listingForm();
236
237
        // assertions
238
        $assertions($result);
239
    }
240
}
241