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

240 lines
8.3 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
use App\Models\Customer;
use App\Models\Employee;
2026-06-14 21:32:07 +02:00
use App\Models\Page;
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\Models\ValueObjects\Code;
use Domain\Models\ValueObjects\CrmCustomer;
use Domain\Models\ValueObjects\Email;
use Domain\Models\ValueObjects\Matricule;
2026-06-14 21:32:07 +02:00
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\View;
use Ramsey\Uuid\Uuid;
use Shared\ValueObjects\CrmHashcode;
2026-06-14 21:32:07 +02:00
describe('CustomerController', function () {
it('can access get /compte', function () {
// GIVEN
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
$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');
// THEN
$response->assertStatus(302);
});
it('can access get /compte/profil', function () {
// GIVEN
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
$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/profil');
// THEN
$response->assertStatus(302);
});
it('can access post /compte/profil', function () {
// GIVEN
/** @var \Tests\TestCase $this */
2026-06-14 21:32:07 +02:00
$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->post('/compte/profil', []);
// THEN
$response->assertStatus(302);
});
it('renders customer edit infos page with crm customer data', 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('jean.dupont@example.com'),
]);
Employee::forceCreate([
'id' => 8882,
'customer_id' => $customer->id,
'matricule' => new Matricule('8888002'),
'nom' => 'Dupont',
'prenom' => 'Jean',
'code' => new Code('ABCDEF13'),
'etablissement' => 'ETAB',
'site' => 'SITE',
'service' => '101',
'created_at' => CarbonImmutable::now(),
'updated_at' => CarbonImmutable::now(),
]);
$crmCustomer = new CrmCustomer(
hashcode: $hashcode->value,
email: 'jean.dupont@example.com',
password: 'pwd',
firstname: 'Jean',
lastname: 'Dupont',
birthDate: '1985-05-15',
phone: '0123456789',
address: '10 rue de la Paix',
address2: null,
city: 'Paris',
postcode: '75002',
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/profil');
// THEN
$response->assertOk();
$response->assertViewIs('fo.customer.edit.infos');
$response->assertViewHas('crmCustomer', $crmCustomer);
$response->assertSee('value="Dupont"', false);
$response->assertSee('value="Jean"', false);
});
it('successfully updates customer infos and redirects back', 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('jean.dupont@example.com'),
]);
Employee::forceCreate([
'id' => 8883,
'customer_id' => $customer->id,
'matricule' => new Matricule('8888003'),
'nom' => 'Dupont',
'prenom' => 'Jean',
'code' => new Code('ABCDEF14'),
'etablissement' => 'ETAB',
'site' => 'SITE',
'service' => '101',
'created_at' => CarbonImmutable::now(),
'updated_at' => CarbonImmutable::now(),
]);
$updatedCrmCustomer = new CrmCustomer(
hashcode: $hashcode->value,
email: 'jean.dupont@example.com',
password: 'pwd',
firstname: 'Jean-Pierre',
lastname: 'Durand',
birthDate: '1985-05-15',
phone: '0123456789',
address: '20 avenue Foch',
address2: null,
city: 'Lyon',
postcode: '69001',
country: Country::france,
optIn: true,
status: true,
lastConnection: null,
);
$crmMock = \Mockery::mock(CrmService::class);
$crmMock->shouldReceive('getCustomerFromHashCode')
->andReturn($updatedCrmCustomer);
$crmMock->shouldReceive('updateCustomerInfos')
->once()
->andReturn($updatedCrmCustomer);
App::instance(CrmService::class, $crmMock);
$this->actingAs($customer, 'frontend');
// WHEN
$response = $this->post('/compte/profil', [
'firstname' => 'Jean-Pierre',
'lastname' => 'Durand',
'birth_date' => '1985-05-15',
'phone' => '0123456789',
'address' => '20 avenue Foch',
'city' => 'Lyon',
'postcode' => '69001',
'opt_in' => true,
]);
// THEN
$response->assertRedirect();
$response->assertSessionHas('success', trans('Your information has been saved.'));
});
2026-06-14 21:32:07 +02:00
});