diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index a72829ed..e7a35e85 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -1,8 +1,12 @@ name: "Continuous Integration" on: - - push - - pull_request + push: + branches: + - master + pull_request: + branches: + - master env: COMPOSER_FLAGS: "--ansi --no-interaction --no-progress --prefer-dist" @@ -17,23 +21,20 @@ jobs: strategy: matrix: php-version: - - "5.3" - - "5.4" - - "5.5" - - "5.6" - - "7.0" - - "7.1" - "7.2" - "7.3" - "7.4" -# - "8.0" + - "8.0" + - "8.1" + - "8.2" + - "8.3" dependencies: [highest] experimental: [false] include: - - php-version: "5.3" + - php-version: "7.2" dependencies: highest experimental: false - - php-version: "5.3" + - php-version: "7.2" dependencies: lowest experimental: false # - php-version: "8.0" diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index d61f8c46..e16034f2 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -13,7 +13,7 @@ jobs: strategy: matrix: php-version: - - "5.3" + - "7.2" - "latest" steps: diff --git a/composer.json b/composer.json index 827cff0d..946fb395 100644 --- a/composer.json +++ b/composer.json @@ -27,14 +27,15 @@ } ], "require": { - "php": ">=5.3.3", + "php": "^7.2 || ^8.0", "marc-mabe/php-enum":"^2.0 || ^3.0 || ^4.0", - "icecave/parity": "1.0.0" + "icecave/parity": "^3.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": "^8.5", + "phpspec/prophecy": "^1.19" }, "extra": { "branch-alias": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0136d8ed..f49455f2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,6 +1,8 @@ - diff --git a/src/JsonSchema/Constraints/BaseConstraint.php b/src/JsonSchema/Constraints/BaseConstraint.php index 2e8339da..bd07d70f 100644 --- a/src/JsonSchema/Constraints/BaseConstraint.php +++ b/src/JsonSchema/Constraints/BaseConstraint.php @@ -39,12 +39,12 @@ class BaseConstraint /** * @param Factory $factory */ - public function __construct(Factory $factory = null) + public function __construct(?Factory $factory = null) { $this->factory = $factory ?: new Factory(); } - public function addError(ConstraintError $constraint, JsonPointer $path = null, array $more = array()) + public function addError(ConstraintError $constraint, ?JsonPointer $path = null, array $more = array()) { $message = $constraint ? $constraint->getMessage() : ''; $name = $constraint ? $constraint->getValue() : ''; diff --git a/src/JsonSchema/Constraints/CollectionConstraint.php b/src/JsonSchema/Constraints/CollectionConstraint.php index 825d4531..98aad564 100644 --- a/src/JsonSchema/Constraints/CollectionConstraint.php +++ b/src/JsonSchema/Constraints/CollectionConstraint.php @@ -23,7 +23,7 @@ class CollectionConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$value, $schema = null, JsonPointer $path = null, $i = null) + public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null) { // Verify minItems if (isset($schema->minItems) && count($value) < $schema->minItems) { @@ -62,7 +62,7 @@ public function check(&$value, $schema = null, JsonPointer $path = null, $i = nu * @param JsonPointer|null $path * @param string $i */ - protected function validateItems(&$value, $schema = null, JsonPointer $path = null, $i = null) + protected function validateItems(&$value, $schema = null, ?JsonPointer $path = null, $i = null) { if (is_object($schema->items)) { // just one type definition for the whole array diff --git a/src/JsonSchema/Constraints/ConstConstraint.php b/src/JsonSchema/Constraints/ConstConstraint.php index b0fa39c0..80c45cca 100644 --- a/src/JsonSchema/Constraints/ConstConstraint.php +++ b/src/JsonSchema/Constraints/ConstConstraint.php @@ -23,7 +23,7 @@ class ConstConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) { // Only validate const if the attribute exists if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) { diff --git a/src/JsonSchema/Constraints/Constraint.php b/src/JsonSchema/Constraints/Constraint.php index d088fd49..8a988cda 100644 --- a/src/JsonSchema/Constraints/Constraint.php +++ b/src/JsonSchema/Constraints/Constraint.php @@ -40,7 +40,7 @@ abstract class Constraint extends BaseConstraint implements ConstraintInterface * * @return JsonPointer; */ - protected function incrementPath(JsonPointer $path = null, $i) + protected function incrementPath(?JsonPointer $path = null, $i) { $path = $path ?: new JsonPointer(''); @@ -66,7 +66,7 @@ protected function incrementPath(JsonPointer $path = null, $i) * @param JsonPointer|null $path * @param mixed $i */ - protected function checkArray(&$value, $schema = null, JsonPointer $path = null, $i = null) + protected function checkArray(&$value, $schema = null, ?JsonPointer $path = null, $i = null) { $validator = $this->factory->createInstanceFor('collection'); $validator->check($value, $schema, $path, $i); @@ -84,7 +84,7 @@ protected function checkArray(&$value, $schema = null, JsonPointer $path = null, * @param mixed $additionalProperties * @param mixed $patternProperties */ - protected function checkObject(&$value, $schema = null, JsonPointer $path = null, $properties = null, + protected function checkObject(&$value, $schema = null, ?JsonPointer $path = null, $properties = null, $additionalProperties = null, $patternProperties = null, $appliedDefaults = array()) { /** @var ObjectConstraint $validator */ @@ -102,7 +102,7 @@ protected function checkObject(&$value, $schema = null, JsonPointer $path = null * @param JsonPointer|null $path * @param mixed $i */ - protected function checkType(&$value, $schema = null, JsonPointer $path = null, $i = null) + protected function checkType(&$value, $schema = null, ?JsonPointer $path = null, $i = null) { $validator = $this->factory->createInstanceFor('type'); $validator->check($value, $schema, $path, $i); @@ -118,7 +118,7 @@ protected function checkType(&$value, $schema = null, JsonPointer $path = null, * @param JsonPointer|null $path * @param mixed $i */ - protected function checkUndefined(&$value, $schema = null, JsonPointer $path = null, $i = null, $fromDefault = false) + protected function checkUndefined(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false) { /** @var UndefinedConstraint $validator */ $validator = $this->factory->createInstanceFor('undefined'); @@ -136,7 +136,7 @@ protected function checkUndefined(&$value, $schema = null, JsonPointer $path = n * @param JsonPointer|null $path * @param mixed $i */ - protected function checkString($value, $schema = null, JsonPointer $path = null, $i = null) + protected function checkString($value, $schema = null, ?JsonPointer $path = null, $i = null) { $validator = $this->factory->createInstanceFor('string'); $validator->check($value, $schema, $path, $i); @@ -147,12 +147,12 @@ protected function checkString($value, $schema = null, JsonPointer $path = null, /** * Checks a number element * - * @param mixed $value - * @param mixed $schema - * @param JsonPointer $path - * @param mixed $i + * @param mixed $value + * @param mixed $schema + * @param JsonPointer|null $path + * @param mixed $i */ - protected function checkNumber($value, $schema = null, JsonPointer $path = null, $i = null) + protected function checkNumber($value, $schema = null, ?JsonPointer $path = null, $i = null) { $validator = $this->factory->createInstanceFor('number'); $validator->check($value, $schema, $path, $i); @@ -168,7 +168,7 @@ protected function checkNumber($value, $schema = null, JsonPointer $path = null, * @param JsonPointer|null $path * @param mixed $i */ - protected function checkEnum($value, $schema = null, JsonPointer $path = null, $i = null) + protected function checkEnum($value, $schema = null, ?JsonPointer $path = null, $i = null) { $validator = $this->factory->createInstanceFor('enum'); $validator->check($value, $schema, $path, $i); @@ -184,7 +184,7 @@ protected function checkEnum($value, $schema = null, JsonPointer $path = null, $ * @param JsonPointer|null $path * @param mixed $i */ - protected function checkConst($value, $schema = null, JsonPointer $path = null, $i = null) + protected function checkConst($value, $schema = null, ?JsonPointer $path = null, $i = null) { $validator = $this->factory->createInstanceFor('const'); $validator->check($value, $schema, $path, $i); @@ -200,7 +200,7 @@ protected function checkConst($value, $schema = null, JsonPointer $path = null, * @param JsonPointer|null $path * @param mixed $i */ - protected function checkFormat($value, $schema = null, JsonPointer $path = null, $i = null) + protected function checkFormat($value, $schema = null, ?JsonPointer $path = null, $i = null) { $validator = $this->factory->createInstanceFor('format'); $validator->check($value, $schema, $path, $i); diff --git a/src/JsonSchema/Constraints/ConstraintInterface.php b/src/JsonSchema/Constraints/ConstraintInterface.php index a1b5aa97..209e5712 100644 --- a/src/JsonSchema/Constraints/ConstraintInterface.php +++ b/src/JsonSchema/Constraints/ConstraintInterface.php @@ -40,7 +40,7 @@ public function addErrors(array $errors); * @param JsonPointer|null $path * @param array $more more array elements to add to the error */ - public function addError(ConstraintError $constraint, JsonPointer $path = null, array $more = array()); + public function addError(ConstraintError $constraint, ?JsonPointer $path = null, array $more = array()); /** * checks if the validator has not raised errors @@ -61,5 +61,5 @@ public function isValid(); * * @throws \JsonSchema\Exception\ExceptionInterface */ - public function check(&$value, $schema = null, JsonPointer $path = null, $i = null); + public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null); } diff --git a/src/JsonSchema/Constraints/EnumConstraint.php b/src/JsonSchema/Constraints/EnumConstraint.php index a66d04a7..73fb8d24 100644 --- a/src/JsonSchema/Constraints/EnumConstraint.php +++ b/src/JsonSchema/Constraints/EnumConstraint.php @@ -24,7 +24,7 @@ class EnumConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) { // Only validate enum if the attribute exists if ($element instanceof UndefinedConstraint && (!isset($schema->required) || !$schema->required)) { diff --git a/src/JsonSchema/Constraints/Factory.php b/src/JsonSchema/Constraints/Factory.php index 0895b265..5f798c99 100644 --- a/src/JsonSchema/Constraints/Factory.php +++ b/src/JsonSchema/Constraints/Factory.php @@ -70,13 +70,13 @@ class Factory private $instanceCache = array(); /** - * @param SchemaStorage $schemaStorage - * @param UriRetrieverInterface $uriRetriever - * @param int $checkMode + * @param ?SchemaStorage $schemaStorage + * @param ?UriRetrieverInterface $uriRetriever + * @param int $checkMode */ public function __construct( - SchemaStorageInterface $schemaStorage = null, - UriRetrieverInterface $uriRetriever = null, + ?SchemaStorageInterface $schemaStorage = null, + ?UriRetrieverInterface $uriRetriever = null, $checkMode = Constraint::CHECK_MODE_NORMAL ) { // set provided config options diff --git a/src/JsonSchema/Constraints/FormatConstraint.php b/src/JsonSchema/Constraints/FormatConstraint.php index ebc791bb..a302573f 100644 --- a/src/JsonSchema/Constraints/FormatConstraint.php +++ b/src/JsonSchema/Constraints/FormatConstraint.php @@ -25,7 +25,7 @@ class FormatConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) { if (!isset($schema->format) || $this->factory->getConfig(self::CHECK_MODE_DISABLE_FORMAT)) { return; diff --git a/src/JsonSchema/Constraints/NumberConstraint.php b/src/JsonSchema/Constraints/NumberConstraint.php index c1b15f94..80b9d4dd 100644 --- a/src/JsonSchema/Constraints/NumberConstraint.php +++ b/src/JsonSchema/Constraints/NumberConstraint.php @@ -23,7 +23,7 @@ class NumberConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) { // Verify minimum if (isset($schema->exclusiveMinimum)) { diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index b4f85650..d633f372 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -28,7 +28,7 @@ class ObjectConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, JsonPointer $path = null, $properties = null, + public function check(&$element, $schema = null, ?JsonPointer $path = null, $properties = null, $additionalProp = null, $patternProperties = null, $appliedDefaults = array()) { if ($element instanceof UndefinedConstraint) { @@ -52,7 +52,7 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $prop $this->validateElement($element, $matches, $schema, $path, $properties, $additionalProp); } - public function validatePatternProperties($element, JsonPointer $path = null, $patternProperties) + public function validatePatternProperties($element, ?JsonPointer $path = null, $patternProperties) { $matches = array(); foreach ($patternProperties as $pregex => $schema) { @@ -84,7 +84,7 @@ public function validatePatternProperties($element, JsonPointer $path = null, $p * @param \StdClass $properties Properties * @param mixed $additionalProp Additional properties */ - public function validateElement($element, $matches, $schema = null, JsonPointer $path = null, + public function validateElement($element, $matches, $schema = null, ?JsonPointer $path = null, $properties = null, $additionalProp = null) { $this->validateMinMaxConstraint($element, $schema, $path); @@ -129,7 +129,7 @@ public function validateElement($element, $matches, $schema = null, JsonPointer * @param \stdClass $properties Property definitions * @param JsonPointer|null $path Path? */ - public function validateProperties(&$element, $properties = null, JsonPointer $path = null) + public function validateProperties(&$element, $properties = null, ?JsonPointer $path = null) { $undefinedConstraint = $this->factory->createInstanceFor('undefined'); @@ -171,7 +171,7 @@ protected function &getProperty(&$element, $property, $fallback = null) * @param \stdClass $objectDefinition ObjectConstraint definition * @param JsonPointer|null $path Path to test? */ - protected function validateMinMaxConstraint($element, $objectDefinition, JsonPointer $path = null) + protected function validateMinMaxConstraint($element, $objectDefinition, ?JsonPointer $path = null) { // Verify minimum number of properties if (isset($objectDefinition->minProperties) && !is_object($objectDefinition->minProperties)) { diff --git a/src/JsonSchema/Constraints/SchemaConstraint.php b/src/JsonSchema/Constraints/SchemaConstraint.php index 425c38f2..b804e6b0 100644 --- a/src/JsonSchema/Constraints/SchemaConstraint.php +++ b/src/JsonSchema/Constraints/SchemaConstraint.php @@ -29,7 +29,7 @@ class SchemaConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) { if ($schema !== null) { // passed schema diff --git a/src/JsonSchema/Constraints/StringConstraint.php b/src/JsonSchema/Constraints/StringConstraint.php index 1ccb23b0..29981f23 100644 --- a/src/JsonSchema/Constraints/StringConstraint.php +++ b/src/JsonSchema/Constraints/StringConstraint.php @@ -23,7 +23,7 @@ class StringConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, JsonPointer $path = null, $i = null) + public function check(&$element, $schema = null, ?JsonPointer $path = null, $i = null) { // Verify maxLength if (isset($schema->maxLength) && $this->strlen($element) > $schema->maxLength) { diff --git a/src/JsonSchema/Constraints/TypeConstraint.php b/src/JsonSchema/Constraints/TypeConstraint.php index be8e0672..c7a0b593 100644 --- a/src/JsonSchema/Constraints/TypeConstraint.php +++ b/src/JsonSchema/Constraints/TypeConstraint.php @@ -40,7 +40,7 @@ class TypeConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$value = null, $schema = null, JsonPointer $path = null, $i = null) + public function check(&$value = null, $schema = null, ?JsonPointer $path = null, $i = null) { $type = isset($schema->type) ? $schema->type : null; $isValid = false; diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index aa5e664f..6b4ad5e8 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -32,7 +32,7 @@ class UndefinedConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$value, $schema = null, JsonPointer $path = null, $i = null, $fromDefault = false) + public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null, $fromDefault = false) { if (is_null($schema) || !is_object($schema)) { return; diff --git a/src/JsonSchema/Exception/JsonDecodingException.php b/src/JsonSchema/Exception/JsonDecodingException.php index c7719828..7641f61b 100644 --- a/src/JsonSchema/Exception/JsonDecodingException.php +++ b/src/JsonSchema/Exception/JsonDecodingException.php @@ -14,7 +14,7 @@ */ class JsonDecodingException extends RuntimeException { - public function __construct($code = JSON_ERROR_NONE, \Exception $previous = null) + public function __construct($code = JSON_ERROR_NONE, ?\Exception $previous = null) { switch ($code) { case JSON_ERROR_DEPTH: diff --git a/src/JsonSchema/Iterator/ObjectIterator.php b/src/JsonSchema/Iterator/ObjectIterator.php index c459713b..12dd1c11 100644 --- a/src/JsonSchema/Iterator/ObjectIterator.php +++ b/src/JsonSchema/Iterator/ObjectIterator.php @@ -39,6 +39,7 @@ public function __construct($object) /** * {@inheritdoc} */ + #[\ReturnTypeWillChange] public function current() { $this->initialize(); @@ -49,7 +50,7 @@ public function current() /** * {@inheritdoc} */ - public function next() + public function next(): void { $this->initialize(); $this->position++; @@ -58,7 +59,7 @@ public function next() /** * {@inheritdoc} */ - public function key() + public function key(): int { $this->initialize(); @@ -68,7 +69,7 @@ public function key() /** * {@inheritdoc} */ - public function valid() + public function valid(): bool { $this->initialize(); @@ -78,7 +79,7 @@ public function valid() /** * {@inheritdoc} */ - public function rewind() + public function rewind(): void { $this->initialize(); $this->position = 0; @@ -87,7 +88,7 @@ public function rewind() /** * {@inheritdoc} */ - public function count() + public function count(): int { $this->initialize(); diff --git a/src/JsonSchema/SchemaStorage.php b/src/JsonSchema/SchemaStorage.php index 81b3508c..4e407413 100644 --- a/src/JsonSchema/SchemaStorage.php +++ b/src/JsonSchema/SchemaStorage.php @@ -17,8 +17,8 @@ class SchemaStorage implements SchemaStorageInterface protected $schemas = array(); public function __construct( - UriRetrieverInterface $uriRetriever = null, - UriResolverInterface $uriResolver = null + ?UriRetrieverInterface $uriRetriever = null, + ?UriResolverInterface $uriResolver = null ) { $this->uriRetriever = $uriRetriever ?: new UriRetriever(); $this->uriResolver = $uriResolver ?: new UriResolver(); diff --git a/src/JsonSchema/Uri/UriResolver.php b/src/JsonSchema/Uri/UriResolver.php index 8ab6650e..9785146d 100644 --- a/src/JsonSchema/Uri/UriResolver.php +++ b/src/JsonSchema/Uri/UriResolver.php @@ -28,7 +28,7 @@ class UriResolver implements UriResolverInterface */ public function parse($uri) { - preg_match('|^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?|', $uri, $match); + preg_match('|^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?|', (string) $uri, $match); $components = array(); if (5 < count($match)) { diff --git a/src/JsonSchema/Uri/UriRetriever.php b/src/JsonSchema/Uri/UriRetriever.php index 41147a2a..ab08e8a3 100644 --- a/src/JsonSchema/Uri/UriRetriever.php +++ b/src/JsonSchema/Uri/UriRetriever.php @@ -84,7 +84,7 @@ public function confirmMediaType($uriRetriever, $uri) } foreach ($this->allowedInvalidContentTypeEndpoints as $endpoint) { - if (strpos($uri, $endpoint) === 0) { + if (!\is_null($uri) && strpos($uri, $endpoint) === 0) { return true; } } diff --git a/tests/ConstraintErrorTest.php b/tests/ConstraintErrorTest.php index bd0082f0..487f7d2a 100644 --- a/tests/ConstraintErrorTest.php +++ b/tests/ConstraintErrorTest.php @@ -24,10 +24,9 @@ public function testGetInvalidMessage() { $e = ConstraintError::MISSING_ERROR(); - $this->setExpectedException( - '\JsonSchema\Exception\InvalidArgumentException', - 'Missing error message for missingError' - ); + $this->expectException('\JsonSchema\Exception\InvalidArgumentException'); + $this->expectExceptionMessage('Missing error message for missingError'); + $e->getMessage(); } } diff --git a/tests/Constraints/CoerciveTest.php b/tests/Constraints/CoerciveTest.php index 22684b3e..3c08d384 100644 --- a/tests/Constraints/CoerciveTest.php +++ b/tests/Constraints/CoerciveTest.php @@ -18,7 +18,7 @@ class CoerciveTest extends VeryBaseTestCase { protected $factory = null; - public function setUp() + public function setUp(): void { $this->factory = new Factory(); $this->factory->setConfig(Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_COERCE_TYPES); diff --git a/tests/Constraints/FactoryTest.php b/tests/Constraints/FactoryTest.php index adc11844..ea65e246 100644 --- a/tests/Constraints/FactoryTest.php +++ b/tests/Constraints/FactoryTest.php @@ -30,7 +30,7 @@ class MyBadConstraint */ class MyStringConstraint extends Constraint { - public function check(&$value, $schema = null, JsonPointer $path = null, $i = null) + public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null) { } } @@ -42,7 +42,7 @@ class FactoryTest extends TestCase */ protected $factory; - protected function setUp() + protected function setUp(): void { $this->factory = new Factory(); } @@ -85,7 +85,7 @@ public function constraintNameProvider() */ public function testExceptionWhenCreateInstanceForInvalidConstraintName($constraintName) { - $this->setExpectedException('JsonSchema\Exception\InvalidArgumentException'); + $this->expectException('JsonSchema\Exception\InvalidArgumentException'); $this->factory->createInstanceFor($constraintName); } @@ -96,19 +96,17 @@ public function invalidConstraintNameProvider() ); } - /** - * @expectedException \JsonSchema\Exception\InvalidArgumentException - */ public function testSetConstraintClassExistsCondition() { + $this->expectException(\JsonSchema\Exception\InvalidArgumentException::class); + $this->factory->setConstraintClass('string', 'SomeConstraint'); } - /** - * @expectedException \JsonSchema\Exception\InvalidArgumentException - */ public function testSetConstraintClassImplementsCondition() { + $this->expectException(\JsonSchema\Exception\InvalidArgumentException::class); + $this->factory->setConstraintClass('string', 'JsonSchema\Tests\Constraints\MyBadConstraint'); } diff --git a/tests/Constraints/FormatTest.php b/tests/Constraints/FormatTest.php index 78706c33..c49b5c68 100644 --- a/tests/Constraints/FormatTest.php +++ b/tests/Constraints/FormatTest.php @@ -17,7 +17,7 @@ class FormatTest extends BaseTestCase { protected $validateSchema = true; - public function setUp() + public function setUp(): void { date_default_timezone_set('UTC'); } diff --git a/tests/Constraints/MinLengthMaxLengthMultiByteTest.php b/tests/Constraints/MinLengthMaxLengthMultiByteTest.php index b19ec4f7..3e7d91e2 100644 --- a/tests/Constraints/MinLengthMaxLengthMultiByteTest.php +++ b/tests/Constraints/MinLengthMaxLengthMultiByteTest.php @@ -13,7 +13,7 @@ class MinLengthMaxLengthMultiByteTest extends BaseTestCase { protected $validateSchema = true; - protected function setUp() + protected function setUp(): void { if (!extension_loaded('mbstring')) { $this->markTestSkipped('mbstring extension is not available'); diff --git a/tests/Constraints/SchemaValidationTest.php b/tests/Constraints/SchemaValidationTest.php index 85907c5d..08bc42cb 100644 --- a/tests/Constraints/SchemaValidationTest.php +++ b/tests/Constraints/SchemaValidationTest.php @@ -102,19 +102,16 @@ public function testValidCases($schema) public function testNonObjectSchema() { - $this->setExpectedException( - '\JsonSchema\Exception\RuntimeException', - 'Cannot validate the schema of a non-object' - ); + $this->expectException('\JsonSchema\Exception\RuntimeException'); + $this->expectExceptionMessage('Cannot validate the schema of a non-object'); + $this->testValidCases('"notAnObject"'); } public function testInvalidSchemaException() { - $this->setExpectedException( - '\JsonSchema\Exception\InvalidSchemaException', - 'Schema did not pass validation' - ); + $this->expectException('\JsonSchema\Exception\InvalidSchemaException'); + $this->expectExceptionMessage('Schema did not pass validation'); $input = json_decode('{}'); $schema = json_decode('{"properties":{"propertyOne":{"type":"string","required":true}}}'); diff --git a/tests/Constraints/SelfDefinedSchemaTest.php b/tests/Constraints/SelfDefinedSchemaTest.php index 70108eb6..2dc4871b 100644 --- a/tests/Constraints/SelfDefinedSchemaTest.php +++ b/tests/Constraints/SelfDefinedSchemaTest.php @@ -74,7 +74,7 @@ public function testInvalidArgumentException() $v = new Validator(); - $this->setExpectedException('\JsonSchema\Exception\InvalidArgumentException'); + $this->expectException('\JsonSchema\Exception\InvalidArgumentException'); $v->validate($value, $schema); } diff --git a/tests/Constraints/TypeTest.php b/tests/Constraints/TypeTest.php index a10996da..ae35b449 100644 --- a/tests/Constraints/TypeTest.php +++ b/tests/Constraints/TypeTest.php @@ -80,7 +80,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->assertIsArray($actualError, sprintf('Failed to assert that Type error is an array, %s given', gettype($actualError))); $messageKey = 'message'; $this->assertArrayHasKey( @@ -116,6 +116,7 @@ public function testValidateTypeNameWording($nameWording) $m->setAccessible(true); $m->invoke($t, $nameWording); + $this->expectNotToPerformAssertions(); } public function testInvalidateTypeNameWording() @@ -125,10 +126,9 @@ public function testInvalidateTypeNameWording() $m = $r->getMethod('validateTypeNameWording'); $m->setAccessible(true); - $this->setExpectedException( - '\UnexpectedValueException', - "No wording for 'notAValidTypeName' available, expected wordings are: [an integer, a number, a boolean, an object, an array, a string, a null]" - ); + $this->expectException('\UnexpectedValueException'); + $this->expectExceptionMessage("No wording for 'notAValidTypeName' available, expected wordings are: [an integer, a number, a boolean, an object, an array, a string, a null]"); + $m->invoke($t, 'notAValidTypeName'); } @@ -138,10 +138,9 @@ public function testValidateTypeException() $data = new \stdClass(); $schema = json_decode('{"type": "notAValidTypeName"}'); - $this->setExpectedException( - 'JsonSchema\Exception\InvalidArgumentException', - 'object is an invalid type for notAValidTypeName' - ); + $this->expectException('JsonSchema\Exception\InvalidArgumentException'); + $this->expectExceptionMessage('object is an invalid type for notAValidTypeName'); + $t->check($data, $schema); } } diff --git a/tests/Constraints/ValidationExceptionTest.php b/tests/Constraints/ValidationExceptionTest.php index 57995769..317fc13f 100644 --- a/tests/Constraints/ValidationExceptionTest.php +++ b/tests/Constraints/ValidationExceptionTest.php @@ -45,7 +45,7 @@ public function testValidationException() $exception->getMessage() ); - $this->setExpectedException('JsonSchema\Exception\ValidationException'); + $this->expectException('JsonSchema\Exception\ValidationException'); throw $exception; } } diff --git a/tests/Entity/JsonPointerTest.php b/tests/Entity/JsonPointerTest.php index d063d32d..70feb172 100644 --- a/tests/Entity/JsonPointerTest.php +++ b/tests/Entity/JsonPointerTest.php @@ -113,10 +113,9 @@ public function testJsonPointerWithPropertyPaths() public function testCreateWithInvalidValue() { - $this->setExpectedException( - '\JsonSchema\Exception\InvalidArgumentException', - 'Ref value must be a string' - ); + $this->expectException('\JsonSchema\Exception\InvalidArgumentException'); + $this->expectExceptionMessage('Ref value must be a string'); + new JsonPointer(null); } } diff --git a/tests/Iterators/ObjectIteratorTest.php b/tests/Iterators/ObjectIteratorTest.php index e36d2ea4..a226c060 100644 --- a/tests/Iterators/ObjectIteratorTest.php +++ b/tests/Iterators/ObjectIteratorTest.php @@ -16,7 +16,7 @@ class ObjectIteratorTest extends TestCase { protected $testObject; - public function setUp() + public function setUp(): void { $this->testObject = (object) array( 'subOne' => (object) array( diff --git a/tests/RefTest.php b/tests/RefTest.php index 64525122..a356a413 100644 --- a/tests/RefTest.php +++ b/tests/RefTest.php @@ -69,7 +69,7 @@ public function testRefIgnoresSiblings($schema, $document, $isValid, $exception $v = new Validator(); if ($exception) { - $this->setExpectedException($exception); + $this->expectException($exception); } $v->validate($document, $schema); diff --git a/tests/SchemaStorageTest.php b/tests/SchemaStorageTest.php index ebbc9477..d4e812cb 100644 --- a/tests/SchemaStorageTest.php +++ b/tests/SchemaStorageTest.php @@ -102,10 +102,8 @@ public function testSchemaWithLocalAndExternalReferencesWithCircularReference() public function testUnresolvableJsonPointExceptionShouldBeThrown() { - $this->setExpectedException( - 'JsonSchema\Exception\UnresolvableJsonPointerException', - 'File: http://www.example.com/schema.json is found, but could not resolve fragment: #/definitions/car' - ); + $this->expectException('JsonSchema\Exception\UnresolvableJsonPointerException'); + $this->expectExceptionMessage('File: http://www.example.com/schema.json is found, but could not resolve fragment: #/definitions/car'); $mainSchema = $this->getInvalidSchema(); $mainSchemaPath = 'http://www.example.com/schema.json'; @@ -121,10 +119,8 @@ public function testUnresolvableJsonPointExceptionShouldBeThrown() public function testResolveRefWithNoAssociatedFileName() { - $this->setExpectedException( - 'JsonSchema\Exception\UnresolvableJsonPointerException', - "Could not resolve fragment '#': no file is defined" - ); + $this->expectException('JsonSchema\Exception\UnresolvableJsonPointerException'); + $this->expectExceptionMessage("Could not resolve fragment '#': no file is defined"); $schemaStorage = new SchemaStorage(); $schemaStorage->resolveRef('#'); diff --git a/tests/Uri/Retrievers/CurlTest.php b/tests/Uri/Retrievers/CurlTest.php index 550baff1..40293905 100644 --- a/tests/Uri/Retrievers/CurlTest.php +++ b/tests/Uri/Retrievers/CurlTest.php @@ -10,24 +10,27 @@ class CurlTest extends TestCase public function testRetrieveFile() { $c = new Curl(); - $c->retrieve(realpath(__DIR__ . '/../../fixtures/foobar.json')); + $result = $c->retrieve(realpath(__DIR__ . '/../../fixtures/foobar.json')); + + self::assertStringEqualsFileCanonicalizing(realpath(__DIR__ . '/../../fixtures/foobar.json'), $result); } public function testRetrieveNonexistantFile() { $c = new Curl(); - $this->setExpectedException( - '\JsonSchema\Exception\ResourceNotFoundException', - 'JSON schema not found' - ); + $this->expectException('\JsonSchema\Exception\ResourceNotFoundException'); + $this->expectExceptionMessage('JSON schema not found'); + $c->retrieve(__DIR__ . '/notARealFile'); } public function testNoContentType() { $c = new Curl(); - $c->retrieve(realpath(__DIR__ . '/../../fixtures') . '/foobar-noheader.json'); + $result = $c->retrieve(realpath(__DIR__ . '/../../fixtures') . '/foobar-noheader.json'); + + self::assertStringEqualsFileCanonicalizing(realpath(__DIR__ . '/../../fixtures/foobar.json'), $result); } } } diff --git a/tests/Uri/Retrievers/FileGetContentsTest.php b/tests/Uri/Retrievers/FileGetContentsTest.php index 2301f3bc..6f8f2b3f 100644 --- a/tests/Uri/Retrievers/FileGetContentsTest.php +++ b/tests/Uri/Retrievers/FileGetContentsTest.php @@ -10,12 +10,12 @@ */ class FileGetContentsTest extends TestCase { - /** - * @expectedException \JsonSchema\Exception\ResourceNotFoundException - */ public function testFetchMissingFile() { $res = new FileGetContents(); + + $this->expectException(\JsonSchema\Exception\ResourceNotFoundException::class); + $res->retrieve(__DIR__ . '/Fixture/missing.json'); } diff --git a/tests/Uri/Retrievers/PredefinedArrayTest.php b/tests/Uri/Retrievers/PredefinedArrayTest.php index 7684bb1f..84ecf3c7 100644 --- a/tests/Uri/Retrievers/PredefinedArrayTest.php +++ b/tests/Uri/Retrievers/PredefinedArrayTest.php @@ -12,7 +12,7 @@ class PredefinedArrayTest extends TestCase { private $retriever; - public function setUp() + public function setUp(): void { $this->retriever = new PredefinedArray( array( @@ -29,11 +29,9 @@ public function testRetrieve() $this->assertEquals('THE_ADDRESS_SCHEMA', $this->retriever->retrieve('http://acme.com/schemas/address#')); } - /** - * @expectedException \JsonSchema\Exception\ResourceNotFoundException - */ public function testRetrieveNonExistsingSchema() { + $this->expectException(\JsonSchema\Exception\ResourceNotFoundException::class); $this->retriever->retrieve('http://acme.com/schemas/plop#'); } diff --git a/tests/Uri/UriResolverTest.php b/tests/Uri/UriResolverTest.php index b0207cb7..38274409 100644 --- a/tests/Uri/UriResolverTest.php +++ b/tests/Uri/UriResolverTest.php @@ -9,7 +9,7 @@ class UriResolverTest extends TestCase { private $resolver; - public function setUp() + public function setUp(): void { $this->resolver = new UriResolver(); } @@ -96,18 +96,10 @@ public function testResolveAbsoluteUri() ); } - /** - * @expectedException \JsonSchema\Exception\UriResolverException - */ public function testResolveRelativeUriNoBase() { - $this->assertEquals( - 'http://example.org/foo/bar.json', - $this->resolver->resolve( - 'bar.json', - null - ) - ); + $this->expectException(\JsonSchema\Exception\UriResolverException::class); + $this->resolver->resolve('bar.json', null); } public function testResolveRelativeUriBaseDir() diff --git a/tests/Uri/UriRetrieverTest.php b/tests/Uri/UriRetrieverTest.php index 3c500699..b494dcd1 100644 --- a/tests/Uri/UriRetrieverTest.php +++ b/tests/Uri/UriRetrieverTest.php @@ -9,7 +9,10 @@ namespace JsonSchema\Tests\Uri; +use JsonSchema\Exception\InvalidSchemaMediaTypeException; use JsonSchema\Exception\JsonDecodingException; +use JsonSchema\Exception\ResourceNotFoundException; +use JsonSchema\Exception\UriResolverException; use JsonSchema\Uri\UriRetriever; use JsonSchema\Validator; use PHPUnit\Framework\TestCase; @@ -21,7 +24,7 @@ class UriRetrieverTest extends TestCase { protected $validator; - protected function setUp() + protected function setUp(): void { $this->validator = new Validator(); } @@ -34,7 +37,7 @@ private function getRetrieverMock($returnSchema) throw new JsonDecodingException($error); } - $retriever = $this->getMock('JsonSchema\Uri\UriRetriever', array('retrieve')); + $retriever = $this->createMock('JsonSchema\Uri\UriRetriever'); $retriever->expects($this->at(0)) ->method('retrieve') @@ -182,9 +185,6 @@ public function testResolvePointerFragment() ); } - /** - * @expectedException \JsonSchema\Exception\ResourceNotFoundException - */ public function testResolvePointerFragmentNotFound() { $schema = (object) array( @@ -197,14 +197,13 @@ public function testResolvePointerFragmentNotFound() ); $retriever = new UriRetriever(); + + $this->expectException(ResourceNotFoundException::class); $retriever->resolvePointer( $schema, 'http://example.org/schema.json#/definitions/bar' ); } - /** - * @expectedException \JsonSchema\Exception\ResourceNotFoundException - */ public function testResolvePointerFragmentNoArray() { $schema = (object) array( @@ -217,17 +216,18 @@ public function testResolvePointerFragmentNoArray() ); $retriever = new UriRetriever(); + + $this->expectException(ResourceNotFoundException::class); $retriever->resolvePointer( $schema, 'http://example.org/schema.json#/definitions/foo' ); } - /** - * @expectedException \JsonSchema\Exception\UriResolverException - */ public function testResolveExcessLevelUp() { $retriever = new UriRetriever(); + + $this->expectException(UriResolverException::class); $retriever->resolve( '../schema.json#', 'http://example.org/schema.json#' ); @@ -235,7 +235,7 @@ public function testResolveExcessLevelUp() public function testConfirmMediaTypeAcceptsJsonSchemaType() { - $uriRetriever = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); + $uriRetriever = $this->createMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); $retriever = new UriRetriever(); $uriRetriever->expects($this->at(0)) @@ -247,7 +247,7 @@ public function testConfirmMediaTypeAcceptsJsonSchemaType() public function testConfirmMediaTypeAcceptsJsonType() { - $uriRetriever = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); + $uriRetriever = $this->createMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); $retriever = new UriRetriever(); $uriRetriever->expects($this->at(0)) @@ -257,19 +257,17 @@ public function testConfirmMediaTypeAcceptsJsonType() $this->assertEquals(null, $retriever->confirmMediaType($uriRetriever, null)); } - /** - * @expectedException \JsonSchema\Exception\InvalidSchemaMediaTypeException - */ public function testConfirmMediaTypeThrowsExceptionForUnsupportedTypes() { - $uriRetriever = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); + $uriRetriever = $this->createMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); $retriever = new UriRetriever(); - $uriRetriever->expects($this->at(0)) ->method('getContentType') ->willReturn('text/html'); - $this->assertEquals(null, $retriever->confirmMediaType($uriRetriever, null)); + $this->expectException(InvalidSchemaMediaTypeException::class); + + $retriever->confirmMediaType($uriRetriever, null); } private function mockRetriever($schema) @@ -302,7 +300,7 @@ public function testPackageURITranslation() $root = sprintf('file://%s/', realpath(__DIR__ . '/../..')); $uri = $retriever->translate('package://foo/bar.json'); - $this->assertEquals("${root}foo/bar.json", $uri); + $this->assertEquals("{$root}foo/bar.json", $uri); } public function testDefaultDistTranslations() @@ -335,7 +333,7 @@ public function testRetrieveSchemaFromPackage() public function testInvalidContentTypeEndpointsDefault() { - $mock = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); + $mock = $this->createMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); $retriever = new UriRetriever(); @@ -343,26 +341,26 @@ public function testInvalidContentTypeEndpointsDefault() $this->assertTrue($retriever->confirmMediaType($mock, 'https://json-schema.org/')); } - /** - * @expectedException \JsonSchema\Exception\InvalidSchemaMediaTypeException - */ public function testInvalidContentTypeEndpointsUnknown() { - $mock = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); + $mock = $this->createMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); $retriever = new UriRetriever(); + $this->expectException(InvalidSchemaMediaTypeException::class); $retriever->confirmMediaType($mock, 'http://example.com'); } public function testInvalidContentTypeEndpointsAdded() { - $mock = $this->getMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); + $mock = $this->createMock('JsonSchema\Uri\Retrievers\UriRetrieverInterface'); $mock->method('getContentType')->willReturn('Application/X-Fake-Type'); $retriever = new UriRetriever(); $retriever->addInvalidContentTypeEndpoint('http://example.com'); - $retriever->confirmMediaType($mock, 'http://example.com'); + $result = $retriever->confirmMediaType($mock, 'http://example.com'); + + self::assertTrue($result); } public function testSchemaCache() @@ -388,10 +386,9 @@ public function testLoadSchemaJSONDecodingException() { $retriever = new UriRetriever(); - $this->setExpectedException( - 'JsonSchema\Exception\JsonDecodingException', - 'JSON syntax is malformed' - ); + $this->expectException('JsonSchema\Exception\JsonDecodingException'); + $this->expectExceptionMessage('JSON syntax is malformed'); + $retriever->retrieve('package://tests/fixtures/bad-syntax.json'); } diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index ca62e45c..774d2c11 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -31,7 +31,7 @@ public function testBadAssocSchemaInput() $validator = new Validator(); - $this->setExpectedException('\JsonSchema\Exception\InvalidArgumentException'); + $this->expectException('\JsonSchema\Exception\InvalidArgumentException'); $validator->validate($data, $schema); }