Passed
Push — master ( 96df15...612a2f )
by Alex
01:53
created

ListBuilderUnitTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 174
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A simpleListingFormDataProvider() 0 22 1
A testListingForm() 0 12 2
A getFields() 0 6 1
A testConstructorValid() 0 7 1
A testSimpleListingForm() 0 12 1
A runAssertions() 0 4 2
A listingFormDataProvider() 0 42 1
1
<?php
2
namespace Mezon\Gui\Tests;
3
4
use PHPUnit\Framework\TestCase;
5
use Mezon\Gui\ListBuilder\ListBuilder;
6
7
class ListBuilderUnitTest extends TestCase
8
{
9
10
    /**
11
     * Method returns list of fields
12
     *
13
     * @return array Fields algorithms object
14
     */
15
    protected function getFields(): array
16
    {
17
        return [
18
            'id',
19
            'domain_id',
20
            'title'
21
        ];
22
    }
23
24
    /**
25
     * Method runs string assertions
26
     *
27
     * @param array $asserts
28
     *            asserts
29
     * @param string $content
30
     *            content to assert
31
     */
32
    protected function runAssertions(array $asserts, string $content): void
33
    {
34
        foreach ($asserts as $assert) {
35
            $this->assertStringContainsString($assert, $content);
36
        }
37
    }
38
39
    /**
40
     * Testing constructor
41
     */
42
    public function testConstructorValid(): void
43
    {
44
        // setup and test body
45
        $listBuilder = new ListBuilder($this->getFields(), new FakeAdapter());
46
47
        // assertions
48
        $this->assertIsArray($listBuilder->getFields(), 'Invalid fields list type');
49
    }
50
51
    /**
52
     * Data provider for the testListingForm
53
     *
54
     * @return array test data
55
     */
56
    public function listingFormDataProvider(): array
57
    {
58
        return [
59
            [
60
                0,
61
                [
62
                    [
63
                        'id' => 1,
64
                    ],
65
                    [
66
                        'id' => 2,
67
                    ]
68
                ],
69
                [
70
                    '>id<',
71
                    '>1<',
72
                    '>2<'
73
                ]
74
            ],
75
            [
76
                1,
77
                [
78
                    [
79
                        'id' => 1,
80
                    ],
81
                    [
82
                        'id' => 2,
83
                    ]
84
                ],
85
                [
86
                    '>id<',
87
                    '>1<',
88
                    '>2<',
89
                    '/create-endpoint/'
90
                ]
91
            ],
92
            [
93
                0,
94
                [],
95
                [
96
                    'class="no-items-title"',
97
                    '../create/'
98
                ]
99
            ]
100
        ];
101
    }
102
103
    /**
104
     * Testing listing form
105
     *
106
     * @param int $createButton
107
     *            do we need to show create button
108
     * @param array $records
109
     *            list of records to be displayed
110
     * @param array $asserts
111
     *            asserts
112
     * @dataProvider listingFormDataProvider
113
     */
114
    public function testListingForm(int $createButton, array $records, array $asserts): void
115
    {
116
        // setup
117
        $_GET['create-page-endpoint'] = $createButton ? '/create-endpoint/' : null;
118
        $_GET['create-button'] = $createButton;
119
        $listBuilder = new ListBuilder($this->getFields(), new FakeAdapter($records));
120
121
        // test body
122
        $content = $listBuilder->listingForm();
123
124
        // assertions
125
        $this->runAssertions($asserts, $content);
126
    }
127
128
    /**
129
     * Data provider for the testSimpleListingForm
130
     *
131
     * @return array test data
132
     */
133
    public function simpleListingFormDataProvider(): array
134
    {
135
        return [
136
            [
137
                [],
138
                [
139
                    'class="no-items-title"'
140
                ]
141
            ],
142
            [
143
                [
144
                    [
145
                        'id' => 1,
146
                    ],
147
                    [
148
                        'id' => 2,
149
                    ]
150
                ],
151
                [
152
                    '>id<',
153
                    '>1<',
154
                    '>2<'
155
                ]
156
            ]
157
        ];
158
    }
159
160
    /**
161
     * Testing listing form
162
     *
163
     * @param array $records
164
     *            records to display
165
     * @param array $asserts
166
     *            asserts
167
     * @dataProvider simpleListingFormDataProvider
168
     */
169
    public function testSimpleListingForm(array $records, array $asserts): void
170
    {
171
        // setup
172
        $_GET['update-button'] = 1;
173
        $_GET['create-button'] = 1;
174
        $listBuilder = new ListBuilder($this->getFields(), new FakeAdapter($records));
175
176
        // test body
177
        $content = $listBuilder->simpleListingForm();
178
179
        // assertions
180
        $this->runAssertions($asserts, $content);
181
    }
182
}
183