Skip to content

Instantly share code, notes, and snippets.

@wanmigs
Last active October 18, 2023 10:42
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wanmigs/8ed74633ddff91062f51e8b9aa2778a9 to your computer and use it in GitHub Desktop.
Save wanmigs/8ed74633ddff91062f51e8b9aa2778a9 to your computer and use it in GitHub Desktop.
Create, Update, Delete and Fetch TestCase
<?php
namespace Tests;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use RefreshDatabase;
protected $base_route = null;
protected $base_model = null;
protected function signIn($user = null)
{
$user = $user ?? create(\App\User::class);
$this->actingAs($user);
return $this;
}
protected function setBaseRoute($route)
{
$this->base_route = $route;
}
protected function setBaseModel($model)
{
$this->base_model = $model;
}
protected function create($attributes = [], $model = '', $route = '')
{
$this->withoutExceptionHandling();
$route = $this->base_route ? "{$this->base_route}.store" : $route;
$model = $this->base_model ?? $model;
$attributes = raw($model, $attributes);
if (! auth()->user()) {
$this->expectException(\Illuminate\Auth\AuthenticationException::class);
}
$response = $this->postJson(route($route), $attributes)->assertSuccessful();
$model = new $model;
$this->assertDatabaseHas($model->getTable(), $attributes);
return $response;
}
protected function update($attributes = [], $model = '', $route = '')
{
$this->withoutExceptionHandling();
$route = $this->base_route ? "{$this->base_route}.update" : $route;
$model = $this->base_model ?? $model;
$model = create($model);
if (! auth()->user()) {
$this->expectException(\Illuminate\Auth\AuthenticationException::class);
}
$response = $this->patchJson(route($route, $model->id), $attributes);
tap($model->fresh(), function ($model) use ($attributes) {
collect($attributes)->each(function($value, $key) use ($model) {
$this->assertEquals($value, $model[$key]);
});
});
return $response;
}
protected function destroy($model = '', $route = '')
{
$this->withoutExceptionHandling();
$route = $this->base_route ? "{$this->base_route}.destroy" : $route;
$model = $this->base_model ?? $model;
$model = create($model);
if (! auth()->user()) {
$this->expectException(\Illuminate\Auth\AuthenticationException::class);
}
$response = $this->deleteJson(route($route, $model->id));
$this->assertDatabaseMissing($model->getTable(), $model->toArray());
return $response;
}
public function multipleDelete ($model = '', $route = '')
{
$this->withoutExceptionHandling();
$route = $this->base_route ? "{$this->base_route}.destroyAll" : $route;
$model = $this->base_model ?? $model;
$model = create($model, [], 5);
$ids = $model->map(function ($item, $key) {
return $item->id;
});
return $this->deleteJson(route($route), ['ids' => $ids ]);
}
}
@sagitarius29
Copy link

Hello good idea,

But i have a question, in this line:

if (! auth()->user()) {
            $this->expectException(\Illuminate\Auth\AuthenticationException::class);
        }

Being a test, Doesn't that mean it should be stop and not continue his execution like asserted test?

Example: I execute test without user authenticated, the test return success, it's not correct.

regards.

@wanmigs
Copy link
Author

wanmigs commented Nov 3, 2019

@sagitarius29
yes you're right, is this line would do then, right?

$response = $this->deleteJson(route($route, $model->id));

if (! auth()->user()) {
    return $response;
}

@wrabit
Copy link

wrabit commented Jan 7, 2020

I don't think update() is testing correctly. You are creating the model already with changed attributes:

        $model = create($model, $attributes); 

Then you are just patching the model again, shouldn't we be patching the attributes over the default factory model? So instead of:

        ...
        $model = create($model, $attributes); 
        ...
        $response = $this->patchJson(route($route, $model->id), $model->toArray());

shouldn't it be:

        ...
        $model = create($model); 
        ....
        $response = $this->patchJson(route($route, $model->id), $attributes));

?

@awcodes
Copy link

awcodes commented Jan 29, 2020

I agree @wrabit, the test should be against data that is changing.

@wanmigs
Copy link
Author

wanmigs commented Feb 7, 2020

@wrabit thanks man! 😄 you're right!

Copy link

ghost commented Mar 6, 2020

image
Please Help ???

@wanmigs
Copy link
Author

wanmigs commented Mar 6, 2020

Hi @r4rishabhgupta, replace use PHPUnit\Framework\Testcase to use Tests\TestCase;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment