Completed
Push — master ( 8d0f52...c97ea9 )
by Alex
02:04
created

commonBehaviourDataProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 29
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 48
rs 9.456
1
<?php
2
namespace Mezon\Gui\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Gui\ListBuilder\Simple as SimpleListBuilder;
6
use Mezon\Router\Router;
7
use Mezon\Transport\Request;
8
9
class SimpleListBuilderUnitTest extends TestCase
10
{
11
12
    /**
13
     * This method is called before each test.
14
     */
15
    protected function setUp(): void
16
    {
17
        $router = new Router();
18
19
        Request::registerRouter($router);
20
    }
21
22
    /**
23
     * Method returns list of fields
24
     *
25
     * @return array Fields algorithms object
26
     */
27
    protected function getFields(): array
28
    {
29
        return [
30
            'id',
31
            'domain_id',
32
            'title'
33
        ];
34
    }
35
36
    /**
37
     * Method runs string assertions
38
     *
39
     * @param array $asserts
40
     *            asserts
41
     * @param string $content
42
     *            content to assert
43
     */
44
    protected function runAssertions(array $asserts, string $content): void
45
    {
46
        foreach ($asserts as $assert) {
47
            $this->assertStringContainsString($assert, $content);
48
        }
49
    }
50
51
    /**
52
     * Testing constructor
53
     */
54
    public function testConstructorValid(): void
55
    {
56
        // setup and test body
57
        $listBuilder = new SimpleListBuilder($this->getFields(), new FakeAdapter());
58
59
        // assertions
60
        $this->assertIsArray($listBuilder->getFields(), 'Invalid fields list type');
61
    }
62
63
    /**
64
     * Method returns testing records
65
     *
66
     * @return array testing records
67
     */
68
    private function getRecords(): array
69
    {
70
        return [
71
            [
72
                'id' => 1,
73
            ],
74
            [
75
                'id' => 2,
76
            ]
77
        ];
78
    }
79
80
    /**
81
     * Data provider for the testSimpleListingForm
82
     *
83
     * @return array test data
84
     */
85
    public function simpleListingFormDataProvider(): array
86
    {
87
        return [
88
            // #0, no records
89
            [
90
                [],
91
                [
92
                    'class="no-items-title"',
93
                    '{action-message}'
94
                ]
95
            ],
96
            // #1, no records
97
            [
98
                $this->getRecords(),
99
                [
100
                    '>1<',
101
                    '>2<',
102
                    '{action-message}'
103
                ]
104
            ]
105
        ];
106
    }
107
108
    /**
109
     * Testing listing form
110
     *
111
     * @param array $records
112
     *            records to display
113
     * @param array $asserts
114
     *            asserts
115
     * @dataProvider simpleListingFormDataProvider
116
     */
117
    public function testSimpleListingForm(array $records, array $asserts): void
118
    {
119
        // setup
120
        $listBuilder = new SimpleListBuilder($this->getFields(), new FakeAdapter($records));
121
122
        // test body
123
        $content = $listBuilder->listingForm();
124
125
        // assertions
126
        $this->runAssertions($asserts, $content);
127
    }
128
129
    /**
130
     * Asserting that string contains substrings
131
     *
132
     * @param array $needles
133
     * @param string $haystack
134
     */
135
    private function assertStringContainsStrings(array $needles, string $haystack): void
136
    {
137
        foreach ($needles as $needle) {
138
            $this->assertStringContainsString($needle, $haystack);
139
        }
140
    }
141
142
    /**
143
     * Testing data provider
144
     *
145
     * @return array testing data
146
     */
147
    public function commonBehaviourDataProvider(): array
148
    {
149
        $assert = function ($result): void {
150
            // asserting method
151
            $this->assertStringNotContainsString('!1!', $result);
152
            $this->assertStringNotContainsString('!2!', $result);
153
        };
154
155
        return [
156
            // #0, listingForm, custom title and description
157
            [
158
                function (): object {
159
                    // setup method
160
                    $listBuilder = new SimpleListBuilder($this->getFields(), new FakeAdapter($this->getRecords()));
161
                    $listBuilder->listTitle = 'List Title';
162
                    $listBuilder->listDescription = 'List Description';
163
                    return $listBuilder;
164
                },
165
                function (string $result) use ($assert) {
166
                    $assert($result);
167
168
                    $this->assertStringContainsStrings([
169
                        '>id<',
170
                        '>1<',
171
                        '>2<',
172
                        'List Title',
173
                        'List Description'
174
                    ], $result);
175
                }
176
            ],
177
            // #1, listingForm, default title and description
178
            [
179
                function (): object {
180
                    // setup method
181
                    return new SimpleListBuilder($this->getFields(), new FakeAdapter($this->getRecords()));
182
                },
183
                function (string $result) use ($assert) {
184
                    $assert($result);
185
186
                    $this->assertStringContainsStrings(
187
                        [
188
                            '>id<',
189
                            '>1<',
190
                            '>2<',
191
                            'Список записей',
192
                            'Выберите необходимое действие'
193
                        ],
194
                        $result);
195
                }
196
            ]
197
        ];
198
    }
199
200
    /**
201
     * Testing method
202
     *
203
     * @param callable $setup
204
     *            setup method
205
     * @param callable $assertions
206
     *            assertions method
207
     * @dataProvider commonBehaviourDataProvider
208
     */
209
    public function testCommonBehaviour(callable $setup, callable $assertions): void
210
    {
211
        // setup
212
        $obj = $setup();
213
214
        // test body
215
        $result = $obj->listingForm();
216
217
        // assertions
218
        $assertions($result);
219
    }
220
}
221