Skip to content

Instantly share code, notes, and snippets.

@tonyfrenzy
Last active June 14, 2020 22:48
Show Gist options
  • Save tonyfrenzy/5cfa961b1108a075c9ffa93abad66123 to your computer and use it in GitHub Desktop.
Save tonyfrenzy/5cfa961b1108a075c9ffa93abad66123 to your computer and use it in GitHub Desktop.
morphedByMany() Polymorphic Relationship Test (Tag-Posts/Videos) - Testing Model Relationships in Laravel
<?php
namespace Tests\Unit;
// ...
use App\User;
use App\Post;
use App\Tag;
use App\Video;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
class TagsTest extends TestCase
{
use RefreshDatabase, WithFaker;
public function setUp() :void
{
parent::setUp();
// ...
$this->user = factory(User::class)->create();
$this->post = factory(Post::class)->create(['user_id' => $this->user->id]);
$this->video = factory(Video::class)->create(['user_id' => $this->user->id]);
$this->tag = factory(Tag::class)->create();
}
/** @test */
public function tags_database_has_expected_columns()
{
$this->assertTrue(Schema::hasColumns('tags',
[
'id', 'name', 'description'
]), 1);
}
/** @test */
public function a_tag_can_be_assigned_to_or_morphed_by_many_videos()
{
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $this->tag->videos);
}
/** @test */
public function a_tag_can_be_assigned_to_or_morphed_by_many_posts()
{
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $this->tag->posts);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment