CreateGroupApiTest.php 2.72 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
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
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
<?php


namespace Tests\Feature\Api\Groups;

use Source\Entities\Group;
use Source\Gateways\Groups\GroupsRepository;
use Source\Exceptions\EntityNotFoundException;
use Illuminate\Foundation\Testing\TestResponse;
use Source\Gateways\Groups\InMemoryGroupsRepository;
use Tests\Feature\AuthenticatesWithApplicationTestCase;

class CreateGroupApiTest extends AuthenticatesWithApplicationTestCase
{
    protected TestResponse $response;

    protected InMemoryGroupsRepository $groups;

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

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

    /**
     * @param array $data
     */
    protected function handleTest(array $data): void
    {
        $this->response = $this->postJson('/groups',
            array_merge(
                ['api_token' => $this->authToken],
                $data
            )
        );
    }

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

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

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

        $this->handleTest([]);

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

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

        return [
            // Missing one of the required
            [
                [
                    'title' => $group->getTitle(),
                ],
                ['description' => ['The description field is required.']],
            ],
            [
                [
                    'description' => $group->getDescription(),
                ],
                ['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($data);
        $this->response->assertJsonFragment($message);
    }

    /**
     * @test
     * @throws EntityNotFoundException
     */
    public function it_creates_a_group(): void
    {
        $this->authenticate();
        $this->handleTest([
            'title' => 'title',
            'description' => 'desc',
        ]);

        $this->response->assertStatus(200);
        $this->response->assertJsonFragment([
            'title' => 'title',
            'description' => 'desc',
        ]);

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