Completed
Push — master ( dadc2f...03811a )
by Alex
03:35
created

ListBuilderUnitTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
c 0
b 0
f 0
dl 0
loc 182
rs 10
wmc 10

8 Methods

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