|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace App\Tests\Behat; |
| 6 | + |
| 7 | +use Behat\Behat\Context\Context; |
| 8 | +use Behat\Behat\Tester\Exception\PendingException; |
| 9 | +use GuzzleHttp\Client; |
| 10 | +use GuzzleHttp\Exception\RequestException; |
| 11 | +use GuzzleHttp\Exception\TransferException; |
| 12 | +use Psr\Http\Message\ResponseInterface; |
| 13 | +use Symfony\Component\HttpFoundation\Request; |
| 14 | +use Symfony\Component\HttpFoundation\Response; |
| 15 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * This context class contains the definitions of the steps used by the demo |
| 19 | + * feature file. Learn how to get started with Behat and BDD on Behat's website. |
| 20 | + * |
| 21 | + * @see http://behat.org/en/latest/quick_start.html |
| 22 | + */ |
| 23 | +final class RestContext implements Context |
| 24 | +{ |
| 25 | + /** @var KernelInterface */ |
| 26 | + private $kernel; |
| 27 | + |
| 28 | + /** @var \GuzzleHttp\Psr7\Response */ |
| 29 | + private $_response; |
| 30 | + private $_bodyDecoded; |
| 31 | + |
| 32 | + /** @var Client */ |
| 33 | + private $_client; |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + public function __construct(KernelInterface $kernel, string $base_url) |
| 38 | + { |
| 39 | + $this->kernel = $kernel; |
| 40 | + $this->_client = new Client(['base_uri' => $base_url]); |
| 41 | +// throw new \Exception("nenne: ".$base_url); |
| 42 | + } |
| 43 | + /** |
| 44 | + * @When I run GET on :arg1 |
| 45 | + */ |
| 46 | + public function iRunGetOn($arg1) |
| 47 | + { |
| 48 | + try { |
| 49 | + $this->_response = $this->_client->get($arg1); |
| 50 | + |
| 51 | + } catch (RequestException $e) { |
| 52 | + |
| 53 | + if ($e->hasResponse()) { |
| 54 | + $this->_response = $e->getResponse(); |
| 55 | + } else { |
| 56 | + throw $e; |
| 57 | + } |
| 58 | + } catch (\Exception $e) { |
| 59 | + |
| 60 | + throw $e; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @Then the status code is :arg1 |
| 66 | + */ |
| 67 | + public function theStatusCodeIs($arg1) |
| 68 | + { |
| 69 | + if ((string) $this->_response->getStatusCode() !== $arg1) { |
| 70 | + throw new \Exception('HTTP code does not match ' . $arg1 . |
| 71 | + ' (actual: ' . $this->_response->getStatusCode() . ')' . substr($this->_response->getBody(),0,10000)); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @Then the body is json |
| 77 | + */ |
| 78 | + public function theBodyIsJson() |
| 79 | + { |
| 80 | + try { |
| 81 | + $body=json_decode($this->_response->getBody()->getContents(), true); |
| 82 | + $this->_bodyDecoded=$body; |
| 83 | + }catch (\Exception $e ){ |
| 84 | + if($body=$this->_response->getBody()){ |
| 85 | + print "$body\n"; |
| 86 | + throw $e; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments