DeleteGroupApiTest.php 3.03 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
<?php


namespace Tests\Feature\Api\Groups;

use Source\Entities\Group;
Jacob Priddy's avatar
Jacob Priddy committed
7
use Illuminate\Testing\TestResponse;
Jacob Priddy's avatar
Jacob Priddy committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use Source\Gateways\Groups\GroupsRepository;
use Source\Exceptions\EntityNotFoundException;
use Tests\Doubles\InMemoryGroupsRepositoryStub;
use Tests\Feature\AuthenticatesWithApplicationTestCase;

class DeleteGroupApiTest extends AuthenticatesWithApplicationTestCase
{
    /**
     * @var \Illuminate\Foundation\Testing\TestResponse
     */
    protected TestResponse $response;

    /**
     * @var mixed|\Source\Gateways\Groups\GroupsRepository
     */
    protected GroupsRepository $groups;

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

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

    protected function stubGroupsRepository(): void
    {
34
        $this->app->singleton(GroupsRepository::class, static function () {
Jacob Priddy's avatar
Jacob Priddy committed
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
            return new InMemoryGroupsRepositoryStub();
        });
    }

    protected function handleTest(string $groupId): void
    {
        $this->response = $this->deleteJson('/groups/' . $groupId, [
            'api_token' => $this->authToken,
        ]);
    }

    /**
     * @test
     */
    public function it_denies_unauthorized(): void
    {
        $this->handleTest('group id');

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

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

        $this->handleTest('group id');

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

    /**
     * @test
     * @throws EntityNotFoundException
     */
    public function it_deletes_a_non_existent_group(): void
    {
        $this->authenticate();
        $this->handleTest('asdfasfd non existent group');

78
        $this->response->assertStatus(404);
Jacob Priddy's avatar
Jacob Priddy committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    }

    /**
     * @test
     * @throws EntityNotFoundException
     */
    public function it_deletes_a_group(): void
    {
        $this->authenticate();

        $group = new Group(69, '', '');
        $this->groups->create($group);

        $this->handleTest('69');

        $this->response->assertStatus(200);
        $this->response->assertJson([
            'message' => 'Group deleted.',
            'status' => 'success',
            'code' => 200,
        ]);

        $this->assertCount(0, $this->groups->all());
    }

    /**
     * @test
     * @throws EntityNotFoundException
107
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
Jacob Priddy's avatar
Jacob Priddy committed
108
109
110
111
112
113
     */
    public function it_handles_a_failed_delete(): void
    {
        $this->authenticate();

        $this->stubGroupsRepository();
114
115
116
        /** @var InMemoryGroupsRepositoryStub $groups */
        $groups = $this->app->make(GroupsRepository::class);
        $groups->setGroupToReturnOnGet(new Group(1, '', ''));
Jacob Priddy's avatar
Jacob Priddy committed
117
118
119
120
121
122
123
124
125
126

        $this->handleTest('asdf');
        $this->response->assertStatus(200);
        $this->response->assertJson([
            'message' => 'Unable to delete group.',
            'status' => 'error',
            'code' => 200,
        ]);
    }
}