Last active
May 19, 2018 15:11
-
-
Save laracasts/d5945d3fa19de7efb7f9 to your computer and use it in GitHub Desktop.
Integration Testing Repositories That Use Eloquent (with Codeception)
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 Larabook\Statuses; | |
use Larabook\Users\User; | |
class StatusRepository { | |
/** | |
* Get all statuses associated with a user. | |
* | |
* @param User $user | |
* @return mixed | |
*/ | |
public function getAllForUser(User $user) | |
{ | |
return $user->statuses()->get(); | |
} | |
/** | |
* Save a new status for a user. | |
* | |
* @param Status $status | |
* @param $userId | |
* @return mixed | |
*/ | |
public function save(Status $status, $userId) | |
{ | |
return User::findOrFail($userId) | |
->statuses() | |
->save($status); | |
} | |
} |
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 | |
use Larabook\Statuses\StatusRepository; | |
use Laracasts\TestDummy\Factory as TestDummy; | |
class StatusRepositoryTest extends \Codeception\TestCase\Test { | |
/** | |
* @var \IntegrationTester | |
*/ | |
protected $tester; | |
/** | |
* @var Status Repository | |
*/ | |
protected $repo; | |
/** | |
* Before each test, do... | |
*/ | |
protected function _before() | |
{ | |
$this->repo = new StatusRepository; | |
} | |
/** @test */ | |
public function it_gets_all_statuses_for_a_user() | |
{ | |
// Given I have two users | |
$users = TestDummy::times(2)->create('Larabook\Users\User'); | |
// And statuses for both of them | |
$statuses = TestDummy::times(2)->create('Larabook\Statuses\Status', ['user_id' => $users[0]->id]); | |
TestDummy::times(2)->create('Larabook\Statuses\Status', ['user_id' => $users[1]->id]); | |
// When I fetch statuses for one user | |
$statusesForUser = $this->repo->getAllForUser($users[0]); | |
// Then I should receive only the relevant ones | |
$this->assertCount(2, $statusesForUser); | |
$this->assertEquals($statuses[0], $statusesForUser[0]); | |
$this->assertEquals($statuses[1], $statusesForUser[1]); | |
} | |
/** @test */ | |
public function it_saves_a_status_for_a_user() | |
{ | |
// Given I have an unsaved status | |
$status = TestDummy::build('Larabook\Statuses\Status', [ | |
'user_id' => null, | |
'body' => 'My status' | |
]); | |
// And an existing user | |
$user = TestDummy::create('Larabook\Users\User'); | |
// When I try to persist this status | |
$this->repo->save($status, $user->id); | |
// Then it should be saved, and have the correct user_id | |
$this->tester->seeRecord('statuses', [ | |
'body' => 'My status', | |
'user_id' => $user->id | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment