Skip to content

Instantly share code, notes, and snippets.

@wanmigs
Last active October 18, 2023 10:42
Show Gist options
  • 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 ]);
}
}
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