Last active
October 18, 2023 10:42
-
-
Save wanmigs/8ed74633ddff91062f51e8b9aa2778a9 to your computer and use it in GitHub Desktop.
Create, Update, Delete and Fetch TestCase
This file contains 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; | |
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
yes you're right, is this line would do then, right?
$response = $this->deleteJson(route($route, $model->id));
if (! auth()->user()) {
return $response;
}
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));
?
I agree @wrabit, the test should be against data that is changing.
@wrabit thanks man! 😄 you're right!
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
Hello good idea,
But i have a question, in this line:
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.