Introduce the NewsComments model

This commit is contained in:
Michael Weimann 2019-11-12 21:49:43 +01:00
parent c0bf0b56f1
commit 26b8d2b921
No known key found for this signature in database
GPG Key ID: 34F0524D4DA694A1
4 changed files with 202 additions and 7 deletions

View File

@ -1,11 +1,12 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Models;
use Carbon\Carbon;
use Engelsystem\Models\User\UsesUserModel;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
@ -16,6 +17,8 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @property-read Collection|NewsComment[] $comments
*
* @method static QueryBuilder|LogEntry[] whereId($value)
* @method static QueryBuilder|LogEntry[] whereTitle($value)
* @method static QueryBuilder|LogEntry[] whereText($value)
@ -42,4 +45,13 @@ class News extends BaseModel
'is_meeting',
'user_id',
];
/**
* @return HasMany
*/
public function comments(): HasMany
{
return $this->hasMany(NewsComment::class)
->orderBy('created_at');
}
}

View File

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Models;
use Carbon\Carbon;
use Engelsystem\Models\User\UsesUserModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
* This class represents a news comment.
*
* @property int $id
* @property int $news_id
* @property string $text
* @property News $news
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @method static QueryBuilder|LogEntry[] whereId($value)
* @method static QueryBuilder|LogEntry[] whereText($value)
* @method static QueryBuilder|LogEntry[] whereCreatedAt($value)
* @method static QueryBuilder|LogEntry[] whereUpdatedAt($value)
*/
class NewsComment extends BaseModel
{
use UsesUserModel;
/** @var bool Enable timestamps */
public $timestamps = true;
/** @var string[] */
protected $fillable = [
'news_id',
'text',
'user_id',
];
/**
* @return BelongsTo
*/
public function news(): BelongsTo
{
return $this->belongsTo(News::class);
}
}

View File

@ -5,6 +5,8 @@ namespace Engelsystem\Models\User;
use Carbon\Carbon;
use Engelsystem\Models\BaseModel;
use Engelsystem\Models\News;
use Engelsystem\Models\NewsComment;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Query\Builder as QueryBuilder;
@ -23,6 +25,7 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
* @property-read QueryBuilder|PersonalData $personalData
* @property-read QueryBuilder|Settings $settings
* @property-read QueryBuilder|State $state
* @property-read Collection|NewsComment[] $newsComments
*
* @method static QueryBuilder|User whereId($value)
* @method static QueryBuilder|User[] whereName($value)
@ -105,4 +108,12 @@ class User extends BaseModel
{
return $this->hasMany(News::class);
}
/**
* @return HasMany
*/
public function newsComments(): HasMany
{
return $this->hasMany(NewsComment::class);
}
}

View File

@ -0,0 +1,125 @@
<?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);
}
}