Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom requester + date match + use trait #56

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"guzzlehttp/guzzle": "6.*",
"guzzlehttp/guzzle": "7.*",
"byjg/uri": "2.0.*",
"php": ">=5.6",
"ext-json": "*"
Expand Down
47 changes: 17 additions & 30 deletions src/AbstractRequester.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
use ByJG\ApiTools\Base\Schema;
use ByJG\ApiTools\Exception\NotMatchedException;
use ByJG\ApiTools\Exception\StatusCodeNotMatchedException;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use ByJG\ApiTools\Response\ResponseInterface;

/**
* Abstract baseclass for request handlers.
Expand Down Expand Up @@ -43,13 +39,16 @@ public function __construct()
/**
* abstract function to be implemented by derived classes
*
* This function must be implemented by derived classes. It should process
* the given request and return an according response.
* This function must be implemented by derived classes. It should build a
* request to given path and headers and return a ResponseInterface (even for failed
* request).
*
* @param string $path
* @param array $headers
*
* @param RequestInterface $request
* @return ResponseInterface
*/
abstract protected function handleRequest(RequestInterface $request);
abstract protected function handleRequest($path, $headers);

/**
* @param Schema $schema
Expand Down Expand Up @@ -125,7 +124,7 @@ public function withQuery($query)
}

/**
* @param null $requestBody
* @param array|null $requestBody
* @return $this
*/
public function withRequestBody($requestBody)
Expand Down Expand Up @@ -155,7 +154,7 @@ public function assertHeaderContains($header, $contains)
* @throws Exception\HttpMethodNotFoundException
* @throws Exception\InvalidDefinitionException
* @throws Exception\PathNotFoundException
* @throws GuzzleException
* @throws Exception\GenericSwaggerException
* @throws NotMatchedException
* @throws StatusCodeNotMatchedException
*/
Expand All @@ -179,33 +178,21 @@ public function send()
);

// Defining Variables
$serverUrl = $this->schema->getServerUrl();
$basePath = $this->schema->getBasePath();
$pathName = $this->path;

// Check if the body is the expected before request
$bodyRequestDef = $this->schema->getRequestParameters("$basePath$pathName", $this->method);
$bodyRequestDef->match($this->requestBody);

// Make the request
$request = new Request(
$this->method,
$serverUrl . $pathName . $paramInQuery,
$header,
json_encode($this->requestBody)
);

$statusReturned = null;
try {
$response = $this->handleRequest($request);
$responseHeader = $response->getHeaders();
$responseBody = json_decode((string) $response->getBody(), true);
$statusReturned = $response->getStatusCode();
} catch (BadResponseException $ex) {
$responseHeader = $ex->getResponse()->getHeaders();
$responseBody = json_decode((string) $ex->getResponse()->getBody(), true);
$statusReturned = $ex->getResponse()->getStatusCode();
}
// Run the request
$response = $this->handleRequest($pathName . $paramInQuery, $header);

// Get the response
$responseHeader = $response->getHeaders();
$responseBody = json_decode((string) $response->getBody(), true);
$statusReturned = $response->getStatusCode();

// Assert results
if ($this->statusExpected != $statusReturned) {
Expand Down
31 changes: 27 additions & 4 deletions src/ApiRequester.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace ByJG\ApiTools;

use ByJG\ApiTools\Response\ResponseInterface;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use ByJG\ApiTools\Response\PsrResponse;

/**
* Request handler based on a Guzzle client.
Expand All @@ -17,11 +20,31 @@ class ApiRequester extends AbstractRequester

public function __construct()
{
parent::__construct();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, that's a fix I'd take.

$this->guzzleHttpClient = new Client(['headers' => ['User-Agent' => 'Swagger Test']]);
}

protected function handleRequest(RequestInterface $request)
/**
* @param string $path
* @param array $headers
*
* @return ResponseInterface
* @throws GuzzleException
*/
protected function handleRequest($path, $headers)
{
return $this->guzzleHttpClient->send($request, ['allow_redirects' => false]);
// Make the request
$request = new Request(
$this->method,
$this->schema->getServerUrl() . $path,
$headers,
json_encode($this->requestBody)
);

try {
return new PsrResponse($this->guzzleHttpClient->send($request, ['allow_redirects' => false]));
} catch (BadResponseException $ex) {
return new PsrResponse($ex->getResponse());
}
}
}
114 changes: 1 addition & 113 deletions src/ApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,122 +3,10 @@
namespace ByJG\ApiTools;

use ByJG\ApiTools\Base\BaseTestCase;
use ByJG\ApiTools\Base\Schema;
use ByJG\ApiTools\Exception\DefinitionNotFoundException;
use ByJG\ApiTools\Exception\GenericSwaggerException;
use ByJG\ApiTools\Exception\HttpMethodNotFoundException;
use ByJG\ApiTools\Exception\InvalidDefinitionException;
use ByJG\ApiTools\Exception\NotMatchedException;
use ByJG\ApiTools\Exception\PathNotFoundException;
use ByJG\ApiTools\Exception\StatusCodeNotMatchedException;
use GuzzleHttp\GuzzleException;
use PHPUnit\Framework\TestCase;

abstract class ApiTestCase extends TestCase
{
/**
* @var Schema
*/
protected $schema;

/**
* configure the schema to use for requests
*
* When set, all requests without an own schema use this one instead.
*
* @param Schema|null $schema
*/
public function setSchema($schema)
{
$this->schema = $schema;
}

/**
* @param string $method The HTTP Method: GET, PUT, DELETE, POST, etc
* @param string $path The REST path call
* @param int $statusExpected
* @param array|null $query
* @param array|null $requestBody
* @param array $requestHeader
* @return mixed
* @throws DefinitionNotFoundException
* @throws GenericSwaggerException
* @throws HttpMethodNotFoundException
* @throws InvalidDefinitionException
* @throws NotMatchedException
* @throws PathNotFoundException
* @throws StatusCodeNotMatchedException
* @throws \GuzzleHttp\Exception\GuzzleException
* @deprecated Use assertRequest instead
*/
protected function makeRequest(
$method,
$path,
$statusExpected = 200,
$query = null,
$requestBody = null,
$requestHeader = []
) {
$this->checkSchema();
$requester = new ApiRequester();
$body = $requester
->withSchema($this->schema)
->withMethod($method)
->withPath($path)
->withQuery($query)
->withRequestBody($requestBody)
->withRequestHeader($requestHeader)
->assertResponseCode($statusExpected)
->send();

// Note:
// This code is only reached if the send is successful and
// all matches are satisfied. Otherwise an error is throwed before
// reach this
$this->assertTrue(true);

return $body;
}

/**
* @param AbstractRequester $request
* @return mixed
* @throws DefinitionNotFoundException
* @throws GenericSwaggerException
* @throws HttpMethodNotFoundException
* @throws InvalidDefinitionException
* @throws NotMatchedException
* @throws PathNotFoundException
* @throws StatusCodeNotMatchedException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function assertRequest(AbstractRequester $request)
{
// Add own schema if nothing is passed.
if (!$request->hasSchema()) {
$this->checkSchema();
$request->withSchema($this->schema);
}

// Request based on the Swagger Request definitios
$body = $request->send();

// Note:
// This code is only reached if the send is successful and
// all matches are satisfied. Otherwise an error is throwed before
// reach this
$this->assertTrue(true);

return $body;
}

/**
* @throws GenericSwaggerException
*/
protected function checkSchema()
{
if (!$this->schema) {
throw new GenericSwaggerException('You have to configure a schema for either the request or the testcase');
}
}
use AssertRequestAgainstSchema;
}
Loading