Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

laravelio/laravel.io

2497 stars
2 code files
View laravelio/laravel.io on GitHub

database/factories/LikeFactory.php

Open in GitHub
use App\Models\Like;
use App\Models\Reply;
use App\Models\Thread;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
 
class LikeFactory extends Factory
{
public function definition()
{
return [
'user_id' => function () {
return User::factory()->create()->id;
},
];
}
 
public function reply()
{
return $this->state(function () {
return [
'likeable_id' => function () {
return Reply::factory()->create()->id;
},
'likeable_type' => 'replies',
];
});
}
 
public function thread()
{
return $this->state(function () {
return [
'likeable_id' => function () {
return Thread::factory()->create()->id;
},
'likeable_type' => 'threads',
];
});
}
}

tests/Integration/Jobs/DeleteThreadTest.php

Open in GitHub
use App\Jobs\DeleteThread;
use App\Models\Like;
use App\Models\Reply;
use App\Models\Thread;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;
 
class DeleteThreadTest extends TestCase
{
use DatabaseMigrations;
 
/** @test */
public function we_can_delete_a_thread_and_its_replies()
{
$thread = Thread::factory()->create();
$reply = Reply::factory()->create(['replyable_id' => $thread->id()]);
Like::factory()->thread()->create(['likeable_id' => $thread->id()]);
Like::factory()->reply()->create(['likeable_id' => $reply->id()]);
 
$this->dispatch(new DeleteThread($thread));
 
$this->assertDatabaseMissing('threads', ['id' => $thread->id()]);
$this->assertDatabaseMissing('replies', ['replyable_id' => $thread->id()]);
$this->assertDatabaseMissing('likes', ['likeable_type' => 'threads', 'likeable_id' => $thread->id()]);
$this->assertDatabaseMissing('likes', ['likeable_type' => 'replies', 'likeable_id' => $reply->id()]);
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.