GroupUserDatabaseTest.php 3.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php


namespace Tests\Database;

use Source\Entities\User;
use Source\Entities\Group;
use Tests\DatabaseTestCase;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\Users\DatabaseUsersRepository;
use Source\Gateways\Groups\DatabaseGroupsRepository;
use Source\Gateways\GroupUser\DatabaseGroupUserRepository;

class GroupUserDatabaseTest extends DatabaseTestCase
{
    /**
     * @var \Source\Gateways\Users\DatabaseUsersRepository
     */
    protected DatabaseUsersRepository $users;

    /**
     * @var \Source\Gateways\Groups\DatabaseGroupsRepository
     */
    protected DatabaseGroupsRepository $groups;

    /**
     * @var \Source\Gateways\GroupUser\DatabaseGroupUserRepository
     */
    protected DatabaseGroupUserRepository $groupUsers;

31
32
33
34
35
36
37
38
39
40
    /**
     * @var \Source\Entities\User|null
     */
    protected User $user;

    /**
     * @var \Source\Entities\Group
     */
    protected Group $group;

41
42
43
44
45
46
47
    public function setUp(): void
    {
        parent::setUp();

        $this->users = new DatabaseUsersRepository();
        $this->groups = new DatabaseGroupsRepository();
        $this->groupUsers = new DatabaseGroupUserRepository();
48
49
        $this->user = $this->users->create(new User(0, '', '', '', '', null, null, null));
        $this->group = $this->groups->create(new Group(0, '', ''));
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    }

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_cannot_get_groups_for_non_existent_user(): void
    {
        $this->expectException(EntityNotFoundException::class);

        $this->groupUsers->getGroupsForUser('ree');
    }

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_cannot_get_users_for_non_existent_group(): void
    {
        $this->expectException(EntityNotFoundException::class);

        $this->groupUsers->getUsersForGroup('ree');
    }

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_cannot_add_user_to_non_existent_group(): void
    {
        $this->expectException(EntityNotFoundException::class);

82
        $this->groupUsers->addUserToGroup($this->user->getId(), 'ree');
83
84
85
86
87
88
89
90
91
92
    }

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_cannot_add_group_to_non_existent_user(): void
    {
        $this->expectException(EntityNotFoundException::class);

93
        $this->groupUsers->addUserToGroup('re', $this->group->getId());
94
95
96
97
98
99
100
101
102
103
104
    }


    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_cannot_remove_user_to_non_existent_group(): void
    {
        $this->expectException(EntityNotFoundException::class);

105
        $this->groupUsers->removeUserFromGroup($this->user->getId(), 'ree');
106
107
108
109
110
111
112
113
114
115
    }

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_cannot_remove_group_to_non_existent_user(): void
    {
        $this->expectException(EntityNotFoundException::class);

116
        $this->groupUsers->removeUserFromGroup('re', $this->group->getId());
117
118
119
120
121
122
123
124
    }

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_can_add_remove_and_get_groups_for_users(): void
    {
125
        $this->groupUsers->addUserToGroup($this->user->getId(), $this->group->getId());
126

127
        $this->assertCount(1, $this->groupUsers->getGroupsForUser($this->user->getId()));
128

129
        $this->groupUsers->removeUserFromGroup($this->user->getId(), $this->group->getId());
130

131
        $this->assertCount(0, $this->groupUsers->getGroupsForUser($this->user->getId()));
132
133
134
135
136
137
138
139
    }

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_can_get_users_for_a_group(): void
    {
140
        $this->groupUsers->addUserToGroup($this->user->getId(), $this->group->getId());
141

142
        $this->assertCount(1, $this->groupUsers->getUsersForGroup($this->group->getId()));
143
144
    }
}