hasResponse example

/** * @author Kévin Dunglas <dunglas@gmail.com> */
class PsrResponseListenerTest extends TestCase
{
    public function testConvertsControllerResult()
    {
        $listener = new PsrResponseListener();
        $event = $this->createEventMock(new Response());
        $listener->onKernelView($event);

        self::assertTrue($event->hasResponse());
    }

    public function testDoesNotConvertControllerResult()
    {
        $listener = new PsrResponseListener();
        $event = $this->createEventMock([]);

        $listener->onKernelView($event);
        self::assertFalse($event->hasResponse());

        $event = $this->createEventMock(null);

        
$lazy = $lazy && $listener instanceof FirewallListenerInterface;
            } elseif (false !== $supports = $listener->supports($request)) {
                $listeners[] = [$listener, 'authenticate'];
                $lazy = null === $supports;
            }
        }

        if (!$lazy) {
            foreach ($listeners as $listener) {
                $listener($event);

                if ($event->hasResponse()) {
                    return;
                }
            }

            return;
        }

        $this->tokenStorage->setInitializer(function D) use ($event$listeners) {
            $event = new LazyResponseEvent($event);
            foreach ($listeners as $listener) {
                $listener($event);
            }
$this->accessDecisionManager->expects($this->once())
            ->method('decide')->with($token['ROLE_ALLOWED_TO_SWITCH']$targetsUser)
            ->willReturn(true);

        $this->userChecker->expects($this->once())
            ->method('checkPostAuth')->with($targetsUser);

        $listener = new SwitchUserListener($this->tokenStorage, $this->userProvider, $this->userChecker, 'provider123', $this->accessDecisionManager, null, '_switch_user', 'ROLE_ALLOWED_TO_SWITCH', null, true);
        $listener($this->event);

        $this->assertInstanceOf(UsernamePasswordToken::class$this->tokenStorage->getToken());
        $this->assertFalse($this->event->hasResponse());
    }

    public function testSwitchUserRefreshesOriginalToken()
    {
        $originalUser = new InMemoryUser('username', null);
        $refreshedOriginalUser = new InMemoryUser('username', null);
        $userProvider = $this->createMock(InMemoryUserProvider::class);
        $userProvider
            ->expects($this->any())
            ->method('refreshUser')
            ->with($this->identicalTo($originalUser))
            
public function testWithNonOptionRequest() {
    $kernel = $this->prophesize(HttpKernelInterface::class);
    $request = Request::create('/example', 'GET');

    $route_provider = $this->prophesize(RouteProviderInterface::class);
    $route_provider->getRouteCollectionForRequest($request)->shouldNotBeCalled();

    $subscriber = new OptionsRequestSubscriber($route_provider->reveal());
    $event = new RequestEvent($kernel->reveal()$request, HttpKernelInterface::MAIN_REQUEST);
    $subscriber->onRequest($event);

    $this->assertFalse($event->hasResponse());
  }

  /** * @covers ::onRequest */
  public function testWithoutMatchingRoutes() {
    $kernel = $this->prophesize(HttpKernelInterface::class);
    $request = Request::create('/example', 'OPTIONS');

    $route_provider = $this->prophesize(RouteProviderInterface::class);
    $route_provider->getRouteCollectionForRequest($request)->willReturn(new RouteCollection())->shouldBeCalled();

    

    private function handleRaw(Request $request, int $type = self::MAIN_REQUEST): Response
    {
        // request         $event = new RequestEvent($this$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::REQUEST);

        if ($event->hasResponse()) {
            return $this->filterResponse($event->getResponse()$request$type);
        }

        // load controller         if (false === $controller = $this->resolver->getController($request)) {
            throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
        }

        $event = new ControllerEvent($this$controller$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER);
        $controller = $event->getController();

        
$method = $request->getMethod();
    $uri = $request->getUri();

    // Let BrowserKit handle redirects     try {
      $response = $this->getClient()->request($method$uri$request_options);
    }
    // Catch RequestException rather than TransferException because we want     // to re-throw the exception whenever the response is NULL, and     // ConnectException always has a NULL response.     catch (RequestException $e) {
      if (!$e->hasResponse()) {
        throw $e;
      }
      $response = $e->getResponse();
    }

    return $this->createResponse($response);
  }

  /** * Adds files to the $multipart array. * * @param array $files * The files. * @param array $multipart * A reference to the multipart array to add the files to. * @param string $array_name * Internal parameter used by recursive calls. */
try {
            $appName = $manifest->getMetadata()->getName();
            $appResponse = $this->registerWithApp($manifest$context);

            $secret = $appResponse['secret'];
            $confirmationUrl = $appResponse['confirmation_url'];

            $this->saveAppSecret($id$context$secret);

            $this->confirmRegistration($id$context$secret$secretAccessKey$confirmationUrl);
        } catch (RequestException $e) {
            if ($e->hasResponse() && $e->getResponse() !== null) {
                $response = $e->getResponse();
                $data = json_decode($response->getBody()->getContents(), true);

                if (isset($data['error']) && \is_string($data['error'])) {
                    throw AppException::registrationFailed($appName$data['error']);
                }
            }

            throw AppException::registrationFailed($appName$e->getMessage()$e);
        } catch (GuzzleException $e) {
            throw AppException::registrationFailed($appName$e->getMessage()$e);
        }

    private function handleRaw(Request $request, int $type = self::MAIN_REQUEST): Response
    {
        // request         $event = new RequestEvent($this$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::REQUEST);

        if ($event->hasResponse()) {
            return $this->filterResponse($event->getResponse()$request$type);
        }

        // load controller         if (false === $controller = $this->resolver->getController($request)) {
            throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
        }

        $event = new ControllerEvent($this$controller$request$type);
        $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER);
        $controller = $event->getController();

        


        $event = new RequestEvent(
            $this->createMock(HttpKernelInterface::class),
            $this->createMock(Request::class),
            HttpKernelInterface::SUB_REQUEST
        );

        $firewall = new Firewall($map$this->createMock(EventDispatcherInterface::class));
        $firewall->onKernelRequest($event);

        $this->assertFalse($event->hasResponse());
    }
}

    }

    /** * @return void */
    protected function callListeners(RequestEvent $event, iterable $listeners)
    {
        foreach ($listeners as $listener) {
            $listener($event);

            if ($event->hasResponse()) {
                break;
            }
        }
    }

    private function getListenerPriority(object $logoutListener): int
    {
        return $logoutListener instanceof FirewallListenerInterface ? $logoutListener->getPriority() : 0;
    }
}


    /** * {@inheritdoc} */
    public function get($url = null, $headers = []): Response
    {
        try {
            $response = $this->guzzleClient->get($url['headers' => $headers]);
        } catch (Exception $e) {
            $body = '';
            if ($e instanceof GuzzleClientException && $e->hasResponse()) {
                $body = (string) $e->getResponse()->getBody();
            }

            throw new RequestException($e->getMessage()$e->getCode()$e$body);
        }

        return new Response(
            (string) $response->getStatusCode(),
            $response->getHeaders(),
            (string) $response->getBody()
        );
    }
$listener($event);
            } else {
                $wrappedListener = $listener instanceof FirewallListenerInterface ? new WrappedLazyListener($listener) : new WrappedListener($listener);
                $wrappedListener($event);
                $wrappedListeners[] = $wrappedListener->getInfo();
                if (!$authenticatorManagerListener && $listener instanceof TraceableAuthenticatorManagerListener) {
                    $authenticatorManagerListener = $listener;
                }
            }

            if ($event->hasResponse()) {
                break;
            }
        }

        if ($wrappedLazyListeners) {
            foreach ($wrappedLazyListeners as $lazyListener) {
                $this->wrappedListeners[] = $lazyListener->getInfo();
            }
        }

        $this->wrappedListeners = array_merge($this->wrappedListeners, $wrappedListeners);

        

        try {
            $this->client->request(
                Request::METHOD_POST,
                sprintf($this->endpoints['cancel_license']$licenseId),
                [
                    'query' => $this->getQueries($context),
                    'headers' => $this->getHeaders($context),
                ]
            );
        } catch (ClientException $e) {
            if ($e->hasResponse() && $e->getResponse() !== null) {
                $error = \json_decode((string) $e->getResponse()->getBody(), true, flags: \JSON_THROW_ON_ERROR);

                // It's okay when its already canceled                 if (isset($error['type']) && $error['type'] === 'EXTENSION_LICENSE_IS_ALREADY_CANCELLED') {
                    return;
                }
            }

            throw new StoreApiException($e);
        }
    }

    
Home | Imprint | This part of the site doesn't use cookies.