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 <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2023-04-02 15:22:37 +02:00
parent 07c4095f11
commit ee8dd98f54
No known key found for this signature in database
GPG key ID: 221E9281655813A6
2 changed files with 40 additions and 26 deletions

View file

@ -0,0 +1,19 @@
<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* 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
}

View file

@ -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(),
],
];
}
}