ContentListTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 127
c 1
b 0
f 0
dl 0
loc 227
rs 10
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A updateProvider() 0 50 1
A setUp() 0 22 1
A testUpdate() 0 40 3
A testCreate() 0 22 3
A createProvider() 0 36 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Form\Factory;
6
7
use AbterPhp\Framework\I18n\ITranslator;
8
use AbterPhp\Website\Domain\Entities\ContentList as Entity;
9
use AbterPhp\Website\Domain\Entities\ContentListItem as Item;
10
use AbterPhp\Website\Form\Factory\ContentList\Item as ItemFactory;
11
use AbterPhp\Website\Orm\ContentListItemRepo as ItemRepo;
12
use Casbin\Enforcer;
13
use Opulence\Http\Requests\RequestMethods;
14
use Opulence\Sessions\ISession;
15
use Opulence\Sessions\Session;
16
use PHPUnit\Framework\MockObject\MockObject;
17
use PHPUnit\Framework\TestCase;
18
19
class ContentListTest extends TestCase
20
{
21
    /** @var ContentList - System Under Test */
22
    protected $sut;
23
24
    /** @var ISession|MockObject */
25
    protected $sessionMock;
26
27
    /** @var ITranslator|MockObject */
28
    protected $translatorMock;
29
30
    /** @var ItemRepo|MockObject */
31
    protected $itemRepoMock;
32
33
    /** @var ItemFactory */
34
    protected $itemFactoryMock;
35
36
    /** @var Enforcer|MockObject */
37
    protected $enforcerMock;
38
39
    public function setUp(): void
40
    {
41
        parent::setUp();
42
43
        $this->sessionMock = $this->createMock(Session::class);
44
        $this->sessionMock->expects($this->any())->method('get')->willReturnArgument(0);
45
46
        $this->translatorMock = $this->createMock(ITranslator::class);
47
        $this->translatorMock->expects($this->any())->method('translate')->willReturnArgument(0);
48
49
        $this->itemRepoMock = $this->createMock(ItemRepo::class);
50
51
        $this->itemFactoryMock = new ItemFactory();
52
53
        $this->enforcerMock = $this->createMock(Enforcer::class);
54
55
        $this->sut = new ContentList(
56
            $this->sessionMock,
57
            $this->translatorMock,
58
            $this->itemRepoMock,
59
            $this->itemFactoryMock,
60
            $this->enforcerMock
61
        );
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    public function createProvider(): array
68
    {
69
        return [
70
            'advanced allowed'     => [
71
                true,
72
                [],
73
                [
74
                    'POST',
75
                    'CSRF',
76
                    '="name"',
77
                    'identifier',
78
                    'classes',
79
                    'with_links',
80
                    'with_label_links',
81
                    'with_html',
82
                    'with_images',
83
                    'with_classes',
84
                ],
85
                [],
86
            ],
87
            'advanced not allowed' => [
88
                false,
89
                [],
90
                [
91
                    'POST',
92
                    'CSRF',
93
                    '="name"',
94
                    'identifier',
95
                    'classes',
96
                    'with_links',
97
                    'with_label_links',
98
                    'with_html',
99
                    'with_images',
100
                    'with_classes',
101
                ],
102
                [],
103
            ],
104
        ];
105
    }
106
107
    /**
108
     * @dataProvider createProvider
109
     *
110
     * @param bool     $advancedAllowed
111
     * @param Item[]   $items
112
     * @param string[] $contains
113
     * @param string[] $missing
114
     */
115
    public function testCreate(bool $advancedAllowed, array $items, array $contains, array $missing)
116
    {
117
        $action   = 'foo';
118
        $method   = RequestMethods::POST;
119
        $showUrl  = 'bar';
120
        $entityId = '4571f468-8d7a-4680-81b5-fb747abaf580';
0 ignored issues
show
Unused Code introduced by
The assignment to $entityId is dead and can be removed.
Loading history...
121
        $name     = 'Blah';
0 ignored issues
show
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
122
123
        $this->enforcerMock->expects($this->any())->method('enforce')->willReturn($advancedAllowed);
124
        $this->itemRepoMock->expects($this->any())->method('getByListId')->willReturn([]);
125
126
        $entity = new Entity('', '', '', '', false, false, false, false, false, false);
127
128
        $form = (string)$this->sut->create($action, $method, $showUrl, $entity);
129
130
        $this->assertStringContainsString($action, $form);
131
        $this->assertStringContainsString($showUrl, $form);
132
        foreach ($contains as $needle) {
133
            $this->assertStringContainsString($needle, $form);
134
        }
135
        foreach ($missing as $needle) {
136
            $this->assertStringNotContainsString($needle, $form);
137
        }
138
    }
139
140
    /**
141
     * @return array
142
     */
143
    public function updateProvider(): array
144
    {
145
        $itemId0 = 'cc1288f3-4873-4438-be60-7d7c1a26ddc6';
146
        $itemId1 = '3ce10679-3e96-4f11-9058-54bb429fbdbd';
147
        $listId0 = '4571f468-8d7a-4680-81b5-fb747abaf580';
148
149
        $containsAll = [
150
            'POST',
151
            'CSRF',
152
            '="name"',
153
            'identifier',
154
            'classes',
155
            'with_links',
156
            'with_label_links',
157
            'with_html',
158
            'with_images',
159
            'with_classes',
160
        ];
161
162
        return [
163
            'unprotected'     => [
164
                false,
165
                false,
166
                [],
167
                $containsAll,
168
                [],
169
            ],
170
            'protected'       => [
171
                false,
172
                true,
173
                [],
174
                ['POST', 'CSRF'],
175
                ['identifier', 'classes', 'with_links', 'with_label_links', 'with_html', 'with_images', 'with_classes'],
176
            ],
177
            'admin protected' => [
178
                true,
179
                true,
180
                [],
181
                $containsAll,
182
                [],
183
            ],
184
            'with items'      => [
185
                true,
186
                true,
187
                [
188
                    new Item($itemId0, $listId0, '', '', '', '', '', '', '', ''),
189
                    new Item($itemId1, $listId0, '', '', '', '', '', '', '', ''),
190
                ],
191
                array_merge($containsAll, [$itemId0, $itemId1]),
192
                [],
193
            ],
194
        ];
195
    }
196
197
    /**
198
     * @dataProvider updateProvider
199
     *
200
     * @param bool     $advancedAllowed
201
     * @param bool     $protected
202
     * @param Item[]   $items
203
     * @param string[] $contains
204
     * @param string[] $missing
205
     */
206
    public function testUpdate(bool $advancedAllowed, bool $protected, array $items, array $contains, array $missing)
207
    {
208
        $action        = 'foo';
209
        $method        = RequestMethods::POST;
210
        $showUrl       = 'bar';
211
        $entityId      = '4571f468-8d7a-4680-81b5-fb747abaf580';
212
        $name          = 'Blah';
213
        $identifier    = 'blah';
214
        $classes       = 'blah1 blah2';
215
        $withLinks     = true;
216
        $withNameLinks = true;
217
        $withHtml      = true;
218
        $withImages    = true;
219
        $withClasses   = true;
220
221
        $this->enforcerMock->expects($this->any())->method('enforce')->willReturn($advancedAllowed);
222
        $this->itemRepoMock->expects($this->any())->method('getByListId')->willReturn($items);
223
224
        $entity = new Entity(
225
            $entityId,
226
            $name,
227
            $identifier,
228
            $classes,
229
            $protected,
230
            $withLinks,
231
            $withNameLinks,
232
            $withHtml,
233
            $withImages,
234
            $withClasses
235
        );
236
237
        $form = (string)$this->sut->create($action, $method, $showUrl, $entity);
238
239
        $this->assertStringContainsString($action, $form);
240
        $this->assertStringContainsString($showUrl, $form);
241
        foreach ($contains as $needle) {
242
            $this->assertStringContainsString($needle, $form);
243
        }
244
        foreach ($missing as $needle) {
245
            $this->assertStringNotContainsString($needle, $form);
246
        }
247
    }
248
}
249