Last active
September 28, 2019 22:58
-
-
Save tonyfrenzy/e8d693123cd0184d288815b37be93a4e to your computer and use it in GitHub Desktop.
morphTo() One-to-Many Polymorphic Relationship Test (Comment, Video, Post) - Testing Model Relationships in Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Tests\Unit; | |
| use App\Comment; | |
| use App\Country; | |
| use App\Post; | |
| use App\Supplier; | |
| use App\User; | |
| use App\Video; | |
| use Illuminate\Foundation\Testing\RefreshDatabase; | |
| use Illuminate\Foundation\Testing\WithFaker; | |
| use Illuminate\Support\Facades\Schema; | |
| use Tests\TestCase; | |
| class CommentsTest extends TestCase | |
| { | |
| use RefreshDatabase, WithFaker; | |
| public function setUp() :void | |
| { | |
| parent::setUp(); | |
| $this->country = factory(Country::class)->create(); | |
| $this->supplier = factory(Supplier::class)->create(); | |
| $this->user = factory(User::class)->create(); | |
| $this->post = factory(Post::class)->create(); | |
| $this->video = factory(Video::class)->create(); | |
| $this->comment = factory(Comment::class)->create(); | |
| } | |
| // ... | |
| /** @test */ | |
| public function a_comment_can_be_morphed_to_a_video_model() | |
| { | |
| $comment = factory(Comment::class)->create([ | |
| "commentable_id" => $this->video->id, | |
| "commentable_type" => "App\Video", | |
| ]); | |
| $this->assertInstanceOf(Video::class, $comment->commentable); | |
| } | |
| /** @test */ | |
| public function a_comment_can_be_morphed_to_a_post_model() | |
| { | |
| $comment = factory(Comment::class)->create([ | |
| "commentable_id" => $this->post->id, | |
| "commentable_type" => "App\Post", | |
| ]); | |
| $this->assertInstanceOf(Post::class, $comment->commentable); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment