Skip to content

Instantly share code, notes, and snippets.

@spacesailor24
Last active October 11, 2017 16:44
Show Gist options
  • Save spacesailor24/3cfbc0b48573146d258db333c81cca92 to your computer and use it in GitHub Desktop.
Save spacesailor24/3cfbc0b48573146d258db333c81cca92 to your computer and use it in GitHub Desktop.
Common PHP Unit Test
######################## Example 1 ############################
class ControllerTest extends TestCase
{
use DatabaseTransactions;
....
###############################################################
######################## Example 2 ############################
public function testSomethingYoureTesting()
{
$response = $this->actingAs($this->userWithAllAuthorization())
->get('/some-page');
$response
->assertStatus(200)
->assertSee('<some-component>');
}
###############################################################
######################## Example 3 ############################
public function testModuleComponent()
{
$response = $this->actingAs($this->userWithAllAuthorization())
->get('/uri');
$response
->assertStatus(200)
->assertSee('<component-name>');
}
###############################################################
######################## Example 4 ############################
public function testApiMethod()
{
$response = $this->actingAs($this->userWithAllAuthorization(), 'api')
->json('GET', '/api/rest-of-uri');
$thingYouDid = ModelYouUsed::find(1);
$response->assertStatus(200)
->assertJson([['key' => 'value', 'key' => $thingYouDid->property]]);
}
###############################################################
######################## Example 5 ############################
public function testApiMethod()
{
$response = $this->actingAs($this->userWithNoAuthorization(), 'api')
->json('GET', '/api/rest-of-uri');
$response->assertStatus(403);
}
###############################################################
######################## Example 6 ############################
public function testApiMethod()
{
$response = $this->actingAs($this->userWithNoAuthorization(), 'api')
->json('POST', '/api/rest-of-uri', ['key' => 'badValue']);
$response->assertStatus(422)
->assertJson([
"key" => ["The key field is invalid."],
]);
}
###############################################################
######################## Example 7 ############################
public function testStoreAppointmentBadRequest()
{
$response = $this
->actingAs($this->userWithAllAuthorization(), 'api')
->json('POST', '/api/appointments', ['bad' => 'request']);
$response
->assertStatus(422)
->assertJson([
"assigned_to" => ["The assigned to field is required."],
"label" => ["The label field is required."],
"time" => ["The time field is required."],
]);
}
###############################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment