256 lines
8.8 KiB
PHP
256 lines
8.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\App\Http\Controllers\Fo\Customers;
|
|
|
|
use App\Models\Customer;
|
|
use App\Models\Employee;
|
|
use App\Models\Page;
|
|
use App\Services\CrmService;
|
|
use Carbon\Carbon;
|
|
use Carbon\CarbonImmutable;
|
|
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;
|
|
use Illuminate\Support\Facades\App;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Date;
|
|
use Illuminate\Support\Facades\View;
|
|
use Ramsey\Uuid\Uuid;
|
|
use Shared\ValueObjects\CrmHashcode;
|
|
|
|
describe('CustomerController', 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', function () {
|
|
// GIVEN
|
|
/** @var \Tests\TestCase $this */
|
|
$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());
|
|
$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 */
|
|
$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());
|
|
$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 */
|
|
$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());
|
|
$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::query()->forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'hashcode' => $hashcode->value,
|
|
'email' => new Email('jean.dupont@example.com'),
|
|
]);
|
|
|
|
Employee::query()->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::query()->forceCreate([
|
|
'id' => Uuid::uuid7()->toString(),
|
|
'hashcode' => $hashcode->value,
|
|
'email' => new Email('jean.dupont@example.com'),
|
|
]);
|
|
|
|
Employee::query()->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.'));
|
|
});
|
|
});
|