function it_performs_action_as_given_admin_user_and_logout(SecurityServiceInterface $adminSecurityService, OrderInterface $order, AdminUserInterface $adminUser) { $adminSecurityService->getCurrentToken()->willThrow(TokenNotFoundException::class); $adminSecurityService->logIn($adminUser)->shouldBeCalled(); $order->completeCheckout()->shouldBeCalled(); $adminSecurityService->restoreToken(Argument::any())->shouldNotBeCalled(); $adminSecurityService->logOut()->shouldBeCalled(); $wrappedOrder = $order->getWrappedObject(); $this->performActionAsAdminUser($adminUser, function () use($wrappedOrder) { $wrappedOrder->completeCheckout(); }); }
/** * @param SecurityServiceInterface $securityService * @param UserInterface $user * @param callable $action */ private function performActionAs(SecurityServiceInterface $securityService, UserInterface $user, callable $action) { try { $token = $securityService->getCurrentToken(); } catch (TokenNotFoundException $exception) { $token = null; } $securityService->logIn($user); $action(); if (null === $token) { $securityService->logOut(); return; } $securityService->restoreToken($token); }