Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ebb4f26

Browse files
J-T-McCestahn
andauthored
BREAKING CHANGE: PHP 8 Support (#27)
* PHP 8 Support - Updated composer requirements - Removed deprecated method - Added test for missing coverage - Updated types throughout - Updated phpunitxml to support new version * Corrected method formatting * Updated php versions in build action * chore: bump php-actions/phpunit to v9 Co-authored-by: Enrico Stahn <enrico.stahn@gmail.com>
1 parent c545cc6 commit ebb4f26

File tree

8 files changed

+72
-97
lines changed

8 files changed

+72
-97
lines changed

‎.github/workflows/build.yml‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,13 @@ jobs:
5151
strategy:
5252
matrix:
5353
php:
54-
- 7.1
55-
# - 7.2
56-
# - 7.3
57-
# - 7.4
58-
# - 8.0
54+
- 7.4
55+
- 8.0
5956
include:
60-
- php: 7.1
61-
phpunit: 7.5.20
62-
# - php: 7.2
63-
# phpunit: 8.5.13
64-
# - php: 7.3
65-
# phpunit: 9.5.0
66-
# - php: 7.4
67-
# phpunit: 9.5.0
68-
# - php: 8.0
69-
# phpunit: 9.5.0
57+
- php: 7.4
58+
phpunit: 9.5.0
59+
- php: 8.0
60+
phpunit: 9.5.0
7061

7162
steps:
7263
- uses: actions/checkout@v2
@@ -81,7 +72,7 @@ jobs:
8172
with:
8273
php_version: ${{ matrix.php }}
8374

84-
- uses: php-actions/phpunit@v2
75+
- uses: php-actions/phpunit@v9
8576
with:
8677
php_version: ${{ matrix.php }}
8778
version: ${{ matrix.phpunit }}

‎composer.json‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
],
1414
"minimum-stability": "stable",
1515
"require": {
16-
"php": "^7.0",
16+
"php": "^7.4|^8.0",
1717
"justinrainbow/json-schema": "^5.0",
18-
"mtdowling/jmespath.php": "^2.3"
18+
"mtdowling/jmespath.php": "^2.3",
19+
"ext-json": "*"
1920
},
2021
"require-dev": {
21-
"phpunit/phpunit": "^6",
22+
"phpunit/phpunit": "^9",
2223
"codacy/coverage": "dev-master",
2324
"symfony/http-foundation": "^2.8|^3.0"
2425
},

‎phpunit.xml.dist‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
43
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
4+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
65
backupGlobals="false"
76
colors="true"
8-
bootstrap="vendor/autoload.php"
9-
>
7+
bootstrap="./vendor/autoload.php">
108
<testsuites>
119
<testsuite name="phpunit-json-assertions Test Suite">
1210
<directory>tests/</directory>
1311
</testsuite>
1412
</testsuites>
1513

16-
<filter>
17-
<whitelist>
14+
<coverage>
15+
<include>
1816
<directory suffix=".php">src/</directory>
19-
</whitelist>
20-
</filter>
21-
22-
<logging>
23-
<log type="coverage-clover" target="build/logs/clover.xml"/>
24-
</logging>
25-
17+
</include>
18+
<report>
19+
<clover outputFile="./build/logs/clover.xml"/>
20+
</report>
21+
</coverage>
2622
</phpunit>

‎src/Assert.php‎

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace EnricoStahn\JsonAssert;
1313

