Skip to content

Commit

Permalink
Merge pull request #23 from phi-rakib/22-refractor-testcase-class
Browse files Browse the repository at this point in the history
code refactored: moved common logic of all testcases to parent testcase
  • Loading branch information
phi-rakib authored Jun 3, 2024
2 parents 7cb352a + cac8cd7 commit 9351ec0
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 71 deletions.
12 changes: 3 additions & 9 deletions tests/Feature/AccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,21 @@

use App\Models\Account;
use App\Models\User;
use Database\Seeders\PermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;

class AccountTest extends TestCase
{
use RefreshDatabase;

private $user;
private User $user;

public function setUp(): void
{
parent::setUp();

$this->user = User::factory()->create();

Sanctum::actingAs($this->user);

$this->artisan('db:seed', ['--class' => PermissionSeeder::class]);
$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
$this->user = Auth::user();
}

public function test_user_can_create_an_account()
Expand Down
12 changes: 3 additions & 9 deletions tests/Feature/DepositCategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,21 @@

use App\Models\DepositCategory;
use App\Models\User;
use Database\Seeders\PermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;

class DepositCategoryTest extends TestCase
{
use RefreshDatabase;

private $user;
private User $user;

public function setUp(): void
{
parent::setUp();

$this->user = User::factory()->create();

Sanctum::actingAs($this->user, ['*']);

$this->artisan('db:seed', ['--class' => PermissionSeeder::class]);
$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
$this->user = Auth::user();
}

public function test_user_can_create_deposit_category(): void
Expand Down
12 changes: 3 additions & 9 deletions tests/Feature/DepositTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,21 @@

use App\Models\Deposit;
use App\Models\User;
use Database\Seeders\PermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;

class DepositTest extends TestCase
{
use RefreshDatabase;

private $user;
private User $user;

public function setUp(): void
{
parent::setUp();

$this->user = User::factory()->create();

Sanctum::actingAs($this->user);

$this->artisan('db:seed', ['--class' => PermissionSeeder::class]);
$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
$this->user = Auth::user();
}

public function test_user_can_deposit()
Expand Down
19 changes: 0 additions & 19 deletions tests/Feature/ExampleTest.php

This file was deleted.

12 changes: 3 additions & 9 deletions tests/Feature/ExpenseCategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,21 @@

use App\Models\ExpenseCategory;
use App\Models\User;
use Database\Seeders\PermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;

class ExpenseCategoryTest extends TestCase
{
use RefreshDatabase;

private $user;
private User $user;

public function setUp(): void
{
parent::setUp();

$this->user = User::factory()->create();
Sanctum::actingAs($this->user);

$this->artisan('db:seed', ['--class' => PermissionSeeder::class]);

$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
$this->user = Auth::user();
}

public function test_user_can_create_expense_category()
Expand Down
11 changes: 3 additions & 8 deletions tests/Feature/ExpenseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,21 @@

use App\Models\Expense;
use App\Models\User;
use Database\Seeders\PermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;

class ExpenseTest extends TestCase
{
use RefreshDatabase;

private $user;
private User $user;

public function setUp(): void
{
parent::setUp();

$this->user = User::factory()->create();
Sanctum::actingAs($this->user);

$this->artisan('db:seed', ['--class' => PermissionSeeder::class]);
$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
$this->user = Auth::user();
}

public function test_user_can_create_expense()
Expand Down
11 changes: 3 additions & 8 deletions tests/Feature/PaymentMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,21 @@

use App\Models\PaymentMethod;
use App\Models\User;
use Database\Seeders\PermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;

class PaymentMethodTest extends TestCase
{
use RefreshDatabase;

private $user;
private User $user;

public function setUp(): void
{
parent::setUp();

$this->user = User::factory()->create();
Sanctum::actingAs($this->user);

$this->artisan('db:seed', ['--class' => PermissionSeeder::class]);
$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
$this->user = Auth::user();
}

public function test_user_can_create_payment_method()
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Tests;

use App\Models\User;
use Database\Seeders\PermissionSeeder;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Laravel\Sanctum\Sanctum;

abstract class TestCase extends BaseTestCase
{
Expand All @@ -11,5 +14,10 @@ public function setUp(): void
parent::setUp();

$this->withoutExceptionHandling();

Sanctum::actingAs(User::factory()->create());

$this->artisan('db:seed', ['--class' => PermissionSeeder::class]);
$this->app->make(\Spatie\Permission\PermissionRegistrar::class)->forgetCachedPermissions();
}
}

0 comments on commit 9351ec0

Please sign in to comment.