-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validation): Handle float with comma in NumberRule and improve o…
…verflow detection
- Loading branch information
Showing
4 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Unit\Validation\Rules; | ||
|
||
use Illuminate\Contracts\Validation\Rule; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
abstract class AbstractRuleTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider testPassesData | ||
*/ | ||
public function testPasses(RuleExpectation $expectation): void | ||
{ | ||
$rule = $this->createRule(); | ||
|
||
$this->assertEquals($expectation->expectedIsValid, $rule->passes('test', $expectation->value)); | ||
} | ||
|
||
public function testMessage(): void | ||
{ | ||
$rule = $this->createRule(); | ||
|
||
$this->assertEquals($this->getExpectedMessage(), str_replace(':attribute', 'test', $rule->message())); | ||
} | ||
|
||
abstract public function createRule(): Rule; | ||
|
||
/** | ||
* @return array<RuleExpectation> | ||
*/ | ||
abstract protected function testData(): array; | ||
|
||
abstract protected function getExpectedMessage(): string; | ||
|
||
protected function testPassesData(): array | ||
{ | ||
$data = []; | ||
foreach ($this->testData() as $index => $entity) { | ||
$data[$index . ' with value: ' . $entity->value] = [$entity]; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Unit\Validation\Rules; | ||
|
||
use Illuminate\Contracts\Validation\Rule; | ||
use LaraStrict\Validation\Rules\NumberRule; | ||
|
||
class NumberRuleTest extends AbstractRuleTest | ||
{ | ||
public function createRule(): Rule | ||
{ | ||
return new NumberRule(); | ||
} | ||
|
||
protected function testData(): array | ||
{ | ||
return [ | ||
new RuleExpectation('test', false), | ||
new RuleExpectation('234', true), | ||
new RuleExpectation(234, true), | ||
new RuleExpectation('-234', true), | ||
new RuleExpectation(-245, true), | ||
new RuleExpectation('245.5', true), | ||
new RuleExpectation('-245.5', true), | ||
new RuleExpectation(245.50, true), | ||
new RuleExpectation(-245.50, true), | ||
new RuleExpectation('9223372036854775807', false), | ||
new RuleExpectation('9223372036854775807.5', false), | ||
new RuleExpectation('922337203685477580703434355.5', false), | ||
new RuleExpectation('-922337203685477580703434355.5', false), | ||
new RuleExpectation('9223372036854775807', false), | ||
new RuleExpectation('9223372036854775807.5', false), | ||
new RuleExpectation('9223372036854775.5', false), | ||
new RuleExpectation('-922337203685477.5', false), | ||
]; | ||
} | ||
|
||
protected function getExpectedMessage(): string | ||
{ | ||
return 'Given test is not a valid number or it exceeds int/float limits.'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\LaraStrict\Unit\Validation\Rules; | ||
|
||
class RuleExpectation | ||
{ | ||
public function __construct( | ||
public mixed $value, | ||
public bool $expectedIsValid, | ||
) { | ||
} | ||
} |