-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from HarmonyIO/positive-negative-rules
Implemented negative and positive validation rules
- Loading branch information
Showing
4 changed files
with
261 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace HarmonyIO\Validation\Rule\Numeric; | ||
|
||
use Amp\Promise; | ||
use HarmonyIO\Validation\Rule\Rule; | ||
use function Amp\call; | ||
|
||
final class Negative implements Rule | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate($value): Promise | ||
{ | ||
return call(static function () use ($value) { | ||
if (!yield (new NumericType())->validate($value)) { | ||
return false; | ||
} | ||
|
||
return $value < 0; | ||
}); | ||
} | ||
} |
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,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace HarmonyIO\Validation\Rule\Numeric; | ||
|
||
use Amp\Promise; | ||
use HarmonyIO\Validation\Rule\Rule; | ||
use function Amp\call; | ||
|
||
final class Positive implements Rule | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate($value): Promise | ||
{ | ||
return call(static function () use ($value) { | ||
if (!yield (new NumericType())->validate($value)) { | ||
return false; | ||
} | ||
|
||
// phpcs:ignore SlevomatCodingStandard.ControlStructures.DisallowEqualOperators.DisallowedEqualOperator | ||
return $value == 0 || $value > 0; | ||
}); | ||
} | ||
} |
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,106 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace HarmonyIO\ValidationTest\Unit\Rule\Numeric; | ||
|
||
use HarmonyIO\PHPUnitExtension\TestCase; | ||
use HarmonyIO\Validation\Rule\Numeric\Negative; | ||
use HarmonyIO\Validation\Rule\Rule; | ||
|
||
class NegativeTest extends TestCase | ||
{ | ||
public function testRuleImplementsInterface(): void | ||
{ | ||
$this->assertInstanceOf(Rule::class, new Negative()); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingAnInteger(): void | ||
{ | ||
$this->assertTrue((new Negative())->validate(-1)); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingAFloat(): void | ||
{ | ||
$this->assertTrue((new Negative())->validate(-1.1)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingABoolean(): void | ||
{ | ||
$this->assertFalse((new Negative())->validate(true)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAnArray(): void | ||
{ | ||
$this->assertFalse((new Negative())->validate([])); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAnObject(): void | ||
{ | ||
$this->assertFalse((new Negative())->validate(new \DateTimeImmutable())); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingNull(): void | ||
{ | ||
$this->assertFalse((new Negative())->validate(null)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAResource(): void | ||
{ | ||
$resource = fopen('php://memory', 'r'); | ||
|
||
if ($resource === false) { | ||
$this->fail('Could not open the memory stream used for the test'); | ||
|
||
return; | ||
} | ||
|
||
$this->assertFalse((new Negative())->validate($resource)); | ||
|
||
fclose($resource); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingACallable(): void | ||
{ | ||
$this->assertFalse((new Negative())->validate(static function (): void { | ||
})); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingAnIntegerAsAString(): void | ||
{ | ||
$this->assertTrue((new Negative())->validate('-1')); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingAFloatAsAString(): void | ||
{ | ||
$this->assertTrue((new Negative())->validate('-1.1')); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingInZeroAsAString(): void | ||
{ | ||
$this->assertFalse((new Negative())->validate('0')); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingInZeroAsAnInteger(): void | ||
{ | ||
$this->assertFalse((new Negative())->validate(0)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingInZeroAsAFloat(): void | ||
{ | ||
$this->assertFalse((new Negative())->validate(0.0)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingInAPositiveAsAString(): void | ||
{ | ||
$this->assertFalse((new Negative())->validate('1')); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingInAPositiveAsAnInteger(): void | ||
{ | ||
$this->assertFalse((new Negative())->validate(1)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingInAPositiveAsAFloat(): void | ||
{ | ||
$this->assertFalse((new Negative())->validate(0.1)); | ||
} | ||
} |
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,106 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace HarmonyIO\ValidationTest\Unit\Rule\Numeric; | ||
|
||
use HarmonyIO\PHPUnitExtension\TestCase; | ||
use HarmonyIO\Validation\Rule\Numeric\Positive; | ||
use HarmonyIO\Validation\Rule\Rule; | ||
|
||
class PositiveTest extends TestCase | ||
{ | ||
public function testRuleImplementsInterface(): void | ||
{ | ||
$this->assertInstanceOf(Rule::class, new Positive()); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingAnInteger(): void | ||
{ | ||
$this->assertTrue((new Positive())->validate(1)); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingAFloat(): void | ||
{ | ||
$this->assertTrue((new Positive())->validate(1.1)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingABoolean(): void | ||
{ | ||
$this->assertFalse((new Positive())->validate(true)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAnArray(): void | ||
{ | ||
$this->assertFalse((new Positive())->validate([])); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAnObject(): void | ||
{ | ||
$this->assertFalse((new Positive())->validate(new \DateTimeImmutable())); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingNull(): void | ||
{ | ||
$this->assertFalse((new Positive())->validate(null)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingAResource(): void | ||
{ | ||
$resource = fopen('php://memory', 'r'); | ||
|
||
if ($resource === false) { | ||
$this->fail('Could not open the memory stream used for the test'); | ||
|
||
return; | ||
} | ||
|
||
$this->assertFalse((new Positive())->validate($resource)); | ||
|
||
fclose($resource); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingACallable(): void | ||
{ | ||
$this->assertFalse((new Positive())->validate(static function (): void { | ||
})); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingAnIntegerAsAString(): void | ||
{ | ||
$this->assertTrue((new Positive())->validate('1')); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingAFloatAsAString(): void | ||
{ | ||
$this->assertTrue((new Positive())->validate('1.1')); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingInZeroAsAString(): void | ||
{ | ||
$this->assertTrue((new Positive())->validate('0')); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingInZeroAsAnInteger(): void | ||
{ | ||
$this->assertTrue((new Positive())->validate(0)); | ||
} | ||
|
||
public function testValidateReturnsTrueWhenPassingInZeroAsAFloat(): void | ||
{ | ||
$this->assertTrue((new Positive())->validate(0.0)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingInANegativeAsAString(): void | ||
{ | ||
$this->assertFalse((new Positive())->validate('-1')); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingInANegativeAsAnInteger(): void | ||
{ | ||
$this->assertFalse((new Positive())->validate(-1)); | ||
} | ||
|
||
public function testValidateReturnsFalseWhenPassingInANegativeAsAFloat(): void | ||
{ | ||
$this->assertFalse((new Positive())->validate(-0.1)); | ||
} | ||
} |