|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace CodelyTv\Tests\Shared\Infrastructure\Mink; |
| 6 | + |
| 7 | +use Behat\Mink\Driver\DriverInterface; |
| 8 | +use Behat\Mink\Session; |
| 9 | +use Symfony\Component\BrowserKit\AbstractBrowser; |
| 10 | +use Symfony\Component\DomCrawler\Crawler; |
| 11 | +use Symfony\Component\HttpFoundation\Request; |
| 12 | + |
| 13 | +final class MinkHelper |
| 14 | +{ |
| 15 | + private $session; |
| 16 | + |
| 17 | + public function __construct(Session $session) |
| 18 | + { |
| 19 | + $this->session = $session; |
| 20 | + } |
| 21 | + |
| 22 | + public function sendRequest($method, $url, array $optionalParams = []): Crawler |
| 23 | + { |
| 24 | + $defaultOptionalParams = [ |
| 25 | + 'parameters' => [], |
| 26 | + 'files' => [], |
| 27 | + 'server' => ['HTTP_ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json'], |
| 28 | + 'content' => null, |
| 29 | + 'changeHistory' => true, |
| 30 | + ]; |
| 31 | + |
| 32 | + $optionalParams = array_merge($defaultOptionalParams, $optionalParams); |
| 33 | + |
| 34 | + $crawler = $this->getClient()->request( |
| 35 | + $method, |
| 36 | + $url, |
| 37 | + $optionalParams['parameters'], |
| 38 | + $optionalParams['files'], |
| 39 | + $optionalParams['server'], |
| 40 | + $optionalParams['content'], |
| 41 | + $optionalParams['changeHistory'] |
| 42 | + ); |
| 43 | + |
| 44 | + $this->resetRequestStuff(); |
| 45 | + |
| 46 | + return $crawler; |
| 47 | + } |
| 48 | + |
| 49 | + public function getResponse(): string |
| 50 | + { |
| 51 | + return $this->getSession()->getPage()->getContent(); |
| 52 | + } |
| 53 | + |
| 54 | + public function getResponseHeaders(): array |
| 55 | + { |
| 56 | + return $this->normalizeHeaders( |
| 57 | + array_change_key_case($this->getSession()->getResponseHeaders(), CASE_LOWER) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public function resetServerParameters(): void |
| 62 | + { |
| 63 | + $this->getClient()->setServerParameters([]); |
| 64 | + } |
| 65 | + |
| 66 | + public function getRequest(): Request |
| 67 | + { |
| 68 | + return $this->getClient()->getRequest(); |
| 69 | + } |
| 70 | + |
| 71 | + private function getSession(): Session |
| 72 | + { |
| 73 | + return $this->session; |
| 74 | + } |
| 75 | + |
| 76 | + private function getDriver(): DriverInterface |
| 77 | + { |
| 78 | + return $this->getSession()->getDriver(); |
| 79 | + } |
| 80 | + |
| 81 | + private function getClient(): AbstractBrowser |
| 82 | + { |
| 83 | + return $this->getDriver()->getClient(); |
| 84 | + } |
| 85 | + |
| 86 | + private function normalizeHeaders(array $headers): array |
| 87 | + { |
| 88 | + return array_map('implode', array_filter($headers)); |
| 89 | + } |
| 90 | + |
| 91 | + private function resetRequestStuff(): void |
| 92 | + { |
| 93 | + $this->getSession()->reset(); |
| 94 | + $this->resetServerParameters(); |
| 95 | + } |
| 96 | +} |
0 commit comments