Browse Source

Add comment time_display attribute

Comment time will shown in diffForHumans if created today
Comment time will shown in date time yesterday or before
Add Comment model test
pull/15/head
Nafies Luthfi 7 years ago
parent
commit
6364790b03
  1. 9
      app/Entities/Projects/Comment.php
  2. 2
      resources/views/projects/partials/comment-section.blade.php
  3. 32
      tests/Unit/Models/CommentTest.php

9
app/Entities/Projects/Comment.php

@ -13,4 +13,13 @@ class Comment extends Model
{
return $this->belongsTo(User::class);
}
public function getTimeDisplayAttribute()
{
if (now()->format('Y-m-d') != $this->created_at->format('Y-m-d')) {
return $this->created_at;
}
return $this->created_at->diffForHumans();
}
}

2
resources/views/projects/partials/comment-section.blade.php

@ -11,7 +11,7 @@
@foreach($comments as $comment)
<div class="alert alert-warning">
<legend style="font-size: 14px;margin-bottom: 10px;">
<span class="label label-default pull-right">{{ $comment->created_at }}</span>
<span class="label label-default pull-right">{{ $comment->time_display }}</span>
<strong>{{ $comment->creator->name }}</strong>
</legend>
<div class="pull-right">

32
tests/Unit/Models/CommentTest.php

@ -0,0 +1,32 @@
<?php
namespace Tests\Unit\Models;
use Tests\TestCase;
use App\Entities\Users\User;
use App\Entities\Projects\Comment;
use Illuminate\Foundation\Testing\DatabaseMigrations;
class CommentTest extends TestCase
{
use DatabaseMigrations;
/** @test */
public function a_comment_has_belongs_to_creator_relation()
{
$comment = factory(Comment::class)->make();
$this->assertInstanceOf(User::class, $comment->creator);
$this->assertEquals($comment->creator_id, $comment->creator->id);
}
/** @test */
public function a_comment_has_time_display_attribute()
{
$comment = factory(Comment::class)->create(['created_at' => now()->subHour()]);
$this->assertEquals($comment->created_at->diffForHumans(), $comment->time_display);
$comment = factory(Comment::class)->create(['created_at' => now()->subDays(3)]);
$this->assertEquals($comment->created_at, $comment->time_display);
}
}
Loading…
Cancel
Save