Skip to content

Commit a950ed4

Browse files
authored
Merge pull request #56 from Maks3w/phpunit_8
PHPUnit v8
2 parents ef08f0b + c501645 commit a950ed4

18 files changed

+183
-207
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
"homepage": "https://github.com/Maks3w/SwaggerAssertions",
1414
"require": {
1515
"php": ">= 7.1",
16+
"ext-json": "*",
1617
"justinrainbow/json-schema": "^5",
17-
"phpunit/phpunit": "^6.0||^7.0",
18+
"phpunit/phpunit": "^7.5||^8.0",
1819
"rize/uri-template": "^0.3.0",
1920
"zendframework/zend-http": "2.5 - 3"
2021
},

examples/PhpUnit/AssertTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ class AssertTest extends TestCase
2626
*/
2727
protected $guzzleHttpClient;
2828

29-
public static function setUpBeforeClass()
29+
public static function setUpBeforeClass(): void
3030
{
3131
self::$schemaManager = SchemaManager::fromUri('http://petstore.swagger.io/v2/swagger.json');
3232
}
3333

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

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

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

48-
$this->assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200);
48+
self::assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200);
4949
}
5050
}

examples/PhpUnit/LocalFileTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ class LocalFileTest extends TestCase
2626
*/
2727
protected $guzzleHttpClient;
2828

29-
public static function setUpBeforeClass()
29+
public static function setUpBeforeClass(): void
3030
{
3131
$filePath = __DIR__ . '/../fixtures/pet_store.json';
3232

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

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

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

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

51-
$this->assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200);
51+
self::assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200);
5252
}
5353
}

examples/PhpUnit/Psr7WithGuzzleV6Test.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,33 @@ class Psr7WithGuzzleV6Test extends TestCase
2626
*/
2727
protected $guzzleHttpClient;
2828

29-
public static function setUpBeforeClass()
29+
public static function setUpBeforeClass(): void
3030
{
3131
self::$schemaManager = SchemaManager::fromUri('http://petstore.swagger.io/v2/swagger.json');
3232
}
3333

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

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

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

46-
$this->assertResponseAndRequestMatch($response, $request, self::$schemaManager);
46+
self::assertResponseAndRequestMatch($response, $request, self::$schemaManager);
4747
}
4848

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

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

56-
$this->assertResponseMatch($response, self::$schemaManager, '/v2/pet/findByStatus', 'get');
56+
self::assertResponseMatch($response, self::$schemaManager, '/v2/pet/findByStatus', 'get');
5757
}
5858
}

src/PhpUnit/AssertsTrait.php

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use FR3D\SwaggerAssertions\SchemaManager;
88
use JsonSchema\Validator;
99
use PHPUnit\Framework\Assert;
10-
use stdClass;
1110
use Zend\Http\Header\ContentType;
1211

