Last active
October 11, 2017 16:44
-
-
Save spacesailor24/3cfbc0b48573146d258db333c81cca92 to your computer and use it in GitHub Desktop.
Common PHP Unit Test
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
######################## 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