test(casts): add tests for ShopperEstablishmentCodeCast functionality test(casts): add tests for ShopperServiceCodeCast functionality test(casts): add tests for ShopperSiteCodeCast functionality test(dto): update ProductDtoTest to include additional fields test(dto): update SettingDtoTest to remove arcadiaSites assertions test(events): update AnOrderHasBeenPlacedTest to use value objects test(entities): update OrderTest to use value objects for employee details test(value-objects): update EmployeeUpsertDataTest to use value objects test(pagination): update PaginationTest to access value properties test(services): update AppCustomerAuthServiceTest to use CrmHashcode test(services): update CrmServiceTest to use CrmHashcode for customer retrieval
201 lines
6.8 KiB
PHP
201 lines
6.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Exceptions\CustomerNotEmployedException;
|
|
use App\Models\Customer;
|
|
use App\Models\Page;
|
|
use App\Services\CrmService;
|
|
use Carbon\CarbonImmutable;
|
|
use Database\Seeders\ContentSeeder;
|
|
use Domain\Enums\Country;
|
|
use Domain\Models\Entities\Shopper;
|
|
use Domain\Models\ValueObjects\CrmCustomer;
|
|
use Domain\Models\ValueObjects\Email;
|
|
use Domain\Models\ValueObjects\Entities\ShopperId;
|
|
use Domain\Models\ValueObjects\Matricule;
|
|
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
|
|
use Domain\Models\ValueObjects\ShopperServiceCode;
|
|
use Domain\Models\ValueObjects\ShopperSiteCode;
|
|
use Domain\Services\ShopperService;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\View;
|
|
use Illuminate\Support\Str;
|
|
|
|
describe('LoginController', function () {
|
|
it('can access get /auth/connexion', function () {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$this->seed(ContentSeeder::class);
|
|
View::share('PageList', Page::getCached()
|
|
->mapWithKeys(fn ($page) => [
|
|
$page->slug->value() => $page,
|
|
]));
|
|
Cache::flush();
|
|
|
|
// WHEN
|
|
$response = $this->get('/auth/connexion');
|
|
|
|
// THEN
|
|
$response->assertStatus(200);
|
|
});
|
|
|
|
it('can access post /auth/connexion', function () {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$this->seed(ContentSeeder::class);
|
|
View::share('PageList', Page::getCached()
|
|
->mapWithKeys(fn ($page) => [
|
|
$page->slug->value() => $page,
|
|
]));
|
|
Cache::flush();
|
|
|
|
// WHEN
|
|
$response = $this->post('/auth/connexion', []);
|
|
|
|
// THEN
|
|
$response->assertStatus(302);
|
|
});
|
|
|
|
it('can access post /auth/deconnexion', 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 = 'dummy_hashcode';
|
|
$user->shouldReceive('getAuthPassword')->andReturn('dummy');
|
|
$this->actingAs($user, 'frontend');
|
|
|
|
// WHEN
|
|
$response = $this->post('/auth/deconnexion');
|
|
|
|
// THEN
|
|
$response->assertStatus(302);
|
|
});
|
|
|
|
it('throws CustomerNotEmployedException when shopper is not active during login', function () {
|
|
/** @var \Tests\TestCase $this */
|
|
// GIVEN
|
|
$this->seed(ContentSeeder::class);
|
|
View::share('PageList', Page::getCached()
|
|
->mapWithKeys(fn ($page) => [
|
|
$page->slug->value() => $page,
|
|
]));
|
|
Cache::flush();
|
|
|
|
$customer = Customer::forceCreate([
|
|
'email' => new Email('test@example.com'),
|
|
'hashcode' => (string) Str::uuid(),
|
|
]);
|
|
|
|
$crmCustomer = new CrmCustomer(
|
|
hashcode: $customer->hashcode->value,
|
|
email: 'test@example.com',
|
|
password: 'password',
|
|
firstname: 'John',
|
|
lastname: 'Doe',
|
|
birthDate: null,
|
|
phone: null,
|
|
address: null,
|
|
address2: null,
|
|
city: null,
|
|
postcode: null,
|
|
country: Country::france,
|
|
optIn: false,
|
|
status: true,
|
|
lastConnection: null,
|
|
);
|
|
$crmService = $this->mock(CrmService::class);
|
|
$crmService->shouldReceive('getCustomerFromEmail')->with('test@example.com')->andReturn($crmCustomer);
|
|
$crmService->shouldReceive('login')->with('test@example.com', 'password')->andReturnTrue();
|
|
|
|
$shopperService = \Mockery::mock(ShopperService::class);
|
|
$shopperService->shouldReceive('getShopperFor')
|
|
->withArgs(function ($arg) use ($customer) {
|
|
return $arg instanceof ShopperId && (string) $arg->value === $customer->id;
|
|
})
|
|
->andReturn(null);
|
|
$this->app->instance(ShopperService::class, $shopperService);
|
|
|
|
// WHEN
|
|
$this->withoutExceptionHandling();
|
|
$this->post('/auth/connexion', [
|
|
'email' => 'test@example.com',
|
|
'password' => 'password',
|
|
]);
|
|
})->throws(CustomerNotEmployedException::class);
|
|
|
|
it('flashes success message when shopper is active during login', function () {
|
|
// GIVEN
|
|
/** @var \Tests\TestCase $this */
|
|
$this->seed(ContentSeeder::class);
|
|
View::share('PageList', Page::getCached()
|
|
->mapWithKeys(fn ($page) => [
|
|
$page->slug->value() => $page,
|
|
]));
|
|
Cache::flush();
|
|
|
|
$customer = Customer::forceCreate([
|
|
'email' => new Email('test2@example.com'),
|
|
'hashcode' => (string) Str::uuid(),
|
|
]);
|
|
|
|
$crmCustomer = new CrmCustomer(
|
|
hashcode: $customer->hashcode->value,
|
|
email: 'test2@example.com',
|
|
password: 'password',
|
|
firstname: 'John',
|
|
lastname: 'Doe',
|
|
birthDate: null,
|
|
phone: null,
|
|
address: null,
|
|
address2: null,
|
|
city: null,
|
|
postcode: null,
|
|
country: Country::france,
|
|
optIn: false,
|
|
status: true,
|
|
lastConnection: null,
|
|
);
|
|
$crmService = $this->mock(CrmService::class);
|
|
$crmService->shouldReceive('getCustomerFromEmail')->with('test2@example.com')->andReturn($crmCustomer);
|
|
$crmService->shouldReceive('login')->with('test2@example.com', 'password')->andReturnTrue();
|
|
|
|
$shopper = new Shopper(
|
|
id: clone $customer->getId(),
|
|
email: new Email('test2@example.com'),
|
|
matricule: new Matricule('1234567'),
|
|
nom: 'Doe',
|
|
prenom: 'John',
|
|
startDate: CarbonImmutable::now()->subDays(1),
|
|
endDate: null,
|
|
etablissement: new ShopperEstablishmentCode('FAC01'),
|
|
site: new ShopperSiteCode('SITE'),
|
|
service: new ShopperServiceCode('101'),
|
|
);
|
|
|
|
$shopperService = \Mockery::mock(ShopperService::class);
|
|
$shopperService->shouldReceive('getShopperFor')
|
|
->withArgs(function ($arg) use ($customer) {
|
|
return $arg instanceof ShopperId && (string) $arg->value === $customer->id;
|
|
})
|
|
->andReturn($shopper);
|
|
$this->app->instance(ShopperService::class, $shopperService);
|
|
|
|
// WHEN
|
|
$response = $this->post('/auth/connexion', [
|
|
'email' => 'test2@example.com',
|
|
'password' => 'password',
|
|
]);
|
|
|
|
// THEN
|
|
$response->assertRedirect(route('fo.pages.home'));
|
|
$response->assertSessionHas('success', \trans('You are logged in!'));
|
|
});
|
|
});
|