Skip to content

Commit

Permalink
Merge pull request #56 from Maks3w/phpunit_8
Browse files Browse the repository at this point in the history
PHPUnit v8
  • Loading branch information
Maks3w authored Mar 19, 2019
2 parents ef08f0b + c501645 commit a950ed4
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 207 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
"homepage": "https://github.com/Maks3w/SwaggerAssertions",
"require": {
"php": ">= 7.1",
"ext-json": "*",
"justinrainbow/json-schema": "^5",
"phpunit/phpunit": "^6.0||^7.0",
"phpunit/phpunit": "^7.5||^8.0",
"rize/uri-template": "^0.3.0",
"zendframework/zend-http": "2.5 - 3"
},
Expand Down
8 changes: 4 additions & 4 deletions examples/PhpUnit/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ class AssertTest extends TestCase
*/
protected $guzzleHttpClient;

public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
self::$schemaManager = SchemaManager::fromUri('http://petstore.swagger.io/v2/swagger.json');
}

protected function setUp()
protected function setUp(): void
{
$this->guzzleHttpClient = new Client(['headers' => ['User-Agent' => 'https://github.com/Maks3w/SwaggerAssertions']]);
}

public function testFetchPetBodyMatchDefinition()
public function testFetchPetBodyMatchDefinition(): void
{
$request = new Request('GET', 'http://petstore.swagger.io/v2/pet/findByStatus');
$request = $request->withHeader('Accept', 'application/json');
Expand All @@ -45,6 +45,6 @@ public function testFetchPetBodyMatchDefinition()

$responseBody = json_decode((string) $response->getBody());

$this->assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200);
self::assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200);
}
}
8 changes: 4 additions & 4 deletions examples/PhpUnit/LocalFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ class LocalFileTest extends TestCase
*/
protected $guzzleHttpClient;

public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
$filePath = __DIR__ . '/../fixtures/pet_store.json';

// Use file:// for local files
self::$schemaManager = new SchemaManager(json_decode(file_get_contents($filePath)));
}

protected function setUp()
protected function setUp(): void
{
$this->guzzleHttpClient = new Client(['headers' => ['User-Agent' => 'https://github.com/Maks3w/SwaggerAssertions']]);
}

public function testFetchPetBodyMatchDefinition()
public function testFetchPetBodyMatchDefinition(): void
{
$request = new Request('GET', 'http://petstore.swagger.io/v2/pet/findByStatus');
$request = $request->withHeader('Accept', 'application/json');
Expand All @@ -48,6 +48,6 @@ public function testFetchPetBodyMatchDefinition()

$responseBody = json_decode((string) $response->getBody());

$this->assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200);
self::assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200);
}
}
12 changes: 6 additions & 6 deletions examples/PhpUnit/Psr7WithGuzzleV6Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,33 @@ class Psr7WithGuzzleV6Test extends TestCase
*/
protected $guzzleHttpClient;

public static function setUpBeforeClass()
public static function setUpBeforeClass(): void
{
self::$schemaManager = SchemaManager::fromUri('http://petstore.swagger.io/v2/swagger.json');
}

protected function setUp()
protected function setUp(): void
{
$this->guzzleHttpClient = new Client(['headers' => ['User-Agent' => 'https://github.com/Maks3w/SwaggerAssertions']]);
}

public function testFetchPetMatchDefinition()
public function testFetchPetMatchDefinition(): void
{
$request = new Request('GET', 'http://petstore.swagger.io/v2/store/inventory');
$request = $request->withHeader('Accept', 'application/json');

$response = $this->guzzleHttpClient->send($request);

$this->assertResponseAndRequestMatch($response, $request, self::$schemaManager);
self::assertResponseAndRequestMatch($response, $request, self::$schemaManager);
}

public function testOnlyResponse()
public function testOnlyResponse(): void
{
$request = new Request('GET', 'http://petstore.swagger.io/v2/pet/findByStatus');
$request = $request->withHeader('Accept', 'application/json');

$response = $this->guzzleHttpClient->send($request);

$this->assertResponseMatch($response, self::$schemaManager, '/v2/pet/findByStatus', 'get');
self::assertResponseMatch($response, self::$schemaManager, '/v2/pet/findByStatus', 'get');
}
}
45 changes: 22 additions & 23 deletions src/PhpUnit/AssertsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use FR3D\SwaggerAssertions\SchemaManager;
use JsonSchema\Validator;
use PHPUnit\Framework\Assert;
use stdClass;
use Zend\Http\Header\ContentType;

/**
Expand All @@ -18,46 +17,46 @@ trait AssertsTrait
/**
* Asserts response body match with the response schema.
*
* @param stdClass|stdClass[] $responseBody
* @param mixed $responseBody
* @param string $path percent-encoded path used on the request.
*/
public function assertResponseBodyMatch(
public static function assertResponseBodyMatch(
$responseBody,
SchemaManager $schemaManager,
string $path,
string $httpMethod,
int $httpCode,
string $message = ''
) {
): void {
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
throw new \RuntimeException('Request URI does not match with any swagger path definition');
}

$bodySchema = $schemaManager->getResponseSchema($template, $httpMethod, (string) $httpCode);
$constraint = new JsonSchemaConstraint($bodySchema, 'response body', $this->getValidator());
$constraint = new JsonSchemaConstraint($bodySchema, 'response body', self::getValidator());

Assert::assertThat($responseBody, $constraint, $message);
}

