From 045431dcb49c31d14ab52523e6c1413f09db7c70 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 19 Mar 2019 09:52:36 +0100 Subject: [PATCH 1/3] Make assertions methods static. Add return typehints --- examples/PhpUnit/AssertTest.php | 8 +-- examples/PhpUnit/LocalFileTest.php | 8 +-- examples/PhpUnit/Psr7WithGuzzleV6Test.php | 12 ++-- src/PhpUnit/AssertsTrait.php | 45 +++++++-------- src/PhpUnit/Psr7AssertsTrait.php | 36 ++++++------ src/PhpUnit/SymfonyAssertsTrait.php | 36 ++++++------ tests/PhpUnit/AssertsTraitTest.php | 32 +++++------ tests/PhpUnit/JsonSchemaConstraintTest.php | 8 +-- tests/PhpUnit/MediaTypeConstraintTest.php | 8 +-- tests/PhpUnit/Psr7AssertsTraitTest.php | 57 +++++++------------ .../PhpUnit/RequestHeadersConstraintTest.php | 14 ++--- tests/PhpUnit/RequestQueryConstraintTest.php | 12 ++-- .../PhpUnit/ResponseHeadersConstraintTest.php | 10 ++-- tests/PhpUnit/SymfonyAssertsTraitTest.php | 51 ++++++----------- tests/SchemaManagerTest.php | 38 ++++++------- 15 files changed, 173 insertions(+), 202 deletions(-) diff --git a/examples/PhpUnit/AssertTest.php b/examples/PhpUnit/AssertTest.php index ceec2af..80e1173 100644 --- a/examples/PhpUnit/AssertTest.php +++ b/examples/PhpUnit/AssertTest.php @@ -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'); @@ -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); } } diff --git a/examples/PhpUnit/LocalFileTest.php b/examples/PhpUnit/LocalFileTest.php index de21a79..946644a 100644 --- a/examples/PhpUnit/LocalFileTest.php +++ b/examples/PhpUnit/LocalFileTest.php @@ -26,7 +26,7 @@ class LocalFileTest extends TestCase */ protected $guzzleHttpClient; - public static function setUpBeforeClass() + public static function setUpBeforeClass(): void { $filePath = __DIR__ . '/../fixtures/pet_store.json'; @@ -34,12 +34,12 @@ public static function setUpBeforeClass() 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'); @@ -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); } } diff --git a/examples/PhpUnit/Psr7WithGuzzleV6Test.php b/examples/PhpUnit/Psr7WithGuzzleV6Test.php index 1c5a7e4..36307fb 100644 --- a/examples/PhpUnit/Psr7WithGuzzleV6Test.php +++ b/examples/PhpUnit/Psr7WithGuzzleV6Test.php @@ -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'); } } diff --git a/src/PhpUnit/AssertsTrait.php b/src/PhpUnit/AssertsTrait.php index 7dab9a2..f8d4147 100644 --- a/src/PhpUnit/AssertsTrait.php +++ b/src/PhpUnit/AssertsTrait.php @@ -7,7 +7,6 @@ use FR3D\SwaggerAssertions\SchemaManager; use JsonSchema\Validator; use PHPUnit\Framework\Assert; -use stdClass; use Zend\Http\Header\ContentType; /** @@ -18,23 +17,23 @@ 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); } @@ -42,22 +41,22 @@ public function assertResponseBodyMatch( /** * 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); } @@ -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'); @@ -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'); @@ -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'); @@ -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); @@ -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); } @@ -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(); } diff --git a/src/PhpUnit/Psr7AssertsTrait.php b/src/PhpUnit/Psr7AssertsTrait.php index 18b82c7..6b81410 100644 --- a/src/PhpUnit/Psr7AssertsTrait.php +++ b/src/PhpUnit/Psr7AssertsTrait.php @@ -21,15 +21,15 @@ trait Psr7AssertsTrait * * @param string $path percent-encoded path used on the request. */ - public function assertResponseMatch( + public static function assertResponseMatch( ResponseInterface $response, SchemaManager $schemaManager, string $path, string $httpMethod, string $message = '' - ) { + ): void { if (!empty((string) $response->getBody())) { - $this->assertResponseMediaTypeMatch( + self::assertResponseMediaTypeMatch( $response->getHeaderLine('Content-Type'), $schemaManager, $path, @@ -39,9 +39,9 @@ public function assertResponseMatch( } $httpCode = $response->getStatusCode(); - $headers = $this->inlineHeaders($response->getHeaders()); + $headers = self::inlineHeaders($response->getHeaders()); - $this->assertResponseHeadersMatch( + self::assertResponseHeadersMatch( $headers, $schemaManager, $path, @@ -50,7 +50,7 @@ public function assertResponseMatch( $message ); - $this->assertResponseBodyMatch( + self::assertResponseBodyMatch( json_decode((string) $response->getBody()), $schemaManager, $path, @@ -63,20 +63,20 @@ public function assertResponseMatch( /** * Asserts request match with the request schema. */ - public function assertRequestMatch( + public static function assertRequestMatch( RequestInterface $request, SchemaManager $schemaManager, string $message = '' - ) { + ): void { $path = $request->getUri()->getPath(); $httpMethod = $request->getMethod(); - $headers = $this->inlineHeaders($request->getHeaders()); + $headers = self::inlineHeaders($request->getHeaders()); $queryString = $request->getUri()->getQuery(); parse_str(html_entity_decode($queryString), $query); - $this->assertRequestHeadersMatch( + self::assertRequestHeadersMatch( $headers, $schemaManager, $path, @@ -85,7 +85,7 @@ public function assertRequestMatch( ); if (!empty((string) $request->getBody())) { - $this->assertRequestMediaTypeMatch( + self::assertRequestMediaTypeMatch( $request->getHeaderLine('Content-Type'), $schemaManager, $path, @@ -94,7 +94,7 @@ public function assertRequestMatch( ); } - $this->assertRequestQueryMatch( + self::assertRequestQueryMatch( $query, $schemaManager, $path, @@ -102,7 +102,7 @@ public function assertRequestMatch( $message ); - $this->assertRequestBodyMatch( + self::assertRequestBodyMatch( json_decode((string) $request->getBody()), $schemaManager, $path, @@ -114,14 +114,14 @@ public function assertRequestMatch( /** * Asserts response match with the response schema. */ - public function assertResponseAndRequestMatch( + public static function assertResponseAndRequestMatch( ResponseInterface $response, RequestInterface $request, SchemaManager $schemaManager, string $message = '' - ) { + ): void { try { - $this->assertRequestMatch($request, $schemaManager, $message); + self::assertRequestMatch($request, $schemaManager, $message); } catch (ExpectationFailedException $e) { // If response represent a Client error then ignore. $statusCode = $response->getStatusCode(); @@ -130,7 +130,7 @@ public function assertResponseAndRequestMatch( } } - $this->assertResponseMatch($response, $schemaManager, $request->getUri()->getPath(), $request->getMethod(), $message); + self::assertResponseMatch($response, $schemaManager, $request->getUri()->getPath(), $request->getMethod(), $message); } /** @@ -138,7 +138,7 @@ public function assertResponseAndRequestMatch( * * @return string[] */ - protected function inlineHeaders(array $headers): array + protected static function inlineHeaders(array $headers): array { return array_map( function (array $headers) { diff --git a/src/PhpUnit/SymfonyAssertsTrait.php b/src/PhpUnit/SymfonyAssertsTrait.php index aedf019..f02714c 100644 --- a/src/PhpUnit/SymfonyAssertsTrait.php +++ b/src/PhpUnit/SymfonyAssertsTrait.php @@ -21,15 +21,15 @@ trait SymfonyAssertsTrait * * @param string $path percent-encoded path used on the request. */ - public function assertResponseMatch( + public static function assertResponseMatch( Response $response, SchemaManager $schemaManager, string $path, string $httpMethod, string $message = '' - ) { + ): void { if (!empty((string) $response->getContent())) { - $this->assertResponseMediaTypeMatch( + self::assertResponseMediaTypeMatch( $response->headers->get('Content-Type'), $schemaManager, $path, @@ -39,9 +39,9 @@ public function assertResponseMatch( } $httpCode = $response->getStatusCode(); - $headers = $this->inlineHeaders($response->headers->all()); + $headers = self::inlineHeaders($response->headers->all()); - $this->assertResponseHeadersMatch( + self::assertResponseHeadersMatch( $headers, $schemaManager, $path, @@ -50,7 +50,7 @@ public function assertResponseMatch( $message ); - $this->assertResponseBodyMatch( + self::assertResponseBodyMatch( json_decode($response->getContent()), $schemaManager, $path, @@ -63,18 +63,18 @@ public function assertResponseMatch( /** * Asserts request match with the request schema. */ - public function assertRequestMatch( + public static function assertRequestMatch( Request $request, SchemaManager $schemaManager, string $message = '' - ) { + ): void { $path = $request->getPathInfo(); $httpMethod = $request->getMethod(); $query = $request->query->all(); - $headers = $this->inlineHeaders($request->headers->all()); + $headers = self::inlineHeaders($request->headers->all()); - $this->assertRequestHeadersMatch( + self::assertRequestHeadersMatch( $headers, $schemaManager, $path, @@ -83,7 +83,7 @@ public function assertRequestMatch( ); if (!empty((string) $request->getContent())) { - $this->assertRequestMediaTypeMatch( + self::assertRequestMediaTypeMatch( $request->headers->get('Content-Type'), $schemaManager, $path, @@ -92,7 +92,7 @@ public function assertRequestMatch( ); } - $this->assertRequestQueryMatch( + self::assertRequestQueryMatch( $query, $schemaManager, $path, @@ -100,7 +100,7 @@ public function assertRequestMatch( $message ); - $this->assertRequestBodyMatch( + self::assertRequestBodyMatch( json_decode($request->getContent()), $schemaManager, $path, @@ -112,14 +112,14 @@ public function assertRequestMatch( /** * Asserts response match with the response schema. */ - public function assertResponseAndRequestMatch( + public static function assertResponseAndRequestMatch( Response $response, Request $request, SchemaManager $schemaManager, string $message = '' - ) { + ): void { try { - $this->assertRequestMatch($request, $schemaManager, $message); + self::assertRequestMatch($request, $schemaManager, $message); } catch (ExpectationFailedException $e) { // If response represent a Client error then ignore. $statusCode = $response->getStatusCode(); @@ -128,7 +128,7 @@ public function assertResponseAndRequestMatch( } } - $this->assertResponseMatch($response, $schemaManager, $request->getRequestUri(), $request->getMethod(), $message); + self::assertResponseMatch($response, $schemaManager, $request->getRequestUri(), $request->getMethod(), $message); } /** @@ -136,7 +136,7 @@ public function assertResponseAndRequestMatch( * * @return string[] */ - protected function inlineHeaders(array $headers): array + protected static function inlineHeaders(array $headers): array { return array_map( function (array $headers) { diff --git a/tests/PhpUnit/AssertsTraitTest.php b/tests/PhpUnit/AssertsTraitTest.php index 63dd30c..790114d 100644 --- a/tests/PhpUnit/AssertsTraitTest.php +++ b/tests/PhpUnit/AssertsTraitTest.php @@ -20,12 +20,12 @@ class AssertsTraitTest extends TestCase */ protected $schemaManager; - protected function setUp() + protected function setUp(): void { $this->schemaManager = SchemaManager::fromUri('file://' . __DIR__ . '/../fixture/petstore-with-external-docs.json'); } - public function testAssertResponseBodyMatch() + public function testAssertResponseBodyMatch(): void { $response = <<<'JSON' { @@ -38,14 +38,14 @@ public function testAssertResponseBodyMatch() self::assertResponseBodyMatch($response, $this->schemaManager, '/api/pets/123456789', 'get', 200); } - public function testAssertResponseBodyMatchWithFile() + public function testAssertResponseBodyMatchWithFile(): void { $valid_gif_file = base64_decode('R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=', true); self::assertResponseBodyMatch($valid_gif_file, $this->schemaManager, '/api/pets/123456789/photo', 'get', 200); } - public function testAssertResponseBodyMatchFail() + public function testAssertResponseBodyMatchFail(): void { $response = <<<'JSON' [ @@ -64,12 +64,12 @@ public function testAssertResponseBodyMatchFail() } } - public function testValidResponseMediaType() + public function testValidResponseMediaType(): void { self::assertResponseMediaTypeMatch('text/html', $this->schemaManager, '/api/pets', 'get'); } - public function testInvalidResponseMediaType() + public function testInvalidResponseMediaType(): void { try { self::assertResponseMediaTypeMatch('application/pdf', $this->schemaManager, '/api/pets', 'get'); @@ -79,7 +79,7 @@ public function testInvalidResponseMediaType() } } - public function testValidResponseHeaders() + public function testValidResponseHeaders(): void { $headers = [ 'ETag' => '123', @@ -88,7 +88,7 @@ public function testValidResponseHeaders() self::assertResponseHeadersMatch($headers, $this->schemaManager, '/api/pets', 'get', 200); } - public function testInvalidResponseHeaders() + public function testInvalidResponseHeaders(): void { $headers = []; @@ -100,7 +100,7 @@ public function testInvalidResponseHeaders() } } - public function testAssertRequestBodyMatch() + public function testAssertRequestBodyMatch(): void { $request = <<<'JSON' { @@ -113,7 +113,7 @@ public function testAssertRequestBodyMatch() self::assertRequestBodyMatch($request, $this->schemaManager, '/api/pets', 'post'); } - public function testAssertRequestBodyMatchFail() + public function testAssertRequestBodyMatchFail(): void { $request = <<<'JSON' { @@ -130,12 +130,12 @@ public function testAssertRequestBodyMatchFail() } } - public function testValidRequestMediaType() + public function testValidRequestMediaType(): void { self::assertRequestMediaTypeMatch('application/json', $this->schemaManager, '/api/pets', 'post'); } - public function testInvalidRequestMediaType() + public function testInvalidRequestMediaType(): void { try { self::assertRequestMediaTypeMatch('application/pdf', $this->schemaManager, '/api/pets', 'post'); @@ -145,7 +145,7 @@ public function testInvalidRequestMediaType() } } - public function testValidRequestHeaders() + public function testValidRequestHeaders(): void { $headers = [ 'X-Required-Header' => 'any', @@ -154,7 +154,7 @@ public function testValidRequestHeaders() self::assertRequestHeadersMatch($headers, $this->schemaManager, '/api/pets/1234', 'patch'); } - public function testInvalidRequestHeaders() + public function testInvalidRequestHeaders(): void { $headers = []; @@ -166,14 +166,14 @@ public function testInvalidRequestHeaders() } } - public function testValidRequestQuery() + public function testValidRequestQuery(): void { $query = ['tags' => ['foo', 'bar'], 'limit' => 1]; self::assertRequestQueryMatch($query, $this->schemaManager, '/api/pets', 'get'); } - public function testInvalidRequestQuery() + public function testInvalidRequestQuery(): void { $query = ['tags' => ['foo', 'bar']]; diff --git a/tests/PhpUnit/JsonSchemaConstraintTest.php b/tests/PhpUnit/JsonSchemaConstraintTest.php index 094d7ab..e83385d 100644 --- a/tests/PhpUnit/JsonSchemaConstraintTest.php +++ b/tests/PhpUnit/JsonSchemaConstraintTest.php @@ -20,7 +20,7 @@ class JsonSchemaConstraintTest extends TestCase */ protected $constraint; - protected function setUp() + protected function setUp(): void { $schema = <<constraint = new JsonSchemaConstraint($schema, 'context', new Validator()); } - public function testConstraintDefinition() + public function testConstraintDefinition(): void { self::assertCount(1, $this->constraint); self::assertEquals('is a valid context', $this->constraint->toString()); } - public function testValidSchema() + public function testValidSchema(): void { $response = <<<'JSON' [ @@ -59,7 +59,7 @@ public function testValidSchema() self::assertTrue($this->constraint->evaluate($response, '', true)); } - public function testInvalidSchema() + public function testInvalidSchema(): void { $response = <<<'JSON' [ diff --git a/tests/PhpUnit/MediaTypeConstraintTest.php b/tests/PhpUnit/MediaTypeConstraintTest.php index 9a37190..80eb394 100644 --- a/tests/PhpUnit/MediaTypeConstraintTest.php +++ b/tests/PhpUnit/MediaTypeConstraintTest.php @@ -19,23 +19,23 @@ class MediaTypeConstraintTest extends TestCase */ protected $constraint; - protected function setUp() + protected function setUp(): void { $this->constraint = new MediaTypeConstraint(['application/json', 'text/xml']); } - public function testConstraintDefinition() + public function testConstraintDefinition(): void { self::assertCount(1, $this->constraint); self::assertEquals('is an allowed media type (application/json, text/xml)', $this->constraint->toString()); } - public function testValidMediaType() + public function testValidMediaType(): void { self::assertTrue($this->constraint->evaluate('text/xml', '', true)); } - public function testInvalidMediaType() + public function testInvalidMediaType(): void { $mediaType = 'application/pdf'; self::assertFalse($this->constraint->evaluate($mediaType, '', true)); diff --git a/tests/PhpUnit/Psr7AssertsTraitTest.php b/tests/PhpUnit/Psr7AssertsTraitTest.php index cbbb505..d765397 100644 --- a/tests/PhpUnit/Psr7AssertsTraitTest.php +++ b/tests/PhpUnit/Psr7AssertsTraitTest.php @@ -6,8 +6,8 @@ use FR3D\SwaggerAssertions\SchemaManager; use PHPUnit\Framework\ExpectationFailedException; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use PHPUnit_Framework_MockObject_MockObject as MockObject; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; @@ -25,19 +25,19 @@ class Psr7AssertsTraitTest extends TestCase */ protected $schemaManager; - protected function setUp() + protected function setUp(): void { $this->schemaManager = SchemaManager::fromUri('file://' . __DIR__ . '/../fixture/petstore-with-external-docs.json'); } - public function testAssertResponseMatch() + public function testAssertResponseMatch(): void { $response = $this->createMockResponse(200, $this->getValidHeaders(), $this->getValidResponseBody()); self::assertResponseMatch($response, $this->schemaManager, '/api/pets', 'get'); } - public function testAssertResponseAndRequestMatch() + public function testAssertResponseAndRequestMatch(): void { $body = $this->getValidRequestBody(); $response = $this->createMockResponse(200, $this->getValidHeaders(), $body); @@ -46,7 +46,7 @@ public function testAssertResponseAndRequestMatch() self::assertResponseAndRequestMatch($response, $request, $this->schemaManager); } - public function testAssertResponseIsValidIfClientErrorAndRequestIsInvalid() + public function testAssertResponseIsValidIfClientErrorAndRequestIsInvalid(): void { $response = $this->createMockResponse(404, $this->getValidHeaders(), '{"code":400,"message":"Invalid"}'); $request = $this->createMockRequest('POST', '/api/pets', ['Content-Type' => ['application/pdf']]); @@ -54,7 +54,7 @@ public function testAssertResponseIsValidIfClientErrorAndRequestIsInvalid() self::assertResponseAndRequestMatch($response, $request, $this->schemaManager); } - public function testAssertRequestIsInvalidIfResponseIsNotAClientError() + public function testAssertRequestIsInvalidIfResponseIsNotAClientError(): void { $response = $this->createMockResponse(200, $this->getValidHeaders(), $this->getValidResponseBody()); $request = $this->createMockRequest('POST', '/api/pets', ['Content-Type' => ['application/pdf']]); @@ -66,7 +66,7 @@ public function testAssertRequestIsInvalidIfResponseIsNotAClientError() } } - public function testAssertResponseBodyDoesNotMatch() + public function testAssertResponseBodyDoesNotMatch(): void { $response = <<<'JSON' [ @@ -93,7 +93,7 @@ public function testAssertResponseBodyDoesNotMatch() } } - public function testAssertResponseMediaTypeDoesNotMatch() + public function testAssertResponseMediaTypeDoesNotMatch(): void { $response = $this->createMockResponse( 200, @@ -112,7 +112,7 @@ public function testAssertResponseMediaTypeDoesNotMatch() } } - public function testAssertResponseHeaderDoesNotMatch() + public function testAssertResponseHeaderDoesNotMatch(): void { $headers = [ 'Content-Type' => ['application/json'], @@ -137,7 +137,7 @@ public function testAssertResponseHeaderDoesNotMatch() } } - public function testAssertRequestBodyDoesNotMatch() + public function testAssertRequestBodyDoesNotMatch(): void { $request = <<<'JSON' { @@ -163,7 +163,7 @@ public function testAssertRequestBodyDoesNotMatch() } } - public function testAssertRequestMediaTypeDoesNotMatch() + public function testAssertRequestMediaTypeDoesNotMatch(): void { $request = $this->createMockRequest( 'POST', @@ -183,7 +183,7 @@ public function testAssertRequestMediaTypeDoesNotMatch() } } - public function testAssertRequestHeaderDoesNotMatch() + public function testAssertRequestHeaderDoesNotMatch(): void { $headers = [ 'Content-Type' => ['application/json'], @@ -208,7 +208,7 @@ public function testAssertRequestHeaderDoesNotMatch() } } - public function testAssertRequestQueryDoesNotMatch() + public function testAssertRequestQueryDoesNotMatch(): void { $query = [ 'tags' => ['foo', '1'], @@ -232,17 +232,14 @@ public function testAssertRequestQueryDoesNotMatch() } } - public function testEmptyResponse() + public function testEmptyResponse(): void { $response = $this->createMockResponse(204, ['Content-Type' => ['']], ''); self::assertResponseMatch($response, $this->schemaManager, '/api/pets/1', 'delete'); } - /** - * @return string - */ - protected function getValidRequestBody() + protected function getValidRequestBody(): string { return <<<'JSON' { @@ -252,10 +249,7 @@ protected function getValidRequestBody() JSON; } - /** - * @return string - */ - protected function getValidResponseBody() + protected function getValidResponseBody(): string { return <<<'JSON' [ @@ -270,7 +264,7 @@ protected function getValidResponseBody() /** * @return string[] */ - protected function getValidHeaders() + protected function getValidHeaders(): array { return [ 'Content-Type' => [ @@ -283,18 +277,15 @@ protected function getValidHeaders() } /** - * @param string $method - * @param string $path * @param string[] $headers - * @param string $body * @param mixed[] $query * * @return MockObject|RequestInterface */ - protected function createMockRequest($method, $path, array $headers, $body = '', $query = []) + protected function createMockRequest(string $method, string $path, array $headers, string $body = '', $query = []) { /** @var UriInterface|MockObject $request */ - $uri = $this->getMockBuilder('Psr\Http\Message\UriInterface')->getMock(); + $uri = $this->getMockBuilder(UriInterface::class)->getMock(); $uri->method('getPath')->willReturn($path); $uri->method('getQuery')->willReturn(http_build_query($query, '', '&')); @@ -312,13 +303,11 @@ protected function createMockRequest($method, $path, array $headers, $body = '', } /** - * @param int $statusCode * @param string[] $headers - * @param string $body * * @return MockObject|ResponseInterface */ - protected function createMockResponse($statusCode, array $headers, $body) + protected function createMockResponse(int $statusCode, array $headers, string $body) { $headersMap = $this->transformHeadersToMap($headers); @@ -333,11 +322,9 @@ protected function createMockResponse($statusCode, array $headers, $body) } /** - * @param string $body - * * @return StreamInterface|MockObject */ - protected function createMockStream($body) + protected function createMockStream(string $body) { /** @var StreamInterface|MockObject $stream */ $stream = $this->getMockBuilder(StreamInterface::class)->getMock(); @@ -351,7 +338,7 @@ protected function createMockStream($body) * * @return array */ - private function transformHeadersToMap(array $headers) + private function transformHeadersToMap(array $headers): array { $headersMap = []; foreach ($headers as $headerName => $headerValues) { diff --git a/tests/PhpUnit/RequestHeadersConstraintTest.php b/tests/PhpUnit/RequestHeadersConstraintTest.php index 6d0b5f0..3f3aa4d 100644 --- a/tests/PhpUnit/RequestHeadersConstraintTest.php +++ b/tests/PhpUnit/RequestHeadersConstraintTest.php @@ -19,22 +19,22 @@ class RequestHeadersConstraintTest extends TestCase * @var Constraint */ protected $constraint; - const TEST_SCHEMA = '[{"name":"X-Required-Header","in":"header","description":"Required header","required":true,"type":"string"},{"name":"X-Optional-Header","in":"header","description":"Optional header","type":"string"}]'; + private const TEST_SCHEMA = '[{"name":"X-Required-Header","in":"header","description":"Required header","required":true,"type":"string"},{"name":"X-Optional-Header","in":"header","description":"Optional header","type":"string"}]'; - protected function setUp() + protected function setUp(): void { $schema = json_decode(self::TEST_SCHEMA); $this->constraint = new RequestHeadersConstraint($schema, new Validator()); } - public function testConstraintDefinition() + public function testConstraintDefinition(): void { self::assertCount(1, $this->constraint); self::assertEquals('is a valid request header', $this->constraint->toString()); } - public function testValidHeaders() + public function testValidHeaders(): void { $headers = [ 'X-Required-Header' => 'any', @@ -43,7 +43,7 @@ public function testValidHeaders() self::assertTrue($this->constraint->evaluate($headers, '', true)); } - public function testCaseInsensitiveValidHeaders() + public function testCaseInsensitiveValidHeaders(): void { $headers = [ 'X-required-HEADER' => 'application/json', @@ -52,7 +52,7 @@ public function testCaseInsensitiveValidHeaders() self::assertTrue($this->constraint->evaluate($headers, '', true)); } - public function testInvalidHeaderType() + public function testInvalidHeaderType(): void { $headers = [ 'X-Optional-Header' => 'any', @@ -76,7 +76,7 @@ public function testInvalidHeaderType() } } - public function testSchemaUnchanged() + public function testSchemaUnchanged(): void { $schema = json_decode(self::TEST_SCHEMA); new RequestHeadersConstraint($schema, new Validator()); diff --git a/tests/PhpUnit/RequestQueryConstraintTest.php b/tests/PhpUnit/RequestQueryConstraintTest.php index d03385d..73ce8b1 100644 --- a/tests/PhpUnit/RequestQueryConstraintTest.php +++ b/tests/PhpUnit/RequestQueryConstraintTest.php @@ -20,7 +20,7 @@ class RequestQueryConstraintTest extends TestCase */ protected $constraint; - protected function setUp() + protected function setUp(): void { $schema = '[{"name":"tags","in":"query","description":"tags to filter by","required":false,"type":"array","items":{"type":"string"},"collectionFormat":"csv"},{"name":"limit","in":"query","description":"maximum number of results to return","required":true,"type":"integer","format":"int32"}]'; $schema = json_decode($schema); @@ -28,13 +28,13 @@ protected function setUp() $this->constraint = new RequestQueryConstraint($schema, new Validator()); } - public function testConstraintDefinition() + public function testConstraintDefinition(): void { self::assertCount(1, $this->constraint); self::assertEquals('is a valid request query', $this->constraint->toString()); } - public function testValidQuery() + public function testValidQuery(): void { $parameters = [ 'tags' => ['foo', 'bar'], @@ -44,7 +44,7 @@ public function testValidQuery() self::assertTrue($this->constraint->evaluate($parameters, '', true)); } - public function testInvalidParameterType() + public function testInvalidParameterType(): void { self::markTestSkipped('Is not possible to check types'); @@ -71,7 +71,7 @@ public function testInvalidParameterType() } } - public function testMissingParameter() + public function testMissingParameter(): void { $parameters = [ 'tags' => ['foo', 'bar'], @@ -95,7 +95,7 @@ public function testMissingParameter() } } - public function testConstructorDoesNotAlterParameters() + public function testConstructorDoesNotAlterParameters(): void { $source = '[{"name":"tags","in":"query","description":"tags to filter by","required":false,"type":"array","items":{"type":"string"},"collectionFormat":"csv"},{"name":"limit","in":"query","description":"maximum number of results to return","required":true,"type":"integer","format":"int32"}]'; $schema = json_decode($source); diff --git a/tests/PhpUnit/ResponseHeadersConstraintTest.php b/tests/PhpUnit/ResponseHeadersConstraintTest.php index b579d53..32394f5 100644 --- a/tests/PhpUnit/ResponseHeadersConstraintTest.php +++ b/tests/PhpUnit/ResponseHeadersConstraintTest.php @@ -20,7 +20,7 @@ class ResponseHeadersConstraintTest extends TestCase */ protected $constraint; - protected function setUp() + protected function setUp(): void { $schema = '{"ETag":{"minimum":1}}'; $schema = json_decode($schema); @@ -28,13 +28,13 @@ protected function setUp() $this->constraint = new ResponseHeadersConstraint($schema, new Validator()); } - public function testConstraintDefinition() + public function testConstraintDefinition(): void { self::assertCount(1, $this->constraint); self::assertEquals('is a valid response header', $this->constraint->toString()); } - public function testValidHeaders() + public function testValidHeaders(): void { $headers = [ 'Content-Type' => 'application/json', @@ -44,7 +44,7 @@ public function testValidHeaders() self::assertTrue($this->constraint->evaluate($headers, '', true)); } - public function testCaseInsensitiveValidHeaders() + public function testCaseInsensitiveValidHeaders(): void { $headers = [ 'Content-Type' => 'application/json', @@ -54,7 +54,7 @@ public function testCaseInsensitiveValidHeaders() self::assertTrue($this->constraint->evaluate($headers, '', true)); } - public function testInvalidHeaderType() + public function testInvalidHeaderType(): void { $headers = [ 'Content-Type' => 'application/json', diff --git a/tests/PhpUnit/SymfonyAssertsTraitTest.php b/tests/PhpUnit/SymfonyAssertsTraitTest.php index 99a0f95..d9fad2a 100644 --- a/tests/PhpUnit/SymfonyAssertsTraitTest.php +++ b/tests/PhpUnit/SymfonyAssertsTraitTest.php @@ -23,19 +23,19 @@ class SymfonyAssertsTraitTest extends TestCase */ protected $schemaManager; - protected function setUp() + protected function setUp(): void { $this->schemaManager = SchemaManager::fromUri('file://' . __DIR__ . '/../fixture/petstore-with-external-docs.json'); } - public function testAssertResponseMatch() + public function testAssertResponseMatch(): void { $response = $this->createMockResponse(200, $this->getValidHeaders(), $this->getValidResponseBody()); self::assertResponseMatch($response, $this->schemaManager, '/api/pets', 'get'); } - public function testAssertResponseAndRequestMatch() + public function testAssertResponseAndRequestMatch(): void { $body = $this->getValidRequestBody(); $response = $this->createMockResponse(200, $this->getValidHeaders(), $body); @@ -44,7 +44,7 @@ public function testAssertResponseAndRequestMatch() self::assertResponseAndRequestMatch($response, $request, $this->schemaManager); } - public function testAssertResponseIsValidIfClientErrorAndRequestIsInvalid() + public function testAssertResponseIsValidIfClientErrorAndRequestIsInvalid(): void { $response = $this->createMockResponse(404, $this->getValidHeaders(), '{"code":400,"message":"Invalid"}'); $request = $this->createMockRequest('POST', '/api/pets', ['Content-Type' => ['application/pdf']]); @@ -52,7 +52,7 @@ public function testAssertResponseIsValidIfClientErrorAndRequestIsInvalid() self::assertResponseAndRequestMatch($response, $request, $this->schemaManager); } - public function testAssertRequestIsInvalidIfResponseIsNotAClientError() + public function testAssertRequestIsInvalidIfResponseIsNotAClientError(): void { $response = $this->createMockResponse(200, $this->getValidHeaders(), $this->getValidResponseBody()); $request = $this->createMockRequest('POST', '/api/pets', ['Content-Type' => ['application/pdf']]); @@ -64,7 +64,7 @@ public function testAssertRequestIsInvalidIfResponseIsNotAClientError() } } - public function testAssertResponseBodyDoesNotMatch() + public function testAssertResponseBodyDoesNotMatch(): void { $response = <<<'JSON' [ @@ -91,7 +91,7 @@ public function testAssertResponseBodyDoesNotMatch() } } - public function testAssertResponseMediaTypeDoesNotMatch() + public function testAssertResponseMediaTypeDoesNotMatch(): void { $response = $this->createMockResponse( 200, @@ -110,7 +110,7 @@ public function testAssertResponseMediaTypeDoesNotMatch() } } - public function testAssertResponseHeaderDoesNotMatch() + public function testAssertResponseHeaderDoesNotMatch(): void { $headers = [ 'Content-Type' => ['application/json'], @@ -135,7 +135,7 @@ public function testAssertResponseHeaderDoesNotMatch() } } - public function testAssertRequestBodyDoesNotMatch() + public function testAssertRequestBodyDoesNotMatch(): void { $request = <<<'JSON' { @@ -161,7 +161,7 @@ public function testAssertRequestBodyDoesNotMatch() } } - public function testAssertRequestMediaTypeDoesNotMatch() + public function testAssertRequestMediaTypeDoesNotMatch(): void { $request = $this->createMockRequest( 'POST', @@ -181,7 +181,7 @@ public function testAssertRequestMediaTypeDoesNotMatch() } } - public function testAssertRequestHeaderDoesNotMatch() + public function testAssertRequestHeaderDoesNotMatch(): void { $headers = [ 'Content-Type' => ['application/json'], @@ -206,7 +206,7 @@ public function testAssertRequestHeaderDoesNotMatch() } } - public function testAssertRequestQueryDoesNotMatch() + public function testAssertRequestQueryDoesNotMatch(): void { $query = [ 'tags' => ['foo', '1'], @@ -230,17 +230,14 @@ public function testAssertRequestQueryDoesNotMatch() } } - public function testEmptyResponse() + public function testEmptyResponse(): void { $response = $this->createMockResponse(204, ['Content-Type' => ['']], ''); self::assertResponseMatch($response, $this->schemaManager, '/api/pets/1', 'delete'); } - /** - * @return string - */ - protected function getValidRequestBody() + protected function getValidRequestBody(): string { return <<<'JSON' { @@ -250,10 +247,7 @@ protected function getValidRequestBody() JSON; } - /** - * @return string - */ - protected function getValidResponseBody() + protected function getValidResponseBody(): string { return <<<'JSON' [ @@ -268,7 +262,7 @@ protected function getValidResponseBody() /** * @return string[] */ - protected function getValidHeaders() + protected function getValidHeaders(): array { return [ 'Content-Type' => 'application/json', @@ -277,15 +271,10 @@ protected function getValidHeaders() } /** - * @param string $method - * @param string $path * @param string[] $headers - * @param string $body * @param mixed[] $query - * - * @return Request */ - protected function createMockRequest($method, $path, array $headers, $body = '', $query = []) + protected function createMockRequest(string $method, string $path, array $headers, string $body = '', $query = []): Request { $request = Request::create($path, $method, $query, [], [], [], $body); $request->headers = new HeaderBag($headers); @@ -294,13 +283,9 @@ protected function createMockRequest($method, $path, array $headers, $body = '', } /** - * @param int $statusCode * @param string[] $headers - * @param string $body - * - * @return Response */ - protected function createMockResponse($statusCode, array $headers, $body) + protected function createMockResponse(int $statusCode, array $headers, string $body): Response { return new Response($body, $statusCode, $headers); } diff --git a/tests/SchemaManagerTest.php b/tests/SchemaManagerTest.php index ccbb998..fbb2dbc 100644 --- a/tests/SchemaManagerTest.php +++ b/tests/SchemaManagerTest.php @@ -16,7 +16,7 @@ class SchemaManagerTest extends TestCase */ protected $schemaManager; - protected function setUp() + protected function setUp(): void { $this->schemaManager = SchemaManager::fromUri('file://' . __DIR__ . '/fixture/petstore-with-external-docs.json'); } @@ -24,14 +24,14 @@ protected function setUp() /** * @dataProvider validPathsProvider */ - public function testFindPathInTemplatesValid($requestPath, $expectedTemplate, array $expectedParameters) + public function testFindPathInTemplatesValid($requestPath, $expectedTemplate, array $expectedParameters): void { self::assertTrue($this->schemaManager->findPathInTemplates($requestPath, $path, $parameters)); self::assertEquals($expectedTemplate, $path); self::assertEquals($expectedParameters, $parameters); } - public function validPathsProvider() + public function validPathsProvider(): array { $dataCases = [ 'integer' => ['/api/pets/1234', '/pets/{id}', ['id' => 1234]], @@ -58,14 +58,14 @@ public function validPathsProvider() /** * @dataProvider responseMediaTypesProvider */ - public function testGetResponseMediaType($path, $method, array $expectedMediaTypes) + public function testGetResponseMediaType($path, $method, array $expectedMediaTypes): void { $mediaTypes = $this->schemaManager->getResponseMediaTypes($path, $method); self::assertEquals($expectedMediaTypes, $mediaTypes); } - public function responseMediaTypesProvider() + public function responseMediaTypesProvider(): array { return [ // Description => [path, method, expectedMediaTypes] @@ -77,14 +77,14 @@ public function responseMediaTypesProvider() /** * @dataProvider responseSchemaProvider */ - public function testGetResponseSchema($path, $method, $httpCode, $expectedSchema) + public function testGetResponseSchema($path, $method, $httpCode, $expectedSchema): void { $schema = $this->schemaManager->getResponseSchema($path, $method, (string) $httpCode); self::assertEquals($expectedSchema, json_encode($schema)); } - public function responseSchemaProvider() + public function responseSchemaProvider(): array { $schema200 = '{"type":"array","items":{"type":"object","required":["id","name"],"externalDocs":{"description":"find more info here","url":"https:\/\/swagger.io\/about"},"properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string"},"tag":{"type":"string"}}}}'; $schemaDefault = '{"type":"object","required":["code","message"],"properties":{"code":{"type":"integer","format":"int32"},"message":{"type":"string"}}}'; @@ -102,14 +102,14 @@ public function responseSchemaProvider() /** * @dataProvider responseHeadersProvider */ - public function testGetResponseHeaders($path, $method, $httpCode, $expectedHeaders) + public function testGetResponseHeaders($path, $method, $httpCode, $expectedHeaders): void { $headers = $this->schemaManager->getResponseHeaders($path, $method, (string) $httpCode); self::assertEquals($expectedHeaders, json_encode($headers)); } - public function responseHeadersProvider() + public function responseHeadersProvider(): array { $dataSet = [ // Description => [path, method, httpCode, expectedHeaders] @@ -123,14 +123,14 @@ public function responseHeadersProvider() /** * @dataProvider requestMediaTypesProvider */ - public function testGetRequestMediaType($path, $method, array $expectedMediaTypes) + public function testGetRequestMediaType($path, $method, array $expectedMediaTypes): void { $mediaTypes = $this->schemaManager->getRequestMediaTypes($path, $method); self::assertEquals($expectedMediaTypes, $mediaTypes); } - public function requestMediaTypesProvider() + public function requestMediaTypesProvider(): array { return [ // Description => [path, method, expectedMediaTypes] @@ -142,14 +142,14 @@ public function requestMediaTypesProvider() /** * @dataProvider requestParameters */ - public function testGetRequestParameters($path, $method, $expectedParameters) + public function testGetRequestParameters($path, $method, $expectedParameters): void { $parameters = $this->schemaManager->getRequestParameters($path, $method); self::assertEquals($expectedParameters, json_encode($parameters)); } - public function requestParameters() + public function requestParameters(): array { $pets_id_shared_parameters = '[{"name":"id","in":"path","description":"ID of pet to fetch","required":true,"type":"integer","format":"int64"}'; $pets_id_patch_parameters = $pets_id_shared_parameters . ',{"name":"X-Required-Header","in":"header","description":"Required header","required":true,"type":"string"},{"name":"X-Optional-Header","in":"header","description":"Optional header","type":"string"},{"name":"pet","in":"body","description":"Pet to update","required":true,"schema":{"type":"object","allOf":[{"type":"object","required":["id","name"],"externalDocs":{"description":"find more info here","url":"https:\/\/swagger.io\/about"},"properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string"},"tag":{"type":"string"}}},{"required":["id"],"properties":{"id":{"type":"integer","format":"int64"}}}]}}]'; @@ -169,14 +169,14 @@ public function requestParameters() /** * @dataProvider requestHeadersParameters */ - public function testGetRequestHeadersParameters($path, $method, $expectedParameters) + public function testGetRequestHeadersParameters($path, $method, $expectedParameters): void { $parameters = $this->schemaManager->getRequestHeadersParameters($path, $method); self::assertEquals($expectedParameters, json_encode($parameters)); } - public function requestHeadersParameters() + public function requestHeadersParameters(): array { $parameters = '[{"name":"X-Required-Header","in":"header","description":"Required header","required":true,"type":"string"},{"name":"X-Optional-Header","in":"header","description":"Optional header","type":"string"}]'; @@ -192,14 +192,14 @@ public function requestHeadersParameters() /** * @dataProvider requestBodyParameters */ - public function testGetRequestBodyParameters($path, $method, $expectedParameters) + public function testGetRequestBodyParameters($path, $method, $expectedParameters): void { $parameters = $this->schemaManager->getRequestSchema($path, $method); self::assertEquals($expectedParameters, json_encode($parameters)); } - public function requestBodyParameters() + public function requestBodyParameters(): array { $parameters = '{"type":"object","allOf":[{"type":"object","required":["id","name"],"externalDocs":{"description":"find more info here","url":"https:\/\/swagger.io\/about"},"properties":{"id":{"type":"integer","format":"int64"},"name":{"type":"string"},"tag":{"type":"string"}}},{"required":["id"],"properties":{"id":{"type":"integer","format":"int64"}}}]}'; @@ -215,14 +215,14 @@ public function requestBodyParameters() /** * @dataProvider requestQueryParameters */ - public function testGetRequestQueryParameters($path, $method, $expectedParameters) + public function testGetRequestQueryParameters($path, $method, $expectedParameters): void { $parameters = $this->schemaManager->getRequestQueryParameters($path, $method); self::assertEquals($expectedParameters, json_encode($parameters)); } - public function requestQueryParameters() + public function requestQueryParameters(): array { $parameters = '[{"name":"tags","in":"query","description":"tags to filter by","required":false,"type":"array","items":{"type":"string"},"collectionFormat":"csv"},{"name":"limit","in":"query","description":"maximum number of results to return","required":true,"type":"integer","format":"int32"}]'; From c1a6f3caa74768ed52e255997de73161fd27a5f5 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 19 Mar 2019 09:59:21 +0100 Subject: [PATCH 2/3] Add PHPUnit v8 and raise minimum version up to 7.5 --- composer.json | 3 ++- tests/PhpUnit/Psr7AssertsTraitTest.php | 2 +- tests/PhpUnit/SymfonyAssertsTraitTest.php | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 6db31bc..5e48e3c 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/tests/PhpUnit/Psr7AssertsTraitTest.php b/tests/PhpUnit/Psr7AssertsTraitTest.php index d765397..0b0dd92 100644 --- a/tests/PhpUnit/Psr7AssertsTraitTest.php +++ b/tests/PhpUnit/Psr7AssertsTraitTest.php @@ -62,7 +62,7 @@ public function testAssertRequestIsInvalidIfResponseIsNotAClientError(): void try { self::assertResponseAndRequestMatch($response, $request, $this->schemaManager); } catch (ExpectationFailedException $e) { - self::assertContains('request', $e->getMessage()); + self::assertStringContainsString('request', $e->getMessage()); } } diff --git a/tests/PhpUnit/SymfonyAssertsTraitTest.php b/tests/PhpUnit/SymfonyAssertsTraitTest.php index d9fad2a..4c3e9d7 100644 --- a/tests/PhpUnit/SymfonyAssertsTraitTest.php +++ b/tests/PhpUnit/SymfonyAssertsTraitTest.php @@ -60,7 +60,7 @@ public function testAssertRequestIsInvalidIfResponseIsNotAClientError(): void try { self::assertResponseAndRequestMatch($response, $request, $this->schemaManager); } catch (ExpectationFailedException $e) { - self::assertContains('request', $e->getMessage()); + self::assertStringContainsString('request', $e->getMessage()); } } From c5016459e66ec1964ce5c3ae09fbc12a034bace0 Mon Sep 17 00:00:00 2001 From: Maks3w Date: Tue, 19 Mar 2019 10:24:22 +0100 Subject: [PATCH 3/3] Fix PHPUnit 8 Constraint::_construct removal --- src/PhpUnit/JsonSchemaConstraint.php | 4 +++- src/PhpUnit/MediaTypeConstraint.php | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/PhpUnit/JsonSchemaConstraint.php b/src/PhpUnit/JsonSchemaConstraint.php index 4c03b8a..bffdbb4 100644 --- a/src/PhpUnit/JsonSchemaConstraint.php +++ b/src/PhpUnit/JsonSchemaConstraint.php @@ -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; diff --git a/src/PhpUnit/MediaTypeConstraint.php b/src/PhpUnit/MediaTypeConstraint.php index 701b1a7..5281486 100644 --- a/src/PhpUnit/MediaTypeConstraint.php +++ b/src/PhpUnit/MediaTypeConstraint.php @@ -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; }