respect-validation/tests/unit/Message/Stringifier/SubjectStringifierTest.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

147 lines
4.6 KiB
PHP

<?php
/*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\Validation\Message\Stringifier;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Message\Placeholder\Subject;
use Respect\Validation\Name;
use Respect\Validation\Path;
use Respect\Validation\Test\Message\TestingStringifier;
use Respect\Validation\Test\TestCase;
use stdClass;
use function sprintf;
#[CoversClass(SubjectStringifier::class)]
final class SubjectStringifierTest extends TestCase
{
#[Test]
#[DataProvider('providerForNonSubjectValues')]
public function itShouldNotStringifyWhenValueIsNotAnInstanceOfSubject(mixed $value): void
{
$stringifier = new SubjectStringifier(new TestingStringifier());
self::assertNull($stringifier->stringify($value, 0));
}
#[Test]
public function itShouldStringifyInputWhenPathAndNameAreNull(): void
{
$input = ['test' => 'value'];
$subject = new Subject($input);
$testingStringifier = new TestingStringifier();
$stringifier = new SubjectStringifier($testingStringifier);
$expected = $testingStringifier->stringify($input, 0);
$actual = $stringifier->stringify($subject, 0);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldStringifyPathWhenNameIsNull(): void
{
$path = new Path('field1');
$subject = new Subject('input', $path);
$testingStringifier = new TestingStringifier();
$stringifier = new SubjectStringifier($testingStringifier);
$expected = $testingStringifier->stringify($path, 0);
$actual = $stringifier->stringify($subject, 0);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldStringifyNameWhenPathIsNull(): void
{
$name = new Name('field_name');
$subject = new Subject('input', null, $name);
$testingStringifier = new TestingStringifier();
$stringifier = new SubjectStringifier($testingStringifier);
$expected = $testingStringifier->stringify($name, 0);
$actual = $stringifier->stringify($subject, 0);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldStringifyNameWhenNameHasPrecedence(): void
{
$path = new Path('field1');
$name = new Name('field_name');
$subject = new Subject('input', $path, $name, true);
$testingStringifier = new TestingStringifier();
$stringifier = new SubjectStringifier($testingStringifier);
$expected = $testingStringifier->stringify($name, 0);
$actual = $stringifier->stringify($subject, 0);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldStringifyWithPathAndNameWhenNameHasNoPrecedence(): void
{
$path = new Path('field1');
$name = new Name('field_name');
$subject = new Subject('input', $path, $name, false);
$testingStringifier = new TestingStringifier();
$stringifier = new SubjectStringifier($testingStringifier);
$pathString = $testingStringifier->stringify($path, 0);
$nameString = $testingStringifier->stringify($name, 0);
$expected = sprintf('%s (<- %s)', $pathString, $nameString);
$actual = $stringifier->stringify($subject, 0);
self::assertSame($expected, $actual);
}
#[Test]
public function itShouldStringifyWithNestedPathWhenNameHasNoPrecedence(): void
{
$path1 = new Path('field1');
$path2 = new Path('field2', $path1);
$name = new Name('field_name');
$subject = new Subject('input', $path2, $name, false);
$testingStringifier = new TestingStringifier();
$stringifier = new SubjectStringifier($testingStringifier);
$pathString = $testingStringifier->stringify($path2, 0);
$nameString = $testingStringifier->stringify($name, 0);
$expected = sprintf('%s (<- %s)', $pathString, $nameString);
$actual = $stringifier->stringify($subject, 0);
self::assertSame($expected, $actual);
}
/** @return array<string, array{mixed}> */
public static function providerForNonSubjectValues(): array
{
return [
'string' => ['test'],
'integer' => [123],
'boolean' => [true],
'array' => [['test']],
'object' => [new stdClass()],
'null' => [null],
];
}
}