UseCaseTest.php 5.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
<?php


namespace Tests\Unit\Source\UseCases\Door\Access;

use Source\Entities\User;
use PHPUnit\Framework\TestCase;
use Source\Entities\HashedSearchable;
use Source\UseCases\Door\Access\Access;
use Source\Exceptions\AuthorizationException;
use Source\Exceptions\AuthenticationException;
use Source\Gateways\Users\InMemoryUsersRepository;
13
use Source\Gateways\Entries\InMemoryEntriesRepository;
14
use Source\Gateways\DoorUser\InMemoryDoorUserRepository;
15
use Source\Gateways\Attempts\InMemoryAttemptsRepository;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

class UseCaseTest extends TestCase
{
    protected const EXPECTED_ACCESS_DOOR_ID = 123;
    protected const SALT = '';
    protected const USER_ID = 1;

    /**
     * @var \Source\Gateways\Users\InMemoryUsersRepository
     */
    protected InMemoryUsersRepository $users;

    /**
     * @var \Source\Gateways\DoorUser\InMemoryDoorUserRepository
     */
    protected InMemoryDoorUserRepository $doorUser;

    /**
     * @var \Source\UseCases\Door\Access\Access
     */
    protected Access $useCase;

38
39
40
41
42
43
44
45
46
47
    /**
     * @var \Source\Gateways\Attempts\InMemoryAttemptsRepository
     */
    protected InMemoryAttemptsRepository $attempts;

    /**
     * @var \Source\Gateways\Entries\InMemoryEntriesRepository
     */
    protected InMemoryEntriesRepository $entries;

48
49
50
51
52
53
    public function setUp(): void
    {
        parent::setUp();

        $this->users = new InMemoryUsersRepository();
        $this->doorUser = new InMemoryDoorUserRepository();
54
55
        $this->attempts = new InMemoryAttemptsRepository();
        $this->entries = new InMemoryEntriesRepository();
56
57
58
        $this->useCase = new Access(
            self::EXPECTED_ACCESS_DOOR_ID,
            $this->users,
59
60
61
            $this->doorUser,
            $this->attempts,
            $this->entries
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
        );
    }

    protected static function createValidUser(string $doorcode): User
    {
        return new User(
            self::USER_ID,
            'first',
            'last',
            'display',
            'email',
            null,
            null,
            HashedSearchable::hash(self::SALT, $doorcode)
        );
    }

    /**
     * @param string|null $doorcode
     * @throws \Source\Exceptions\AuthenticationException
     * @throws \Source\Exceptions\AuthorizationException
     */
    protected function handleTest(?string $doorcode): void
    {
        $this->useCase->protectUserDoorAccess(self::SALT, $doorcode);
    }

    /**
     * @test
     * @throws \Source\Exceptions\AuthenticationException
     * @throws \Source\Exceptions\AuthorizationException
     */
    public function it_will_not_check_for_non_existent_doors(): void
    {
        $this->expectException(AuthenticationException::class);
97
98
99
100
101
102
103
        $this->useCase = new Access(
            null,
            $this->users,
            $this->doorUser,
            $this->attempts,
            $this->entries
        );
104
105
106
107
108
109
110
111
112
113
114
115
116
        $this->handleTest('');
    }

    /**
     * @test
     * @throws \Source\Exceptions\AuthenticationException
     * @throws \Source\Exceptions\AuthorizationException
     */
    public function it_wont_unlock_on_bad_doorcode(): void
    {
        $this->expectException(AuthorizationException::class);
        $this->users->create(self::createValidUser('doorcode'));
        $this->handleTest('asdf');
117
118
119

        $this->assertCount(1, $this->attempts->all());
        $this->assertEquals(self::EXPECTED_ACCESS_DOOR_ID, $this->attempts->all()[0]->getDoorId());
120
121
122
123
124
125
126
127
128
129
130
131
    }

    /**
     * @test
     * @throws \Source\Exceptions\AuthenticationException
     * @throws \Source\Exceptions\AuthorizationException
     */
    public function it_wont_unlock_if_user_is_not_in_group(): void
    {
        $this->expectException(AuthorizationException::class);
        $this->users->create(self::createValidUser('doorcode'));
        $this->handleTest('doorcode');
132
133
134

        $this->assertCount(1, $this->entries->all());
        $this->assertFalse($this->entries->all()[0]->wasSuccessful());
135
136
137
138
139
140
141
142
143
144
145
146
147
148
    }

    /**
     * @test
     * @throws \Source\Exceptions\AuthenticationException
     * @throws \Source\Exceptions\AuthorizationException
     */
    public function it_wont_unlock_if_user_and_door_in_different_groups(): void
    {
        $this->expectException(AuthorizationException::class);
        $this->users->create(self::createValidUser('doorcode'));
        $this->doorUser->addGroupToDoor(self::EXPECTED_ACCESS_DOOR_ID, 5);
        $this->doorUser->addGroupToUser(self::USER_ID, 9);
        $this->handleTest('doorcode');
149
150
151

        $this->assertCount(1, $this->entries->all());
        $this->assertFalse($this->entries->all()[0]->wasSuccessful());
152
153
154
155
156
157
158
159
160
161
162
163
164
    }

    /**
     * @test
     * @throws \Source\Exceptions\AuthenticationException
     * @throws \Source\Exceptions\AuthorizationException
     */
    public function it_unlocks_doors(): void
    {
        $this->users->create(self::createValidUser('doorcode'));
        $this->doorUser->addGroupToDoor(self::EXPECTED_ACCESS_DOOR_ID, 1);
        $this->doorUser->addGroupToUser(self::USER_ID, 1);
        $this->handleTest('doorcode');
165
166
167

        $this->assertCount(1, $this->entries->all());
        $this->assertTrue($this->entries->all()[0]->wasSuccessful());
168
169
    }
}