Skip to content

Another attempt to make it work under PHP 5.3 and PHP 8+ #693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ff33d72
Another attempt to make it work under PHP 5.3 and PHP 8+
sanmai Aug 31, 2022
ac97290
Use adapter instead
sanmai Aug 31, 2022
cc3d1cb
Use a different TestCase implementation
sanmai Aug 31, 2022
861deec
Renames, dependencies
sanmai Aug 31, 2022
5bb24e3
/getMock/createPartialMock/
sanmai Aug 31, 2022
80bb70e
expectException instead
sanmai Aug 31, 2022
43c49e7
Fix more tests
sanmai Sep 1, 2022
6530d3f
Fix remaining errors
sanmai Sep 1, 2022
ebc1243
Require PHPUnit <10
sanmai Sep 1, 2022
df08460
Swap dependencies
sanmai Sep 1, 2022
9fc4671
PHP 5.3 fix
sanmai Sep 1, 2022
f21197f
Bump PHPUnit a bit
sanmai Sep 1, 2022
8436a48
We have to use PolyfillTrait
sanmai Sep 1, 2022
01ec2c9
Use bundleted trait instead
sanmai Sep 1, 2022
a9eae67
Use vendored phpunit
sanmai Sep 1, 2022
d31898e
Just use the polyfill all the time
sanmai Sep 1, 2022
7f8b32f
Bummer, we still need to account for return void
sanmai Sep 1, 2022
ab4622d
Try with PHP 7.1 as lowest
sanmai Sep 1, 2022
52640e1
Revert "Try with PHP 7.1 as lowest"
sanmai Sep 1, 2022
44cc011
Revert "Bummer, we still need to account for return void"
sanmai Sep 1, 2022
c980858
Revert "Just use the polyfill all the time"
sanmai Sep 1, 2022
c52483b
Revert "Use bundleted trait instead"
sanmai Sep 1, 2022
872b34c
Revert "We have to use PolyfillTrait"
sanmai Sep 1, 2022
095ae91
Can't use traits in PHP 5.3
sanmai Sep 1, 2022
4350562
Use compat methods
sanmai Sep 1, 2022
7b74df3
8.1 + lowest-ignore can be built ever
sanmai Sep 1, 2022
4083f14
Use getMockCompat
sanmai Sep 1, 2022
3e00bdb
Make it private
sanmai Sep 1, 2022
f911163
CS
sanmai Sep 1, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:
- "7.2"
- "7.3"
- "7.4"
# - "8.0"
- "8.0"
- "8.1"
dependencies: [highest]
experimental: [false]
include:
Expand All @@ -36,15 +37,12 @@ jobs:
- php-version: "5.3"
dependencies: lowest
experimental: false
# - php-version: "8.0"
# dependencies: highest
# experimental: false
# - php-version: "8.1"
# dependencies: lowest-ignore
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to drop this because it leads to installing dependencies broken on PHP 8.1.

# experimental: true
# - php-version: "8.1"
# dependencies: highest-ignore
# experimental: true
- php-version: "8.0"
dependencies: highest
experimental: false
- php-version: "8.1"
dependencies: highest-ignore
experimental: true

