Skip to content

Commit

Permalink
deprecate(spa): Validation rules will not implement deprecated `Illum…
Browse files Browse the repository at this point in the history
…inate\Contracts\Validation\Rule` in the next major version.
  • Loading branch information
LastDragon-ru committed Mar 28, 2024
1 parent 615dde6 commit d485119
Show file tree
Hide file tree
Showing 15 changed files with 267 additions and 112 deletions.
1 change: 1 addition & 0 deletions packages/spa/defaults/translations/en/validation.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php declare(strict_types = 1);

return [
'default' => 'The :attribute is invalid.',
'bool' => 'The :attribute is not a boolean.',
'string' => 'The :attribute is not a string.',
'int' => 'The :attribute is not an integer.',
Expand Down
5 changes: 1 addition & 4 deletions packages/spa/src/Validation/Rules/BoolRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
* Boolean value.
*/
class BoolRule extends Rule {
/**
* @inheritDoc
*/
#[Override]
public function passes($attribute, $value) {
public function isValid(string $attribute, mixed $value): bool {
return is_bool($value);
}
}
35 changes: 24 additions & 11 deletions packages/spa/src/Validation/Rules/BoolRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace LastDragon_ru\LaraASP\Spa\Validation\Rules;

use Illuminate\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\Validation\Factory;
use LastDragon_ru\LaraASP\Spa\Testing\Package\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -15,19 +15,32 @@
final class BoolRuleTest extends TestCase {
// <editor-fold desc="Tests">
// =========================================================================
#[DataProvider('dataProviderPasses')]
public function testPasses(bool $expected, mixed $value): void {
$translator = Container::getInstance()->make(Translator::class);
$rule = new BoolRule($translator);
#[DataProvider('dataProviderIsValid')]
public function testRule(bool $expected, mixed $value): void {
$rule = Container::getInstance()->make(BoolRule::class);
$factory = Container::getInstance()->make(Factory::class);
$validator = $factory->make(['value' => $value], ['value' => $rule]);

self::assertEquals($expected, $rule->passes('attribute', $value));
self::assertEquals($expected, !$validator->fails());

if ($expected === false) {
self::assertEquals(
[
'value' => [
'The value is not a boolean.',
],
],
$validator->errors()->toArray(),
);
}
}

public function testMessage(): void {
$translator = Container::getInstance()->make(Translator::class);
$rule = new BoolRule($translator);
#[DataProvider('dataProviderIsValid')]
public function testIsValid(bool $expected, mixed $value): void {
$rule = Container::getInstance()->make(BoolRule::class);
$actual = $rule->isValid('attribute', $value);

self::assertEquals('The :attribute is not a boolean.', $rule->message());
self::assertEquals($expected, $actual);
}
// </editor-fold>

Expand All @@ -36,7 +49,7 @@ public function testMessage(): void {
/**
* @return array<array-key, mixed>
*/
public static function dataProviderPasses(): array {
public static function dataProviderIsValid(): array {
return [
'true' => [true, true],
'false' => [true, false],
Expand Down
13 changes: 5 additions & 8 deletions packages/spa/src/Validation/Rules/DateRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@
* ISO 8601 Date.
*/
class DateRule extends Rule implements ValueProvider {
/**
* @inheritDoc
*/
#[Override]
public function passes($attribute, $value) {
$passes = false;
public function isValid(string $attribute, mixed $value): bool {
$valid = false;

try {
$date = $this->getValue($value);
$passes = $date && $date->format($this->getFormat()) === $value;
$date = $this->getValue($value);
$valid = $date && $date->format($this->getFormat()) === $value;
} catch (InvalidArgumentException $exception) {
// ignored
}

return $passes;
return $valid;
}

#[Override]
Expand Down
34 changes: 24 additions & 10 deletions packages/spa/src/Validation/Rules/DateRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Illuminate\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\Validation\Factory;
use InvalidArgumentException;
use LastDragon_ru\LaraASP\Spa\Testing\Package\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -17,19 +18,32 @@
final class DateRuleTest extends TestCase {
// <editor-fold desc="Tests">
// =========================================================================
#[DataProvider('dataProviderPasses')]
public function testPasses(bool $expected, string $value): void {
$translator = Container::getInstance()->make(Translator::class);
$rule = new DateRule($translator);
#[DataProvider('dataProviderIsValid')]
public function testRule(bool $expected, mixed $value): void {
$rule = Container::getInstance()->make(DateRule::class);
$factory = Container::getInstance()->make(Factory::class);
$validator = $factory->make(['value' => $value], ['value' => $rule]);

self::assertEquals($expected, !$validator->fails());

self::assertEquals($expected, $rule->passes('attribute', $value));
if ($expected === false) {
self::assertEquals(
[
'value' => [
'The value is not a valid date.',
],
],
$validator->errors()->toArray(),
);
}
}

public function testMessage(): void {
$translator = Container::getInstance()->make(Translator::class);
$rule = new DateRule($translator);
#[DataProvider('dataProviderIsValid')]
public function testIsValid(bool $expected, string $value): void {
$rule = Container::getInstance()->make(DateRule::class);
$actual = $rule->isValid('attribute', $value);

self::assertEquals('The :attribute is not a valid date.', $rule->message());
self::assertEquals($expected, $actual);
}

#[DataProvider('dataProviderGetValue')]
Expand All @@ -51,7 +65,7 @@ public function testGetValue(Exception|string|null $expected, string $value): vo
/**
* @return array<array-key, mixed>
*/
public static function dataProviderPasses(): array {
public static function dataProviderIsValid(): array {
return [
'valid date' => [true, '2102-12-01'],
'invalid date' => [false, '02-12-01'],
Expand Down
34 changes: 24 additions & 10 deletions packages/spa/src/Validation/Rules/DateTimeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\Validation\Factory;
use InvalidArgumentException;
use LastDragon_ru\LaraASP\Spa\Testing\Package\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
Expand All @@ -19,19 +20,32 @@
final class DateTimeRuleTest extends TestCase {
// <editor-fold desc="Tests">
// =========================================================================
#[DataProvider('dataProviderPasses')]
public function testPasses(bool $expected, string $value): void {
$translator = Container::getInstance()->make(Translator::class);
$rule = new DateTimeRule($translator);
#[DataProvider('dataProviderIsValid')]
public function testRule(bool $expected, mixed $value): void {
$rule = Container::getInstance()->make(DateTimeRule::class);
$factory = Container::getInstance()->make(Factory::class);
$validator = $factory->make(['value' => $value], ['value' => $rule]);

self::assertEquals($expected, $rule->passes('attribute', $value));
self::assertEquals($expected, !$validator->fails());

if ($expected === false) {
self::assertEquals(
[
'value' => [
'The value is not a valid datetime.',
],
],
$validator->errors()->toArray(),
);
}
}

public function testMessage(): void {
$translator = Container::getInstance()->make(Translator::class);
$rule = new DateTimeRule($translator);
#[DataProvider('dataProviderIsValid')]
public function testIsValid(bool $expected, string $value): void {
$rule = Container::getInstance()->make(DateTimeRule::class);
$actual = $rule->isValid('attribute', $value);

self::assertEquals('The :attribute is not a valid datetime.', $rule->message());
self::assertEquals($expected, $actual);
}

/**
Expand Down Expand Up @@ -62,7 +76,7 @@ public function testGetValue(string|array $expected, ?string $tz, string $value)
/**
* @return array<array-key, mixed>
*/
public static function dataProviderPasses(): array {
public static function dataProviderIsValid(): array {
return [
'valid date' => [false, '2102-12-01'],
'invalid date' => [false, '02-12-01'],
Expand Down
5 changes: 1 addition & 4 deletions packages/spa/src/Validation/Rules/IntRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
* Int value.
*/
class IntRule extends Rule {
/**
* @inheritDoc
*/
#[Override]
public function passes($attribute, $value) {
public function isValid(string $attribute, mixed $value): bool {
return is_int($value);
}
}
35 changes: 24 additions & 11 deletions packages/spa/src/Validation/Rules/IntRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace LastDragon_ru\LaraASP\Spa\Validation\Rules;

use Illuminate\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\Validation\Factory;
use LastDragon_ru\LaraASP\Spa\Testing\Package\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -18,19 +18,32 @@
final class IntRuleTest extends TestCase {
// <editor-fold desc="Tests">
// =========================================================================
#[DataProvider('dataProviderPasses')]
public function testPasses(bool $expected, mixed $value): void {
$translator = Container::getInstance()->make(Translator::class);
$rule = new IntRule($translator);
#[DataProvider('dataProviderIsValid')]
public function testRule(bool $expected, mixed $value): void {
$rule = Container::getInstance()->make(IntRule::class);
$factory = Container::getInstance()->make(Factory::class);
$validator = $factory->make(['value' => $value], ['value' => $rule]);

self::assertEquals($expected, $rule->passes('attribute', $value));
self::assertEquals($expected, !$validator->fails());

if ($expected === false) {
self::assertEquals(
[
'value' => [
'The value is not an integer.',
],
],
$validator->errors()->toArray(),
);
}
}

public function testMessage(): void {
$translator = Container::getInstance()->make(Translator::class);
$rule = new IntRule($translator);
#[DataProvider('dataProviderIsValid')]
public function testIsValid(bool $expected, mixed $value): void {
$rule = Container::getInstance()->make(IntRule::class);
$actual = $rule->isValid('attribute', $value);

self::assertEquals('The :attribute is not an integer.', $rule->message());
self::assertEquals($expected, $actual);
}
// </editor-fold>

Expand All @@ -39,7 +52,7 @@ public function testMessage(): void {
/**
* @return array<array-key, mixed>
*/
public static function dataProviderPasses(): array {
public static function dataProviderIsValid(): array {
return [
'true' => [false, true],
'false' => [false, false],
Expand Down
7 changes: 2 additions & 5 deletions packages/spa/src/Validation/Rules/NumberRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
* Number value.
*/
class NumberRule extends IntRule {
/**
* @inheritDoc
*/
#[Override]
public function passes($attribute, $value) {
return (is_float($value) && is_finite($value)) || parent::passes($attribute, $value);
public function isValid(string $attribute, mixed $value): bool {
return (is_float($value) && is_finite($value)) || parent::isValid($attribute, $value);
}
}
35 changes: 24 additions & 11 deletions packages/spa/src/Validation/Rules/NumberRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace LastDragon_ru\LaraASP\Spa\Validation\Rules;

use Illuminate\Container\Container;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\Validation\Factory;
use LastDragon_ru\LaraASP\Spa\Testing\Package\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
Expand All @@ -18,19 +18,32 @@
final class NumberRuleTest extends TestCase {
// <editor-fold desc="Tests">
// =========================================================================
#[DataProvider('dataProviderPasses')]
public function testPasses(bool $expected, mixed $value): void {
$translator = Container::getInstance()->make(Translator::class);
$rule = new NumberRule($translator);
#[DataProvider('dataProviderIsValid')]
public function testRule(bool $expected, mixed $value): void {
$rule = Container::getInstance()->make(NumberRule::class);
$factory = Container::getInstance()->make(Factory::class);
$validator = $factory->make(['value' => $value], ['value' => $rule]);

self::assertEquals($expected, $rule->passes('attribute', $value));
self::assertEquals($expected, !$validator->fails());

if ($expected === false) {
self::assertEquals(
[
'value' => [
'The value is not an number.',
],
],
$validator->errors()->toArray(),
);
}
}

public function testMessage(): void {
$translator = Container::getInstance()->make(Translator::class);
$rule = new NumberRule($translator);
#[DataProvider('dataProviderIsValid')]
public function testIsValid(bool $expected, mixed $value): void {
$rule = Container::getInstance()->make(NumberRule::class);
$actual = $rule->isValid('attribute', $value);

self::assertEquals('The :attribute is not an number.', $rule->message());
self::assertEquals($expected, $actual);
}
// </editor-fold>

Expand All @@ -39,7 +52,7 @@ public function testMessage(): void {
/**
* @return array<array-key, mixed>
*/
public static function dataProviderPasses(): array {
public static function dataProviderIsValid(): array {
return [
'true' => [false, true],
'false' => [false, false],
Expand Down
Loading

0 comments on commit d485119

Please sign in to comment.