From e3606713377fe04dff4c4373b7f84227e4f60a4b Mon Sep 17 00:00:00 2001 From: Danny van der Sluijs Date: Sun, 24 Nov 2024 23:11:28 +0100 Subject: [PATCH] refactor: Cleanup test from @param annotations --- tests/Constraints/FactoryTest.php | 134 +++++++++++++------------ tests/Constraints/TypeTest.php | 5 +- tests/Constraints/VeryBaseTestCase.php | 7 +- tests/Drafts/BaseDraftTestCase.php | 8 +- tests/Entity/JsonPointerTest.php | 109 ++++++++++---------- tests/Rfc3339Test.php | 81 +++++++-------- 6 files changed, 169 insertions(+), 175 deletions(-) diff --git a/tests/Constraints/FactoryTest.php b/tests/Constraints/FactoryTest.php index f29614ee..5dc20c22 100644 --- a/tests/Constraints/FactoryTest.php +++ b/tests/Constraints/FactoryTest.php @@ -7,27 +7,22 @@ * file that was distributed with this source code. */ +declare(strict_types=1); + namespace JsonSchema\Tests\Constraints; use JsonSchema\Constraints\Constraint; use JsonSchema\Constraints\Factory; use JsonSchema\Entity\JsonPointer; use PHPUnit\Framework\TestCase; +use JsonSchema\Constraints; +use JsonSchema\Constraints\ConstraintInterface; +use JsonSchema\Exception\InvalidArgumentException; -/** - * Class MyBadConstraint - * - * @package JsonSchema\Tests\Constraints - */ class MyBadConstraint { } -/** - * Class MyStringConstraint - * - * @package JsonSchema\Tests\Constraints - */ class MyStringConstraint extends Constraint { public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = null) @@ -37,110 +32,125 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n class FactoryTest extends TestCase { - /** - * @var Factory - */ - protected $factory; - - protected function setUp(): void - { - $this->factory = new Factory(); - } - /** * @dataProvider constraintNameProvider - * - * @param string $constraintName - * @param string $expectedClass */ - public function testCreateInstanceForConstraintName($constraintName, $expectedClass): void + public function testCreateInstanceForConstraintName(string $constraintName, string $expectedClass): void { - $constraint = $this->factory->createInstanceFor($constraintName); + $factory = new Factory(); + $constraint = $factory->createInstanceFor($constraintName); $this->assertInstanceOf($expectedClass, $constraint); - $this->assertInstanceOf('JsonSchema\Constraints\ConstraintInterface', $constraint); + $this->assertInstanceOf(ConstraintInterface::class, $constraint); } - public function constraintNameProvider(): array + public static function constraintNameProvider(): \Generator { - return [ - ['array', 'JsonSchema\Constraints\CollectionConstraint'], - ['collection', 'JsonSchema\Constraints\CollectionConstraint'], - ['object', 'JsonSchema\Constraints\ObjectConstraint'], - ['type', 'JsonSchema\Constraints\TypeConstraint'], - ['undefined', 'JsonSchema\Constraints\UndefinedConstraint'], - ['string', 'JsonSchema\Constraints\StringConstraint'], - ['number', 'JsonSchema\Constraints\NumberConstraint'], - ['enum', 'JsonSchema\Constraints\EnumConstraint'], - ['const', 'JsonSchema\Constraints\ConstConstraint'], - ['format', 'JsonSchema\Constraints\FormatConstraint'], - ['schema', 'JsonSchema\Constraints\SchemaConstraint'], - ]; + yield 'Array' => ['array', Constraints\CollectionConstraint::class]; + yield 'Collection' => ['collection', Constraints\CollectionConstraint::class]; + yield 'Object' => ['object', Constraints\ObjectConstraint::class]; + yield 'Type' => ['type', Constraints\TypeConstraint::class]; + yield 'Undefined' => ['undefined', Constraints\UndefinedConstraint::class]; + yield 'String' => ['string', Constraints\StringConstraint::class]; + yield 'Number' => ['number', Constraints\NumberConstraint::class]; + yield 'Enum' => ['enum', Constraints\EnumConstraint::class]; + yield 'Const' => ['const', Constraints\ConstConstraint::class]; + yield 'Format' => ['format', Constraints\FormatConstraint::class]; + yield 'Schema' => ['schema', Constraints\SchemaConstraint::class]; } /** * @dataProvider invalidConstraintNameProvider - * - * @param string $constraintName */ - public function testExceptionWhenCreateInstanceForInvalidConstraintName($constraintName): void + public function testExceptionWhenCreateInstanceForInvalidConstraintName(string $constraintName): void { - $this->expectException('JsonSchema\Exception\InvalidArgumentException'); - $this->factory->createInstanceFor($constraintName); + $factory = new Factory(); + + $this->expectException(InvalidArgumentException::class); + + $factory->createInstanceFor($constraintName); } - public function invalidConstraintNameProvider(): array + public static function invalidConstraintNameProvider(): \Generator { - return [ - ['invalidConstraintName'], - ]; + yield 'InvalidConstraint' => ['invalidConstraintName']; } public function testSetConstraintClassExistsCondition(): void { + $factory = new Factory(); + $this->expectException(\JsonSchema\Exception\InvalidArgumentException::class); - $this->factory->setConstraintClass('string', 'SomeConstraint'); + $factory->setConstraintClass('string', 'SomeConstraint'); } public function testSetConstraintClassImplementsCondition(): void { + $factory = new Factory(); + $this->expectException(\JsonSchema\Exception\InvalidArgumentException::class); - $this->factory->setConstraintClass('string', 'JsonSchema\Tests\Constraints\MyBadConstraint'); + $factory->setConstraintClass('string', MyBadConstraint::class); } public function testSetConstraintClassInstance(): void { - $this->factory->setConstraintClass('string', 'JsonSchema\Tests\Constraints\MyStringConstraint'); - $constraint = $this->factory->createInstanceFor('string'); - $this->assertInstanceOf('JsonSchema\Tests\Constraints\MyStringConstraint', $constraint); - $this->assertInstanceOf('JsonSchema\Constraints\ConstraintInterface', $constraint); + $factory = new Factory(); + $factory->setConstraintClass('string', MyStringConstraint::class); + + $constraint = $factory->createInstanceFor('string'); + + $this->assertInstanceOf(MyStringConstraint::class, $constraint); + $this->assertInstanceOf(ConstraintInterface::class, $constraint); } - public function testCheckMode(): void + public function testCheckModeDefaultConfig(): void { $f = new Factory(); - // test default value $this->assertEquals(Constraint::CHECK_MODE_NORMAL, $f->getConfig()); + } + + public function testCheckModeWhenOverridingConfig(): void + { + $f = new Factory(); - // test overriding config $f->setConfig(Constraint::CHECK_MODE_COERCE_TYPES); + $this->assertEquals(Constraint::CHECK_MODE_COERCE_TYPES, $f->getConfig()); + } + + public function testCheckModeWhenAddingConfig(): void + { + $f = new Factory(); - // test adding config + $f->setConfig(Constraint::CHECK_MODE_COERCE_TYPES); $f->addConfig(Constraint::CHECK_MODE_NORMAL); + $this->assertEquals(Constraint::CHECK_MODE_NORMAL | Constraint::CHECK_MODE_COERCE_TYPES, $f->getConfig()); + } + + public function testCheckModeWhenGettingFilteredConfig(): void + { + $f = new Factory(); - // test getting filtered config $this->assertEquals(Constraint::CHECK_MODE_NORMAL, $f->getConfig(Constraint::CHECK_MODE_NORMAL)); + } + + public function testCheckModeWhenRemovingConfig(): void + { + $f = new Factory(); - // test removing config $f->removeConfig(Constraint::CHECK_MODE_COERCE_TYPES); + $this->assertEquals(Constraint::CHECK_MODE_NORMAL, $f->getConfig()); + } + + public function testCheckModeWhenResettingToDefault(): void + { + $f = new Factory(); - // test resetting to defaults $f->setConfig(Constraint::CHECK_MODE_COERCE_TYPES | Constraint::CHECK_MODE_TYPE_CAST); $f->setConfig(); $this->assertEquals(Constraint::CHECK_MODE_NORMAL, $f->getConfig()); diff --git a/tests/Constraints/TypeTest.php b/tests/Constraints/TypeTest.php index 1590d473..1b6edc40 100644 --- a/tests/Constraints/TypeTest.php +++ b/tests/Constraints/TypeTest.php @@ -66,11 +66,8 @@ public function testLooseTypeChecking(): void /** * Helper to assert an error message - * - * @param string $expected - * @param TypeConstraint $actual */ - private function assertTypeConstraintError($expected, TypeConstraint $actual): void + private function assertTypeConstraintError(string $expected, TypeConstraint $actual): void { $actualErrors = $actual->getErrors(); diff --git a/tests/Constraints/VeryBaseTestCase.php b/tests/Constraints/VeryBaseTestCase.php index a19749a4..ea018bda 100644 --- a/tests/Constraints/VeryBaseTestCase.php +++ b/tests/Constraints/VeryBaseTestCase.php @@ -1,5 +1,7 @@ assertEquals($expectedFileName, $jsonPointer->getFilename()); @@ -42,51 +39,49 @@ public function testJsonPointer( $this->assertEquals($expectedToString, (string) $jsonPointer); } - public function getTestData(): array + public static function jsonPointerDataProvider(): \Generator { - return [ - 'testDataSet_01' => [ - 'testValue' => '#/definitions/date', - 'expectedFileName' => '', - 'expectedPropertyPaths' => ['definitions', 'date'], - 'expectedPropertyPathAsString' => '#/definitions/date', - 'expectedToString' => '#/definitions/date' - ], - 'testDataSet_02' => [ - 'testValue' => 'http://www.example.com/definitions.json#/definitions/date', - 'expectedFileName' => 'http://www.example.com/definitions.json', - 'expectedPropertyPaths' => ['definitions', 'date'], - 'expectedPropertyPathAsString' => '#/definitions/date', - 'expectedToString' => 'http://www.example.com/definitions.json#/definitions/date' - ], - 'testDataSet_03' => [ - 'testValue' => '/tmp/schema.json#definitions/common/date/', - 'expectedFileName' => '/tmp/schema.json', - 'expectedPropertyPaths' => ['definitions', 'common', 'date'], - 'expectedPropertyPathAsString' => '#/definitions/common/date', - 'expectedToString' => '/tmp/schema.json#/definitions/common/date' - ], - 'testDataSet_04' => [ - 'testValue' => './definitions.json#', - 'expectedFileName' => './definitions.json', - 'expectedPropertyPaths' => [], - 'expectedPropertyPathAsString' => '#', - 'expectedToString' => './definitions.json#' - ], - 'testDataSet_05' => [ - 'testValue' => '/schema.json#~0definitions~1general/%custom%25', - 'expectedFileName' => '/schema.json', - 'expectedPropertyPaths' => ['~definitions/general', '%custom%'], - 'expectedPropertyPathAsString' => '#/~0definitions~1general/%25custom%25', - 'expectedToString' => '/schema.json#/~0definitions~1general/%25custom%25' - ], - 'testDataSet_06' => [ - 'testValue' => '#/items/0', - 'expectedFileName' => '', - 'expectedPropertyPaths' => ['items', '0'], - 'expectedPropertyPathAsString' => '#/items/0', - 'expectedToString' => '#/items/0' - ] + yield 'testDataSet_01' => [ + 'testValue' => '#/definitions/date', + 'expectedFileName' => '', + 'expectedPropertyPaths' => ['definitions', 'date'], + 'expectedPropertyPathAsString' => '#/definitions/date', + 'expectedToString' => '#/definitions/date' + ]; + yield 'testDataSet_02' => [ + 'testValue' => 'https://www.example.com/definitions.json#/definitions/date', + 'expectedFileName' => 'https://www.example.com/definitions.json', + 'expectedPropertyPaths' => ['definitions', 'date'], + 'expectedPropertyPathAsString' => '#/definitions/date', + 'expectedToString' => 'https://www.example.com/definitions.json#/definitions/date' + ]; + yield 'testDataSet_03' => [ + 'testValue' => '/tmp/schema.json#definitions/common/date/', + 'expectedFileName' => '/tmp/schema.json', + 'expectedPropertyPaths' => ['definitions', 'common', 'date'], + 'expectedPropertyPathAsString' => '#/definitions/common/date', + 'expectedToString' => '/tmp/schema.json#/definitions/common/date' + ]; + yield 'testDataSet_04' => [ + 'testValue' => './definitions.json#', + 'expectedFileName' => './definitions.json', + 'expectedPropertyPaths' => [], + 'expectedPropertyPathAsString' => '#', + 'expectedToString' => './definitions.json#' + ]; + yield 'testDataSet_05' => [ + 'testValue' => '/schema.json#~0definitions~1general/%custom%25', + 'expectedFileName' => '/schema.json', + 'expectedPropertyPaths' => ['~definitions/general', '%custom%'], + 'expectedPropertyPathAsString' => '#/~0definitions~1general/%25custom%25', + 'expectedToString' => '/schema.json#/~0definitions~1general/%25custom%25' + ]; + yield 'testDataSet_06' => [ + 'testValue' => '#/items/0', + 'expectedFileName' => '', + 'expectedPropertyPaths' => ['items', '0'], + 'expectedPropertyPathAsString' => '#/items/0', + 'expectedToString' => '#/items/0' ]; } @@ -110,7 +105,7 @@ public function testJsonPointerWithPropertyPaths(): void public function testCreateWithInvalidValue(): void { - $this->expectException('\JsonSchema\Exception\InvalidArgumentException'); + $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Ref value must be a string'); new JsonPointer(null); diff --git a/tests/Rfc3339Test.php b/tests/Rfc3339Test.php index 73c1d597..2a0d128a 100644 --- a/tests/Rfc3339Test.php +++ b/tests/Rfc3339Test.php @@ -1,5 +1,7 @@ assertNull(Rfc3339::createFromString($string), sprintf('String "%s" should not be converted to DateTime', $string)); } - public function provideValidFormats(): array + public static function provideValidFormats(): \Generator { - return [ - [ - '2000-05-01T12:12:12Z', - \DateTime::createFromFormat('Y-m-d\TH:i:s', '2000-05-01T12:12:12', new \DateTimeZone('UTC')) - ], - [ - '2000-05-01T12:12:12+0100', - \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00') - ], - [ - '2000-05-01T12:12:12+01:00', - \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00') - ], - [ - '2000-05-01T12:12:12.123456Z', - \DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123456', new \DateTimeZone('UTC')) - ], - [ - '2000-05-01T12:12:12.123Z', - \DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123000', new \DateTimeZone('UTC')) - ], - [ - '2000-05-01 12:12:12.123Z', - \DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123000', new \DateTimeZone('UTC')) - ], - [ - '2000-05-01 12:12:12.123456Z', - \DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123456', new \DateTimeZone('UTC')) - ] + yield 'Zulu time' => [ + '2000-05-01T12:12:12Z', + \DateTime::createFromFormat('Y-m-d\TH:i:s', '2000-05-01T12:12:12', new \DateTimeZone('UTC')) + ]; + yield 'With time offset - without colon' => [ + '2000-05-01T12:12:12+0100', + \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00') + ]; + yield 'With time offset - with colon' => [ + '2000-05-01T12:12:12+01:00', + \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00') + ]; + yield 'Zulu time - with microseconds' => [ + '2000-05-01T12:12:12.123456Z', + \DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123456', new \DateTimeZone('UTC')) + ]; + yield 'Zulu time - with milliseconds' => [ + '2000-05-01T12:12:12.123Z', + \DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123000', new \DateTimeZone('UTC')) + ]; + yield 'Zulu time - with milliseconds, without T separator' => [ + '2000-05-01 12:12:12.123Z', + \DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123000', new \DateTimeZone('UTC')) + ]; + yield 'Zulu time - with microseconds, without T separator' => [ + '2000-05-01 12:12:12.123456Z', + \DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123456', new \DateTimeZone('UTC')) ]; } - public function provideInvalidFormats(): array + public static function provideInvalidFormats(): \Generator { - return [ - ['1999-1-11T00:00:00Z'], - ['1999-01-11T00:00:00+100'], - ['1999-01-11T00:00:00+1:00'], - ['1999-01-01 00:00:00Z'], - ['1999-1-11 00:00:00Z'] - ]; + yield 'Missing leading zero in month - with T separator' => ['1999-1-11T00:00:00Z']; + yield 'Missing leading zero in timezone offset - without colon' => ['1999-01-11T00:00:00+100']; + yield 'Missing leading zero in timezone offset - with colon' => ['1999-01-11T00:00:00+1:00']; + yield 'Double space between date and time' => ['1999-01-01 00:00:00Z']; + yield 'Missing leading zero in month - without T separator' => ['1999-1-11 00:00:00Z']; } }