UseCaseTest.php 2.85 KB
Newer Older
1
2
3
4
5
<?php


namespace Tests\Unit\Source\UseCases\Users\CreateUser;

Jacob Priddy's avatar
Jacob Priddy committed
6
use Source\Entities\User;
7
use PHPUnit\Framework\TestCase;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\Exceptions\EntityExistsException;
9
use Tests\Doubles\InMemoryUsersRepositoryStub;
10
11
12
13
use Source\UseCases\Users\CreateUser\CreateUser;
use Source\Gateways\Users\InMemoryUsersRepository;
use Source\UseCases\Users\CreateUser\ResponseModel;

Jacob Priddy's avatar
Jacob Priddy committed
14
15
class UseCaseTest extends TestCase
{
16
17
18
19
20
21
    protected InMemoryUsersRepository $usersRepository;

    protected CreateUser $useCase;

    protected ResponseModel $response;

Jacob Priddy's avatar
Jacob Priddy committed
22
23
    protected PresenterStub $presenter;

Jacob Priddy's avatar
Jacob Priddy committed
24
25
    public function setUp(): void
    {
26
27
28
29
        parent::setUp();

        $this->usersRepository = new InMemoryUsersRepository();
        $this->useCase = new CreateUser($this->usersRepository);
Jacob Priddy's avatar
Jacob Priddy committed
30
31
32
        $this->presenter = new PresenterStub();
    }

Jacob Priddy's avatar
Jacob Priddy committed
33
34
    protected function createUserAttributes(User $user): array
    {
Jacob Priddy's avatar
Jacob Priddy committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        $expires = $user->getExpiresAt();
        if ($expires) {
            $expires = $expires->toString();
        }

        return [
            'first_name' => $user->getFirstName(),
            'last_name' => $user->getLastName(),
            'display_name' => $user->getDisplayName(),
            'emplid' => $user->getEmplid(),
            'email' => $user->getEmail(),
            'password' => $user->getPassword(),
            'doorcode' => $user->getDoorcode(),
            'expires_at' => $expires,
        ];
50
51
    }

Jacob Priddy's avatar
Jacob Priddy committed
52
53
54
55
    /**
     * @param array $attributes
     * @throws EntityExistsException
     */
Jacob Priddy's avatar
Jacob Priddy committed
56
57
    public function handleTest(array $attributes): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
58
59
60
61
62
63
64
65
66
        $this->useCase->create($attributes, $this->presenter);

        $this->response = $this->presenter->response;
    }

    /**
     * @test
     * @throws EntityExistsException
     */
Jacob Priddy's avatar
Jacob Priddy committed
67
68
    public function presenter_was_called(): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
69
70
71
72
73
74
75
76
77
78
79
        $user = new User(0, 'special name', '', '', null, '', '', '', null, null, null);

        $this->handleTest($this->createUserAttributes($user));

        $this->assertTrue($this->presenter->wasPresenterCalled());
    }

    /**
     * @test
     * @throws EntityExistsException
     */
Jacob Priddy's avatar
Jacob Priddy committed
80
81
    public function it_creates_a_user(): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
82
83
84
85
86
87
        $user = new User(0, 'special name', '', '', null, '', '', '', null, null, null);

        $this->handleTest($this->createUserAttributes($user));

        $this->assertEquals($user->getFirstName(), $this->response->getUser()->getFirstName());
        $this->assertCount(1, $this->usersRepository->all());
88
    }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

    /**
     * @test
     * @throws \Source\Exceptions\EntityExistsException
     */
    public function it_cannot_create_group_that_already_exists(): void
    {
        $user = new User(1, '', '', '', '', '', '', '');

        $this->useCase = new CreateUser(new InMemoryUsersRepositoryStub());

        $this->expectException(EntityExistsException::class);

        $this->handleTest($this->createUserAttributes($user));
    }
104
}