-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from phi-rakib/deposit-policy-validation-observ…
…er-test Deposit policy validation observer test
- Loading branch information
Showing
10 changed files
with
266 additions
and
7 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class StoreDepositRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return Auth::check(); | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'account_id' => 'required|integer|exists:accounts,id', | ||
'deposit_category_id' => 'required|integer|exists:deposit_categories,id', | ||
'payment_method_id' => 'required|integer|exists:payment_methods,id', | ||
'amount' => 'required|numeric', | ||
'deposit_date' => 'required|date', | ||
'notes' => 'nullable', | ||
]; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Observers; | ||
|
||
use App\Models\Deposit; | ||
|
||
class DepositObserver | ||
{ | ||
public function creating(Deposit $deposit) | ||
{ | ||
$deposit->created_by = auth()->id(); | ||
} | ||
|
||
public function updating(Deposit $deposit) | ||
{ | ||
$deposit->updated_by = auth()->id(); | ||
} | ||
|
||
public function deleting(Deposit $deposit) | ||
{ | ||
$deposit->deleted_by = auth()->id(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace App\Policies; | ||
|
||
use App\Models\Deposit; | ||
use App\Models\User; | ||
|
||
class DepositPolicy | ||
{ | ||
/** | ||
* Determine whether the user can view any models. | ||
*/ | ||
public function viewAny(User $user): bool | ||
{ | ||
return $user->can('deposit-list'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can view the model. | ||
*/ | ||
public function view(User $user, Deposit $deposit): bool | ||
{ | ||
return $user->can('deposit-list'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can create models. | ||
*/ | ||
public function create(User $user): bool | ||
{ | ||
return $user->can('deposit-create'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can update the model. | ||
*/ | ||
public function update(User $user, Deposit $deposit): bool | ||
{ | ||
return $user->can('deposit-edit'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can delete the model. | ||
*/ | ||
public function delete(User $user, Deposit $deposit): bool | ||
{ | ||
return $user->can('deposit-delete'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can restore the model. | ||
*/ | ||
public function restore(User $user, Deposit $deposit): bool | ||
{ | ||
return $user->can('deposit-restore'); | ||
} | ||
|
||
/** | ||
* Determine whether the user can permanently delete the model. | ||
*/ | ||
public function forceDelete(User $user, Deposit $deposit): bool | ||
{ | ||
return $user->can('deposit-force-delete'); | ||
} | ||
} |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Deposit; | ||
use App\Models\User; | ||
use Database\Seeders\PermissionSeeder; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Laravel\Sanctum\Sanctum; | ||
use Tests\TestCase; | ||
|
||
class DepositTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
private $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(); | ||
} | ||
|
||
public function test_user_can_deposit() | ||
{ | ||
$this->user->givePermissionTo('deposit-create'); | ||
|
||
$deposit = Deposit::factory()->make(); | ||
|
||
$response = $this->post(route('deposits.store'), $deposit->toArray()); | ||
|
||
$response->assertStatus(201); | ||
|
||
$response->assertJson([ | ||
'message' => "$deposit->amount Deposited in account $deposit->account->name", | ||
]); | ||
|
||
$this->assertDatabaseHas('deposits', [ | ||
'account_id' => $deposit->account_id, | ||
'amount' => $deposit->amount, | ||
'deposit_date' => $deposit->deposit_date, | ||
]); | ||
} | ||
|
||
public function test_user_can_update_deposit() | ||
{ | ||
$this->user->givePermissionTo('deposit-edit'); | ||
|
||
$deposit = Deposit::factory()->create(); | ||
|
||
$response = $this->put(route('deposits.update', $deposit), [ | ||
'amount' => 5000, | ||
]); | ||
|
||
$response->assertStatus(200); | ||
|
||
$this->assertDatabaseHas('deposits', [ | ||
'id' => $deposit->id, | ||
'amount' => 5000, | ||
]); | ||
} | ||
|
||
public function test_user_can_delete_deposit() | ||
{ | ||
$this->user->givePermissionTo('deposit-delete'); | ||
|
||
$deposit = Deposit::factory()->create(); | ||
|
||
$response = $this->delete(route('deposits.destroy', $deposit)); | ||
|
||
$response->assertStatus(204); | ||
|
||
$this->assertSoftDeleted('deposits', [ | ||
'id' => $deposit->id, | ||
]); | ||
} | ||
|
||
public function test_user_can_view_all_deposits() | ||
{ | ||
$this->user->givePermissionTo('deposit-list'); | ||
|
||
Deposit::factory(10)->create(); | ||
|
||
$response = $this->get(route('deposits.index')); | ||
|
||
$response->assertStatus(200); | ||
|
||
$response->assertJsonCount(10, 'data'); | ||
} | ||
|
||
public function test_user_can_view_deposit() | ||
{ | ||
$this->user->givePermissionTo('deposit-list'); | ||
|
||
$deposit = Deposit::factory()->create(); | ||
|
||
$response = $this->get(route('deposits.show', $deposit->id)); | ||
|
||
$response->assertStatus(200); | ||
|
||
$response->assertJson($deposit->toArray()); | ||
} | ||
} |