Created
September 28, 2019 08:49
-
-
Save tonyfrenzy/8d0c649467b4c2960c2addb0ab1e8002 to your computer and use it in GitHub Desktop.
hasOneThrough() Relationship Test (Supplier-History-User) - 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\History; | |
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 SuppliersTest extends TestCase | |
{ | |
use RefreshDatabase, WithFaker; | |
/** @test */ | |
public function suppliers_database_has_expected_columns() | |
{ | |
$this->assertTrue( | |
Schema::hasColumns('suppliers', [ | |
'id', 'name', 'services' | |
]), 1); | |
} | |
/** @test */ | |
public function a_supplier_has_an_history_through_user() | |
{ | |
$supplier = factory(Supplier::class)->create(); | |
$user = factory(User::class)->create(); | |
$history = factory(History::class)->create(['user_id' => $user->id]); | |
// Method 1: | |
$this->assertInstanceOf(History::class, $supplier->userHistory); | |
// Method 2: | |
$this->assertEquals(1, $supplier->userHistory->count()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment