respect-validation/tests/unit/Rules/AttributeTest.php
Henrique Moody ee8dd98f54
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>
2023-04-03 16:28:38 +02:00

80 lines
2.4 KiB
PHP

<?php
/*
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-License-Identifier: MIT
*/
declare(strict_types=1);
namespace Respect\Validation\Rules;
use Respect\Validation\Test\RuleTestCase;
use Respect\Validation\Test\Stubs\WithProperties;
/**
* @group rule
*
* @covers \Respect\Validation\Rules\AbstractRelated
* @covers \Respect\Validation\Rules\Attribute
*
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
* @author Emmerson Siqueira <emmersonsiqueira@gmail.com>
* @author Gabriel Caruso <carusogabriel34@gmail.com>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class AttributeTest extends RuleTestCase
{
public const PROPERTY_VALUE = 'foo';
/**
* @var string
*/
private $bar = self::PROPERTY_VALUE;
/**
* {@inheritDoc}
*/
public function providerForValidInput(): array
{
return [
'attribute is present without extra validator' => [new Attribute('public'), new WithProperties()],
'private attribute is present without extra validator' => [
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(),
],
'non mandatory attribute is not present with extra validator' => [
new Attribute('nonexistent', new AlwaysValid(), false),
new WithProperties(),
],
];
}
/**
* {@inheritDoc}
*/
public function providerForInvalidInput(): array
{
return [
'attribute is absent without extra validator' => [new Attribute('barr'), new WithProperties()],
'private attribute is not valid based on extra validator' => [
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('public', new AlwaysInvalid()),
new WithProperties(),
],
];
}
}