From ee8dd98f54ecf68dacf56e1dd7251bb1f83c6df9 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Sun, 2 Apr 2023 15:22:37 +0200 Subject: [PATCH] Create class to help testing the Attribute rule With that, the tests will be more straightforward, and we won't need to use the test class in the data providers. That will help us later because, on PHPUnit 10, all data providers need to be static. Signed-off-by: Henrique Moody --- tests/library/Stubs/WithProperties.php | 19 +++++++++++ tests/unit/Rules/AttributeTest.php | 47 ++++++++++++-------------- 2 files changed, 40 insertions(+), 26 deletions(-) create mode 100644 tests/library/Stubs/WithProperties.php diff --git a/tests/library/Stubs/WithProperties.php b/tests/library/Stubs/WithProperties.php new file mode 100644 index 00000000..25a9e855 --- /dev/null +++ b/tests/library/Stubs/WithProperties.php @@ -0,0 +1,19 @@ + + * SPDX-License-Identifier: MIT + */ + +declare(strict_types=1); + +namespace Respect\Validation\Test\Stubs; + +final class WithProperties +{ + public string $public = 'public'; + + protected string $protected = 'protected'; + + private string $private = 'private'; // @phpstan-ignore-line +} diff --git a/tests/unit/Rules/AttributeTest.php b/tests/unit/Rules/AttributeTest.php index 8d4c4dee..e6e3132e 100644 --- a/tests/unit/Rules/AttributeTest.php +++ b/tests/unit/Rules/AttributeTest.php @@ -10,8 +10,7 @@ declare(strict_types=1); namespace Respect\Validation\Rules; use Respect\Validation\Test\RuleTestCase; -use Respect\Validation\Validatable; -use stdClass; +use Respect\Validation\Test\Stubs\WithProperties; /** * @group rule @@ -38,24 +37,23 @@ final class AttributeTest extends RuleTestCase */ public function providerForValidInput(): array { - $obj = new stdClass(); - $obj->bar = 'foo'; - - $extraValidator = $this->createMock(Validatable::class); - $extraValidator->method('validate') - ->willReturn(true); - return [ - 'attribute is present without extra validator' => [new Attribute('bar'), $obj], + 'attribute is present without extra validator' => [new Attribute('public'), new WithProperties()], 'private attribute is present without extra validator' => [ - new Attribute('bar'), - $this, + new Attribute('private'), + new WithProperties(), + ], + 'attribute is present with extra validator' => [ + new Attribute('public', new AlwaysValid()), + new WithProperties(), + ], + 'non mandatory attribute is not present' => [ + new Attribute('nonexistent', null, false), + new WithProperties(), ], - 'attribute is present with extra validator' => [new Attribute('bar', $extraValidator), $obj], - 'non mandatory attribute is not present' => [new Attribute('foo', null, false), $obj], 'non mandatory attribute is not present with extra validator' => [ - new Attribute('foo', $extraValidator, false), - $obj, + new Attribute('nonexistent', new AlwaysValid(), false), + new WithProperties(), ], ]; } @@ -65,20 +63,17 @@ final class AttributeTest extends RuleTestCase */ public function providerForInvalidInput(): array { - $obj = new stdClass(); - $obj->bar = 'foo'; - - $extraValidatorMock = $this->createMock(Validatable::class); - $extraValidatorMock->method('validate')->willReturn(false); - return [ - 'attribute is absent without extra validator' => [new Attribute('barr'), $obj], + 'attribute is absent without extra validator' => [new Attribute('barr'), new WithProperties()], 'private attribute is not valid based on extra validator' => [ - new Attribute('bar', $extraValidatorMock), - $this, + new Attribute('private', new AlwaysInvalid()), + new WithProperties(), ], 'value provided is an empty string' => [new Attribute('barr'), ''], - 'validator related to attribute does not validate' => [new Attribute('bar', $extraValidatorMock), $obj], + 'validator related to attribute does not validate' => [ + new Attribute('public', new AlwaysInvalid()), + new WithProperties(), + ], ]; } }