UseCaseTest.php 2.57 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
7
8
<?php


namespace Tests\Unit\Source\UseCases\Groups\CreateGroup;

use Source\Entities\Group;
use PHPUnit\Framework\TestCase;
use Source\Exceptions\EntityExistsException;
9
use Tests\Doubles\InMemoryGroupsRepositoryStub;
Jacob Priddy's avatar
Jacob Priddy committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
82
use Source\UseCases\Groups\CreateGroup\CreateGroup;
use Source\Gateways\Groups\InMemoryGroupsRepository;
use Source\UseCases\Groups\CreateGroup\ResponseModel;

class UseCaseTest extends TestCase
{
    protected InMemoryGroupsRepository $groupsRepository;

    protected CreateGroup $useCase;

    protected ResponseModel $response;

    protected PresenterStub $presenter;

    public function setUp(): void
    {
        parent::setUp();

        $this->groupsRepository = new InMemoryGroupsRepository();
        $this->useCase = new CreateGroup($this->groupsRepository);
        $this->presenter = new PresenterStub();
    }

    /**
     * @param \Source\Entities\Group $group
     * @return array
     */
    protected function createGroupAttributes(Group $group): array
    {
        return [
            'title' => $group->getTitle(),
            'description' => $group->getDescription(),
        ];
    }

    /**
     * @param array $attributes
     * @throws EntityExistsException
     */
    public function handleTest(array $attributes): void
    {
        $this->useCase->create($attributes, $this->presenter);

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

    /**
     * @test
     * @throws EntityExistsException
     */
    public function presenter_was_called(): void
    {
        $group = new Group(0, 'title', 'desc');

        $this->handleTest($this->createGroupAttributes($group));

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

    /**
     * @test
     * @throws EntityExistsException
     */
    public function it_creates_a_group(): void
    {
        $group = new Group(0, 'title', 'desc');

        $this->handleTest($this->createGroupAttributes($group));

        $this->assertEquals($group->getTitle(), $this->response->getGroup()->getTitle());
        $this->assertEquals($group->getDescription(), $this->response->getGroup()->getDescription());
        $this->assertCount(1, $this->groupsRepository->all());
    }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

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

        $this->useCase = new CreateGroup(new InMemoryGroupsRepositoryStub());

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

        $this->handleTest($this->createGroupAttributes($group));
    }
Jacob Priddy's avatar
Jacob Priddy committed
98
}