Last active
November 5, 2018 04:43
-
-
Save jjcodes78/af1b2eb96cc43d68884c60ff20119942 to your computer and use it in GitHub Desktop.
Simple (Client x Employer x Occupation) pivot testing
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\Feature; | |
use App\Client; | |
use App\Employer; | |
use App\Occupation; | |
use Tests\TestCase; | |
use Illuminate\Foundation\Testing\WithFaker; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
class PivotTest extends TestCase | |
{ | |
use RefreshDatabase; | |
protected $employer; | |
protected $client; | |
protected $client2; | |
protected $occupation; | |
protected $occupation2; | |
protected $occupation3; | |
protected function buildData() | |
{ | |
$this->employer = factory(Employer::class)->create(); | |
$this->client = factory(Client::class)->create(); | |
$this->client2 = factory(Client::class)->create(); | |
$this->occupation = factory(Occupation::class)->create(); | |
$this->occupation2 = factory(Occupation::class)->create(); | |
$this->occupation3 = factory(Occupation::class)->create(); | |
$this->client->employers()->attach($this->employer); | |
$this->client2->employers()->attach($this->employer); | |
$this->employer->occupations()->attach([ | |
$this->occupation->id => ['client_id' => $this->client->id], | |
$this->occupation2->id => ['client_id' => $this->client->id] | |
]); | |
$this->employer->occupations()->attach([ | |
$this->occupation2->id => ['client_id' => $this->client2->id], | |
$this->occupation3->id => ['client_id' => $this->client2->id] | |
]); | |
} | |
/** @test **/ | |
public function a_employer_can_belongs_to_many_clients() | |
{ | |
$employer = factory(Employer::class)->create(); | |
$client = factory(Client::class)->create(); | |
$client2 = factory(Client::class)->create(); | |
$client->employers()->attach($employer); | |
$client2->employers()->attach($employer); | |
$this->assertDatabaseHas('client_employer', [ | |
'client_id' => 1, 'employer_id' => 1 | |
]); | |
$this->assertDatabaseHas('client_employer', [ | |
'client_id' => 2, 'employer_id' => 1 | |
]); | |
} | |
/** @test **/ | |
public function a_employer_can_have_many_occupations_from_many_clients() | |
{ | |
$this->buildData(); | |
$this->assertCount(4, $this->employer->occupations); | |
$this->assertCount(2, $this->employer->occupations()->wherePivot('client_id', $this->client->id)->get()); | |
$this->assertCount(2, $this->employer->occupations()->wherePivot('client_id', $this->client2->id)->get()); | |
} | |
/** @test **/ | |
public function a_employer_occupation_from_a_client_can_be_removed() | |
{ | |
$this->buildData(); | |
// Here we assert that employer have 2 occupations on each client | |
$this->assertCount(2, $this->employer->occupations()->wherePivot('client_id', $this->client->id)->get()); | |
$this->assertCount(2, $this->employer->occupations()->wherePivot('client_id', $this->client2->id)->get()); | |
// Employer have 4 records into pivot with 3 occupations | |
// we can make possible remove one occupation from this employer | |
// using native laravel API | |
$this->employer->occupations() | |
->wherePivot('client_id', $this->client->id) | |
->detach($this->occupation2->id); | |
// now this employer has one occupation on $client | |
$this->assertCount(1, $this->employer->occupations()->wherePivot('client_id', $this->client->id)->get()); | |
// and remains 2 occupations on $client2 | |
$this->assertCount(2, $this->employer->occupations()->wherePivot('client_id', $this->client2->id)->get()); | |
} | |
/** @test **/ | |
public function a_client_can_filter_employers_by_occupation() | |
{ | |
$this->buildData(); | |
$newEmployer = factory(Employer::class)->create(['name' => 'NewEmployer']); | |
$this->client->employers()->attach($newEmployer); | |
$newEmployer->occupations()->attach([ | |
$this->occupation->id => ['client_id' => $this->client->id] | |
]); | |
$newEmployer2 = factory(Employer::class)->create(); | |
$this->client2->employers()->attach($newEmployer2); | |
$newEmployer2->occupations()->attach([ | |
$this->occupation3->id => ['client_id' => $this->client->id] | |
]); | |
$employers = $this->client->employers()->whereHas('occupations', function ($query) { | |
$query->where('occupation_id', 1); | |
})->get(); | |
$this->assertCount(2, $employers); | |
$this->assertEquals('NewEmployer', $employers->where('name', 'NewEmployer')->first()->name); | |
$this->assertNull($employers->where('name', $newEmployer2->name)->first()); | |
} | |
/** @test **/ | |
public function it_list_clients_that_have_employers_of_a_occupation() | |
{ | |
$this->buildData(); | |
// Builds a single match result | |
$newEmployer = factory(Employer::class)->create(['name' => 'NewEmployer']); | |
$newClient = factory(Client::class)->create(); | |
$newOccupation = factory(Occupation::class)->create(); | |
$newClient->employers()->attach($newEmployer); | |
$newEmployer->occupations()->attach([ | |
$newOccupation->id => ['client_id' => $newClient] | |
]); | |
$clients = Client::whereHas('employers.occupations', function ($query) { | |
$query->where('occupation_id', 4); | |
})->get(); | |
$this->assertCount(1, $clients); | |
$this->assertEquals($newClient->name, $clients[0]->name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment