EntriesDatabaseTest.php 3.42 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
<?php


namespace Tests\Database;

use Carbon\Carbon;
use Source\Entities\Door;
use Source\Entities\User;
use Source\Entities\Entry;
use Tests\DatabaseTestCase;
use Source\Entities\HashedSearchable;
use Source\Gateways\Doors\DatabaseDoorsRepository;
use Source\Gateways\Users\DatabaseUsersRepository;
use Source\Gateways\Entries\DatabaseEntriesRepository;

class EntriesDatabaseTest extends DatabaseTestCase
{
    /**
     * @var \Source\Gateways\Users\DatabaseUsersRepository
     */
    protected DatabaseUsersRepository $users;

    /**
     * @var \Source\Gateways\Doors\DatabaseDoorsRepository
     */
    protected DatabaseDoorsRepository $doors;

    /**
     * @var \Source\Gateways\Entries\DatabaseEntriesRepository
     */
    protected DatabaseEntriesRepository $repository;

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

        $this->users = new DatabaseUsersRepository();
        $this->doors = new DatabaseDoorsRepository();
        $this->repository = new DatabaseEntriesRepository();
    }

    /**
     * @param string $name
     * @return \Source\Entities\Door
     * @throws \Source\Exceptions\EntityExistsException
     */
    protected function createDoor(string $name = ''): Door
    {
        return $this->doors->create(new Door(0, '', $name, new HashedSearchable($name)));
    }

52
53
54
55
56
    /**
     * @param string $email
     * @return \Source\Entities\User
     * @throws \Source\Exceptions\EntityExistsException
     */
Jacob Priddy's avatar
Jacob Priddy committed
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
    protected function createUser(string $email = ''): User
    {
        return $this->users->create(new User(
            0,
            '',
            '',
            '',
            $email
        ));
    }

    /**
     * @test
     * @throws \Source\Exceptions\EntityExistsException
     */
    public function it_gets_entries_for_a_user_between_dates(): void
    {
        $begin = Carbon::now()->subMinute();
        $end = Carbon::now()->addMinute();

        $d1 = $this->createDoor('d1');
        $u1 = $this->createUser('u1');
        $d2 = $this->createDoor('d2');
        $u2 = $this->createUser('u2');
        $this->repository->add(new Entry(0, $u1->getId(), $d1->getId(), true));
        $this->repository->add(new Entry(0, $u2->getId(), $d1->getId(), true));
        $this->repository->add(new Entry(0, $u2->getId(), $d2->getId(), false));
        $this->assertCount(2, $this->repository->getForDoorBetween($d1->getId(), $begin, $end));
        $this->assertCount(2, $this->repository->getForUserBetween($u2->getId(), $begin, $end));
        $this->assertCount(1, $this->repository->getForUserAndDoorBetween($u2->getId(), $d1->getId(), $begin, $end));
    }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

    /**
     * @test
     * @throws \Source\Exceptions\EntityExistsException
     */
    public function it_gets_all_entries_in_a_date_range(): void
    {
        $d1 = $this->createDoor('d1');
        $u1 = $this->createUser('u1');
        $d2 = $this->createDoor('d2');
        $u2 = $this->createUser('u2');
        $e = new \App\Entry();
        $e->setAttribute('user_id', $u1->getId());
        $e->setAttribute('door_id', $d2->getId());
        $e->setCreatedAt(Carbon::now()->subDays(100));
        $this->repository->add(new Entry(0, $u1->getId(), $d1->getId(), true));
        $this->repository->add(new Entry(0, $u2->getId(), $d1->getId(), true));
        $this->repository->add(new Entry(0, $u2->getId(), $d2->getId(), false));
        $this->assertCount(3, $this->repository->getBetween(Carbon::now()->subDays(1), Carbon::now()->addDay()));
    }
Jacob Priddy's avatar
Jacob Priddy committed
108
}