mirror of
https://github.com/Respect/Validation.git
synced 2026-03-17 23:59:51 +01:00
This change will bring many breaking changes. The good thing is that we can finally use more modern resources available in PHP. I can imagine that's not a popular change since it will bring many breaking changes to users, but we shouldn't be stuck in time because of that. Using some of those features will make it easier to contribute to the project. At least, I hope so. There are still some useless doc-blocks, and we're not using "readonly" properties when we could. I aim to send those changes soon. Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
196 lines
4.8 KiB
PHP
196 lines
4.8 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-License-Identifier: MIT
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Validation\Exceptions;
|
|
|
|
use Respect\Validation\Message\Formatter;
|
|
use Respect\Validation\Message\Stringifier\KeepOriginalStringName;
|
|
use Respect\Validation\Test\TestCase;
|
|
|
|
use function trim;
|
|
|
|
/**
|
|
* @group core
|
|
* @covers \Respect\Validation\Exceptions\ValidationException
|
|
*/
|
|
final class ValidationExceptionTest extends TestCase
|
|
{
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldImplementException(): void
|
|
{
|
|
$sut = new ValidationException('input', 'id', [], $this->createFormatter());
|
|
|
|
self::assertInstanceOf(Exception::class, $sut);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldRetrieveId(): void
|
|
{
|
|
$id = 'my id';
|
|
$sut = new ValidationException('input', $id, [], $this->createFormatter());
|
|
|
|
self::assertSame($id, $sut->getId());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldRetrieveParams(): void
|
|
{
|
|
$params = ['foo' => true, 'bar' => 23];
|
|
|
|
$sut = new ValidationException('input', 'id', $params, $this->createFormatter());
|
|
|
|
self::assertSame($params, $sut->getParams());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldRetrieveOneSingleParameter(): void
|
|
{
|
|
$name = 'any name';
|
|
$value = 'any value';
|
|
|
|
$sut = new ValidationException('input', 'id', [$name => $value], $this->createFormatter());
|
|
|
|
self::assertSame($value, $sut->getParam($name));
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldReturnNullWhenParameterCanNotBeFound(): void
|
|
{
|
|
$sut = new ValidationException('input', 'id', [], $this->createFormatter());
|
|
|
|
self::assertNull($sut->getParam('foo'));
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldHaveTemplateByDefault(): void
|
|
{
|
|
$sut = new ValidationException('input', 'id', [], $this->createFormatter());
|
|
|
|
self::assertSame('"input" must be valid', $sut->getMessage());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldUpdateMode(): void
|
|
{
|
|
$sut = new ValidationException('input', 'id', [], $this->createFormatter());
|
|
$sut->updateMode(ValidationException::MODE_NEGATIVE);
|
|
|
|
self::assertSame('"input" must not be valid', $sut->getMessage());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldUpdateTemplate(): void
|
|
{
|
|
$template = 'This is my new template';
|
|
|
|
$sut = new ValidationException('input', 'id', [], $this->createFormatter());
|
|
$sut->updateTemplate($template);
|
|
|
|
self::assertEquals($template, $sut->getMessage());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldTellWhenHasAsCustomUpdateTemplate(): void
|
|
{
|
|
$sut = new ValidationException('input', 'id', [], $this->createFormatter());
|
|
|
|
self::assertFalse($sut->hasCustomTemplate());
|
|
|
|
$sut->updateTemplate('This is my new template');
|
|
|
|
self::assertTrue($sut->hasCustomTemplate());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldUseFormatter(): void
|
|
{
|
|
$template = ' This is my new template ';
|
|
$expected = trim($template);
|
|
|
|
$sut = new ValidationException('input', 'id', [], new Formatter('trim', new KeepOriginalStringName()));
|
|
$sut->updateTemplate($template);
|
|
|
|
self::assertEquals($expected, $sut->getMessage());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldReplacePlaceholders(): void
|
|
{
|
|
$sut = new ValidationException('foo', 'id', ['bar' => 1, 'baz' => 2], $this->createFormatter());
|
|
$sut->updateTemplate('{{name}} {{bar}} {{baz}}');
|
|
|
|
self::assertEquals(
|
|
'"foo" 1 2',
|
|
$sut->getMessage()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldKeepPlaceholdersThatCanNotReplace(): void
|
|
{
|
|
$sut = new ValidationException('foo', 'id', ['foo' => 1], $this->createFormatter());
|
|
$sut->updateTemplate('{{name}} {{foo}} {{bar}}');
|
|
|
|
self::assertEquals(
|
|
'"foo" 1 {{bar}}',
|
|
$sut->getMessage()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldUpdateParams(): void
|
|
{
|
|
$sut = new ValidationException('input', 'id', ['foo' => 1], $this->createFormatter());
|
|
$sut->updateTemplate('{{foo}}');
|
|
$sut->updateParams(['foo' => 2]);
|
|
|
|
self::assertEquals('2', $sut->getMessage());
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function itShouldConvertToString(): void
|
|
{
|
|
$sut = new ValidationException('input', 'id', [], $this->createFormatter());
|
|
|
|
self::assertSame('"input" must be valid', (string) $sut);
|
|
}
|
|
|
|
private function createFormatter(): Formatter
|
|
{
|
|
return new Formatter('strval', new KeepOriginalStringName());
|
|
}
|
|
}
|