GetCurrentUserGroupsApiTest.php 1.87 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php


namespace Tests\Feature\Api\Me;

use Source\Entities\Group;
use Source\Gateways\Groups\GroupsRepository;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\GroupUser\GroupUserRepository;
use Source\Gateways\Groups\InMemoryGroupsRepository;
use Tests\Feature\AuthenticatesWithApplicationTestCase;
use Source\Gateways\GroupUser\InMemoryGroupUserRepository;

class GetCurrentUserGroupsApiTest extends AuthenticatesWithApplicationTestCase
{
16
17
18
    /**
     * @var \Illuminate\Foundation\Testing\TestResponse
     */
Jacob Priddy's avatar
Jacob Priddy committed
19
20
    protected TestResponse $response;

21
22
23
    /**
     * @var mixed|\Source\Gateways\GroupUser\InMemoryGroupUserRepository
     */
Jacob Priddy's avatar
Jacob Priddy committed
24
25
    protected InMemoryGroupUserRepository $repository;

26
27
28
    /**
     * @var mixed|\Source\Gateways\Groups\InMemoryGroupsRepository
     */
Jacob Priddy's avatar
Jacob Priddy committed
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
    protected InMemoryGroupsRepository $groups;

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

        $this->groups = $this->app->make(GroupsRepository::class);
        $this->repository = $this->app->make(GroupUserRepository::class);
    }

    protected function handleTest(): void
    {
        $this->response = $this->getJson('/me/groups?api_token=' . $this->authToken);
    }

    /**
     * @test
     */
    public function it_requires_a_logged_in_user(): void
    {
        $this->handleTest();

        $this->response->assertStatus(401);
    }

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

        $group = $this->groups->create(new Group(0, 'title', 'descriptions'));

        $this->repository->addUserToGroup($this->authUser->getId(), $group->getId());

        $this->handleTest();

        $this->response->assertStatus(200);
        $this->response->assertJson(['groups' => [[
            'id' => $group->getId()
        ]]]);
    }
}