Skip to content

Commit

Permalink
Merge pull request #6 from wmde/integer_value_validator
Browse files Browse the repository at this point in the history
Create integer value validator
  • Loading branch information
timEulitz authored Jul 17, 2018
2 parents 29fd4b7 + d09a67e commit 6e1da9e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Validators/IntegerValueValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare( strict_types = 1 );

namespace WMDE\FunValidators\Validators;

use WMDE\FunValidators\ConstraintViolation;
use WMDE\FunValidators\ValidationResult;

/**
* @license GNU GPL v2+
*/
class IntegerValueValidator {

public function validate( string $input ): ValidationResult {
if ( !ctype_digit( $input ) ) {
return new ValidationResult( new ConstraintViolation( $input, 'field_numeric' ) );
}

return new ValidationResult();
}

}
28 changes: 28 additions & 0 deletions tests/Unit/Validators/IntegerValueValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare( strict_types = 1 );

namespace WMDE\FunValidators\Tests\Unit\Validators;

use WMDE\FunValidators\Validators\IntegerValueValidator;

/**
* @covers \WMDE\FunValidators\Validators\IntegerValueValidator
*
* @license GNU GPL v2+
*/
class IntegerValueValidatorTest extends \PHPUnit\Framework\TestCase {

public function testGivenIntegerValues_validationSucceeds(): void {
$validator = new IntegerValueValidator();
$this->assertTrue( $validator->validate( '1234567890' )->isSuccessful() );
$this->assertTrue( $validator->validate( '000123456789' )->isSuccessful() );
}

public function testGivenInvalidValues_validationFails(): void {
$validator = new IntegerValueValidator();
$this->assertFalse( $validator->validate( '-1234567890' )->isSuccessful() );
$this->assertFalse( $validator->validate( '21391e213123' )->isSuccessful() );
$this->assertFalse( $validator->validate( '21391e213123' )->isSuccessful() );
}
}

0 comments on commit 6e1da9e

Please sign in to comment.