forked from guillaumebriday/laravel-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommentTest.php
53 lines (43 loc) · 1.44 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
52
53
<?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 = Comment::factory()->create();
$this->assertEqualsWithDelta($comment->posted_at->timestamp, now()->timestamp, 1);
}
public function testGettingOnlyLastWeekComments()
{
$faker = Factory::create();
// Older Comments
Comment::factory()
->count(3)
->create()
->each(function ($comment) use ($faker) {
$comment->posted_at = $faker->dateTimeBetween(carbon('3 months ago'), carbon('2 months ago'));
$comment->save();
});
// Newer Comments
Comment::factory()
->count(3)
->create()
->each(function ($comment) use ($faker) {
$comment->posted_at = $faker->dateTimeBetween(carbon('1 week ago'), now());
$comment->save();
});
$isDuringLastWeek = true;
foreach (Comment::lastWeek()->get() as $comment) {
$isDuringLastWeek = $comment->posted_at->between(carbon('1 week ago'), now());
if (! $isDuringLastWeek) {
break;
}
}
$this->assertTrue($isDuringLastWeek);
}
}