/**
  * {@inheritDoc}
  *
  * @see \Mcustiel\PowerRoute\Actions\ActionInterface::execute()
  */
 public function execute(TransactionData $transactionData, $argument = null)
 {
     $transactionData->setResponse($this->processAndGetResponse($transactionData, function (TransactionData $transaction, Expectation $expectation) {
         $this->validateRequestOrThrowException($expectation, $this->logger);
         $count = $this->searchForExecutionsCount($expectation);
         $this->logger->debug('Found ' . $count . ' request matching the expectation');
         return $transaction->getResponse()->withStatus(200)->withHeader('Content-Type', 'application/json')->withBody(new StringStream(json_encode(['count' => $count])));
     }));
 }
 public function createResponse(Expectation $expectation, TransactionData $transactionData)
 {
     $responseConfig = $expectation->getResponse();
     $httpResponse = $transactionData->getResponse();
     $httpResponse = $this->getResponseWithBody($expectation, $httpResponse, $transactionData->getRequest());
     $httpResponse = $this->getResponseWithStatusCode($responseConfig, $httpResponse);
     $httpResponse = $this->getResponseWithHeaders($responseConfig, $httpResponse);
     $this->processDelay($responseConfig);
     return $httpResponse;
 }
 /**
  *
  * {@inheritDoc}
  *
  * @see \Mcustiel\PowerRoute\Actions\ActionInterface::execute()
  */
 public function execute(TransactionData $transactionData, $argument = null)
 {
     $this->logger->debug('Searching matching expectation for request');
     $request = $transactionData->getRequest();
     $this->logger->info('Request received: ' . $this->getLoggableRequest($request));
     $foundExpectation = $this->searchForMatchingExpectation($request);
     if ($foundExpectation === null) {
         $transactionData->set('foundExpectation', false);
         return;
     }
     $transactionData->set('foundExpectation', $foundExpectation);
 }
 public function execute(TransactionData $transactionData, $argument = null)
 {
     /**
      * @var \Mcustiel\Phiremock\Domain\Expectation $foundExpectation
      */
     $foundExpectation = $transactionData->get('foundExpectation');
     if (!$foundExpectation) {
         (new NotFound())->execute($transactionData);
         return;
     }
     $this->processScenario($foundExpectation);
     $response = $this->responseStrategyFactory->getStrategyForExpectation($foundExpectation)->createResponse($foundExpectation, $transactionData);
     $this->logger->debug('Responding: ' . $this->getLoggableResponse($response));
     $transactionData->setResponse($response);
 }
 protected function processAndGetResponse(TransactionData $transactionData, callable $process)
 {
     try {
         /**
          * @var \Mcustiel\Phiremock\Domain\Expectation $expectation
          */
         $expectation = $this->requestBuilder->parseRequest($this->parseJsonBody($transactionData->getRequest()), Expectation::class, RequestBuilder::RETURN_ALL_ERRORS_IN_EXCEPTION);
         $this->logger->debug('Parsed expectation: ' . var_export($expectation, true));
         return $process($transactionData, $expectation);
     } catch (InvalidRequestException $e) {
         $this->logger->warning('Invalid request received');
         return $this->constructErrorResponse($e->getErrors(), $transactionData->getResponse());
     } catch (\Exception $e) {
         $this->logger->warning('An unexpected exception occurred: ' . $e->getMessage());
         return $this->constructErrorResponse([$e->getMessage()], $transactionData->getResponse());
     }
 }
 /**
  *
  * {@inheritdoc}
  *
  * @see \Mcustiel\Phiremock\Server\Utils\Strategies\ResponseCreatorInterface::createResponse()
  */
 public function createResponse(Expectation $expectation, TransactionData $transactionData)
 {
     $url = $expectation->getProxyTo();
     $this->logger->debug('Proxying request to : ' . $url);
     return $this->httpService->send($transactionData->getRequest()->withUri(new Uri($url)));
 }
 /**
  * {@inheritDoc}
  *
  * @see \Mcustiel\PowerRoute\Actions\ActionInterface::execute()
  */
 public function execute(TransactionData $transactionData, $argument = null)
 {
     $this->requestsStorage->addRequest($transactionData->getRequest());
 }
 /**
  *
  * {@inheritDoc}
  *
  * @see \Mcustiel\PowerRoute\Actions\ActionInterface::execute()
  */
 public function execute(TransactionData $transactionData, $argument = null)
 {
     $this->storage->clearRequests();
     $transactionData->setResponse($transactionData->getResponse()->withStatus(200));
 }
 /**
  *
  * {@inheritDoc}
  *
  * @see \Mcustiel\PowerRoute\Actions\ActionInterface::execute()
  */
 public function execute(TransactionData $transactionData, $argument = null)
 {
     $list = json_encode($this->storage->listExpectations());
     $transactionData->setResponse($transactionData->getResponse()->withBody(new StringStream($list))->withHeader('Content-type', 'application/json'));
 }