steps:
- name: "Checkout"
Expand Down
12 changes: 7 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@
"require": {
"php": ">=5.3.3",
"marc-mabe/php-enum":"^2.0 || ^3.0 || ^4.0",
"icecave/parity": "1.0.0"
"icecave/parity": ">=1.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"icecave/parity": ">=1.0.0"
"icecave/parity": "^1.0.0"

},
"require-dev": {
"friendsofphp/php-cs-fixer": "~2.2.20 || ~2.19.0",
"json-schema/json-schema-test-suite": "1.2.0",
"phpunit/phpunit": "^4.8.35"
"phpunit/phpunit": "^4.8.36 || ^5.7.27 || ^6.5.14 || ^7.5.20 || ^8.5.29 || ^9.5.24",
"sanmai/phpunit-legacy-adapter": "^6.4 || ^8.2.1",
Copy link
Author

@sanmai sanmai Sep 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a certain bias since I'm the author of this package. In my defense, it is used by several high-profile packages with millions of installs to solve the very this problem.

Check out the readme: https://github.com/sanmai/phpunit-legacy-adapter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should probably not need this I think there should be another way of managing this, if I understand correctly PHPUnit 9 is not compatible with php < 8 right? IMO we should try to run PHPUnit 9 if possible, if not we should take the highest one possible. We can definitely do this inside the CI directly but leave this requirement to 9.

"phpspec/prophecy-phpunit": "^1.1 || ^2.0.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this dependency?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC most recent PHPUnit does not depend on this library, where in fact it is used by this library.

But I digress: it has been two years; my memory is foggy.

},
"extra": {
"branch-alias": {
Expand Down Expand Up @@ -69,10 +71,10 @@
"bin/validate-json"
],
"scripts": {
"coverage": "phpunit --coverage-text",
"coverage": "vendor/bin/phpunit --coverage-text",
"style-check": "php-cs-fixer fix --dry-run --verbose --diff",
"style-fix": "php-cs-fixer fix --verbose",
"test": "phpunit",
"testOnly": "phpunit --colors --filter"
"test": "vendor/bin/phpunit",
"testOnly": "vendor/bin/phpunit --colors --filter"
}
}
6 changes: 3 additions & 3 deletions tests/ConstraintErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
namespace JsonSchema\Tests;

use JsonSchema\ConstraintError;
use PHPUnit\Framework\TestCase;
use JsonSchema\Tests\Constraints\VeryBaseTestCase;

class ConstraintErrorTest extends TestCase
class ConstraintErrorTest extends VeryBaseTestCase
{
public function testGetValidMessage()
{
Expand All @@ -24,7 +24,7 @@ public function testGetInvalidMessage()
{
$e = ConstraintError::MISSING_ERROR();

$this->setExpectedException(
$this->expectExceptionCompat(
'\JsonSchema\Exception\InvalidArgumentException',
'Missing error message for missingError'
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Constraints/CoerciveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class CoerciveTest extends VeryBaseTestCase
{
protected $factory = null;

public function setUp()
public function doSetUp()
{
$this->factory = new Factory();
$this->factory->setConfig(Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_COERCE_TYPES);
Expand Down
17 changes: 7 additions & 10 deletions tests/Constraints/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use JsonSchema\Constraints\Constraint;
use JsonSchema\Constraints\Factory;
use JsonSchema\Entity\JsonPointer;
use PHPUnit\Framework\TestCase;

/**
* Class MyBadConstraint
Expand All @@ -35,14 +34,14 @@ public function check(&$value, $schema = null, JsonPointer $path = null, $i = nu
}
}

class FactoryTest extends TestCase
class FactoryTest extends VeryBaseTestCase
{
/**
* @var Factory
*/
protected $factory;

protected function setUp()
protected function doSetUp()
{
$this->factory = new Factory();
}
Expand Down Expand Up @@ -85,7 +84,7 @@ public function constraintNameProvider()
*/
public function testExceptionWhenCreateInstanceForInvalidConstraintName($constraintName)
{
$this->setExpectedException('JsonSchema\Exception\InvalidArgumentException');
$this->expectExceptionCompat('JsonSchema\Exception\InvalidArgumentException');
$this->factory->createInstanceFor($constraintName);
}

Expand All @@ -96,19 +95,17 @@ public function invalidConstraintNameProvider()
);
}

/**
* @expectedException \JsonSchema\Exception\InvalidArgumentException
*/
public function testSetConstraintClassExistsCondition()
{
$this->expectExceptionCompat('\JsonSchema\Exception\InvalidArgumentException');

$this->factory->setConstraintClass('string', 'SomeConstraint');
}

/**
* @expectedException \JsonSchema\Exception\InvalidArgumentException
*/
public function testSetConstraintClassImplementsCondition()
{
$this->expectExceptionCompat('\JsonSchema\Exception\InvalidArgumentException');

$this->factory->setConstraintClass('string', 'JsonSchema\Tests\Constraints\MyBadConstraint');
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Constraints/FormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FormatTest extends BaseTestCase
{
protected $validateSchema = true;

public function setUp()
public function doSetUp()
{
date_default_timezone_set('UTC');
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Constraints/MinLengthMaxLengthMultiByteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MinLengthMaxLengthMultiByteTest extends BaseTestCase
{
protected $validateSchema = true;

protected function setUp()
protected function doSetUp()
{
if (!extension_loaded('mbstring')) {
$this->markTestSkipped('mbstring extension is not available');
Expand Down
2 changes: 1 addition & 1 deletion tests/Constraints/PointerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace JsonSchema\Tests\Constraints;

use JsonSchema\Validator;
use PHPUnit\Framework\TestCase;
use LegacyPHPUnit\TestCase;

class PointerTest extends TestCase
{
Expand Down
7 changes: 3 additions & 4 deletions tests/Constraints/SchemaValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator;
use PHPUnit\Framework\TestCase;

class SchemaValidationTest extends TestCase
class SchemaValidationTest extends VeryBaseTestCase
{
protected $validateSchema = true;

Expand Down Expand Up @@ -102,7 +101,7 @@ public function testValidCases($schema)

public function testNonObjectSchema()
{
$this->setExpectedException(
$this->expectExceptionCompat(
'\JsonSchema\Exception\RuntimeException',
'Cannot validate the schema of a non-object'
);
Expand All @@ -111,7 +110,7 @@ public function testNonObjectSchema()

public function testInvalidSchemaException()
{
$this->setExpectedException(
$this->expectExceptionCompat(
'\JsonSchema\Exception\InvalidSchemaException',
'Schema did not pass validation'
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Constraints/SelfDefinedSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testInvalidArgumentException()

$v = new Validator();

$this->setExpectedException('\JsonSchema\Exception\InvalidArgumentException');
$this->expectExceptionCompat('\JsonSchema\Exception\InvalidArgumentException');

$v->validate($value, $schema);
}
Expand Down
9 changes: 4 additions & 5 deletions tests/Constraints/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use JsonSchema\Constraints\TypeCheck\LooseTypeCheck;
use JsonSchema\Constraints\TypeConstraint;
use PHPUnit\Framework\TestCase;

/**
* Class TypeTest
Expand All @@ -20,7 +19,7 @@
*
* @author hakre <https://github.com/hakre>
*/
class TypeTest extends TestCase
class TypeTest extends VeryBaseTestCase
{
/**
* @see testIndefiniteArticleForTypeInTypeCheckErrorMessage
Expand Down Expand Up @@ -80,7 +79,7 @@ private function assertTypeConstraintError($expected, TypeConstraint $actual)

$actualError = $actualErrors[0];

$this->assertInternalType('array', $actualError, sprintf('Failed to assert that Type error is an array, %s given', gettype($actualError)));
$this->assertIsArrayCompat($actualError, sprintf('Failed to assert that Type error is an array, %s given', gettype($actualError)));

$messageKey = 'message';
$this->assertArrayHasKey(
Expand Down Expand Up @@ -125,7 +124,7 @@ public function testInvalidateTypeNameWording()
$m = $r->getMethod('validateTypeNameWording');
$m->setAccessible(true);

$this->setExpectedException(
$this->expectExceptionCompat(
'\UnexpectedValueException',
"No wording for 'notAValidTypeName' available, expected wordings are: [an integer, a number, a boolean, an object, an array, a string, a null]"
);
Expand All @@ -138,7 +137,7 @@ public function testValidateTypeException()
$data = new \stdClass();
$schema = json_decode('{"type": "notAValidTypeName"}');

$this->setExpectedException(
$this->expectExceptionCompat(
'JsonSchema\Exception\InvalidArgumentException',
'object is an invalid type for notAValidTypeName'
);
Expand Down
5 changes: 2 additions & 3 deletions tests/Constraints/ValidationExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
use JsonSchema\Constraints\Constraint;
use JsonSchema\Exception\ValidationException;
use JsonSchema\Validator;
use PHPUnit\Framework\TestCase;

class ValidationExceptionTest extends TestCase
class ValidationExceptionTest extends VeryBaseTestCase
{
public function testValidationException()
{
Expand Down Expand Up @@ -45,7 +44,7 @@ public function testValidationException()
$exception->getMessage()
);

$this->setExpectedException('JsonSchema\Exception\ValidationException');
$this->expectExceptionCompat('JsonSchema\Exception\ValidationException');
throw $exception;
}
}
20 changes: 19 additions & 1 deletion tests/Constraints/VeryBaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace JsonSchema\Tests\Constraints;

use PHPUnit\Framework\TestCase;
use LegacyPHPUnit\TestCase;
use Prophecy\Argument;

/**
Expand Down Expand Up @@ -87,4 +87,22 @@ private function getJsonSchemaDraft04()

return $this->jsonSchemaDraft04;
}

public function expectExceptionCompat($exception)
{
if (\is_callable(array('PHPUnit\Framework\TestCase', 'expectException'))) {
parent::expectException($exception);
} else {
$this->setExpectedException($exception);
}
}

public static function assertIsArrayCompat($actual, $message = '')
{
if (\is_callable(array('PHPUnit\Framework\TestCase', 'assertIsArray'))) {
parent::assertIsArray($actual, $message);
} else {
static::assertInternalType('array', $actual, $message);
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Target PHP versions do not support traits, therefore we can't use off-the-shelf polyfill.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about replacing assertInternalType with an assertTrue(is_array($data), $message) instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following the same logic we should use assertTrue(is_array()) instead of assertIsArray(); what do you think?

}
6 changes: 3 additions & 3 deletions tests/Entity/JsonPointerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
namespace JsonSchema\Tests\Entity;

use JsonSchema\Entity\JsonPointer;
use PHPUnit\Framework\TestCase;
use JsonSchema\Tests\Constraints\VeryBaseTestCase;

/**
* @package JsonSchema\Tests\Entity
*
* @author Joost Nijhuis <[email protected]>
*/
class JsonPointerTest extends TestCase
class JsonPointerTest extends VeryBaseTestCase
{
/**
* @dataProvider getTestData
Expand Down Expand Up @@ -113,7 +113,7 @@ public function testJsonPointerWithPropertyPaths()

public function testCreateWithInvalidValue()
{
$this->setExpectedException(
$this->expectExceptionCompat(
'\JsonSchema\Exception\InvalidArgumentException',
'Ref value must be a string'
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Exception/InvalidArgumentExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JsonSchema\Tests\Exception;

use JsonSchema\Exception\InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use LegacyPHPUnit\TestCase;

class InvalidArgumentExceptionTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Exception/InvalidSchemaMediaTypeExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JsonSchema\Tests\Exception;

use JsonSchema\Exception\InvalidSchemaMediaTypeException;
use PHPUnit\Framework\TestCase;
use LegacyPHPUnit\TestCase;

class InvalidSchemaMediaTypeExceptionTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Exception/InvalidSourceUriExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JsonSchema\Tests\Exception;

use JsonSchema\Exception\InvalidSourceUriException;
use PHPUnit\Framework\TestCase;
use LegacyPHPUnit\TestCase;

class InvalidSourceUriExceptionTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Exception/JsonDecodingExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JsonSchema\Tests\Exception;

use JsonSchema\Exception\JsonDecodingException;
use PHPUnit\Framework\TestCase;
use LegacyPHPUnit\TestCase;

class JsonDecodingExceptionTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Exception/ResourceNotFoundExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JsonSchema\Tests\Exception;

use JsonSchema\Exception\ResourceNotFoundException;
use PHPUnit\Framework\TestCase;
use LegacyPHPUnit\TestCase;

class ResourceNotFoundExceptionTest extends TestCase
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Exception/RuntimeExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JsonSchema\Tests\Exception;

use JsonSchema\Exception\RuntimeException;
use PHPUnit\Framework\TestCase;
use LegacyPHPUnit\TestCase;

class RuntimeExceptionTest extends TestCase
{
Expand Down
Loading