ldc-cocotte/tests/Feature/App/Http/Controllers/Fo/Customers/OrderControllerTest.php

370 lines
14 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\App\Http\Controllers\Fo\Customers;
use App\Dto\OrderDto;
use App\Models\Customer;
use App\Models\Employee;
use App\Models\Order;
use App\Models\Page;
use App\Models\Shop;
use App\Services\CrmService;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Database\Seeders\ContentSeeder;
use Domain\Enums\Country;
use Domain\Enums\OrderStatus;
use Domain\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\CrmCustomer;
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 Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\View;
use Money\Currency;
use Money\Money;
use Shared\Helpers\UuidHelper;
use Shared\ValueObjects\CrmHashcode;
describe('OrderController', function () {
beforeEach(function (): void {
$now = CarbonImmutable::parse('2026-06-15 10:00:00', 'Europe/Paris');
Date::setTestNow($now);
CarbonImmutable::setTestNow($now);
Carbon::setTestNow($now);
});
afterEach(function (): void {
Date::setTestNow();
CarbonImmutable::setTestNow();
Carbon::setTestNow();
});
it('can access get /compte/commandes', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$user = \Mockery::mock(Customer::class)->makePartial();
$user->shouldReceive('getAuthIdentifier')->andReturn(1);
$user->hashcode = new CrmHashcode(UuidHelper::v4());
$user->shouldReceive('getAuthPassword')->andReturn('dummy');
$this->actingAs($user, 'frontend');
$crmMock = \Mockery::mock(CrmService::class)->makePartial();
$crmMock->shouldReceive('getCustomerFromHashCode')->andReturn(null);
App::instance(CrmService::class, $crmMock);
// WHEN
$response = $this->get('/compte/commandes');
// THEN
$response->assertStatus(302);
});
it('can access get /compte/commandes/1', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$user = \Mockery::mock(Customer::class)->makePartial();
$user->shouldReceive('getAuthIdentifier')->andReturn(1);
$user->hashcode = new CrmHashcode(UuidHelper::v4());
$user->shouldReceive('getAuthPassword')->andReturn('dummy');
$this->actingAs($user, 'frontend');
$crmMock = \Mockery::mock(CrmService::class)->makePartial();
$crmMock->shouldReceive('getCustomerFromHashCode')->andReturn(null);
App::instance(CrmService::class, $crmMock);
// WHEN
$response = $this->get('/compte/commandes/1');
// THEN
$response->assertStatus(404);
});
it('renders customer orders index with DTOs when verified', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$hashcode = new CrmHashcode(UuidHelper::v4());
$customer = Customer::query()->forceCreate([
'id' => UuidHelper::v7()->toString(),
'hashcode' => $hashcode->value,
'email' => new Email('verified@example.com'),
]);
Employee::query()->forceCreate([
'id' => 8881,
'customer_id' => $customer->id,
'matricule' => new Matricule('8888001'),
'nom' => 'Dupont',
'prenom' => 'Jean',
'code' => new Code('ABCDEF12'),
'etablissement' => 'ETAB',
'site' => 'SITE',
'service' => '101',
'created_at' => CarbonImmutable::now(),
'updated_at' => CarbonImmutable::now(),
]);
$shop = Shop::factory()->create();
$refUuid = UuidHelper::v7();
$reference = new OrderReference($refUuid);
$ref = (string) $reference;
Order::query()->forceCreate([
'id' => UuidHelper::v7()->toString(),
'shop_id' => $shop->id,
'reference' => $reference,
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(5000, new Currency('EUR')),
'employee' => new OrderEmployee(
new Matricule('8888001'),
'Jean',
'Dupont',
new ShopperEstablishmentCode('FAC01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101'),
),
'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'),
'pickup_day' => Date::now()->toDateString(),
]);
$crmCustomer = new CrmCustomer(
hashcode: $hashcode->value,
email: 'verified@example.com',
password: 'pwd',
firstname: 'Jean',
lastname: 'Dupont',
birthDate: null,
phone: '0123456789',
address: '10 rue test',
address2: null,
city: 'Paris',
postcode: '75000',
country: Country::france,
optIn: true,
status: true,
lastConnection: null,
);
$crmMock = \Mockery::mock(CrmService::class)->makePartial();
$crmMock->shouldReceive('getCustomerFromHashCode')->andReturn($crmCustomer);
App::instance(CrmService::class, $crmMock);
$this->actingAs($customer, 'frontend');
// WHEN
$response = $this->get('/compte/commandes');
// THEN
expect($response->status())->toBe(200);
$response->assertViewIs('fo.customer.orders.index');
$response->assertViewHas('orders', function ($orders) use ($ref): bool {
if (! $orders instanceof LengthAwarePaginator || $orders->total() !== 1) {
return false;
}
$first = $orders->items()[0] ?? null;
return is_object($first)
&& property_exists($first, 'reference')
&& ($first->reference instanceof \Stringable || is_string($first->reference))
&& (string) $first->reference === $ref;
});
});
it('renders customer order detail when owned by customer', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$hashcode = new CrmHashcode(UuidHelper::v4());
$customer = Customer::query()->forceCreate([
'id' => UuidHelper::v7()->toString(),
'hashcode' => $hashcode->value,
'email' => new Email('verified_show@example.com'),
]);
Employee::query()->forceCreate([
'id' => 8882,
'customer_id' => $customer->id,
'matricule' => new Matricule('8888002'),
'nom' => 'Martin',
'prenom' => 'Paul',
'code' => new Code('ABCDEF34'),
'etablissement' => 'ETAB',
'site' => 'SITE',
'service' => '101',
'created_at' => CarbonImmutable::now(),
'updated_at' => CarbonImmutable::now(),
]);
$shop = Shop::factory()->create();
$refUuid = UuidHelper::v7();
$reference = new OrderReference($refUuid);
$ref = (string) $reference;
$order = Order::query()->forceCreate([
'id' => UuidHelper::v7()->toString(),
'shop_id' => $shop->id,
'reference' => $reference,
'status' => OrderStatus::VALIDATED->value,
'total' => new Money(5000, new Currency('EUR')),
'employee' => new OrderEmployee(
new Matricule('8888002'),
'Paul',
'Martin',
new ShopperEstablishmentCode('FAC01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101'),
),
'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'),
'pickup_day' => Date::now()->toDateString(),
]);
$crmCustomer = new CrmCustomer(
hashcode: $hashcode->value,
email: 'verified_show@example.com',
password: 'pwd',
firstname: 'Paul',
lastname: 'Martin',
birthDate: null,
phone: '0123456789',
address: '10 rue test',
address2: null,
city: 'Paris',
postcode: '75000',
country: Country::france,
optIn: true,
status: true,
lastConnection: null,
);
$crmMock = \Mockery::mock(CrmService::class)->makePartial();
$crmMock->shouldReceive('getCustomerFromHashCode')->andReturn($crmCustomer);
App::instance(CrmService::class, $crmMock);
$this->actingAs($customer, 'frontend');
// WHEN
$response = $this->get("/compte/commandes/{$ref}");
// THEN
expect($response->status())->toBe(200);
$response->assertViewIs('fo.customer.orders.show');
$response->assertViewHas('order', fn ($orderData) => $orderData instanceof OrderDto && (string) $orderData->reference === $ref);
$response->assertViewHas('mapUrl');
});
it('forbids viewing technical PENDING or FAILED orders on GET /compte/commandes/{reference}', function (OrderStatus $status) {
/** @var \Tests\TestCase $this */
// GIVEN
$this->seed(ContentSeeder::class);
View::share('PageList', Page::getCached()
->mapWithKeys(fn ($page) => [
$page->slug->value() => $page,
]));
Cache::flush();
$hashcode = new CrmHashcode(UuidHelper::v4());
$customer = Customer::query()->forceCreate([
'id' => UuidHelper::v7()->toString(),
'hashcode' => $hashcode->value,
'email' => new Email('tech-order-' . $status->value . '@example.com'),
]);
Employee::query()->forceCreate([
'customer_id' => $customer->id,
'matricule' => new Matricule('8888003'),
'nom' => 'Martin',
'prenom' => 'Paul',
'code' => new Code('ABCDEF35'),
'etablissement' => 'ETAB',
'site' => 'SITE',
'service' => '101',
'created_at' => CarbonImmutable::now(),
'updated_at' => CarbonImmutable::now(),
]);
$shop = Shop::factory()->create();
$reference = new OrderReference(UuidHelper::v7());
Order::query()->forceCreate([
'id' => UuidHelper::v7()->toString(),
'shop_id' => $shop->id,
'reference' => $reference,
'status' => $status->value,
'total' => new Money(5000, new Currency('EUR')),
'employee' => new OrderEmployee(
new Matricule('8888003'),
'Paul',
'Martin',
new ShopperEstablishmentCode('FAC01'),
new ShopperSiteCode('SITE'),
new ShopperServiceCode('101'),
),
'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'),
'pickup_day' => Date::now()->toDateString(),
]);
$crmCustomer = new CrmCustomer(
hashcode: $hashcode->value,
email: 'tech-order-' . $status->value . '@example.com',
password: 'pwd',
firstname: 'Paul',
lastname: 'Martin',
birthDate: null,
phone: '0123456789',
address: '10 rue test',
address2: null,
city: 'Paris',
postcode: '75000',
country: Country::france,
optIn: true,
status: true,
lastConnection: null,
);
$crmMock = \Mockery::mock(CrmService::class)->makePartial();
$crmMock->shouldReceive('getCustomerFromHashCode')->andReturn($crmCustomer);
App::instance(CrmService::class, $crmMock);
$this->actingAs($customer, 'frontend');
// WHEN
$response = $this->get('/compte/commandes/' . (string) $reference);
// THEN
expect($response->status())->toBe(403);
})->with([
'pending' => [OrderStatus::PENDING],
'failed' => [OrderStatus::FAILED],
]);
});