Last active
May 7, 2020 23:35
-
-
Save darklight9811/9c2df9920ec8f05c0837cf37d00eda60 to your computer and use it in GitHub Desktop.
Laravel default model resource 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
| <?php | |
| namespace Tests; | |
| //General | |
| use Laravel\Sanctum\Sanctum; | |
| use Illuminate\Foundation\Testing\TestCase as BaseTestCase; | |
| use Model\User; | |
| abstract class DefaultResource extends BaseTestCase { | |
| //------------------------------------------------- | |
| // Variables | |
| //------------------------------------------------- | |
| public $user = User::class; | |
| //------------------------------------------------- | |
| // Trait methods | |
| //------------------------------------------------- | |
| use CreatesApplication; | |
| //------------------------------------------------- | |
| // Login methods | |
| //------------------------------------------------- | |
| public function loginAdministratorUser () { | |
| $user = new $this->user([ | |
| 'id' => 1, | |
| 'name' => 'Administrator', | |
| "email" => "test@email.com", | |
| "type" => "administrator", | |
| ]); | |
| $user->save(); | |
| //Mocking | |
| $this->be($user); | |
| $this->actingAs($user); | |
| Sanctum::actingAs($user); | |
| } | |
| public function loginNormalUser () { | |
| $user = new $this->user([ | |
| 'id' => 1, | |
| 'name' => 'User', | |
| "email" => "test@email.com", | |
| "type" => "user" | |
| ]); | |
| $user->save(); | |
| //Mocking | |
| $this->be($user); | |
| $this->actingAs($user); | |
| Sanctum::actingAs($user); | |
| } | |
| //------------------------------------------------- | |
| // Main methods | |
| //------------------------------------------------- | |
| //Run before every test | |
| public function setUp () : void { | |
| parent::setUp(); | |
| //Skip if missing necessary data | |
| if (!isset($this->model)) | |
| $this->markTestSkipped("Missing model to test"); | |
| if (!isset($this->route)) | |
| $this->markTestSkipped("Missing route to test"); | |
| if (!isset($this->data) && !(isset($this->insertData) && isset($this->updateData))) | |
| $this->markTestSkipped("Missing data to interact with model"); | |
| //Create user | |
| if (!isset($this->adminUser) || $this->adminUser === false) | |
| $this->loginNormalUser(); | |
| else | |
| $this->loginAdministratorUser(); | |
| } | |
| public function tearDown () : void { | |
| //Delete model | |
| $this->model::clear(); | |
| } | |
| //------------------------------------------------- | |
| // View tests | |
| //------------------------------------------------- | |
| //List | |
| public function testListModels() { | |
| //Test | |
| $response = $this->get($this->route); | |
| //Assertions | |
| $response->assertJsonStructure([ "data" ]); | |
| } | |
| //Show | |
| public function testDisplayModel() { | |
| //Prepare | |
| $object = factory($this->model)->Create(); | |
| $object->save(); | |
| $id = $object->{$object->getKeyName()}; | |
| //Test | |
| $response = $this->get($this->route."/".$id); | |
| //Assertions | |
| $response->assertStatus(200); | |
| $response->assertJsonStructure([ "data" ]); | |
| } | |
| //------------------------------------------------- | |
| // Insert tests | |
| //------------------------------------------------- | |
| //Insert full | |
| public function testAddModel () { | |
| $data = isset($this->insertData) ? $this->insertData : $this->data; | |
| //Test | |
| $response = $this->post($this->route, $data); | |
| //Assertions | |
| $response->assertStatus(201); | |
| $response->assertJsonStructure([ "data" ]); | |
| } | |
| //------------------------------------------------- | |
| // Update tests | |
| //------------------------------------------------- | |
| //Update full | |
| public function testUpdateModel () { | |
| $data = isset($this->insertData) ? $this->insertData : $this->data; | |
| //Prepare | |
| $object = factory($this->model)->Create(); | |
| $object->save(); | |
| $id = $object->{$object->getKeyName()}; | |
| //Test | |
| if (isset($this->singleResource) && $this->singleResource == true) | |
| $response = $this->patch($this->route, $data); | |
| else | |
| $response = $this->patch($this->route."/".$id, $data); | |
| //Assertions | |
| $response->assertStatus(200); | |
| $response->assertJsonStructure([ "data" ]); | |
| } | |
| //------------------------------------------------- | |
| // Delete tests | |
| //------------------------------------------------- | |
| //Delete | |
| public function testDeleteModel () { | |
| //Prepare | |
| $object = factory($this->model)->Create(); | |
| $object->save(); | |
| $id = $object->{$object->getKeyName()}; | |
| //Test | |
| $response = $this->delete($this->route."/".$id); | |
| //Assertions | |
| $response->assertStatus(200); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment