UserTest::testSetOwner()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 4
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApplicationTest\Model;
6
7
use Application\Model\Product;
8
use Application\Model\User;
9
use PHPUnit\Framework\TestCase;
10
11
class UserTest extends TestCase
12
{
13
    protected function tearDown(): void
14
    {
15
        User::setCurrent(null);
16
    }
17
18
    /**
19
     * @dataProvider providerSetRole
20
     */
21
    public function testSetRole(string $currentRole, string $oldRole, string $newRole, ?string $exception): void
22
    {
23
        User::setCurrent(null);
24
        if ($currentRole !== User::ROLE_ANONYMOUS) {
25
            $currentUser = new User($currentRole);
26
            User::setCurrent($currentUser);
27
        }
28
29
        $user2 = new User($oldRole);
30
31
        if ($exception) {
32
            $this->expectExceptionMessage($exception);
33
        }
34
35
        $user2->setRole($newRole);
36
        self::assertSame($newRole, $user2->getRole());
37
    }
38
39
    public static function providerSetRole(): iterable
40
    {
41
        yield [User::ROLE_ANONYMOUS, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, 'anonymous is not allowed to change role from administrator to member'];
42
        yield [User::ROLE_ANONYMOUS, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'anonymous is not allowed to change role from member to administrator'];
43
        yield [User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_MEMBER, null];
44
        yield [User::ROLE_MEMBER, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, 'member is not allowed to change role from member to administrator'];
45
        yield [User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, User::ROLE_ADMINISTRATOR, null];
46
        yield [User::ROLE_ADMINISTRATOR, User::ROLE_ADMINISTRATOR, User::ROLE_MEMBER, null];
47
    }
48
49
    /**
50
     * @dataProvider providerSetOwner
51
     */
52
    public function testSetOwner(?User $currentUser, ?User $originalOwner, ?User $newOwner, ?string $exception = null): void
53
    {
54
        User::setCurrent($currentUser);
55
56
        $subject = new Product();
57
        self::assertNull($subject->getOwner());
58
59
        $subject->setOwner($originalOwner);
60
        self::assertSame($originalOwner, $subject->getOwner());
61
62
        if ($exception) {
63
            $this->expectExceptionMessage($exception);
64
        }
65
66
        $subject->setOwner($newOwner);
67
        self::assertSame($newOwner, $subject->getOwner());
68
    }
69
70
    public static function providerSetOwner(): iterable
71
    {
72
        $u1 = new User();
73
        $u1->setFirstName('u1');
74
        $u2 = new User();
75
        $u2->setFirstName('u2');
76
        $u3 = new User();
77
        $u3->setFirstName('u3');
78
        $admin = new User(User::ROLE_ADMINISTRATOR);
79
        $admin->setFirstName('admin');
80
        yield 'can change nothing' => [null, null, null];
81
        yield 'can set owner for first time' => [null, null, $u3];
82
        yield 'can set owner for first time to myself' => [$u1, null, $u1];
83
        yield 'can set owner for first time even if it is not myself' => [$u1, null, $u3];
84
        yield 'can donate my stuff' => [$u1, $u1, $u3];
85
        yield 'cannot donate stuff that are not mine' => [$u1, $u2, $u3, 'u1 is not allowed to change owner to u3 because it belongs to u2'];
86
        yield 'admin cannot donate stuff that are not mine' => [$admin, $u2, $u3];
87
    }
88
}
89