From f5f25b77cc18f8cdfb49364ebe9c53eb1700d784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomo=20=C5=A0ala?= Date: Tue, 19 Dec 2017 15:41:32 +0100 Subject: [PATCH] WIP@refactor testing for http response codes in behat - match NoEventsToRaffleException to HTTP response status 400 --- features/bootstrap/RaffleApiContext.php | 24 ++++++++++++++++++- features/bootstrap/RaffleContext.php | 16 +++++++++++++ .../raffle/picking-events-to-raffle.feature | 2 +- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/features/bootstrap/RaffleApiContext.php b/features/bootstrap/RaffleApiContext.php index 6aa9a8d..3450244 100644 --- a/features/bootstrap/RaffleApiContext.php +++ b/features/bootstrap/RaffleApiContext.php @@ -20,12 +20,16 @@ class RaffleApiContext implements Context /** @var string|null */ private $raffleId; + /** @var array|null */ private $picked; /** @var string */ private $testApiUrl = 'http://test.raffler.loc:8000/api'; + /** @var int */ + private $responseCode; + public function __construct(KernelInterface $kernel) { $this->kernel = $kernel; @@ -237,9 +241,13 @@ private function apiPostJson(string $url, array $options = []) try { $response = $this->getGuzzle()->post($this->testApiUrl.$url, $options); + $this->responseCode = $response->getStatusCode(); + return json_decode($response->getBody()->getContents(), true); } catch (ClientException $ex) { - return $ex->getMessage(); + $this->responseCode = $ex->getCode(); + + return; } } @@ -252,4 +260,18 @@ protected function getService(string $name) { return $this->kernel->getContainer()->get($name); } + + /** + * @Then /^we get an http response with status code (\d+)$/ + */ + public function weGetAnHttpResponseWithCode(int $statusCode) + { + $options = [ + 'json' => ['events' => []], + ]; + + $this->apiPostJson('/raffle/start', $options); + + Assert::eq($this->responseCode, $statusCode); + } } diff --git a/features/bootstrap/RaffleContext.php b/features/bootstrap/RaffleContext.php index 159e223..40e65ee 100644 --- a/features/bootstrap/RaffleContext.php +++ b/features/bootstrap/RaffleContext.php @@ -240,6 +240,22 @@ public function weCannotContinueRaffling() } } + /** + * @Then /^we get an http response with status code (\d+)$/ + */ + public function weGetAnHttpResponseWithCode(int $statusCode) + { +// $options = [ +// 'json' => ['events' => []], +// ]; +// +// $this->apiPostJson('/raffle/start', $options); +// +// Assert::eq($this->responseCode, $statusCode); + + $this->weGetAnExceptionForARaffleWithNoMeetups(); + } + /** * @Then we get an exception for a raffle with no comments */ diff --git a/features/raffle/picking-events-to-raffle.feature b/features/raffle/picking-events-to-raffle.feature index d3a63e1..2c96050 100644 --- a/features/raffle/picking-events-to-raffle.feature +++ b/features/raffle/picking-events-to-raffle.feature @@ -36,7 +36,7 @@ Feature: Then there should be 2 events on the raffle Scenario: Organizer will try to start a raffle with no events - Then we get an exception for a raffle with no meetups + Then we get an http response with status code 400 Scenario: Organizer will try to start a raffle with events not having any comments Given we have these uncommented meetups in the system