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

250 lines
9.4 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
use App\Dto\OrderDto;
2026-06-14 21:32:07 +02:00
use App\Models\Customer;
use App\Models\Employee;
use App\Models\Order;
2026-06-14 21:32:07 +02:00
use App\Models\Page;
use App\Models\Shop;
2026-06-14 21:32:07 +02:00
use App\Services\CrmService;
use Carbon\CarbonImmutable;
2026-06-14 21:32:07 +02:00
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;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Date;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\View;
use Money\Currency;
use Money\Money;
use Ramsey\Uuid\Uuid;
use Shared\ValueObjects\CrmHashcode;
2026-06-14 21:32:07 +02:00
describe('OrderController', function () {
it('can access get /compte/commandes', function () {
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
// 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(Uuid::uuid4());
2026-06-14 21:32:07 +02:00
$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 */
2026-06-14 21:32:07 +02:00
// 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(Uuid::uuid4());
2026-06-14 21:32:07 +02:00
$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(Uuid::uuid4());
$customer = Customer::forceCreate([
'id' => Uuid::uuid7()->toString(),
'hashcode' => $hashcode->value,
'email' => new Email('verified@example.com'),
]);
Employee::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 = Uuid::uuid7();
$reference = new OrderReference($refUuid);
$ref = (string) $reference;
Order::forceCreate([
'id' => Uuid::uuid7()->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 {
return $orders instanceof LengthAwarePaginator
&& $orders->total() === 1
&& (string) $orders->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(Uuid::uuid4());
$customer = Customer::forceCreate([
'id' => Uuid::uuid7()->toString(),
'hashcode' => $hashcode->value,
'email' => new Email('verified_show@example.com'),
]);
Employee::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 = Uuid::uuid7();
$reference = new OrderReference($refUuid);
$ref = (string) $reference;
$order = Order::forceCreate([
'id' => Uuid::uuid7()->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');
});
2026-06-14 21:32:07 +02:00
});