Passed
Push — master ( 892132...fe0f69 )
by Jan
04:10
created

PermissionsEmbedTest::testInvalidBit3()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
7
 *
8
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License
12
 * as published by the Free Software Foundation; either version 2
13
 * of the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
23
 */
24
25
namespace App\Tests\Entity\UserSystem;
26
27
use App\Entity\UserSystem\PermissionsEmbed;
28
use Doctrine\ORM\Mapping\Embedded;
29
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
31
class PermissionsEmbedTest extends TestCase
32
{
33
    public function testGetPermissionValue(): void
34
    {
35
        $embed = new PermissionsEmbed();
36
        //For newly created embedded, all things should be set to inherit => null
37
        //Test both normal name and constants
38
39
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
40
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::CONFIG, 0));
41
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::ATTACHMENT_TYPES, 0));
42
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::CATEGORIES, 0));
43
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DATABASE, 0));
44
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DEVICE_PARTS, 0));
45
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DEVICES, 0));
46
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::FOOTRPINTS, 0));
47
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::GROUPS, 0));
48
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::DATABASE, 0));
49
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::LABELS, 0));
50
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::MANUFACTURERS, 0));
51
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ATTACHMENTS, 0));
52
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_COMMENT, 0));
53
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_DESCRIPTION, 0));
54
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_FOOTPRINT, 0));
55
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_MANUFACTURER, 0));
56
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_MINAMOUNT, 0));
57
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_NAME, 0));
58
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ORDER, 0));
59
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS_ORDERDETAILS, 0));
60
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::USERS, 0));
61
62
        //Set a value for testing to the part property
63
        $reflection = new \ReflectionClass($embed);
64
        $property = $reflection->getProperty('parts');
65
        $property->setAccessible(true);
66
67
        $property->setValue($embed, 0b11011000); // 11 01 10 00
68
69
        //Test if function is working correctly
70
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
71
        $this->assertFalse($embed->getPermissionValue(PermissionsEmbed::PARTS, 2));
72
        $this->assertTrue($embed->getPermissionValue(PermissionsEmbed::PARTS, 4));
73
        // 11 is reserved, but it should be also treat as INHERIT.
74
        $this->assertNull($embed->getPermissionValue(PermissionsEmbed::PARTS, 6));
75
    }
76
77
    public function testGetBitValue(): void
78
    {
79
        $embed = new PermissionsEmbed();
80
81
        //Set a value for testing to the part property
82
        $reflection = new \ReflectionClass($embed);
83
        $property = $reflection->getProperty('parts');
84
        $property->setAccessible(true);
85
86
        $property->setValue($embed, 0b11011000); // 11 01 10 00
87
88
        //Test if function is working correctly
89
        $this->assertSame(PermissionsEmbed::INHERIT, $embed->getBitValue(PermissionsEmbed::PARTS, 0));
90
        $this->assertSame(PermissionsEmbed::DISALLOW, $embed->getBitValue(PermissionsEmbed::PARTS, 2));
91
        $this->assertSame(PermissionsEmbed::ALLOW, $embed->getBitValue(PermissionsEmbed::PARTS, 4));
92
        // 11 is reserved, but it should be also treat as INHERIT.
93
        $this->assertSame(0b11, $embed->getBitValue(PermissionsEmbed::PARTS, 6));
94
    }
95
96
    public function testInvalidPermissionName(): void
97
    {
98
        $embed = new PermissionsEmbed();
99
        //When encoutering an unknown permission name the class must throw an exception
100
        $this->expectException(\InvalidArgumentException::class);
101
        $embed->getPermissionValue('invalid', 0);
102
    }
103
104
    public function testInvalidBit1(): void
105
    {
106
        $embed = new PermissionsEmbed();
107
        //When encoutering an negative bit the class must throw an exception
108
        $this->expectException(\InvalidArgumentException::class);
109
        $embed->getPermissionValue('parts', -1);
110
    }
111
112
    public function testInvalidBit2(): void
113
    {
114
        $embed = new PermissionsEmbed();
115
        //When encoutering an odd bit number it must throw an error.
116
        $this->expectException(\InvalidArgumentException::class);
117
        $embed->getPermissionValue('parts', 1);
118
    }
119
120
    public function testInvalidBit3(): void
121
    {
122
        $embed = new PermissionsEmbed();
123
        //When encoutering an too high bit number it must throw an error.
124
        $this->expectException(\InvalidArgumentException::class);
125
        $embed->getPermissionValue('parts', 32);
126
    }
127
128
    public function getStatesBINARY()
129
    {
130
        return [
131
            'ALLOW' => [PermissionsEmbed::ALLOW],
132
            'DISALLOW' => [PermissionsEmbed::DISALLOW],
133
            'INHERIT' => [PermissionsEmbed::INHERIT],
134
            '0b11' => [0b11],
135
        ];
136
    }
137
138
    public function getStatesBOOL()
139
    {
140
        return [
141
            'ALLOW' => [true],
142
            'DISALLOW' => [false],
143
            'INHERIT' => [null],
144
            '0b11' => [null],
145
        ];
146
    }
147
148
    /**
149
     * @dataProvider getStatesBINARY
150
     */
151
    public function testTestsetBitValue($value): void
152
    {
153
        $embed = new PermissionsEmbed();
154
        //Check if it returns itself, for chaining.
155
        $this->assertSame($embed, $embed->setBitValue(PermissionsEmbed::PARTS, 0, $value));
156
        $this->assertSame($value, $embed->getBitValue(PermissionsEmbed::PARTS, 0));
157
    }
158
159
    /**
160
     * @dataProvider getStatesBOOL
161
     */
162
    public function testSetPermissionValue($value): void
163
    {
164
        $embed = new PermissionsEmbed();
165
        //Check if it returns itself, for chaining.
166
        $this->assertSame($embed, $embed->setPermissionValue(PermissionsEmbed::PARTS, 0, $value));
167
        $this->assertSame($value, $embed->getPermissionValue(PermissionsEmbed::PARTS, 0));
168
    }
169
170
    public function testSetRawPermissionValue(): void
171
    {
172
        $embed = new PermissionsEmbed();
173
        $embed->setRawPermissionValue(PermissionsEmbed::PARTS, 10);
174
        $this->assertSame(10, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
175
    }
176
177
    public function testSetRawPermissionValues(): void
178
    {
179
        $embed = new PermissionsEmbed();
180
        $embed->setRawPermissionValues([
181
            PermissionsEmbed::PARTS => 0,
182
            PermissionsEmbed::USERS => 100,
183
            PermissionsEmbed::CATEGORIES => 1304,
184
        ]);
185
186
        $this->assertSame(0, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
187
        $this->assertSame(100, $embed->getRawPermissionValue(PermissionsEmbed::USERS));
188
        $this->assertSame(1304, $embed->getRawPermissionValue(PermissionsEmbed::CATEGORIES));
189
190
        //Test second method to pass perm names and values
191
        $embed->setRawPermissionValues(
192
            [PermissionsEmbed::PARTS, PermissionsEmbed::USERS, PermissionsEmbed::CATEGORIES],
193
            [0, 100, 1304]
194
        );
195
196
        $this->assertSame(0, $embed->getRawPermissionValue(PermissionsEmbed::PARTS));
197
        $this->assertSame(100, $embed->getRawPermissionValue(PermissionsEmbed::USERS));
198
        $this->assertSame(1304, $embed->getRawPermissionValue(PermissionsEmbed::CATEGORIES));
199
    }
200
}
201