respect-validation/tests/unit/ValidatorTest.php
Alexandre Gomes Gaigalas d9cdc118b2 Introduce REUSE compliance
This commit introduces REUSE compliance by annotating all files
with SPDX information and placing the reused licences in the
LICENSES folder.

We additionally removed the docheader tool which is made obsolete
by this change.

The main LICENSE and copyright text of the project is now not under
my personal name anymore, and it belongs to "The Respect Project
Contributors" instead.

This change restores author names to several files, giving the
appropriate attribution for contributions.
2026-01-21 06:28:11 +00:00

168 lines
4.8 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
* SPDX-FileContributor: Andy Wendt <andy@awendt.com>
* SPDX-FileContributor: Gabriel Caruso <carusogabriel34@gmail.com>
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
* SPDX-FileContributor: Nick Lombard <github@jigsoft.co.za>
* SPDX-FileContributor: Kir Kolyshkin <kolyshkin@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Validation;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Test\TestCase;
use Respect\Validation\Test\Validators\Stub;
use function uniqid;
#[CoversClass(ValidatorBuilder::class)]
final class ValidatorTest extends TestCase
{
#[Test]
public function invalidRuleClassShouldThrowComponentException(): void
{
$this->expectException(ComponentException::class);
// @phpstan-ignore-next-line
ValidatorBuilder::iDoNotExistSoIShouldThrowException();
}
#[Test]
public function shouldReturnValidatorInstanceWhenTheNotRuleIsCalledWithArguments(): void
{
$validator = ValidatorBuilder::init();
// @phpstan-ignore-next-line
self::assertNotSame($validator, $validator->not($validator->falsy()));
}
#[Test]
public function itShouldProxyResultWithTheIsValidMethod(): void
{
$validator = ValidatorBuilder::init(Stub::fail(1));
self::assertFalse($validator->isValid('whatever'));
}
#[Test]
#[DoesNotPerformAssertions]
public function itShouldAssertAndNotThrowAnExceptionWhenValidatorPasses(): void
{
$validator = ValidatorBuilder::init(Stub::pass(1));
$validator->assert('whatever');
}
#[Test]
public function itShouldAssertAndThrowAnExceptionWhenValidatorFails(): void
{
$this->expectException(ValidationException::class);
$validator = ValidatorBuilder::init(Stub::fail(1));
$validator->assert('whatever');
}
#[Test]
public function itShouldAssertUsingTheGivingStringTemplate(): void
{
$template = 'This is my new template';
$this->expectExceptionMessage($template);
$validator = ValidatorBuilder::init(Stub::fail(1));
$validator->assert('whatever', $template);
}
#[Test]
public function itShouldValidateAndReturnValidResultQueryWhenValidationPasses(): void
{
$validator = ValidatorBuilder::init(Stub::pass(1));
$resultQuery = $validator->validate('whatever');
self::assertTrue($resultQuery->isValid());
}
#[Test]
public function itShouldValidateAndReturnInvalidResultQueryWhenValidationFails(): void
{
$validator = ValidatorBuilder::init(Stub::fail(1));
$resultQuery = $validator->validate('whatever');
self::assertFalse($resultQuery->isValid());
}
#[Test]
public function itShouldValidateUsingStringTemplateWhenProvided(): void
{
$template = uniqid();
$validator = ValidatorBuilder::init(Stub::fail(1));
$resultQuery = $validator->validate('whatever', $template);
self::assertSame($template, $resultQuery->toMessage());
}
#[Test]
public function itShouldValidateUsingArrayTemplatesWhenProvided(): void
{
$template = uniqid();
$validator = ValidatorBuilder::init(Stub::fail(1));
$resultQuery = $validator->validate('whatever', ['stub' => $template]);
self::assertSame($template, $resultQuery->toMessage());
}
#[Test]
public function itShouldEvaluateAndThrowExceptionWhenNoValidatorsAreAdded(): void
{
$this->expectException(ComponentException::class);
$this->expectExceptionMessage('No validators have been added.');
$validator = ValidatorBuilder::init();
$validator->evaluate('whatever');
}
#[Test]
public function itShouldEvaluateAndReturnResultWhenOneRuleIsAdded(): void
{
$validator = ValidatorBuilder::init(Stub::pass(1));
$result = $validator->evaluate('whatever');
self::assertTrue($result->hasPassed);
}
#[Test]
public function itShouldEvaluateAndReturnResultWhenMultipleValidatorsAreAdded(): void
{
$validator = ValidatorBuilder::init(Stub::pass(1), Stub::fail(2));
$result = $validator->evaluate('whatever');
self::assertFalse($result->hasPassed);
}
#[Test]
public function itShouldEvaluateAndReturnResultWhenSingleFailingRuleIsAdded(): void
{
$validator = ValidatorBuilder::init(Stub::fail(1));
$result = $validator->evaluate('whatever');
self::assertFalse($result->hasPassed);
}
}