ldc-boutique-cnc-sable/tests/Unit/App/Http/Middleware/EnsureCustomerEmailIsVerifiedTest.php

106 lines
4 KiB
PHP
Raw Permalink Normal View History

2026-06-14 21:32:07 +02:00
<?php
declare(strict_types=1);
use App\Http\Middleware\EnsureCustomerEmailIsVerified;
use App\Models\Customer;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\URL;
use Infrastructure\Adapters\Services\AppCustomerAuthService;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Tests\TestCase;
uses(TestCase::class);
describe('EnsureCustomerEmailIsVerified', function () {
it('redirects to fo.auth.verification.notice if customer is not verified', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$customer = \Mockery::mock(Customer::class);
$authService->shouldReceive('getCustomer')->andReturn($customer);
$authService->shouldReceive('hasVerifiedEmail')->with($customer)->andReturn(false);
$middleware = new EnsureCustomerEmailIsVerified($authService);
$request = Request::create('/some-url', 'GET');
URL::shouldReceive('route')->with('fo.auth.verification.notice')->andReturn('http://localhost/verification-notice');
Redirect::shouldReceive('guest')->with('http://localhost/verification-notice')->andReturn(new RedirectResponse('http://localhost/verification-notice'));
// WHEN
$response = $middleware->handle($request, function ($req) {
return new Response('Next');
});
// THEN
expect($response)->toBeInstanceOf(RedirectResponse::class);
expect($response->getTargetUrl())->toBe('http://localhost/verification-notice');
});
it('aborts with 403 if customer is not verified and expects JSON', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$customer = \Mockery::mock(Customer::class);
$authService->shouldReceive('getCustomer')->andReturn($customer);
$authService->shouldReceive('hasVerifiedEmail')->with($customer)->andReturn(false);
$middleware = new EnsureCustomerEmailIsVerified($authService);
$request = Request::create('/some-url', 'GET');
$request->headers->set('Accept', 'application/json');
URL::shouldReceive('route')->with('fo.auth.verification.notice')->andReturn('http://localhost/verification-notice');
// WHEN / THEN
expect(fn () => $middleware->handle($request, function ($req) {
return new Response('Next');
}))->toThrow(HttpException::class, 'Your email address is not verified.');
});
it('proceeds if customer is verified', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$customer = \Mockery::mock(Customer::class);
$authService->shouldReceive('getCustomer')->andReturn($customer);
$authService->shouldReceive('hasVerifiedEmail')->with($customer)->andReturn(true);
$middleware = new EnsureCustomerEmailIsVerified($authService);
$request = Request::create('/some-url', 'GET');
// WHEN
$response = $middleware->handle($request, function ($req) {
return new Response('Next');
});
// THEN
expect($response->getContent())->toBe('Next');
});
it('proceeds if no customer is logged in', function () {
// GIVEN
$authService = \Mockery::mock(AppCustomerAuthService::class);
$authService->shouldReceive('getCustomer')->andReturn(null);
$middleware = new EnsureCustomerEmailIsVerified($authService);
$request = Request::create('/some-url', 'GET');
// WHEN
$response = $middleware->handle($request, function ($req) {
return new Response('Next');
});
// THEN
expect($response->getContent())->toBe('Next');
});
it('returns redirectTo static method', function () {
// GIVEN / WHEN / THEN
expect(EnsureCustomerEmailIsVerified::redirectTo('my.route'))->toBe(EnsureCustomerEmailIsVerified::class . ':my.route');
});
});