Last active
August 29, 2015 13:58
-
-
Save laracasts/10337133 to your computer and use it in GitHub Desktop.
Example
This file contains 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 Faker\Factory as Faker; | |
abstract class ApiTester extends TestCase { | |
/** | |
* @var int | |
*/ | |
protected $times = 1; | |
/** | |
* @var Faker | |
*/ | |
protected $fake; | |
/** | |
* Initialize | |
*/ | |
function __construct() | |
{ | |
$this->fake = Faker::create(); | |
} | |
/** | |
* Setup database for each test | |
*/ | |
public function setUp() | |
{ | |
parent::setUp(); | |
$this->app['artisan']->call('migrate'); | |
} | |
/** | |
* Reset environment | |
*/ | |
public function tearDown() | |
{ | |
$this->times = 1; | |
} | |
/** | |
* Number of times to repeat DB insertion | |
* | |
* @param $count | |
* @return $this | |
*/ | |
protected function times($count) | |
{ | |
$this->times = $count; | |
return $this; | |
} | |
/** | |
* Get JSON output from API | |
* | |
* @param $uri | |
* @param string $method | |
* @param array $parameters | |
* @return mixed | |
*/ | |
protected function getJson($uri, $method = 'GET', $parameters = []) | |
{ | |
return json_decode($this->call($method, $uri, $parameters)->getContent()); | |
} | |
/** | |
* Save record to DB table | |
* | |
* @param $type | |
* @param array $fields | |
*/ | |
protected function make($type, array $fields = []) | |
{ | |
while ($this->times--) | |
{ | |
$stub = array_merge($this->getStub(), $fields); | |
$type::create($stub); | |
} | |
} | |
/** | |
* Entity stub | |
* | |
* @return array | |
*/ | |
protected function getStub() | |
{ | |
return []; | |
} | |
/** | |
* Assert object has any number of attributes | |
*/ | |
protected function assertObjectHasAttributes() | |
{ | |
$args = func_get_args(); | |
$object = array_shift($args); | |
foreach ($args as $attribute) | |
{ | |
$this->assertObjectHasAttribute($attribute, $object); | |
} | |
} | |
} |
This file contains 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 | |
class LessonsTest extends ApiTester { | |
/** @test */ | |
public function it_fetches_lessons() | |
{ | |
$this->make('Lesson'); | |
$this->getJson('api/v1/lessons'); | |
$this->assertResponseOk(); | |
} | |
/** @test */ | |
public function it_fetches_a_single_lesson() | |
{ | |
$this->make('Lesson'); | |
$lesson = $this->getJson('api/v1/lessons/1')->data; | |
$this->assertResponseOk(); | |
$this->assertObjectHasAttributes($lesson, 'body', 'active'); | |
} | |
/** @test */ | |
public function it_404s_if_a_lesson_is_not_found() | |
{ | |
$this->getJson('api/v1/lessons/x'); | |
$this->assertResponseStatus(404); | |
} | |
/** @test */ | |
public function it_creates_a_new_lesson_with_valid_params() | |
{ | |
$this->getJson('api/v1/lessons', 'POST', $this->getStub()); | |
$this->assertResponseStatus(201); | |
// Should this process return the id of the new post? | |
// Write a test for it here. | |
} | |
/** @test */ | |
public function it_throws_a_422_if_a_new_lesson_request_fails_validation() | |
{ | |
$json = $this->getJson('api/v1/lessons', 'POST'); | |
$this->assertResponseStatus(422); | |
$this->assertObjectHasAttributes($json, 'error'); | |
} | |
/** | |
* Get a lesson's attributes | |
* | |
* @return array | |
*/ | |
protected function getStub() | |
{ | |
return [ | |
'title' => $this->fake->sentence, | |
'body' => $this->fake->paragraph, | |
'some_bool' => $this->fake->boolean | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment