Failed Conditions
Push — master ( ab912b...01f77f )
by Adrien
10:12
created

UserTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Model;
6
7
use Application\Model\Account;
8
use Application\Model\Booking;
9
use Application\Model\User;
10
use PHPUnit\Framework\TestCase;
11
12
class UserTest extends TestCase
13
{
14
    protected function tearDown(): void
15
    {
16
        User::setCurrent(null);
17
    }
18
19
    /**
20
     * @dataProvider providerSetRole
21
     */
22
    public function testSetRole(string $currentRole, string $oldRole, string $newRole, ?string $exception): void
23
    {
24
        User::setCurrent(null);
25
        if ($currentRole !== User::ROLE_ANONYMOUS) {
26
            $currentUser = new User($currentRole);
27
            User::setCurrent($currentUser);
28
        }
29
30
        $user2 = new User($oldRole);
31
32
        if ($exception) {
33
            $this->expectExceptionMessage($exception);
34
        }
35
36
        $user2->setRole($newRole);
37
        self::assertSame($newRole, $user2->getRole());
38
    }
39
40
    public function providerSetRole(): array
41
    {
42
        return [
43
            [User::ROLE_ANONYMOUS, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, 'anonymous is not allowed to change role from administrator to member'],
44
            [User::ROLE_ANONYMOUS, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'anonymous is not allowed to change role from member to administrator'],
45
46
            [User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_MEMBER, null],
47
            [User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'member is not allowed to change role from member to administrator'],
48
49
            [User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, null],
50
            [User::ROLE_ADMINISTRATOR, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, null],
51
        ];
52
    }
53
54
    /**
55
     * @dataProvider providerSetOwner
56
     */
57
    public function testSetOwner(?User $currentUser, ?User $originalOwner, ?User $newOwner, ?string $exception = null): void
58
    {
59
        User::setCurrent($currentUser);
60
61
        $subject = new Booking();
62
        self::assertNull($subject->getOwner());
63
64
        $subject->setOwner($originalOwner);
65
        self::assertSame($originalOwner, $subject->getOwner());
66
67
        if ($exception) {
68
            $this->expectExceptionMessage($exception);
69
        }
70
71
        $subject->setOwner($newOwner);
72
        self::assertSame($newOwner, $subject->getOwner());
73
    }
74
75
    public function providerSetOwner(): array
76
    {
77
        $u1 = new User();
78
        $u1->setLogin('u1');
79
        $u2 = new User();
80
        $u2->setLogin('u2');
81
        $u3 = new User();
82
        $u3->setLogin('u3');
83
        $admin = new User(User::ROLE_ADMINISTRATOR);
84
        $admin->setLogin('admin');
85
86
        return [
87
            'can change nothing' => [null, null, null],
88
            'can set owner for first time' => [null, null, $u3],
89
            'can set owner for first time to myself' => [$u1, null, $u1],
90
            'can set owner for first time even if it is not myself' => [$u1, null, $u3],
91
            'can donate my stuff' => [$u1, $u1, $u3],
92
            'cannot donate stuff that are not mine' => [$u1, $u2, $u3, 'u1 is not allowed to change owner to u3 because it belongs to u2'],
93
            'admin cannot donate stuff that are not mine' => [$admin, $u2, $u3],
94
        ];
95
    }
96
97
    public function providerCanOpenDoor(): array
98
    {
99
        return [
100
            'anonymous cannot open' => [
101
                User::ROLE_ANONYMOUS,
102
                User::STATUS_ACTIVE,
103
                ['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true],
104
                ['door1' => false, 'door2' => false, 'door3' => false, 'door4' => false],
105
            ],
106
            'individual member can open' => [
107
                User::ROLE_INDIVIDUAL,
108
                User::STATUS_ACTIVE,
109
                ['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false],
110
                ['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false],
111
            ],
112
            'active member can open' => [
113
                User::ROLE_MEMBER,
114
                User::STATUS_ACTIVE,
115
                ['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false],
116
                ['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false],
117
            ],
118
            'inactive member cannot open' => [
119
                User::ROLE_MEMBER,
120
                User::STATUS_INACTIVE,
121
                ['door1' => true, 'door2' => true, 'door3' => true, 'door4' => false],
122
                ['door1' => false, 'door2' => false, 'door3' => false, 'door4' => false],
123
            ],
124
            'responsible can open' => [
125
                User::ROLE_RESPONSIBLE,
126
                User::STATUS_ACTIVE,
127
                ['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true],
128
                ['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true],
129
            ],
130
            'administrator can open' => [
131
                User::ROLE_ADMINISTRATOR,
132
                User::STATUS_ACTIVE,
133
                ['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true],
134
                ['door1' => true, 'door2' => true, 'door3' => true, 'door4' => true],
135
            ],
136
        ];
137
    }
138
139
    /**
140
     * @dataProvider providerCanOpenDoor,
141
     */
142
    public function testCanOpenDoor(string $role, string $status, array $doors, array $result): void
143
    {
144
        $user = new User($role);
145
        $user->setStatus($status);
146
        foreach ($doors as $door => $value) {
147
            $setter = 'set' . ucfirst($door);
148
            $user->$setter($value);
149
        }
150
151
        foreach ($result as $door => $canOpen) {
152
            self::assertSame($canOpen, $user->getCanOpenDoor($door));
153
        }
154
    }
155
156
    public function testGetAccount(): void
157
    {
158
        $user1 = new User();
159
        $user2 = new User();
160
161
        self::assertNull($user1->getAccount());
162
        self::assertNull($user2->getAccount());
163
164
        $account1 = new Account();
165
        $account2 = new Account();
166
        $account1->setOwner($user1);
167
        $account2->setOwner($user2);
168
169
        self::assertSame($account1, $user1->getAccount());
170
        self::assertSame($account2, $user2->getAccount());
171
172
        $user2->setOwner($user1);
173
174
        self::assertSame($account1, $user1->getAccount());
175
        self::assertSame($account1, $user2->getAccount(), 'user2 should now use user1 account');
176
177
        User::setCurrent($user1);
178
        $user2->setOwner($user2);
179
180
        self::assertSame($account1, $user1->getAccount());
181
        self::assertSame($account2, $user2->getAccount(), 'user2 should be use his own account again');
182
183
        User::setCurrent($user2);
184
        $user2->setOwner(null);
185
186
        self::assertSame($account1, $user1->getAccount());
187
        self::assertSame($account2, $user2->getAccount(), 'user2 should be use his own account again');
188
    }
189
}
190