summaryrefslogtreecommitdiff
path: root/tests/Unit/Models/NewsCommentsTest.php
blob: dfded712d5c0f23937375642a75acbbb8f3f34ce (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
<?php

declare(strict_types=1);

namespace Engelsystem\Test\Unit\Models;

use Engelsystem\Models\News;
use Engelsystem\Models\NewsComment;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;

/**
 * This class provides tests for the NewsComments model.
 */
class NewsCommentsTest extends TestCase
{
    use HasDatabase;

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

    /** @var News */
    private $news;

    /** @var array */
    private $newsCommentData;

    /**
     * Sets up some test objects and test data.
     *
     * @return void
     */
    protected function setUp(): void
    {
        parent::setUp();
        $this->initDatabase();

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

        $this->news = News::create([
            'title'   => 'test title',
            'text'    => 'test text',
            'user_id' => $this->user->id,
        ]);

        $this->newsCommentData = [
            'news_id' => $this->news->id,
            'text'    => 'test comment',
            'user_id' => $this->user->id,
        ];
    }

    /**
     * Tests that a NewsComment can be created and loaded.
     *
     * @return void
     */
    public function testCreate(): void
    {
        $createdNewsComment = NewsComment::create($this->newsCommentData);

        $newsComment = NewsComment::find($createdNewsComment->id);
        $this->assertInstanceOf(NewsComment::class, $newsComment);
        $this->assertEquals($this->newsCommentData['news_id'], $newsComment->news_id);
        $this->assertSame($this->newsCommentData['text'], $newsComment->text);
        $this->assertEquals($this->newsCommentData['user_id'], $newsComment->user_id);
    }

    /**
     * Tests that accessing the User of a NewsComment works.
     *
     * @return void
     */
    public function testUser(): void
    {
        $newsComment = NewsComment::create($this->newsCommentData);
        $this->assertInstanceOf(User::class, $newsComment->user);
        $this->assertSame($this->user->id, $newsComment->user->id);
    }

    /**
     * Tests that accessing the News of a NewsComment works.
     *
     * @return void
     */
    public function testNews(): void
    {
        $newsComment = NewsComment::create($this->newsCommentData);
        $this->assertInstanceOf(News::class, $newsComment->news);
        $this->assertSame($this->news->id, $newsComment->news->id);
    }

    /**
     * Tests that accessing the NewsComments of a News works.
     *
     * @return void
     */
    public function testNewsComments(): void
    {
        $newsComment = NewsComment::create($this->newsCommentData);
        $comments = $this->news->comments;
        $this->assertCount(1, $comments);
        $comment = $comments->first();
        $this->assertSame($newsComment->id, $comment->id);
    }

    /**
     * Tests that accessing the NewsComments of an User works.
     *
     * @return void
     */
    public function testUserNewsComments(): void
    {
        $newsComment = NewsComment::create($this->newsCommentData);
        $comments = $this->user->newsComments;
        $this->assertCount(1, $comments);
        $comment = $comments->first();
        $this->assertSame($newsComment->id, $comment->id);
    }
}