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

declare(strict_types=1);

namespace Engelsystem\Test\Unit\Models;

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

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

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

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

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

        $this->user = (new User())->create([
            'name'     => 'lorem',
            'password' => '',
            'email'    => 'foo@bar.batz',
            'api_key'  => '',
        ]);

        $this->newsData = [
            'title'   => 'test title',
            'text'    => 'test text',
            'user_id' => $this->user->id
        ];
    }

    /**
     * Tests that creating a News item with default values works.
     *
     * @covers \Engelsystem\Models\News
     */
    public function testCreateDefault(): void
    {
        $news = (new News())->create($this->newsData);
        $news = $news->find($news->id);

        $this->assertSame(1, $news->id);
        $this->assertSame($this->newsData['title'], $news->title);
        $this->assertSame($this->newsData['text'], $news->text);
        $this->assertFalse($news->is_meeting);
    }

    /**
     * Tests that accessing the NewsComments of a News works.
     *
     * @covers \Engelsystem\Models\News::comments
     */
    public function testNewsComments(): void
    {
        $news = (new News())->create($this->newsData);
        $comment = $news->comments()->create(['text' => 'test comment', 'user_id' => $this->user->id]);

        $comments = $news->comments;
        $this->assertCount(1, $comments);
        $this->assertEquals($comment->toArray(), $news->comments->first()->toArray());
    }

    /**
     * Tests that creating a News item with all fill values works.
     *
     * @covers \Engelsystem\Models\News
     */
    public function testCreate(): void
    {
        $news = (new News())->create(
            $this->newsData + ['is_meeting' => true]
        );
        $news = $news->find($news->id);

        $this->assertSame(1, $news->id);
        $this->assertSame($this->newsData['title'], $news->title);
        $this->assertSame($this->newsData['text'], $news->text);
        $this->assertTrue($news->is_meeting);
    }
}