forked from guillaumebriday/laravel-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommentTest.php
51 lines (41 loc) · 1.4 KB
/
CommentTest.php
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
<?php
namespace Tests\Unit;
use App\Models\Comment;
use Faker\Factory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CommentTest extends TestCase
{
use RefreshDatabase;
public function testPostedAt()
{
$comment = factory(Comment::class)->create();
$this->assertEquals($comment->posted_at->toDateTimeString(), now()->toDateTimeString());
}
public function testGettingOnlyLastWeekComments()
{
$faker = Factory::create();
// Older Comments
factory(Comment::class, 3)
->create()
->each(function ($comment) use ($faker) {
$comment->posted_at = $faker->dateTimeBetween(now()->subMonths(3), now()->subMonths(2));
$comment->save();
});
// Newer Comments
factory(Comment::class, 3)
->create()
->each(function ($comment) use ($faker) {
$comment->posted_at = $faker->dateTimeBetween(now()->subWeek(), now());
$comment->save();
});
$isDuringLastWeek = true;
foreach (Comment::lastWeek()->get() as $comment) {
$isDuringLastWeek = $comment->posted_at->between(now()->subWeek(), now());
if (! $isDuringLastWeek) {
break;
}
}
$this->assertTrue($isDuringLastWeek);
}
}