Skip to content

Instantly share code, notes, and snippets.

@tonyfrenzy
Created September 28, 2019 15:22
Show Gist options
  • Save tonyfrenzy/6fbeddec373a0491ff592ced01a0d723 to your computer and use it in GitHub Desktop.
Save tonyfrenzy/6fbeddec373a0491ff592ced01a0d723 to your computer and use it in GitHub Desktop.
morphOne() Polymorphic Relationship Test (User-Image) - Testing Model Relationships in Laravel
<?php
namespace Tests\Unit;
use App\Country;
use App\Image;
use App\Post;
use App\Supplier;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class UserTest 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->image = factory(Image::class)->create([
"imageable_id" => $this->user->id,
"imageable_type" => "App\User",
]);
}
// ...
/** @test */
public function a_user_morphs_one_image()
{
$this->assertInstanceOf(Image::class, $this->user->image);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment