221 lines
9.2 KiB
PHP
221 lines
9.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Dto\OrderDto;
|
|
use App\Models\Customer;
|
|
use App\Models\Employee;
|
|
use App\Models\Order;
|
|
use App\Models\Shop;
|
|
use App\Models\User;
|
|
use App\Services\CrmService;
|
|
use Carbon\CarbonImmutable;
|
|
use Domain\Enums\OrderStatus;
|
|
use Domain\Enums\UserRole;
|
|
use Domain\Models\ValueObjects\Code;
|
|
use Domain\Models\ValueObjects\DeliveryAddress;
|
|
use Domain\Models\ValueObjects\Email;
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
use Domain\Models\ValueObjects\OrderEmployee;
|
|
use Domain\Models\ValueObjects\OrderReference;
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Money\Currency;
|
|
use Money\Money;
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
describe('Bo\OrdersController', function (): void {
|
|
beforeEach(function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$user = User::factory()->create(['role' => UserRole::SuperAdmin]);
|
|
$this->actingAs($user, 'backend');
|
|
});
|
|
|
|
it('can access index with paginated orders and filters', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$shop = Shop::factory()->create(['name' => 'Boutique Test']);
|
|
Order::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'shop_id' => $shop->id,
|
|
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'total' => new Money(2500, new Currency('EUR')),
|
|
'employee' => new OrderEmployee(new Matricule('1000101'), 'Alice', 'Smith', 'Facility', 'Site', 'Service'),
|
|
'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'),
|
|
'pickup_day' => Date::now()->toDateString(),
|
|
]);
|
|
|
|
// WHEN
|
|
$response = $this->get('/bo/orders');
|
|
|
|
// THEN
|
|
expect($response->status())->toBe(200);
|
|
$response->assertViewIs('bo.orders.index');
|
|
$response->assertViewHas('orders', fn ($orders) => $orders instanceof LengthAwarePaginator && $orders->total() >= 1);
|
|
$response->assertViewHas('searchFields');
|
|
$response->assertViewHas('orderStatusList');
|
|
});
|
|
|
|
it('can filter index by search term', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$ref = \Symfony\Component\Uid\Uuid::v7()->toBase58();
|
|
$shop = Shop::factory()->create(['name' => 'UniqueShopSearch']);
|
|
Order::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'shop_id' => $shop->id,
|
|
'reference' => new OrderReference($ref),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'total' => new Money(1500, new Currency('EUR')),
|
|
'employee' => new OrderEmployee(new Matricule('1000102'), 'Bob', 'Jones', 'Facility', 'Site', 'Service'),
|
|
'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'),
|
|
'pickup_day' => Date::now()->toDateString(),
|
|
]);
|
|
|
|
// WHEN
|
|
$response = $this->get("/bo/orders?search={$ref}");
|
|
|
|
// THEN
|
|
expect($response->status())->toBe(200);
|
|
$response->assertViewHas('orders', function ($orders) use ($ref): bool {
|
|
return $orders instanceof LengthAwarePaginator
|
|
&& $orders->total() === 1
|
|
&& (string) $orders->first()->reference === $ref;
|
|
});
|
|
});
|
|
|
|
it('can access today orders', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$shop = Shop::factory()->create();
|
|
Order::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'shop_id' => $shop->id,
|
|
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'total' => new Money(3000, new Currency('EUR')),
|
|
'employee' => new OrderEmployee(new Matricule('1000103'), 'Charlie', 'Brown', 'Facility', 'Site', 'Service'),
|
|
'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'),
|
|
'pickup_day' => Date::now()->toDateString(),
|
|
]);
|
|
|
|
// WHEN
|
|
$response = $this->get('/bo/orders/today');
|
|
|
|
// THEN
|
|
expect($response->status())->toBe(200);
|
|
$response->assertViewIs('bo.orders.index-today');
|
|
$response->assertViewHas('orders', fn ($orders) => $orders instanceof LengthAwarePaginator && $orders->total() >= 1);
|
|
});
|
|
|
|
it('can access order show detail', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$shop = Shop::factory()->create();
|
|
$customer = Customer::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'hashcode' => Uuid::uuid4()->toString(),
|
|
'email' => new Email('customer@example.com'),
|
|
]);
|
|
Employee::forceCreate([
|
|
'id' => 9991,
|
|
'customer_id' => $customer->id,
|
|
'matricule' => new Matricule('1000999'),
|
|
'nom' => 'Dupont',
|
|
'prenom' => 'Jean',
|
|
'code' => new Code('ABCDEF12'),
|
|
'etablissement' => 'E',
|
|
'site' => 'S',
|
|
'service' => 'S',
|
|
'created_at' => CarbonImmutable::now(),
|
|
'updated_at' => CarbonImmutable::now(),
|
|
]);
|
|
|
|
$crmMock = \Mockery::mock(CrmService::class);
|
|
$crmMock->shouldReceive('getCustomerFromHashCode')->andReturn(null);
|
|
App::instance(CrmService::class, $crmMock);
|
|
|
|
$order = Order::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'shop_id' => $shop->id,
|
|
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'total' => new Money(4000, new Currency('EUR')),
|
|
'employee' => new OrderEmployee(new Matricule('1000999'), 'Jean', 'Dupont', 'Facility', 'Site', 'Service'),
|
|
'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'),
|
|
'pickup_day' => Date::now()->toDateString(),
|
|
]);
|
|
|
|
// WHEN
|
|
$response = $this->get("/bo/orders/{$order->id}");
|
|
|
|
// THEN
|
|
expect($response->status())->toBe(200);
|
|
$response->assertViewIs('bo.orders.view');
|
|
$response->assertViewHas('order', fn ($orderData) => $orderData instanceof OrderDto && $orderData->id === (string) $order->id);
|
|
});
|
|
|
|
it('can cancel an order', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$shop = Shop::factory()->create();
|
|
$order = Order::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'shop_id' => $shop->id,
|
|
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
|
|
'status' => OrderStatus::VALIDATED->value,
|
|
'total' => new Money(2000, new Currency('EUR')),
|
|
'employee' => new OrderEmployee(new Matricule('1000104'), 'David', 'Clark', 'Facility', 'Site', 'Service'),
|
|
'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'),
|
|
'pickup_day' => Date::now()->toDateString(),
|
|
]);
|
|
|
|
// WHEN
|
|
$response = $this->post("/bo/orders/{$order->id}/cancel");
|
|
|
|
// THEN
|
|
$response->assertRedirect();
|
|
$response->assertSessionHas('warning', trans('Order cancelled'));
|
|
|
|
$refreshedOrder = Order::query()->find($order->id);
|
|
expect($refreshedOrder)->not->toBeNull()
|
|
->and($refreshedOrder->status)->toBe(OrderStatus::CANCELED);
|
|
});
|
|
|
|
it('rejects unauthenticated users', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
auth('backend')->logout();
|
|
|
|
// WHEN
|
|
$response = $this->get('/bo/orders');
|
|
|
|
// THEN
|
|
$response->assertRedirect('/bo/login');
|
|
});
|
|
|
|
it('rejects cancellation of already canceled order', function (): void {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$shop = Shop::factory()->create();
|
|
$order = Order::forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'shop_id' => $shop->id,
|
|
'reference' => new OrderReference(\Symfony\Component\Uid\Uuid::v7()->toBase58()),
|
|
'status' => OrderStatus::CANCELED->value,
|
|
'total' => new Money(2000, new Currency('EUR')),
|
|
'employee' => new OrderEmployee(new Matricule('1000105'), 'Eve', 'Stone', 'Facility', 'Site', 'Service'),
|
|
'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'),
|
|
'pickup_day' => Date::now()->toDateString(),
|
|
]);
|
|
|
|
// WHEN
|
|
$response = $this->post("/bo/orders/{$order->id}/cancel");
|
|
|
|
// THEN
|
|
$response->assertStatus(403);
|
|
});
|
|
});
|