177 lines
6.4 KiB
PHP
177 lines
6.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Http\Requests\Basket\AddBasketRequest;
|
|
use App\Models\Product;
|
|
use App\Models\Range;
|
|
use Domain\Enums\ProductBrand;
|
|
use Domain\Enums\SellUnit;
|
|
use Domain\Enums\TaxRate;
|
|
use Domain\Enums\Units\InvoicingUnit;
|
|
use Domain\Models\ValueObjects\Entities\ProductId;
|
|
use Illuminate\Support\Facades\Gate;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Money\Currency;
|
|
use Money\Money;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
describe('AddBasketRequest', function () {
|
|
it('authorizes when product exists and gate allows', function () {
|
|
// GIVEN
|
|
$range = Range::first() ?? Range::forceCreate(['name' => 'Test', 'slug' => 'test', 'order' => 1, 'picture_alt' => '', 'picture_title' => '']);
|
|
$product = Product::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'name' => 'Test',
|
|
'slug' => Uuid::uuid7()->toString(),
|
|
'brand' => ProductBrand::cases()[0]->value,
|
|
'description' => 'Test',
|
|
'price' => new Money(100, resolve(Currency::class)),
|
|
'invoicing_unit' => InvoicingUnit::cases()[0]->value,
|
|
'tax_rate' => TaxRate::cases()[0]->value,
|
|
'stock' => 10,
|
|
'picture_alt' => '',
|
|
'picture_title' => '',
|
|
'picture_large_alt' => '',
|
|
'picture_large_title' => '',
|
|
'sell_unit' => SellUnit::cases()[0]->value,
|
|
'status' => true,
|
|
'star' => false,
|
|
'weight_variable' => false,
|
|
'range_id' => $range->id,
|
|
]);
|
|
|
|
Gate::shouldReceive('allows')
|
|
->with('addToBasket', \Mockery::on(fn ($p) => $p->id === $product->id))
|
|
->andReturn(true);
|
|
|
|
$request = new AddBasketRequest();
|
|
$request->initialize();
|
|
|
|
// WHEN
|
|
$request->merge(['rowId' => $product->id]);
|
|
|
|
// THEN
|
|
expect($request->authorize())->toBeTrue();
|
|
});
|
|
|
|
it('does not authorize when product does not exist', function () {
|
|
// GIVEN
|
|
$request = new AddBasketRequest();
|
|
$request->initialize();
|
|
|
|
// WHEN
|
|
$request->merge(['rowId' => Uuid::uuid7()->toString()]);
|
|
|
|
// THEN
|
|
expect($request->authorize())->toBeFalse();
|
|
});
|
|
|
|
it('does not authorize when gate denies', function () {
|
|
// GIVEN
|
|
$range = Range::first() ?? Range::forceCreate(['name' => 'Test', 'slug' => 'test', 'order' => 1, 'picture_alt' => '', 'picture_title' => '']);
|
|
$product = Product::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'name' => 'Test',
|
|
'slug' => Uuid::uuid7()->toString(),
|
|
'brand' => ProductBrand::cases()[0]->value,
|
|
'description' => 'Test',
|
|
'price' => new Money(100, resolve(Currency::class)),
|
|
'invoicing_unit' => InvoicingUnit::cases()[0]->value,
|
|
'tax_rate' => TaxRate::cases()[0]->value,
|
|
'stock' => 10,
|
|
'picture_alt' => '',
|
|
'picture_title' => '',
|
|
'picture_large_alt' => '',
|
|
'picture_large_title' => '',
|
|
'sell_unit' => SellUnit::cases()[0]->value,
|
|
'status' => true,
|
|
'star' => false,
|
|
'weight_variable' => false,
|
|
'range_id' => $range->id,
|
|
]);
|
|
|
|
Gate::shouldReceive('allows')
|
|
->with('addToBasket', \Mockery::any())
|
|
->andReturn(false);
|
|
|
|
$request = new AddBasketRequest();
|
|
$request->initialize();
|
|
|
|
// WHEN
|
|
$request->merge(['rowId' => $product->id]);
|
|
|
|
// THEN
|
|
expect($request->authorize())->toBeFalse();
|
|
});
|
|
|
|
it('validates correct rules', function () {
|
|
// GIVEN
|
|
$range = Range::first() ?? Range::forceCreate(['name' => 'Test', 'slug' => 'test', 'order' => 1, 'picture_alt' => '', 'picture_title' => '']);
|
|
$product = Product::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'name' => 'Test',
|
|
'slug' => Uuid::uuid7()->toString(),
|
|
'brand' => ProductBrand::cases()[0]->value,
|
|
'description' => 'Test',
|
|
'price' => new Money(100, resolve(Currency::class)),
|
|
'invoicing_unit' => InvoicingUnit::cases()[0]->value,
|
|
'tax_rate' => TaxRate::cases()[0]->value,
|
|
'stock' => 10,
|
|
'picture_alt' => '',
|
|
'picture_title' => '',
|
|
'picture_large_alt' => '',
|
|
'picture_large_title' => '',
|
|
'sell_unit' => SellUnit::cases()[0]->value,
|
|
'status' => true,
|
|
'star' => false,
|
|
'weight_variable' => false,
|
|
'range_id' => $range->id,
|
|
]);
|
|
|
|
$request = new AddBasketRequest();
|
|
$request->initialize();
|
|
$request->merge([
|
|
'rowId' => $product->id,
|
|
'qty' => 1,
|
|
]);
|
|
|
|
// WHEN
|
|
$validator = Validator::make($request->all(), $request->rules());
|
|
|
|
// THEN
|
|
expect($validator->passes())->toBeTrue();
|
|
});
|
|
|
|
it('fails validation on incorrect rules', function () {
|
|
// GIVEN
|
|
$request = new AddBasketRequest();
|
|
$request->initialize();
|
|
$request->merge([
|
|
'rowId' => 'not-a-uuid',
|
|
'qty' => 0,
|
|
]);
|
|
|
|
// WHEN
|
|
$validator = Validator::make($request->all(), $request->rules());
|
|
|
|
// THEN
|
|
expect($validator->passes())->toBeFalse()
|
|
->and($validator->errors()->keys())->toContain('rowId', 'qty');
|
|
});
|
|
|
|
it('can get product id as value object', function () {
|
|
// GIVEN
|
|
$uuid = Uuid::uuid7()->toString();
|
|
$request = new AddBasketRequest();
|
|
$request->initialize();
|
|
$request->merge(['rowId' => $uuid]);
|
|
|
|
// WHEN
|
|
$productId = $request->getProductId();
|
|
|
|
// THEN
|
|
expect($productId)->toBeInstanceOf(ProductId::class)
|
|
->and($productId->value->toString())->toBe($uuid);
|
|
});
|
|
});
|