-
database/factories/LikeFactory.php
Open in GitHubuse 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 GitHubuse 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()]); } }