Basically, I have to write tests for many Laravel controllers, most of which are CRUD (read, store, update), and most of the logic is placed inside these (Inherited code, not mine).
What I need to do is automate testing from a user perspective. So I need to hit all the endpoints and test the real database and see if everything works out well.
I have almost no testing experience, but from what I collect, the controllers should be tested using integration / acceptance testing. Now I have done a great job with testing reading methods by extending the Laravel TestCase, here is an example:
class SongsTest extends TestCase
{
public function testBasicIndex()
{
$arguments = [];
$response = $this->call('GET', '/songs', $arguments);
$this->assertResponseOk();
$this->seeJson();
}
public function testSearchIndex($query)
{
$arguments = ['srquery' => $query];
$response = $this->call('GET', '/songs', $arguments);
$this->assertResponseOk();
$this->seeJson();
}
public function providerSearchQuery()
{
return array(
array('a'),
array('as%+='),
array('test?Someqsdag8hj$%$')
);
}
public function testGetSongsById()
{
$id = 1;
$response = $this->call('GET', '/songs/' . $id);
$this->assertContains($response->getStatusCode(), [200, 404]);
$this->seeJson();
if($response->getStatusCode() == 404)
{
$content = json_decode($response->getContent());
$this->assertContains($content->message[0], ['Song not found', 'Item not active']);
}
}
}
, 200, JSON . .
:
, , UserController , . TokensController , - .
:
: UserController ( ), TokensController , , .
, .