UpdateGroupApiTest.php 4.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
use Source\Gateways\Groups\GroupsRepository;
use Source\Exceptions\EntityNotFoundException;
10
use Tests\Doubles\InMemoryGroupsRepositoryStub;
Jacob Priddy's avatar
Jacob Priddy committed
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
use Tests\Feature\AuthenticatesWithApplicationTestCase;

class UpdateGroupApiTest 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);
    }

    /**
     * @param string $groupId
     * @param array  $data
     */
    protected function handleTest(string $groupId, array $data): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
38
39
        $this->response = $this->putJson(
            '/groups/' . $groupId,
Jacob Priddy's avatar
Jacob Priddy committed
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
            array_merge(['api_token' => $this->authToken], $data)
        );
    }

    /**
     * @test
     */
    public function it_denies_unauthorized(): void
    {
        $this->handleTest('radlfj', []);

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

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

        $this->handleTest('group', []);

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

    public function invalidGroupProvider(): array
    {
        $group = new Group(0, 'first', 'last');

        return [
            // Missing one of the required
            [
                [
Jacob Priddy's avatar
Jacob Priddy committed
75
                    'title' => $group->getTitle(),
Jacob Priddy's avatar
Jacob Priddy committed
76
77
78
79
80
                ],
                ['description' => ['The description field is required.']],
            ],
            [
                [
Jacob Priddy's avatar
Jacob Priddy committed
81
                    'description' => $group->getDescription(),
Jacob Priddy's avatar
Jacob Priddy committed
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
                ],
                ['title' => ['The title field is required.']],
            ],
        ];
    }

    /**
     * @test
     * @param array $data
     * @param array $message
     * @dataProvider invalidGroupProvider
     * @throws EntityNotFoundException
     */
    public function it_tests_the_validation_rules(array $data, array $message): void
    {
        $this->authenticate();
        $this->handleTest('69', $data);
        $this->response->assertJsonFragment($message);
    }

    /**
     * @test
     * @throws EntityNotFoundException
     */
    public function it_updates_a_group(): void
    {
        $group = new Group(69, '', '');
        $this->groups->create($group);

        $this->authenticate();

        $this->handleTest('69', [
            'title' => 'new title',
            'description' => 'new desc',
        ]);

        $this->response->assertStatus(200);
        $this->response->assertJsonFragment([
            'title' => 'new title',
            'description' => 'new desc',
        ]);
        $this->assertEquals('new title', $this->groups->get('69')->getTitle());
    }

    /**
     * @test
     * @throws EntityNotFoundException
     */
130
    public function it_cannot_update_non_existent_group(): void
Jacob Priddy's avatar
Jacob Priddy committed
131
132
133
134
135
136
137
138
139
    {
        $this->authenticate();
        $this->handleTest('asdf', [
            'title' => 'title',
            'description' => 'description',
        ]);

        $this->response->assertStatus(404);
    }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

    /**
     * @test
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    public function it_presents_error(): void
    {
        $groups = new InMemoryGroupsRepositoryStub();
        $groups->setGroupToReturnOnGet(new Group(69, 'whee', 'asdf'));
        $this->app->bind(GroupsRepository::class, static function () use (&$groups) {
            return $groups;
        });

        $this->authenticate();

        $this->handleTest('69', [
            'title' => 'new title',
            'description' => 'new desc',
        ]);

        $this->response->assertStatus(200);
        $this->response->assertJson(['status' => 'error', 'message' => 'Unable to update group.']);
    }
Jacob Priddy's avatar
Jacob Priddy committed
163
}