engelsystem/tests/Unit/Models/NewsTest.php

96 lines
2.4 KiB
PHP
Raw Normal View History

2019-10-31 20:02:34 +01:00
<?php
2019-10-31 20:02:34 +01:00
declare(strict_types=1);
namespace Engelsystem\Test\Unit\Models;
use Engelsystem\Models\News;
2019-10-31 20:02:34 +01:00
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 */
2019-10-31 20:02:34 +01:00
private $newsData;
/** @var User */
2019-10-31 20:02:34 +01:00
private $user;
/**
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->initDatabase();
$this->user = (new User())->create([
2019-10-31 20:02:34 +01:00
'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.
*
2020-01-02 15:08:08 +01:00
* @covers \Engelsystem\Models\News
2019-10-31 20:02:34 +01:00
*/
public function testCreateDefault(): void
{
$news = (new News())->create($this->newsData);
$news = $news->find($news->id);
2019-10-31 20:02:34 +01:00
$this->assertSame(1, $news->id);
$this->assertSame($this->newsData['title'], $news->title);
$this->assertSame($this->newsData['text'], $news->text);
$this->assertFalse($news->is_meeting);
}
2020-01-02 15:08:08 +01:00
/**
* 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());
}
2019-10-31 20:02:34 +01:00
/**
* Tests that creating a News item with all fill values works.
*
2020-01-02 15:08:08 +01:00
* @covers \Engelsystem\Models\News
2019-10-31 20:02:34 +01:00
*/
public function testCreate(): void
{
$news = (new News())->create(
$this->newsData + ['is_meeting' => true]
2019-10-31 20:02:34 +01:00
);
$news = $news->find($news->id);
2019-10-31 20:02:34 +01:00
$this->assertSame(1, $news->id);
$this->assertSame($this->newsData['title'], $news->title);
$this->assertSame($this->newsData['text'], $news->text);
$this->assertTrue($news->is_meeting);
}
}