Passed
Push — master ( 7263be...72ea90 )
by Alex
02:23
created

ListBuilderUnitTest::customActionsDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 21
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 44
rs 9.584
1
<?php
2
namespace Mezon\Gui\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Gui\ListBuilder\ListBuilder;
6
7
class ListBuilderUnitTest 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 ListBuilder($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 ListBuilder($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
     * Data provider for the testSimpleListingForm
133
     *
134
     * @return array test data
135
     */
136
    public function simpleListingFormDataProvider(): array
137
    {
138
        return [
139
            [
140
                [],
141
                [
142
                    'class="no-items-title"'
143
                ]
144
            ],
145
            [
146
                $this->getRecords(),
147
                [
148
                    '>id<',
149
                    '>1<',
150
                    '>2<'
151
                ]
152
            ]
153
        ];
154
    }
155
156
    /**
157
     * Testing listing form
158
     *
159
     * @param array $records
160
     *            records to display
161
     * @param array $asserts
162
     *            asserts
163
     * @dataProvider simpleListingFormDataProvider
164
     */
165
    public function testSimpleListingForm(array $records, array $asserts): void
166
    {
167
        // setup
168
        $_GET['update-button'] = 1;
169
        $_GET['create-button'] = 1;
170
        $listBuilder = new ListBuilder($this->getFields(), new FakeAdapter($records));
171
172
        // test body
173
        $content = $listBuilder->simpleListingForm();
174
175
        // assertions
176
        $this->runAssertions($asserts, $content);
177
    }
178
179
    /**
180
     * Testing data provider
181
     *
182
     * @return array testing data
183
     */
184
    public function customActionsDataProvider(): array
185
    {
186
        $setup = function (): object {
187
            // setup method
188
            $listBuilder = new ListBuilder($this->getFields(), new FakeAdapter($this->getRecords()));
189
190
            $listBuilder->setCustomActions('!{id}!');
191
192
            return $listBuilder;
193
        };
194
195
        $assert = function ($result): void {
196
            // asserting method
197
            $this->assertStringNotContainsString('!1!', $result);
198
            $this->assertStringNotContainsString('!2!', $result);
199
        };
200
201
        return [
202
            // #0, simpleListingForm
203
            [
204
                $setup,
205
                $assert,
206
                'simpleListingForm'
207
            ],
208
            // #1, listingForm
209
            [
210
                $setup,
211
                function ($result): void {
212
                    // asserting method
213
                    $this->assertStringContainsString('!1!', $result);
214
                    $this->assertStringContainsString('!2!', $result);
215
                },
216
                'listingForm'
217
            ],
218
            // #2, listingForm, no custom buttons
219
            [
220
                function (): object {
221
                    // setup method
222
                    $listBuilder = new ListBuilder($this->getFields(), new FakeAdapter($this->getRecords()));
223
224
                    return $listBuilder;
225
                },
226
                $assert,
227
                'listingForm'
228
            ]
229
        ];
230
    }
231
232
    /**
233
     * Testing method
234
     *
235
     * @param callable $setup
236
     *            setup method
237
     * @param callable $assertions
238
     *            assertions method
239
     * @paran string $method method to be called
240
     * @dataProvider customActionsDataProvider
241
     */
242
    public function testCustomActions(callable $setup, callable $assertions, string $method): void
243
    {
244
        // setup
245
        $obj = $setup();
246
247
        // test body
248
        $result = $obj->$method();
249
250
        // assertions
251
        $assertions($result);
252
    }
253
}
254