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'); }); });