Skip to content

Instantly share code, notes, and snippets.

@tonyfrenzy
Created September 28, 2019 08:49
Show Gist options
  • Save tonyfrenzy/8d0c649467b4c2960c2addb0ab1e8002 to your computer and use it in GitHub Desktop.
Save tonyfrenzy/8d0c649467b4c2960c2addb0ab1e8002 to your computer and use it in GitHub Desktop.
hasOneThrough() Relationship Test (Supplier-History-User) - Testing Model Relationships in Laravel
<?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