Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Integration Testing Repositories That Use Eloquent (with Codeception)
<?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);
}
}
<?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
You can’t perform that action at this time.