Skip to content

Commit

Permalink
Enhancement: Implement between() and betweenExclusive() assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Oct 5, 2016
1 parent 9c90e34 commit 30fb376
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,11 @@
# Change Log
All notable changes to this project will be documented in this file.

## 2.6.* - unreleased
### Added assertions
- `Assert\Assertion::between()`
- `Assert\Assertion::betweenExclusive()`

## 2.6.4 - 2016-10-03
### Added assertions
- `Assert\Assertion::e164()` - The international public telecommunication numbering plan
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -137,6 +137,8 @@ This can be useful for example to build a failure response for the user.
use Assert\Assertion;

Assertion::alnum($value);
Assertion::between($value, $lowerLimit, $upperLimit);
Assertion::betweenExclusive($value, $lowerLimit, $upperLimit);
Assertion::betweenLength($value, $minLength, $maxLength);
Assertion::boolean($value);
Assertion::choice($value, $choices);
Expand Down
52 changes: 52 additions & 0 deletions lib/Assert/Assertion.php
Expand Up @@ -21,6 +21,8 @@
* @author Benjamin Eberlei <kontakt@beberlei.de>
*
* @method static void allAlnum($value, $message = null, $propertyPath = null) Assert that value is alphanumeric for all values.
* @method static void allBetween($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit for all values.
* @method static void allBetweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit for all values.
* @method static void allBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string length is between min,max lengths for all values.
* @method static void allBoolean($value, $message = null, $propertyPath = null) Assert that value is php boolean for all values.
* @method static void allChoice($value, $choices, $message = null, $propertyPath = null) Assert that value is in array of choices for all values.
Expand Down Expand Up @@ -91,6 +93,8 @@
* @method static void allUuid($value, $message = null, $propertyPath = null) Assert that the given string is a valid UUID for all values.
* @method static void allWriteable($value, $message = null, $propertyPath = null) Assert that the value is something writeable for all values.
* @method static void nullOrAlnum($value, $message = null, $propertyPath = null) Assert that value is alphanumeric or that the value is null.
* @method static void nullOrBetween($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit or that the value is null.
* @method static void nullOrBetweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit or that the value is null.
* @method static void nullOrBetweenLength($value, $minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string length is between min,max lengths or that the value is null.
* @method static void nullOrBoolean($value, $message = null, $propertyPath = null) Assert that value is php boolean or that the value is null.
* @method static void nullOrChoice($value, $choices, $message = null, $propertyPath = null) Assert that value is in array of choices or that the value is null.
Expand Down Expand Up @@ -227,6 +231,8 @@ class Assertion
const INVALID_KEY_NOT_EXISTS = 216;
const INVALID_SATISFY = 217;
const INVALID_IP = 218;
const INVALID_BETWEEN = 219;
const INVALID_BETWEEN_EXCLUSIVE = 220;

/**
* Exception to throw when an assertion failed.
Expand Down Expand Up @@ -1791,6 +1797,52 @@ public static function greaterOrEqualThan($value, $limit, $message = null, $prop
}
}

/**
* Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit.
*
* @param mixed $value
* @param mixed $lowerLimit
* @param mixed $upperLimit
* @param string $message
* @param string $propertyPath
*/
public static function between($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null)
{
if ($lowerLimit > $value || $value > $upperLimit) {
$message = sprintf(
$message ?: 'Provided "%s" is neither greater than or equal to "%s" nor less than or equal to "%s".',
static::stringify($value),
static::stringify($lowerLimit),
static::stringify($upperLimit)
);

throw static::createException($value, $message, static::INVALID_BETWEEN, $propertyPath);
}
}

/**
* Assert that a value is greater than a lower limit, and less than an upper limit.
*
* @param mixed $value
* @param mixed $lowerLimit
* @param mixed $upperLimit
* @param string $message
* @param string $propertyPath
*/
public static function betweenExclusive($value, $lowerLimit, $upperLimit, $message = null, $propertyPath = null)
{
if ($lowerLimit >= $value || $value >= $upperLimit) {
$message = sprintf(
$message ?: 'Provided "%s" is neither greater than "%s" nor less than "%s".',
static::stringify($value),
static::stringify($lowerLimit),
static::stringify($upperLimit)
);

throw static::createException($value, $message, static::INVALID_BETWEEN_EXCLUSIVE, $propertyPath);
}
}

/**
* Assert that date is valid and corresponds to the given format.
*
Expand Down
2 changes: 2 additions & 0 deletions lib/Assert/AssertionChain.php
Expand Up @@ -22,6 +22,8 @@
* @author Benjamin Eberlei <kontakt@beberlei.de>
*
* @method AssertionChain alnum($message = null, $propertyPath = null) Assert that value is alphanumeric.
* @method AssertionChain between($lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit.
* @method AssertionChain betweenExclusive($lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit.
* @method AssertionChain betweenLength($minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string length is between min,max lengths.
* @method AssertionChain boolean($message = null, $propertyPath = null) Assert that value is php boolean.
* @method AssertionChain choice($choices, $message = null, $propertyPath = null) Assert that value is in array of choices.
Expand Down
2 changes: 2 additions & 0 deletions lib/Assert/LazyAssertion.php
Expand Up @@ -19,6 +19,8 @@
* @author Benjamin Eberlei <kontakt@beberlei.de>
*
* @method LazyAssertion alnum($message = null, $propertyPath = null) Assert that value is alphanumeric.
* @method LazyAssertion between($lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater or equal than a lower limit, and less than or equal to an upper limit.
* @method LazyAssertion betweenExclusive($lowerLimit, $upperLimit, $message = null, $propertyPath = null) Assert that a value is greater than a lower limit, and less than an upper limit.
* @method LazyAssertion betweenLength($minLength, $maxLength, $message = null, $propertyPath = null, $encoding = "utf8") Assert that string length is between min,max lengths.
* @method LazyAssertion boolean($message = null, $propertyPath = null) Assert that value is php boolean.
* @method LazyAssertion choice($choices, $message = null, $propertyPath = null) Assert that value is in array of choices.
Expand Down
110 changes: 110 additions & 0 deletions tests/Assert/Tests/AssertTest.php
Expand Up @@ -3,6 +3,7 @@

use Assert\Assertion;
use Assert\AssertionFailedException;
use Webmozart\Assert\Assert;

class AssertTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -1364,6 +1365,115 @@ public function testValidInterfaceExists()
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_INTERFACE);
Assertion::interfaceExists("\\Countable");
}

/**
* @dataProvider providerInvalidBetween
*
* @param mixed $value
* @param mixed $lowerLimit
* @param mixed $upperLimit
*/
public function testInvalidBetween($value, $lowerLimit, $upperLimit)
{
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_BETWEEN);

Assertion::between($value, $lowerLimit, $upperLimit);
}

/**
* @return array
*/
public function providerInvalidBetween()
{
return array(
array(1, 2, 3),
array(3, 1, 2),
array('aaa', 'bbb', 'ccc'),
array('ddd', 'bbb', 'ccc'),
array(new \DateTime('yesterday'), new \DateTime('today'), new \DateTime('tomorrow')),
array(new \DateTime('tomorrow'), new \DateTime('yesterday'), new \DateTime('today')),
);
}

/**
* @dataProvider providerValidBetween
*
* @param mixed $value
* @param mixed $lowerLimit
* @param mixed $upperLimit
*/
public function testValidBetween($value, $lowerLimit, $upperLimit)
{
$this->assertNull(Assertion::between($value, $lowerLimit, $upperLimit));
}

/**
* @return array
*/
public function providerValidBetween()
{
return array(
array(2, 1, 3),
array(1, 1, 1),
array('bbb', 'aaa', 'ccc'),
array('aaa', 'aaa', 'aaa'),
array(new \DateTime('today'), new \DateTime('yesterday'), new \DateTime('tomorrow')),
array(new \DateTime('today'), new \DateTime('today'), new \DateTime('today')),
);
}

/**
* @dataProvider providerInvalidBetweenExclusive
*
* @param mixed $value
* @param mixed $lowerLimit
* @param mixed $upperLimit
*/
public function testInvalidBetweenExclusive($value, $lowerLimit, $upperLimit)
{
$this->setExpectedException('Assert\AssertionFailedException', null, Assertion::INVALID_BETWEEN_EXCLUSIVE);

Assertion::betweenExclusive($value, $lowerLimit, $upperLimit);
}

/**
* @return array
*/
public function providerInvalidBetweenExclusive()
{
return array(
array(1, 1, 2),
array(2, 1, 2),
array('aaa', 'aaa', 'bbb'),
array('bbb', 'aaa', 'bbb'),
array(new \DateTime('today'), new \DateTime('today'), new \DateTime('tomorrow')),
array(new \DateTime('tomorrow'), new \DateTime('today'), new \DateTime('tomorrow')),
);
}

/**
* @dataProvider providerValidBetweenExclusive
*
* @param mixed $value
* @param mixed $lowerLimit
* @param mixed $upperLimit
*/
public function testValidBetweenExclusive($value, $lowerLimit, $upperLimit)
{
$this->assertNull(Assertion::betweenExclusive($value, $lowerLimit, $upperLimit));
}

/**
* @return array
*/
public function providerValidBetweenExclusive()
{
return array(
array(2, 1, 3),
array('bbb', 'aaa', 'ccc'),
array(new \DateTime('today'), new \DateTime('yesterday'), new \DateTime('tomorrow')),
);
}
}

class ChildStdClass extends \stdClass
Expand Down

0 comments on commit 30fb376

Please sign in to comment.