Completed
Push — master ( c97ea9...aac971 )
by Alex
03:39
created

SimpleListBuilderUnitTest::getRecords()   A

Complexity

Conditions 1
Paths 1

Size

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