14+
use JmesPath\Env;
1415
use JsonSchema\Constraints\Factory;
1516
use JsonSchema\SchemaStorage;
1617
use JsonSchema\Validator;
@@ -26,9 +27,9 @@
2627
trait Assert
2728
{
2829
/**
29-
* @var SchemaStorage
30+
* @var ?SchemaStorage
3031
*/
31-
private static $schemaStorage = null;
32+
private static ?SchemaStorage$schemaStorage = null;
3233

3334
/**
3435
* Asserts that json content is valid according to the provided schema file.
@@ -37,10 +38,10 @@ trait Assert
3738
*
3839
* static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json')
3940
*
40-
* @param string|null $schema Path to the schema file
4141
* @param array|object $content JSON array or object
42+
* @param ?string $schema Path to the schema file
4243
*/
43-
public static function assertJsonMatchesSchema($content, $schema = null)
44+
public static function assertJsonMatchesSchema($content, ?string$schema = null): void
4445
{
4546
if (self::$schemaStorage === null) {
4647
self::$schemaStorage = new SchemaStorage();
@@ -60,7 +61,7 @@ public static function assertJsonMatchesSchema($content, $schema = null)
6061
$validator = new Validator(new Factory(self::$schemaStorage));
6162
$validator->validate($content, $schemaObject);
6263

63-
$message = '- Property: %s, Contraint: %s, Message: %s';
64+
$message = '- Property: %s, Constraint: %s, Message: %s';
6465
$messages = array_map(function ($exception) use ($message) {
6566
return sprintf($message, $exception['property'], $exception['constraint'], $exception['message']);
6667
}, $validator->getErrors());
@@ -69,30 +70,13 @@ public static function assertJsonMatchesSchema($content, $schema = null)
6970
\PHPUnit\Framework\Assert::assertTrue($validator->isValid(), implode("\n", $messages));
7071
}
7172

72-
/**
73-
* Asserts that json content is valid according to the provided schema file.
74-
*
75-
* Example:
76-
*
77-
* static::assertJsonMatchesSchema(json_decode('{"foo":1}'), './schema.json')
78-
*
79-
* @param string|null $schema Path to the schema file
80-
* @param array|object $content JSON array or object
81-
*
82-
* @deprecated This will be removed in the next major version (4.x).
83-
*/
84-
public static function assertJsonMatchesSchemaDepr($schema, $content)
85-
{
86-
self::assertJsonMatchesSchema($content, $schema);
87-
}
88-
8973
/**
9074
* Asserts that json content is valid according to the provided schema string.
9175
*
9276
* @param string $schema Schema data
9377
* @param array|object $content JSON content
9478
*/
95-
public static function assertJsonMatchesSchemaString($schema, $content)
79+
public static function assertJsonMatchesSchemaString(string$schema, $content): void
9680
{
9781
$file = tempnam(sys_get_temp_dir(), 'json-schema-');
9882
file_put_contents($file, $schema);
@@ -107,17 +91,17 @@ public static function assertJsonMatchesSchemaString($schema, $content)
10791
*
10892
* static::assertJsonValueEquals(33, 'foo.bar[0]', $json);
10993
*
110-
* @param mixed $expected Expected value
111-
* @param string $expression Expression to retrieve the result
112-
* (e.g. locations[?state == 'WA'].name | sort(@))
113-
* @param array|object $json JSON Content
94+
* @param mixed $expected Expected value
95+
* @param string $expression Expression to retrieve the result
96+
* (e.g. locations[?state == 'WA'].name | sort(@))
97+
* @param array|object|string $json JSON Content
11498
*/
115-
public static function assertJsonValueEquals($expected, $expression, $json)
99+
public static function assertJsonValueEquals($expected, string$expression, $json): void
116100
{
117-
$result = \JmesPath\Env::search($expression, $json);
101+
$result = Env::search($expression, $json);
118102

119103
\PHPUnit\Framework\Assert::assertEquals($expected, $result);
120-
\PHPUnit\Framework\Assert::assertInternalType(strtolower(gettype($expected)), $result);
104+
\PHPUnit\Framework\Assert::assertEquals(gettype($expected), gettype($result));
121105
}
122106

123107
/**

‎src/Extension/Symfony.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ trait Symfony
2626
* @param string $schema Path to the schema file
2727
* @param Response $response JSON array or object
2828
*/
29-
public static function assertJsonMatchesSchema($schema, Response $response)
29+
public static function assertJsonMatchesSchema(string$schema, Response $response): void
3030
{
31-
Assert::assertJsonMatchesSchemaDepr($schema, json_decode($response->getContent()));
31+
Assert::assertJsonMatchesSchema(json_decode($response->getContent()), $schema);
3232
}
3333

3434
/**
@@ -37,7 +37,7 @@ public static function assertJsonMatchesSchema($schema, Response $response)
3737
* @param string $schema Schema data
3838
* @param Response $response JSON content
3939
*/
40-
public static function assertJsonMatchesSchemaString($schema, Response $response)
40+
public static function assertJsonMatchesSchemaString(string$schema, Response $response): void
4141
{
4242
Assert::assertJsonMatchesSchemaString($schema, json_decode($response->getContent()));
4343
}
@@ -54,7 +54,7 @@ public static function assertJsonMatchesSchemaString($schema, Response $response
5454
* (e.g. locations[?state == 'WA'].name | sort(@))
5555
* @param Response $response JSON Content
5656
*/
57-
public static function assertJsonValueEquals($expected, $expression, $response)
57+
public static function assertJsonValueEquals($expected, string$expression, Response$response): void
5858
{
5959
Assert::assertJsonValueEquals($expected, $expression, json_decode($response->getContent()));
6060
}
@@ -67,7 +67,7 @@ public static function assertJsonValueEquals($expected, $expression, $response)
6767
*
6868
* @see \Bazinga\Bundle\RestExtraBundle\Test\WebTestCase::assertJsonResponse()
6969
*/
70-
public static function assertJsonResponse(Response $response, $statusCode = 200)
70+
public static function assertJsonResponse(Response $response, int$statusCode = 200): void
7171
{
7272
\PHPUnit\Framework\Assert::assertEquals(
7373
$statusCode,

‎tests/AssertTraitImpl.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ class AssertTraitImpl extends TestCase
1919
{
2020
use JsonAssert;
2121

22-
public function setUp()
22+
public function setUp(): void
2323
{
2424
self::$schemaStorage = new SchemaStorage();
2525
}
2626

2727
/**
28-
* @param string $id
29-
* @param string $schema
28+
* @param string $id
29+
* @param array|object $schema
3030
*
3131
* @return SchemaStorage
3232
*/
33-
public function testWithSchemaStore($id, $schema)
33+
public function testWithSchemaStore(string$id, $schema): SchemaStorage
3434
{
3535
self::$schemaStorage->addSchema($id, $schema);
3636

‎tests/AssertTraitTest.php‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\ExpectationFailedException;
1515
use PHPUnit\Framework\TestCase;
16+
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
1617

1718
class AssertTraitTest extends TestCase
1819
{
@@ -25,24 +26,30 @@ public function testAssertJsonMatchesSchemaSimple()
2526
{
2627
$content = json_decode(file_get_contents(Utils::getJsonPath('assertJsonMatchesSchema_simple.json')));
2728

28-
AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json'), $content);
29+
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('assertJsonMatchesSchema_simple.schema.json'));
2930
}
3031

3132
public function testAssertJsonMatchesSchema()
3233
{
3334
$content = json_decode('{"foo":123}');
3435

35-
AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('test.schema.json'), $content);
36+
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json'));
3637
}
3738

38-
/**
39-
* @expectedException \PHPUnit\Framework\ExpectationFailedException
40-
*/
4139
public function testAssertJsonMatchesSchemaFail()
4240
{
41+
$this->expectException(ExpectationFailedException::class);
4342
$content = json_decode('{"foo":"123"}');
4443

45-
AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('test.schema.json'), $content);
44+
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json'));
45+
}
46+
47+
public function testAssertThrowsFileNotFoundException()
48+
{
49+
$this->expectException(FileNotFoundException::class);
50+
$content = json_decode('{"foo":"123"}');
51+
52+
AssertTraitImpl::assertJsonMatchesSchema($content, 'not-found.json');
4653
}
4754

4855
public function testAssertJsonMatchesSchemaFailMessage()
@@ -52,10 +59,10 @@ public function testAssertJsonMatchesSchemaFailMessage()
5259
$exception = null;
5360

5461
try {
55-
AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('test.schema.json'), $content);
62+
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('test.schema.json'));
5663
} catch (ExpectationFailedException $exception) {
57-
self::assertContains('- Property: foo, Contraint: type, Message: String value found, but an integer is required', $exception->getMessage());
58-
self::assertContains('- Response: {"foo":"123"}', $exception->getMessage());
64+
self::assertStringContainsString('- Property: foo, Constraint: type, Message: String value found, but an integer is required', $exception->getMessage());
65+
self::assertStringContainsString('- Response: {"foo":"123"}', $exception->getMessage());
5966
}
6067

6168
self::assertInstanceOf('\PHPUnit\Framework\ExpectationFailedException', $exception);
@@ -68,17 +75,15 @@ public function testAssertJsonMatchesSchemaWithRefs()
6875
{
6976
$content = json_decode('{"code":123, "message":"Nothing works."}');
7077

71-
AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('error.schema.json'), $content);
78+
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('error.schema.json'));
7279
}
7380

74-
/**
75-
* @expectedException \PHPUnit\Framework\ExpectationFailedException
76-
*/
7781
public function testAssertJsonMatchesSchemaWithRefsFails()
7882
{
83+
$this->expectException(ExpectationFailedException::class);
7984
$content = json_decode('{"code":"123", "message":"Nothing works."}');
8085

81-
AssertTraitImpl::assertJsonMatchesSchemaDepr(Utils::getSchemaPath('error.schema.json'), $content);
86+
AssertTraitImpl::assertJsonMatchesSchema($content, Utils::getSchemaPath('error.schema.json'));
8287
}
8388

8489
public function testAssertJsonMatchesSchemaString()
@@ -97,7 +102,7 @@ public function testAssertJsonMatchesSchemaString()
97102
* @param string $expression
98103
* @param mixed $value
99104
*/
100-
public function testAssertJsonValueEquals($expression, $value)
105+
public function testAssertJsonValueEquals(string$expression, $value)
101106
{
102107
$content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json')));
103108

@@ -109,28 +114,26 @@ public function testAssertWithSchemaStore()
109114
$obj = new AssertTraitImpl();
110115
$obj->setUp();
111116

112-
$schemastore = $obj->testWithSchemaStore('foobar', (object) ['type' => 'string']);
117+
$schemaStore = $obj->testWithSchemaStore('foobar', (object) ['type' => 'string']);
113118

114-
self::assertInstanceOf('JsonSchema\SchemaStorage', $schemastore);
115-
self::assertEquals($schemastore->getSchema('foobar'), (object) ['type' => 'string']);
119+
self::assertInstanceOf('JsonSchema\SchemaStorage', $schemaStore);
120+
self::assertEquals($schemaStore->getSchema('foobar'), (object) ['type' => 'string']);
116121
}
117122

118-
public function assertJsonValueEqualsProvider()
123+
public function assertJsonValueEqualsProvider(): array
119124
{
120125
return [
121126
['foo', '123'],
122127
['a.b.c[0].d[1][0]', 1],
123128
];
124129
}
125130

126-
/**
127-
* @expectedException \PHPUnit\Framework\ExpectationFailedException
128-
*/
129131
public function testAssertJsonValueEqualsFailsOnWrongDataType()
130132
{
133+
$this->expectException(ExpectationFailedException::class);
131134
$content = json_decode(file_get_contents(Utils::getJsonPath('testAssertJsonValueEquals.json')));
132135

133-
AssertTraitImpl::assertJsonValueEquals($content, 'a.b.c[0].d[1][0]', '1');
136+
AssertTraitImpl::assertJsonValueEquals($content, 'a.b.c[0].d[1][0]', '{}');
134137
}
135138

136139
/**
@@ -141,7 +144,7 @@ public function testGetJsonObject($expected, $actual)
141144
self::assertEquals($expected, AssertTraitImpl::getJsonObject($actual));
142145
}
143146

144-
public function jsonObjectProvider()
147+
public function jsonObjectProvider(): array
145148
{
146149
return [
147150
[[], []],

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /