Skip to content

Commit

Permalink
Test error message
Browse files Browse the repository at this point in the history
  • Loading branch information
arogachev committed Apr 8, 2024
1 parent b7a05f1 commit a695678
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 34 deletions.
54 changes: 44 additions & 10 deletions tests/Rule/Type/BooleanTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace Rule\Type;

use Yiisoft\Validator\AttributeTranslator\ArrayAttributeTranslator;
use Yiisoft\Validator\AttributeTranslatorInterface;
use Yiisoft\Validator\AttributeTranslatorProviderInterface;
use Yiisoft\Validator\Rule\Type\BooleanType;
use Yiisoft\Validator\Rule\Type\BooleanTypeHandler;
use Yiisoft\Validator\RulesProviderInterface;
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
Expand Down Expand Up @@ -40,7 +44,7 @@ public function dataOptions(): array
],
],
'custom' => [
new BooleanType(message: 'Custom message.', skipOnError: true, skipOnEmpty: true, ),
new BooleanType(message: 'Custom message.', skipOnError: true, skipOnEmpty: true),
[
'message' => [
'template' => 'Custom message.',
Expand All @@ -56,8 +60,8 @@ public function dataOptions(): array
public function dataValidationPassed(): array
{
return [
[false, [new BooleanType()]],
[true, [new BooleanType()]],
[false, new BooleanType()],
[true, new BooleanType()],
];
}

Expand All @@ -67,13 +71,43 @@ public function dataValidationFailed(): array

return [
[0.0, new BooleanType(), ['' => [$message]]],
[0, [new BooleanType()], ['' => [$message]]],
[1, [new BooleanType()], ['' => [$message]]],
['0', [new BooleanType()], ['' => [$message]]],
['1', [new BooleanType()], ['' => [$message]]],
['false', [new BooleanType()], ['' => [$message]]],
['true', [new BooleanType()], ['' => [$message]]],
[[], [new BooleanType()], ['' => [$message]]],
[0, new BooleanType(), ['' => [$message]]],
[1, new BooleanType(), ['' => [$message]]],
['0', new BooleanType(), ['' => [$message]]],
['1', new BooleanType(), ['' => [$message]]],
['false', new BooleanType(), ['' => [$message]]],
['true', new BooleanType(), ['' => [$message]]],
[[], new BooleanType(), ['' => [$message]]],
'message, custom' => [['active' => []], ['active' => new BooleanType('{attribute}')], ['active' => ['active']]],
'message, translated attribute' => [
new class () implements RulesProviderInterface, AttributeTranslatorProviderInterface {
public function __construct(
public ?bool $active = null,
) {
}

public function getAttributeLabels(): array
{
return [
'active' => 'Активен',
];
}

public function getAttributeTranslator(): ?AttributeTranslatorInterface
{
return new ArrayAttributeTranslator($this->getAttributeLabels());
}

public function getRules(): array
{
return [
'active' => new BooleanType(message: '"{attribute}" - не булево значение.'),
];
}
},
null,
['active' => ['"Активен" - не булево значение.']],
],
];
}

Expand Down
54 changes: 46 additions & 8 deletions tests/Rule/Type/FloatTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace Rule\Type;

use Yiisoft\Validator\AttributeTranslator\ArrayAttributeTranslator;
use Yiisoft\Validator\AttributeTranslatorInterface;
use Yiisoft\Validator\AttributeTranslatorProviderInterface;
use Yiisoft\Validator\Rule\Type\FloatType;
use Yiisoft\Validator\Rule\Type\FloatTypeHandler;
use Yiisoft\Validator\RulesProviderInterface;
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
Expand Down Expand Up @@ -40,7 +44,7 @@ public function dataOptions(): array
],
],
'custom' => [
new FloatType(message: 'Custom message.', skipOnError: true, skipOnEmpty: true, ),
new FloatType(message: 'Custom message.', skipOnError: true, skipOnEmpty: true),
[
'message' => [
'template' => 'Custom message.',
Expand All @@ -56,9 +60,9 @@ public function dataOptions(): array
public function dataValidationPassed(): array
{
return [
[-1.5, [new FloatType()]],
[0.0, [new FloatType()]],
[1.5, [new FloatType()]],
[-1.5, new FloatType()],
[0.0, new FloatType()],
[1.5, new FloatType()],
];
}

Expand All @@ -67,10 +71,44 @@ public function dataValidationFailed(): array
$message = 'Value must be a float.';

return [
[false, [new FloatType()], ['' => [$message]]],
[0, [new FloatType()], ['' => [$message]]],
['1.5', [new FloatType()], ['' => [$message]]],
[[], [new FloatType()], ['' => [$message]]],
[false, new FloatType(), ['' => [$message]]],
[0, new FloatType(), ['' => [$message]]],
['1.5', new FloatType(), ['' => [$message]]],
[[], new FloatType(), ['' => [$message]]],
'message, custom' => [
['sum' => []],
['sum' => new FloatType('{attribute}')],
['sum' => ['sum']],
],
'message, translated attribute' => [
new class () implements RulesProviderInterface, AttributeTranslatorProviderInterface {
public function __construct(
public ?bool $active = null,
) {
}

public function getAttributeLabels(): array
{
return [
'sum' => 'Сумма',
];
}

public function getAttributeTranslator(): ?AttributeTranslatorInterface
{
return new ArrayAttributeTranslator($this->getAttributeLabels());
}

public function getRules(): array
{
return [
'sum' => new FloatType(message: '"{attribute}" - невещественное число.'),
];
}
},
null,
['sum' => ['"Сумма" - невещественное число.']],
],
];
}

Expand Down
50 changes: 42 additions & 8 deletions tests/Rule/Type/IntegerTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace Yiisoft\Validator\Tests\Rule\Type;

use Yiisoft\Validator\AttributeTranslator\ArrayAttributeTranslator;
use Yiisoft\Validator\AttributeTranslatorInterface;
use Yiisoft\Validator\AttributeTranslatorProviderInterface;
use Yiisoft\Validator\Rule\Type\IntegerType;
use Yiisoft\Validator\Rule\Type\IntegerTypeHandler;
use Yiisoft\Validator\RulesProviderInterface;
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
Expand Down Expand Up @@ -40,7 +44,7 @@ public function dataOptions(): array
],
],
'custom' => [
new IntegerType(message: 'Custom message.', skipOnError: true, skipOnEmpty: true, ),
new IntegerType(message: 'Custom message.', skipOnError: true, skipOnEmpty: true),
[
'message' => [
'template' => 'Custom message.',
Expand All @@ -56,9 +60,9 @@ public function dataOptions(): array
public function dataValidationPassed(): array
{
return [
[-1, [new IntegerType()]],
[0, [new IntegerType()]],
[1, [new IntegerType()]],
[-1, new IntegerType()],
[0, new IntegerType()],
[1, new IntegerType()],
];
}

Expand All @@ -67,10 +71,40 @@ public function dataValidationFailed(): array
$message = 'Value must be an integer.';

return [
[false, [new IntegerType()], ['' => [$message]]],
[1.5, [new IntegerType()], ['' => [$message]]],
['1.5', [new IntegerType()], ['' => [$message]]],
[[], [new IntegerType()], ['' => [$message]]],
[false, new IntegerType(), ['' => [$message]]],
[1.5, new IntegerType(), ['' => [$message]]],
['1.5', new IntegerType(), ['' => [$message]]],
[[], new IntegerType(), ['' => [$message]]],
'message, custom' => [['sum' => []], ['sum' => new IntegerType('{attribute}')], ['sum' => ['sum']]],
'message, translated attribute' => [
new class () implements RulesProviderInterface, AttributeTranslatorProviderInterface {
public function __construct(
public ?bool $active = null,
) {
}

public function getAttributeLabels(): array
{
return [
'sum' => 'Сумма',
];
}

public function getAttributeTranslator(): ?AttributeTranslatorInterface
{
return new ArrayAttributeTranslator($this->getAttributeLabels());
}

public function getRules(): array
{
return [
'sum' => new IntegerType(message: '"{attribute}" - нецелое число.'),
];
}
},
null,
['sum' => ['"Сумма" - нецелое число.']],
],
];
}

Expand Down
50 changes: 42 additions & 8 deletions tests/Rule/Type/StringTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
namespace Rule\Type;

use Stringable;
use Yiisoft\Validator\AttributeTranslator\ArrayAttributeTranslator;
use Yiisoft\Validator\AttributeTranslatorInterface;
use Yiisoft\Validator\AttributeTranslatorProviderInterface;
use Yiisoft\Validator\Rule\Type\StringType;
use Yiisoft\Validator\Rule\Type\StringTypeHandler;
use Yiisoft\Validator\RulesProviderInterface;
use Yiisoft\Validator\Tests\Rule\Base\DifferentRuleInHandlerTestTrait;
use Yiisoft\Validator\Tests\Rule\Base\RuleTestCase;
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
Expand Down Expand Up @@ -41,7 +45,7 @@ public function dataOptions(): array
],
],
'custom' => [
new StringType(message: 'Custom message.', skipOnError: true, skipOnEmpty: true, ),
new StringType(message: 'Custom message.', skipOnError: true, skipOnEmpty: true),
[
'message' => [
'template' => 'Custom message.',
Expand All @@ -57,8 +61,8 @@ public function dataOptions(): array
public function dataValidationPassed(): array
{
return [
['', [new StringType()]],
['test', [new StringType()]],
['', new StringType()],
['test', new StringType()],
];
}

Expand All @@ -67,20 +71,50 @@ public function dataValidationFailed(): array
$message = 'Value must be a string.';

return [
[false, [new StringType()], ['' => [$message]]],
[1.5, [new StringType()], ['' => [$message]]],
[1, [new StringType()], ['' => [$message]]],
[false, new StringType(), ['' => [$message]]],
[1.5, new StringType(), ['' => [$message]]],
[1, new StringType(), ['' => [$message]]],
[
new class () implements Stringable {
public function __toString(): string
{
return 'test';
}
},
[new StringType()],
new StringType(),
['' => [$message]],
],
[[], [new StringType()], ['' => [$message]]],
[[], new StringType(), ['' => [$message]]],
'message, custom' => [['name' => []], ['name' => new StringType('{attribute}')], ['name' => ['name']]],
'message, translated attribute' => [
new class () implements RulesProviderInterface, AttributeTranslatorProviderInterface {
public function __construct(
public ?bool $active = null,
) {
}

public function getAttributeLabels(): array
{
return [
'name' => 'Название',
];
}

public function getAttributeTranslator(): ?AttributeTranslatorInterface
{
return new ArrayAttributeTranslator($this->getAttributeLabels());
}

public function getRules(): array
{
return [
'name' => new StringType(message: '"{attribute}" - не строка.'),
];
}
},
null,
['name' => ['"Название" - не строка.']],
],
];
}

Expand Down

0 comments on commit a695678

Please sign in to comment.