Skip to content

Instantly share code, notes, and snippets.

@panvid
Created August 3, 2018 12:50
Show Gist options
  • Save panvid/d3deb5032208a626c385f08959c913c0 to your computer and use it in GitHub Desktop.
Save panvid/d3deb5032208a626c385f08959c913c0 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace CoreTest;
use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;
/**
* Class AbstractApiTest
*
* @author david.pauli
* @copyright Copyright (c) 2018 CHECK24 Vergleichsportal Reise GmbH
* @link https://bitbucket.check24.de/projects/DP/repos/core/browse
* @package CoreTest
* @since 03.08.2018
*/
class AbstractApiTest extends TestCase
{
/** @var Client */
private $client;
private $mdWriter;
private $annotationReader;
private $response;
/** @var string */
protected $uri;
/** @var string[] */
private $collectedHeaders;
/** @var array */
private $collectedParameters;
/**
* Getting a resource is the coolest way to make something.
*
* This is the nice description of the test get.
*/
public function testGet(): void
{
$this->addHeaders(['Authorization' => 'Basic FooBar']);
$this->addParameters(['requestType' => 'search']);
$response = $this->get();
static::assertEquals(200, $response->getStatusCode());
}
### Abstraction Magic
protected function addHeaders(array $headers)
{
$this->collectedHeaders[] = $headers;
}
protected function addParameters(array $parameters)
{
$this->collectedParameters[] = $parameters;
}
protected function get()
{
$this->response = $this->client->get();
$this->writeToMd();
return $this->response;
}
protected function put()
{
$this->response = $this->client->put();
$this->writeToMd();
return $this->response;
}
protected function post()
{
$this->response = $this->client->post();
$this->writeToMd();
return $this->response;
}
protected function delete()
{
$this->response = $this->client->delete();
$this->writeToMd();
return $this->response;
}
private function writeToMd()
{
$annotations = $this->annotationReader->read();
$this->mdWriter
->addHead($annotations)
->addRequestUrl($this->uri)
->addRequestParams($this->collectedParameters)
->addRequestHeaders($this->collectedHeaders)
->addResponse($this->response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment