Skip to content

Commit 8c25dd4

Browse files
Merge pull request martin-helmich#9 from martin-helmich/feature/phpunit-6-compat
PHPUnit 6 compatibility
2 parents 0850b99 + dc07667 commit 8c25dd4

12 files changed

+68
-52
lines changed

.travis.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ sudo: false
33
php:
44
- 7.1
55
- 7.0
6-
- 5.6
7-
- 5.5
8-
- 5.4
9-
- hhvm
106
install:
117
- composer install --prefer-dist
128
script:

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ This library is [MIT-licensed](LICENSE.txt).
1414

1515
$ composer require --dev helmich/phpunit-json-assert
1616

17+
**Compatibility notice**: [Version 1](https://github.com/martin-helmich/phpunit-json-assert/tree/v1)
18+
(the `v1` branch) of this library is compatible with PHPUnit 4.8 to 5. [Version 2](https://github.com/martin-helmich/phpunit-json-assert/tree/master)
19+
(the `master` branch) is compatible with PHPUnit 6 and later. When using `composer require`, Composer should automatically
20+
pick the correct version for you.
21+
1722
## Usage
1823

1924
Simply use the trait `Helmich\JsonAssert\JsonAssertions` in your test case. This
@@ -23,8 +28,9 @@ cases:
2328
```php
2429
<?php
2530
use Helmich\JsonAssert\JsonAssertions;
31+
use PHPUnit\Framework\TestCase;
2632

27-
class MyTestCase extends PHPUnit_Framework_TestCase
33+
class MyTestCase extends TestCase
2834
{
2935
use JsonAssertions;
3036

@@ -58,10 +64,11 @@ Alternatively, you can use the functional interface by including the file
5864
```php
5965
<?php
6066
use Helmich\JsonAssert\JsonAssertions;
67+
use PHPUnit\Framework\TestCase;
6168

6269
require_once('path/to/Functions.php');
6370

64-
class MyTestCase extends PHPUnit_Framework_TestCase
71+
class MyTestCase extends TestCase
6572
{
6673
use JsonAssertions;
6774

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
],
1111
"require": {
1212
"flow/jsonpath": "^0.3.1",
13-
"phpunit/phpunit": ">=4.8,<6.0",
14-
"justinrainbow/json-schema": "^2.0"
13+
"phpunit/phpunit": ">=6.0",
14+
"justinrainbow/json-schema": "^2.0",
15+
"php": ">=7.0"
1516
},
1617
"require-dev": {
1718
"codeclimate/php-test-reporter": "dev-master"

src/Constraint/JsonValueMatches.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace Helmich\JsonAssert\Constraint;
33

44
use Flow\JSONPath\JSONPath;
5-
use PHPUnit_Framework_Constraint as Constraint;
5+
use PHPUnit\Framework\Constraint\Constraint;
66

77
/**
88
* A simple constraints that asserts that a single value of a JSON document
@@ -39,7 +39,7 @@ class JsonValueMatches extends Constraint
3939
* _all_ found values must match the
4040
* constraint.
4141
*/
42-
public function __construct($jsonPath, Constraint $constraint, $matchAll = false)
42+
public function __construct(string $jsonPath, Constraint $constraint, bool $matchAll = false)
4343
{
4444
parent::__construct();
4545

@@ -53,15 +53,15 @@ public function __construct($jsonPath, Constraint $constraint, $matchAll = false
5353
*
5454
* @return string
5555
*/
56-
public function toString()
56+
public function toString(): string
5757
{
5858
return "matches " . $this->constraint->toString() . " at JSON path '{$this->jsonPath}'";
5959
}
6060

6161
/**
6262
* @inheritdoc
6363
*/
64-
protected function matches($other)
64+
protected function matches($other): bool
6565
{
6666
if (is_string($other)) {
6767
$other = json_decode($other);
@@ -88,9 +88,9 @@ protected function matches($other)
8888
}
8989

9090
/**
91-
* @return \Closure
91+
* @return callable
9292
*/
93-
protected function buildCombinationFunction()
93+
protected function buildCombinationFunction(): callable
9494
{
9595
if ($this->matchAll) {
9696
return function ($first, $second) {

src/Constraint/JsonValueMatchesMany.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22
namespace Helmich\JsonAssert\Constraint;
33

4-
use PHPUnit_Framework_Constraint as Constraint;
4+
use PHPUnit\Framework\Constraint\Constraint;
5+
use PHPUnit\Framework\Constraint\IsEqual;
56

67
/**
78
* Constraint that asserts that a JSON document matches an entire set of JSON
@@ -30,7 +31,7 @@ public function __construct(array $constraints)
3031

3132
foreach ($constraints as $key => $constraint) {
3233
if (!$constraint instanceof Constraint) {
33-
$constraint = new \PHPUnit_Framework_Constraint_IsEqual($constraint);
34+
$constraint = new IsEqual($constraint);
3435
}
3536

3637
$this->constraints[] = new JsonValueMatches($key, $constraint);
@@ -42,7 +43,7 @@ public function __construct(array $constraints)
4243
*
4344
* @return string
4445
*/
45-
public function toString()
46+
public function toString(): string
4647
{
4748
return implode(
4849
' and ',
@@ -58,7 +59,7 @@ function (Constraint $constraint) {
5859
/**
5960
* @inheritdoc
6061
*/
61-
protected function matches($other)
62+
protected function matches($other): bool
6263
{
6364
foreach ($this->constraints as $constraint) {
6465
if (!$constraint->evaluate($other, '', true)) {

src/Constraint/JsonValueMatchesSchema.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace Helmich\JsonAssert\Constraint;
33

44
use JsonSchema\Validator;
5-
use PHPUnit_Framework_Constraint as Constraint;
5+
use PHPUnit\Framework\Constraint\Constraint;
66
use stdClass;
77

88
/**
@@ -37,7 +37,7 @@ public function __construct($schema)
3737
* @param array|stdClass $jsonDocument
3838
* @return stdClass
3939
*/
40-
private function forceToObject($jsonDocument)
40+
private function forceToObject($jsonDocument): stdClass
4141
{
4242
if (is_string($jsonDocument)) {
4343
return json_decode($jsonDocument);
@@ -49,7 +49,7 @@ private function forceToObject($jsonDocument)
4949
/**
5050
* @inheritdoc
5151
*/
52-
protected function matches($other)
52+
protected function matches($other): bool
5353
{
5454
$other = $this->forceToObject($other);
5555

@@ -62,7 +62,7 @@ protected function matches($other)
6262
/**
6363
* @inheritdoc
6464
*/
65-
protected function additionalFailureDescription($other)
65+
protected function additionalFailureDescription($other): string
6666
{
6767
$other = $this->forceToObject($other);
6868

@@ -79,7 +79,7 @@ protected function additionalFailureDescription($other)
7979
*
8080
* @return string
8181
*/
82-
public function toString()
82+
public function toString(): string
8383
{
8484
return 'matches JSON schema';
8585
}

src/Functions.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
<?php
22

3-
function containsJsonValue($path, $constraint)
3+
use Helmich\JsonAssert\Constraint\JsonValueMatches;
4+
use Helmich\JsonAssert\Constraint\JsonValueMatchesMany;
5+
use Helmich\JsonAssert\Constraint\JsonValueMatchesSchema;
6+
use PHPUnit\Framework\Constraint\Constraint;
7+
use PHPUnit\Framework\Constraint\IsEqual;
8+
9+
function containsJsonValue(string $path, $constraint): JsonValueMatches
410
{
5-
if (!$constraint instanceof PHPUnit_Framework_Constraint) {
6-
$constraint = new PHPUnit_Framework_Constraint_IsEqual($constraint);
11+
if (!$constraint instanceof Constraint) {
12+
$constraint = new IsEqual($constraint);
713
}
814

9-
return new \Helmich\JsonAssert\Constraint\JsonValueMatches($path, $constraint);
15+
return new JsonValueMatches($path, $constraint);
1016
}
1117

12-
function matchesJsonConstraints(array $constraints)
18+
function matchesJsonConstraints(array $constraints): JsonValueMatchesMany
1319
{
14-
return new \Helmich\JsonAssert\Constraint\JsonValueMatchesMany($constraints);
20+
return new JsonValueMatchesMany($constraints);
1521
}
1622

17-
function matchesJsonSchema(array $schema)
23+
function matchesJsonSchema(array $schema): JsonValueMatchesSchema
1824
{
19-
return new \Helmich\JsonAssert\Constraint\JsonValueMatchesSchema($schema);
25+
return new JsonValueMatchesSchema($schema);
2026
}

src/JsonAssertions.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
use Helmich\JsonAssert\Constraint\JsonValueMatches;
55
use Helmich\JsonAssert\Constraint\JsonValueMatchesMany;
66
use Helmich\JsonAssert\Constraint\JsonValueMatchesSchema;
7-
use PHPUnit_Framework_Assert as Assert;
8-
use PHPUnit_Framework_Constraint as Constraint;
7+
use PHPUnit\Framework\Assert;
8+
use PHPUnit\Framework\Constraint\Constraint;
9+
use PHPUnit\Framework\Constraint\IsEqual;
910

1011
/**
1112
* A trait that can be used in test classes for easy use of JSON assertions
@@ -28,7 +29,7 @@ trait JsonAssertions
2829
* must match
2930
* @return void
3031
*/
31-
public static function assertJsonValueMatches($jsonDocument, $jsonPath, Constraint $constraint)
32+
public static function assertJsonValueMatches($jsonDocument, string $jsonPath, Constraint $constraint)
3233
{
3334
Assert::assertThat($jsonDocument, new JsonValueMatches($jsonPath, $constraint));
3435
}
@@ -46,7 +47,7 @@ public static function assertJsonValueMatches($jsonDocument, $jsonPath, Constrai
4647
* must match
4748
* @return void
4849
*/
49-
public static function assertAllJsonValuesMatch($jsonDocument, $jsonPath, Constraint $constraint)
50+
public static function assertAllJsonValuesMatch($jsonDocument, string $jsonPath, Constraint $constraint)
5051
{
5152
Assert::assertThat($jsonDocument, new JsonValueMatches($jsonPath, $constraint, true));
5253
}
@@ -63,12 +64,12 @@ public static function assertAllJsonValuesMatch($jsonDocument, $jsonPath, Constr
6364
* equal to.
6465
* @return void
6566
*/
66-
public static function assertJsonValueEquals($jsonDocument, $jsonPath, $expectedValue)
67+
public static function assertJsonValueEquals($jsonDocument, string $jsonPath, $expectedValue)
6768
{
6869
static::assertJsonValueMatches(
6970
$jsonDocument,
7071
$jsonPath,
71-
new \PHPUnit_Framework_Constraint_IsEqual($expectedValue)
72+
new IsEqual($expectedValue)
7273
);
7374
}
7475

@@ -85,12 +86,12 @@ public static function assertJsonValueEquals($jsonDocument, $jsonPath, $expected
8586
* equal to.
8687
* @return void
8788
*/
88-
public static function assertAllJsonValuesEqual($jsonDocument, $jsonPath, $expectedValue)
89+
public static function assertAllJsonValuesEqual($jsonDocument, string $jsonPath, $expectedValue)
8990
{
9091
static::assertAllJsonValuesMatch(
9192
$jsonDocument,
9293
$jsonPath,
93-
new \PHPUnit_Framework_Constraint_IsEqual($expectedValue)
94+
new IsEqual($expectedValue)
9495
);
9596
}
9697

tests/Functional/JsonValueMatchesFluentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Helmich\JsonAssert\Tests\Functional;
33

4-
use PHPUnit_Framework_TestCase as TestCase;
4+
use PHPUnit\Framework\TestCase;
55

66
class JsonValueMatchesFluentTest extends TestCase
77
{

tests/Functional/JsonValueMatchesSchemaFluentTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
namespace Helmich\JsonAssert\Tests\Functional;
33

44
use Helmich\JsonAssert\JsonAssertions;
5-
use PHPUnit_Framework_TestCase as TestCase;
5+
use PHPUnit\Framework\TestCase;
66

77
class JsonValueMatchesSchemaFluentTest extends TestCase
88
{
@@ -60,7 +60,7 @@ public function testJsonDocumentMatchesSchema()
6060
}
6161

6262
/**
63-
* @expectedException \PHPUnit_Framework_AssertionFailedError
63+
* @expectedException \PHPUnit\Framework\AssertionFailedError
6464
*/
6565
public function testJsonDocumentDoesNotMatchSchema()
6666
{

0 commit comments

Comments
 (0)