summaryrefslogtreecommitdiff
path: root/tests/Unit/Models/MessageTest.php
blob: 7fb3e028cfa0c62ba7c7f875d35edb874ea1e8b9 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php

declare(strict_types=1);

namespace Engelsystem\Test\Unit\Models;

use Engelsystem\Models\Message;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;

/**
 * This class provides tests covering the Message model and its relations.
 */
class MessageTest extends TestCase
{
    use HasDatabase;

    /** @var User */
    private $user1;

    /** @var User */
    private $user2;

    /** @var Message */
    private $message1;

    /** @var Message */
    private $message2;

    /** @var Message */
    private $message3;

    /**
     * @return void
     */
    protected function setUp(): void
    {
        parent::setUp();
        $this->initDatabase();

        $this->user1 = User::create([
            'name'     => 'user1',
            'password' => '',
            'email'    => 'user1@example.com',
            'api_key'  => '',
        ]);

        $this->user2 = User::create([
            'name'     => 'user2',
            'password' => '',
            'email'    => 'user2@example.com',
            'api_key'  => '',
        ]);

        $this->message1 = Message::create([
            'user_id'     => $this->user1->id,
            'receiver_id' => $this->user2->id,
            'text'        => 'message1',
        ]);

        $this->message2 = Message::create([
            'user_id'     => $this->user1->id,
            'receiver_id' => $this->user2->id,
            'read'        => true,
            'text'        => 'message2',
        ]);

        $this->message3 = Message::create([
            'user_id'     => $this->user2->id,
            'receiver_id' => $this->user1->id,
            'text'        => 'message3',
        ]);
    }

    /**
     * Tests that loading Messages works.
     *
     * @covers \Engelsystem\Models\Message::__construct
     *
     * @return void
     */
    public function testLoad(): void
    {
        $message1 = Message::find($this->message1->id);
        $this->assertSame($this->message1->user_id, $message1->user_id);
        $this->assertSame($this->message1->receiver_id, $message1->receiver_id);
        $this->assertSame($this->message1->read, $message1->read);
        $this->assertSame($this->message1->text, $message1->text);

        $message2 = Message::find($this->message2->id);
        $this->assertSame($this->message2->user_id, $message2->user_id);
        $this->assertSame($this->message2->receiver_id, $message2->receiver_id);
        $this->assertSame($this->message2->read, $message2->read);
        $this->assertSame($this->message2->text, $message2->text);
    }

    /**
     * Tests that the Messages have the correct senders.
     *
     * @covers \Engelsystem\Models\Message::user
     *
     * @return void
     */
    public function testSenders(): void
    {
        $this->assertSame($this->user1->id, $this->message1->user->id);
        $this->assertSame($this->user1->id, $this->message2->user->id);
        $this->assertSame($this->user2->id, $this->message3->user->id);
    }

    /**
     * Tests that the Messages have the correct receivers.
     *
     * @covers \Engelsystem\Models\Message::receiver
     *
     * @return void
     */
    public function testReceivers(): void
    {
        $this->assertSame($this->user2->id, $this->message1->receiver->id);
        $this->assertSame($this->user2->id, $this->message2->receiver->id);
        $this->assertSame($this->user1->id, $this->message3->receiver->id);
    }

    /**
     * Tests that the Users have the correct sent Messages.
     *
     * @covers \Engelsystem\Models\User\User::messagesSent
     *
     * @return void
     */
    public function testUserSentMessages(): void
    {
        $sentByUser1 = $this->user1->messagesSent->all();
        $this->assertCount(2, $sentByUser1);
        $this->assertSame($this->message2->id, $sentByUser1[0]->id);
        $this->assertSame($this->message1->id, $sentByUser1[1]->id);

        $sentByUser2 = $this->user2->messagesSent->all();
        $this->assertCount(1, $sentByUser2);
        $this->assertSame($this->message3->id, $sentByUser2[0]->id);
    }

    /**
     * Tests that the Users have the correct received Messages.
     *
     * @covers \Engelsystem\Models\User\User::messagesReceived
     *
     * @return void
     */
    public function testUserReceivedMessages(): void
    {
        $receivedByUser1 = $this->user1->messagesReceived->all();
        $this->assertCount(1, $receivedByUser1);
        $this->assertSame($this->message3->id, $receivedByUser1[0]->id);

        $receivedByUser2 = $this->user2->messagesReceived->all();
        $this->assertCount(2, $receivedByUser2);
        $this->assertSame($this->message1->id, $receivedByUser2[0]->id);
        $this->assertSame($this->message2->id, $receivedByUser2[1]->id);
    }

    /**
     * Tests that the user have the correct Messages.
     *
     * @covers \Engelsystem\Models\User\User::messages
     */
    public function testUserMessages(): void
    {
        $user1Messages = $this->user1->messages->all();
        $this->assertCount(3, $user1Messages);
        $this->assertSame($this->message3->id, $user1Messages[0]->id);
        $this->assertSame($this->message1->id, $user1Messages[1]->id);
        $this->assertSame($this->message2->id, $user1Messages[2]->id);

        $user2Messages = $this->user2->messages->all();
        $this->assertCount(3, $user2Messages);
        $this->assertSame($this->message3->id, $user2Messages[0]->id);
        $this->assertSame($this->message1->id, $user2Messages[1]->id);
        $this->assertSame($this->message2->id, $user2Messages[2]->id);
    }
}