EntriesGetApiTest.php 3.17 KB
Newer Older
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
<?php


namespace Tests\Feature\Api\Entries;

use Carbon\Carbon;
use Source\Entities\Entry;
use Illuminate\Testing\TestResponse;
use Source\Gateways\Entries\EntriesRepository;
use Source\Gateways\Entries\InMemoryEntriesRepository;
use Tests\Feature\AuthenticatesWithApplicationTestCase;

class EntriesGetApiTest extends AuthenticatesWithApplicationTestCase
{
    /**
     * @var \Source\Gateways\Entries\InMemoryEntriesRepository
     */
    protected InMemoryEntriesRepository $entries;

    /**
     * @var \Illuminate\Testing\TestResponse
     */
    protected TestResponse $response;

    /**
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
     * @throws \Source\Exceptions\EntityExistsException
     * @throws \Source\Exceptions\EntityNotFoundException
     */
    protected function setUp(): void
    {
        parent::setUp();

        $this->entries = new InMemoryEntriesRepository();
        $this->instance(EntriesRepository::class, $this->entries);

        $this->authenticate();
    }

40
    protected function handleTest(?string $start, ?string $end, ?string $doorId, ?string $userId): void
41
42
43
44
45
    {
        $this->response = $this->getJson(
            'api/entries?api_token=' . $this->authToken
            . '&start=' . $start
            . '&end=' . $end
46
            . '&door_id=' . $doorId
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
            . '&user_id=' . $userId
        );
    }

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

        $this->response->assertJsonCount(0, 'data');
    }

    /**
     * @test
     */
    public function it_filters_by_start(): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
66
67
        $this->entries->add(new Entry(1, 1, 2, true, 'reason', new Carbon('2020-01-02')));
        $this->entries->add(new Entry(2, 1, 2, true, 'reason', new Carbon('2020-04-02')));
68
69
70
71
72
73
74
75
76
77
78

        $this->handleTest('2020-02-05', null, null, null);
        $this->response->assertJsonCount(1, 'data');
        $this->response->assertJson(['data' => [['id' => 2]]]);
    }

    /**
     * @test
     */
    public function it_filters_by_end(): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
79
80
        $this->entries->add(new Entry(1, 1, 2, true, 'reason', new Carbon('2020-01-02')));
        $this->entries->add(new Entry(2, 1, 2, true, 'reason', new Carbon('2020-04-02')));
81
82
83
84
85
86
87
88
89
90
        $this->handleTest(null, '2020-02-05', null, null);
        $this->response->assertJsonCount(1, 'data');
        $this->response->assertJson(['data' => [['id' => 1]]]);
    }

    /**
     * @test
     */
    public function it_filters_by_user_id(): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
91
92
        $this->entries->add(new Entry(1, 1, 2, true, 'reason'));
        $this->entries->add(new Entry(2, 2, 1, true, 'reason'));
93
94
95
96
97
98
99
100
        $this->handleTest(null, null, null, 1);
        $this->response->assertJsonCount(1, 'data');
        $this->response->assertJson(['data' => [['id' => 1]]]);
    }

    /**
     * @test
     */
101
    public function it_filters_by_door_id(): void
102
    {
Jacob Priddy's avatar
Jacob Priddy committed
103
104
        $this->entries->add(new Entry(1, 1, 2, true, 'reason'));
        $this->entries->add(new Entry(2, 2, 1, true, 'reason'));
105
        $this->handleTest(null, null, 1, null);
106
107
108
109
        $this->response->assertJsonCount(1, 'data');
        $this->response->assertJson(['data' => [['id' => 2]]]);
    }
}