Skip to content

Commit

Permalink
refactor: Cleanup test from @param annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyvdSluijs committed Nov 24, 2024
1 parent 3772314 commit e360671
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 175 deletions.
134 changes: 72 additions & 62 deletions tests/Constraints/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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());
Expand Down
5 changes: 1 addition & 4 deletions tests/Constraints/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
7 changes: 3 additions & 4 deletions tests/Constraints/VeryBaseTestCase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/*
* This file is part of the JsonSchema package.
*
Expand All @@ -23,10 +25,7 @@ abstract class VeryBaseTestCase extends TestCase
/** @var object */
private $jsonSchemaDraft04;

/**
* @param object $schema
*/
protected function getUriRetrieverMock($schema): object
protected function getUriRetrieverMock(?object $schema): object
{
$relativeTestsRoot = realpath(__DIR__ . '/../../vendor/json-schema/json-schema-test-suite/remotes');

Expand Down
8 changes: 3 additions & 5 deletions tests/Drafts/BaseDraftTestCase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace JsonSchema\Tests\Drafts;

use JsonSchema\Tests\Constraints\BaseTestCase;
Expand Down Expand Up @@ -71,12 +73,8 @@ abstract protected function getSkippedTests(): array;

/**
* Generates a readable path to Json Schema Test Suite data set under test
*
* @param string $filename
* @param string $suiteDesc
* @param string $testCaseDesc
*/
private function createDataSetPath($filename, $suiteDesc, $testCaseDesc): string
private function createDataSetPath(string $filename, string $suiteDesc, string $testCaseDesc): string
{
$separator = ' / ';

Expand Down
Loading

0 comments on commit e360671

Please sign in to comment.