1312
/**
@@ -18,46 +17,46 @@ trait AssertsTrait
1817
/**
1918
* Asserts response body match with the response schema.
2019
*
21-
* @param stdClass|stdClass[] $responseBody
20+
* @param mixed $responseBody
2221
* @param string $path percent-encoded path used on the request.
2322
*/
24-
public function assertResponseBodyMatch(
23+
public static function assertResponseBodyMatch(
2524
$responseBody,
2625
SchemaManager $schemaManager,
2726
string $path,
2827
string $httpMethod,
2928
int $httpCode,
3029
string $message = ''
31-
) {
30+
): void {
3231
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
3332
throw new \RuntimeException('Request URI does not match with any swagger path definition');
3433
}
3534

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

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

4241
/**
4342
* Asserts request body match with the request schema.
4443
*
45-
* @param stdClass|stdClass[] $requestBody
44+
* @param mixed $requestBody
4645
* @param string $path percent-encoded path used on the request.
4746
*/
48-
public function assertRequestBodyMatch(
47+
public static function assertRequestBodyMatch(
4948
$requestBody,
5049
SchemaManager $schemaManager,
5150
string $path,
5251
string $httpMethod,
5352
string $message = ''
54-
) {
53+
): void {
5554
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
5655
throw new \RuntimeException('Request URI does not match with any swagger path definition');
5756
}
5857

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

6261
Assert::assertThat($requestBody, $constraint, $message);
6362
}
@@ -67,13 +66,13 @@ public function assertRequestBodyMatch(
6766
*
6867
* @param string $path percent-encoded path used on the request.
6968
*/
70-
public function assertResponseMediaTypeMatch(
69+
public static function assertResponseMediaTypeMatch(
7170
string $responseMediaType,
7271
SchemaManager $schemaManager,
7372
string $path,
7473
string $httpMethod,
7574
string $message = ''
76-
) {
75+
): void {
7776
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
7877
// @codeCoverageIgnoreStart
7978
throw new \RuntimeException('Request URI does not match with any swagger path definition');
@@ -94,13 +93,13 @@ public function assertResponseMediaTypeMatch(
9493
*
9594
* @param string $path percent-encoded path used on the request.
9695
*/
97-
public function assertRequestMediaTypeMatch(
96+
public static function assertRequestMediaTypeMatch(
9897
string $requestMediaType,
9998
SchemaManager $schemaManager,
10099
string $path,
101100
string $httpMethod,
102101
string $message = ''
103-
) {
102+
): void {
104103
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
105104
// @codeCoverageIgnoreStart
106105
throw new \RuntimeException('Request URI does not match with any swagger path definition');
@@ -122,14 +121,14 @@ public function assertRequestMediaTypeMatch(
122121
* @param string[] $headers
123122
* @param string $path percent-encoded path used on the request.
124123
*/
125-
public function assertResponseHeadersMatch(
124+
public static function assertResponseHeadersMatch(
126125
array $headers,
127126
SchemaManager $schemaManager,
128127
string $path,
129128
string $httpMethod,
130129
int $httpCode,
131130
string $message = ''
132-
) {
131+
): void {
133132
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
134133
// @codeCoverageIgnoreStart
135134
throw new \RuntimeException('Request URI does not match with any swagger path definition');
@@ -138,7 +137,7 @@ public function assertResponseHeadersMatch(
138137

139138
$constraint = new ResponseHeadersConstraint(
140139
$schemaManager->getResponseHeaders($template, $httpMethod, (string) $httpCode),
141-
$this->getValidator()
140+
self::getValidator()
142141
);
143142

144143
Assert::assertThat($headers, $constraint, $message);
@@ -150,20 +149,20 @@ public function assertResponseHeadersMatch(
150149
* @param string[] $headers
151150
* @param string $path percent-encoded path used on the request.
152151
*/
153-
public function assertRequestHeadersMatch(
152+
public static function assertRequestHeadersMatch(
154153
array $headers,
155154
SchemaManager $schemaManager,
156155
string $path,
157156
string $httpMethod,
158157
string $message = ''
159-
) {
158+
): void {
160159
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
161160
// @codeCoverageIgnoreStart
162161
throw new \RuntimeException('Request URI does not match with any swagger path definition');
163162
// @codeCoverageIgnoreEnd
164163
}
165164

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

168167
Assert::assertThat($headers, $constraint, $message);
169168
}
@@ -174,25 +173,25 @@ public function assertRequestHeadersMatch(
174173
* @param mixed[] $query
175174
* @param string $path percent-encoded path used on the request.
176175
*/
177-
public function assertRequestQueryMatch(
176+
public static function assertRequestQueryMatch(
178177
$query,
179178
SchemaManager $schemaManager,
180179
string $path,
181180
string $httpMethod,
182181
string $message = ''
183-
) {
182+
): void {
184183
if (!$schemaManager->findPathInTemplates($path, $template, $params)) {
185184
// @codeCoverageIgnoreStart
186185
throw new \RuntimeException('Request URI does not match with any swagger path definition');
187186
// @codeCoverageIgnoreEnd
188187
}
189188

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

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

195-
protected function getValidator(): Validator
194+
protected static function getValidator(): Validator
196195
{
197196
return new Validator();
198197
}

src/PhpUnit/JsonSchemaConstraint.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class JsonSchemaConstraint extends Constraint
2929

3030
public function __construct($expectedSchema, string $context, Validator $validator)
3131
{
32-
parent::__construct();
32+
if (is_callable([Constraint::class, '__construct'])) {
33+
parent::__construct();
34+
}
3335

3436
$this->expectedSchema = $expectedSchema;
3537
$this->context = $context;

src/PhpUnit/MediaTypeConstraint.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ class MediaTypeConstraint extends Constraint
2121
*/
2222
public function __construct(array $allowedMediaTypes)
2323
{
24-
parent::__construct();
24+
if (is_callable([Constraint::class, '__construct'])) {
25+
parent::__construct();
26+
}
2527

2628
$this->allowedMediaTypes = $allowedMediaTypes;
2729
}

0 commit comments

Comments
 (0)