/**
* Asserts request body match with the request schema.
*
* @param stdClass|stdClass[] $requestBody
* @param mixed $requestBody
* @param string $path percent-encoded path used on the request.
*/
public function assertRequestBodyMatch(
public static function assertRequestBodyMatch(
$requestBody,
SchemaManager $schemaManager,
string $path,
string $httpMethod,
string $message = ''
) {
): void {
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
throw new \RuntimeException('Request URI does not match with any swagger path definition');
}

$bodySchema = $schemaManager->getRequestSchema($template, $httpMethod);
$constraint = new JsonSchemaConstraint($bodySchema, 'request body', $this->getValidator());
$constraint = new JsonSchemaConstraint($bodySchema, 'request body', self::getValidator());

Assert::assertThat($requestBody, $constraint, $message);
}
Expand All @@ -67,13 +66,13 @@ public function assertRequestBodyMatch(
*
* @param string $path percent-encoded path used on the request.
*/
public function assertResponseMediaTypeMatch(
public static function assertResponseMediaTypeMatch(
string $responseMediaType,
SchemaManager $schemaManager,
string $path,
string $httpMethod,
string $message = ''
) {
): void {
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
// @codeCoverageIgnoreStart
throw new \RuntimeException('Request URI does not match with any swagger path definition');
Expand All @@ -94,13 +93,13 @@ public function assertResponseMediaTypeMatch(
*
* @param string $path percent-encoded path used on the request.
*/
public function assertRequestMediaTypeMatch(
public static function assertRequestMediaTypeMatch(
string $requestMediaType,
SchemaManager $schemaManager,
string $path,
string $httpMethod,
string $message = ''
) {
): void {
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
// @codeCoverageIgnoreStart
throw new \RuntimeException('Request URI does not match with any swagger path definition');
Expand All @@ -122,14 +121,14 @@ public function assertRequestMediaTypeMatch(
* @param string[] $headers
* @param string $path percent-encoded path used on the request.
*/
public function assertResponseHeadersMatch(
public static function assertResponseHeadersMatch(
array $headers,
SchemaManager $schemaManager,
string $path,
string $httpMethod,
int $httpCode,
string $message = ''
) {
): void {
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
// @codeCoverageIgnoreStart
throw new \RuntimeException('Request URI does not match with any swagger path definition');
Expand All @@ -138,7 +137,7 @@ public function assertResponseHeadersMatch(

$constraint = new ResponseHeadersConstraint(
$schemaManager->getResponseHeaders($template, $httpMethod, (string) $httpCode),
$this->getValidator()
self::getValidator()
);

Assert::assertThat($headers, $constraint, $message);
Expand All @@ -150,20 +149,20 @@ public function assertResponseHeadersMatch(
* @param string[] $headers
* @param string $path percent-encoded path used on the request.
*/
public function assertRequestHeadersMatch(
public static function assertRequestHeadersMatch(
array $headers,
SchemaManager $schemaManager,
string $path,
string $httpMethod,
string $message = ''
) {
): void {
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
// @codeCoverageIgnoreStart
throw new \RuntimeException('Request URI does not match with any swagger path definition');
// @codeCoverageIgnoreEnd
}

$constraint = new RequestHeadersConstraint($schemaManager->getRequestHeadersParameters($template, $httpMethod), $this->getValidator());
$constraint = new RequestHeadersConstraint($schemaManager->getRequestHeadersParameters($template, $httpMethod), self::getValidator());

Assert::assertThat($headers, $constraint, $message);
}
Expand All @@ -174,25 +173,25 @@ public function assertRequestHeadersMatch(
* @param mixed[] $query
* @param string $path percent-encoded path used on the request.
*/
public function assertRequestQueryMatch(
public static function assertRequestQueryMatch(
$query,
SchemaManager $schemaManager,
string $path,
string $httpMethod,
string $message = ''
) {
): void {
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
// @codeCoverageIgnoreStart
throw new \RuntimeException('Request URI does not match with any swagger path definition');
// @codeCoverageIgnoreEnd
}

$constraint = new RequestQueryConstraint($schemaManager->getRequestQueryParameters($template, $httpMethod), $this->getValidator());
$constraint = new RequestQueryConstraint($schemaManager->getRequestQueryParameters($template, $httpMethod), self::getValidator());

Assert::assertThat($query, $constraint, $message);
}

protected function getValidator(): Validator
protected static function getValidator(): Validator
{
return new Validator();
}
Expand Down
4 changes: 3 additions & 1 deletion src/PhpUnit/JsonSchemaConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class JsonSchemaConstraint extends Constraint

public function __construct($expectedSchema, string $context, Validator $validator)
{
parent::__construct();
if (is_callable([Constraint::class, '__construct'])) {
parent::__construct();
}

$this->expectedSchema = $expectedSchema;
$this->context = $context;
Expand Down
4 changes: 3 additions & 1 deletion src/PhpUnit/MediaTypeConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class MediaTypeConstraint extends Constraint
*/
public function __construct(array $allowedMediaTypes)
{
parent::__construct();
if (is_callable([Constraint::class, '__construct'])) {
parent::__construct();
}

$this->allowedMediaTypes = $allowedMediaTypes;
}
Expand Down
Loading

0 comments on commit a950ed4

Please sign in to comment.