Passed
Push — master ( 4d86c4...586e6b )
by Alex
02:58
created

ListBuilderUnitTest::testConstructorValid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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