ListBuilderUnitTest::commonBehaviourDataProvider()   A
last analyzed

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