Created
September 28, 2019 15:22
-
-
Save tonyfrenzy/6fbeddec373a0491ff592ced01a0d723 to your computer and use it in GitHub Desktop.
morphOne() Polymorphic Relationship Test (User-Image) - 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\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