116 lines
4.1 KiB
PHP
116 lines
4.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\Discount;
|
|
use App\Models\User;
|
|
use Domain\Enums\DiscountType;
|
|
use Domain\Enums\UserRole;
|
|
use Domain\Models\ValueObjects\DiscountValue;
|
|
|
|
describe('DiscountsController', function () {
|
|
beforeEach(function () {
|
|
$this->user = User::factory()->create(['role' => UserRole::SuperAdmin]);
|
|
$this->actingAs($this->user, 'backend');
|
|
});
|
|
|
|
it('can access index', function () {
|
|
$response = $this->get('/bo/discounts');
|
|
expect($response->status())->toBe(200);
|
|
$response->assertViewIs('bo.discounts.index');
|
|
});
|
|
|
|
it('can access create', function () {
|
|
$response = $this->get('/bo/discounts/create');
|
|
expect($response->status())->toBe(200);
|
|
$response->assertViewIs('bo.discounts.create');
|
|
});
|
|
|
|
it('can store a discount', function () {
|
|
$data = [
|
|
'type' => DiscountType::PERCENT->value,
|
|
'value' => 15,
|
|
'is_old_price_displayed' => true,
|
|
'date_start' => null,
|
|
'date_end' => null,
|
|
'client_timezone' => 'UTC',
|
|
'associated_products' => [],
|
|
];
|
|
|
|
$response = $this->post('/bo/discounts', $data);
|
|
|
|
$discount = Discount::first();
|
|
expect($discount)->not->toBeNull()
|
|
->and($discount->type)->toBe(DiscountType::PERCENT);
|
|
|
|
$response->assertRedirect("/bo/discounts/{$discount->id}/edit");
|
|
});
|
|
|
|
it('can access edit', function () {
|
|
$discount = new Discount();
|
|
$discount->type = DiscountType::PERCENT;
|
|
$discount->value = DiscountValue::fromPercentage(10);
|
|
$discount->is_old_price_displayed = true;
|
|
$discount->save();
|
|
|
|
$response = $this->get("/bo/discounts/{$discount->id}/edit");
|
|
expect($response->status())->toBe(200);
|
|
$response->assertViewIs('bo.discounts.edit');
|
|
});
|
|
|
|
it('can update a discount', function () {
|
|
$discount = new Discount();
|
|
$discount->type = DiscountType::PERCENT;
|
|
$discount->value = DiscountValue::fromPercentage(10);
|
|
$discount->is_old_price_displayed = true;
|
|
$discount->save();
|
|
|
|
$data = [
|
|
'type' => DiscountType::PERCENT->value,
|
|
'value' => 20,
|
|
'is_old_price_displayed' => false,
|
|
'date_start' => null,
|
|
'date_end' => null,
|
|
'client_timezone' => 'UTC',
|
|
'associated_products' => [],
|
|
];
|
|
|
|
$response = $this->put("/bo/discounts/{$discount->id}", $data);
|
|
|
|
$discount->refresh();
|
|
expect($discount->is_old_price_displayed)->toBeFalse()
|
|
->and($discount->value->getPercentage())->toBe(20);
|
|
|
|
$response->assertRedirect("/bo/discounts/{$discount->id}/edit");
|
|
});
|
|
|
|
it('can delete a discount', function () {
|
|
$discount = new Discount();
|
|
$discount->type = DiscountType::PERCENT;
|
|
$discount->value = DiscountValue::fromPercentage(10);
|
|
$discount->is_old_price_displayed = true;
|
|
$discount->save();
|
|
|
|
$response = $this->delete("/bo/discounts/{$discount->id}");
|
|
// THEN
|
|
expect(Discount::find($discount->id))->toBeNull();
|
|
$response->assertRedirect('/bo/discounts');
|
|
});
|
|
|
|
describe('authorization rejections', function () {
|
|
it('redirects unauthenticated guest to login', function () {
|
|
auth('backend')->logout();
|
|
|
|
$response = $this->get('/bo/discounts');
|
|
$response->assertRedirect('/bo/login');
|
|
});
|
|
|
|
it('denies access to unauthorized user with customer role', function () {
|
|
$user = User::factory()->make(['role' => UserRole::Customer]);
|
|
$this->actingAs($user, 'backend');
|
|
|
|
$response = $this->get('/bo/discounts');
|
|
expect($response->status())->toBe(403);
|
|
});
|
|
});
|
|
});
|