Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ Assertion::ipv6(string $value, int $flag = null);
Assertion::isArray(mixed $value);
Assertion::isArrayAccessible(mixed $value);
Assertion::isCallable(mixed $value);
Assertion::isCountable(mixed $value);
Assertion::isCountable(array|\Countable|\ResourceBundle|\SimpleXMLElement $value);
Assertion::isInstanceOf(mixed $value, string $className);
Assertion::isJsonString(mixed $value);
Assertion::isObject(mixed $value);
Expand All @@ -231,9 +231,11 @@ Assertion::length(mixed $value, int $length);
Assertion::lessOrEqualThan(mixed $value, mixed $limit);
Assertion::lessThan(mixed $value, mixed $limit);
Assertion::max(mixed $value, mixed $maxValue);
Assertion::maxCount(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count);
Assertion::maxLength(mixed $value, int $maxLength);
Assertion::methodExists(string $value, mixed $object);
Assertion::min(mixed $value, mixed $minValue);
Assertion::minCount(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count);
Assertion::minLength(mixed $value, int $minLength);
Assertion::noContent(mixed $value);
Assertion::notBlank(mixed $value);
Expand Down
66 changes: 61 additions & 5 deletions lib/Assert/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
* @method static bool allIsArray(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array for all values.
* @method static bool allIsArrayAccessible(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object for all values.
* @method static bool allIsCallable(mixed $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable for all values.
* @method static bool allIsCountable(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is countable for all values.
* @method static bool allIsCountable(array|\Countable|\ResourceBundle|\SimpleXMLElement $value, string|callable $message = null, string $propertyPath = null) Assert that value is countable for all values.
* @method static bool allIsInstanceOf(mixed $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name for all values.
* @method static bool allIsJsonString(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string for all values.
* @method static bool allIsObject(mixed $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object for all values.
Expand All @@ -71,9 +71,11 @@
* @method static bool allLessOrEqualThan(mixed $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit for all values.
* @method static bool allLessThan(mixed $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit for all values.
* @method static bool allMax(mixed $value, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit for all values.
* @method static bool allMaxCount(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count, string $message = null, string $propertyPath = null) Assert that the countable have at most $count elements for all values.
* @method static bool allMaxLength(mixed $value, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars for all values.
* @method static bool allMethodExists(string $value, mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object for all values.
* @method static bool allMin(mixed $value, mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit for all values.
* @method static bool allMinCount(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count, string $message = null, string $propertyPath = null) Assert that the countable have at least $count elements for all values.
* @method static bool allMinLength(mixed $value, int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long for all values.
* @method static bool allNoContent(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is empty for all values.
* @method static bool allNotBlank(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is not blank for all values.
Expand Down Expand Up @@ -142,7 +144,7 @@
* @method static bool nullOrIsArray(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or that the value is null.
* @method static bool nullOrIsArrayAccessible(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is an array or an array-accessible object or that the value is null.
* @method static bool nullOrIsCallable(mixed $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is callable or that the value is null.
* @method static bool nullOrIsCountable(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is countable or that the value is null.
* @method static bool nullOrIsCountable(array|\Countable|\ResourceBundle|\SimpleXMLElement $value, string|callable $message = null, string $propertyPath = null) Assert that value is countable or that the value is null.
* @method static bool nullOrIsInstanceOf(mixed $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is instance of given class-name or that the value is null.
* @method static bool nullOrIsJsonString(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that the given string is a valid json string or that the value is null.
* @method static bool nullOrIsObject(mixed $value, string|callable $message = null, string $propertyPath = null) Determines that the provided value is an object or that the value is null.
Expand All @@ -155,9 +157,11 @@
* @method static bool nullOrLessOrEqualThan(mixed $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit or that the value is null.
* @method static bool nullOrLessThan(mixed $value, mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit or that the value is null.
* @method static bool nullOrMax(mixed $value, mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit or that the value is null.
* @method static bool nullOrMaxCount(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count, string $message = null, string $propertyPath = null) Assert that the countable have at most $count elements or that the value is null.
* @method static bool nullOrMaxLength(mixed $value, int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars or that the value is null.
* @method static bool nullOrMethodExists(string $value, mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object or that the value is null.
* @method static bool nullOrMin(mixed $value, mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit or that the value is null.
* @method static bool nullOrMinCount(array|\Countable|\ResourceBundle|\SimpleXMLElement $countable, int $count, string $message = null, string $propertyPath = null) Assert that the countable have at least $count elements or that the value is null.
* @method static bool nullOrMinLength(mixed $value, int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long or that the value is null.
* @method static bool nullOrNoContent(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is empty or that the value is null.
* @method static bool nullOrNotBlank(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is not blank or that the value is null.
Expand Down Expand Up @@ -267,6 +271,8 @@ class Assertion
const INVALID_PROPERTY = 224;
const INVALID_RESOURCE = 225;
const INVALID_COUNTABLE = 226;
const INVALID_MIN_COUNT = 227;
const INVALID_MAX_COUNT = 228;

/**
* Exception to throw when an assertion failed.
Expand Down Expand Up @@ -1072,9 +1078,9 @@ public static function isArrayAccessible($value, $message = null, $propertyPath
/**
* Assert that value is countable.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
* @param array|\Countable|\ResourceBundle|\SimpleXMLElement $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
Expand Down Expand Up @@ -1814,6 +1820,56 @@ public static function count($countable, $count, $message = null, $propertyPath
return true;
}

/**
* Assert that the countable have at least $count elements.
*
* @param array|\Countable|\ResourceBundle|\SimpleXMLElement $countable
* @param int $count
* @param string|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function minCount($countable, $count, $message = null, $propertyPath = null)
{
if ($count > \count($countable)) {
$message = \sprintf(
static::generateMessage($message ?: 'List should have at least %d elements, but has %d elements.'),
static::stringify($count),
static::stringify(\count($countable))
);

throw static::createException($countable, $message, static::INVALID_MIN_COUNT, $propertyPath, ['count' => $count]);
}

return true;
}

/**
* Assert that the countable have at most $count elements.
*
* @param array|\Countable|\ResourceBundle|\SimpleXMLElement $countable
* @param int $count
* @param string|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function maxCount($countable, $count, $message = null, $propertyPath = null)
{
if ($count < \count($countable)) {
$message = \sprintf(
static::generateMessage($message ?: 'List should have at most %d elements, but has %d elements.'),
static::stringify($count),
static::stringify(\count($countable))
);

throw static::createException($countable, $message, static::INVALID_MAX_COUNT, $propertyPath, ['count' => $count]);
}

return true;
}

/**
* static call handler to implement:
* - "null or assertion" delegation
Expand Down
2 changes: 2 additions & 0 deletions lib/Assert/AssertionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@
* @method AssertionChain lessOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit.
* @method AssertionChain lessThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit.
* @method AssertionChain max(mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit.
* @method AssertionChain maxCount(int $count, string $message = null, string $propertyPath = null) Assert that the countable have at most $count elements.
* @method AssertionChain maxLength(int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars.
* @method AssertionChain methodExists(mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object.
* @method AssertionChain min(mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit.
* @method AssertionChain minCount(int $count, string $message = null, string $propertyPath = null) Assert that the countable have at least $count elements.
* @method AssertionChain minLength(int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long.
* @method AssertionChain noContent(string|callable $message = null, string $propertyPath = null) Assert that value is empty.
* @method AssertionChain notBlank(string|callable $message = null, string $propertyPath = null) Assert that value is not blank.
Expand Down
2 changes: 2 additions & 0 deletions lib/Assert/LazyAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@
* @method LazyAssertion lessOrEqualThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less or equal than given limit.
* @method LazyAssertion lessThan(mixed $limit, string|callable $message = null, string $propertyPath = null) Determines if the value is less than given limit.
* @method LazyAssertion max(mixed $maxValue, string|callable $message = null, string $propertyPath = null) Assert that a number is smaller as a given limit.
* @method LazyAssertion maxCount(int $count, string $message = null, string $propertyPath = null) Assert that the countable have at most $count elements.
* @method LazyAssertion maxLength(int $maxLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that string value is not longer than $maxLength chars.
* @method LazyAssertion methodExists(mixed $object, string|callable $message = null, string $propertyPath = null) Determines that the named method is defined in the provided object.
* @method LazyAssertion min(mixed $minValue, string|callable $message = null, string $propertyPath = null) Assert that a value is at least as big as a given limit.
* @method LazyAssertion minCount(int $count, string $message = null, string $propertyPath = null) Assert that the countable have at least $count elements.
* @method LazyAssertion minLength(int $minLength, string|callable $message = null, string $propertyPath = null, string $encoding = 'utf8') Assert that a string is at least $minLength chars long.
* @method LazyAssertion noContent(string|callable $message = null, string $propertyPath = null) Assert that value is empty.
* @method LazyAssertion notBlank(string|callable $message = null, string $propertyPath = null) Assert that value is not blank.
Expand Down
73 changes: 68 additions & 5 deletions tests/Assert/Tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ public function testFileWithEmptyFilename()
*/
public function testFileDoesNotExists()
{
Assertion::file(__DIR__ . '/does-not-exists');
Assertion::file(__DIR__.'/does-not-exists');
}

/**
Expand All @@ -1100,7 +1100,7 @@ public function testDirectory()
{
$this->assertTrue(Assertion::directory(__DIR__));

Assertion::directory(__DIR__ . '/does-not-exist');
Assertion::directory(__DIR__.'/does-not-exist');
}

/**
Expand All @@ -1111,7 +1111,7 @@ public function testReadable()
{
$this->assertTrue(Assertion::readable(__FILE__));

Assertion::readable(__DIR__ . '/does-not-exist');
Assertion::readable(__DIR__.'/does-not-exist');
}

/**
Expand All @@ -1122,7 +1122,7 @@ public function testWriteable()
{
$this->assertTrue(Assertion::writeable(\sys_get_temp_dir()));

Assertion::writeable(__DIR__ . '/does-not-exist');
Assertion::writeable(__DIR__.'/does-not-exist');
}

/**
Expand Down Expand Up @@ -1389,6 +1389,69 @@ public function testInvalidCount($countable, $count)
Assertion::count($countable, $count);
}

public function testValidMinCount()
{
$this->assertTrue(Assertion::minCount(['Hi'], 1));
$this->assertTrue(Assertion::minCount(['Hi', 'There'], 1));
$this->assertTrue(Assertion::minCount(new Fixtures\OneCountable(), 1));
$this->assertTrue(Assertion::minCount(new SimpleXMLElement('<a><b /><c /></a>'), 1));
$this->assertTrue(Assertion::minCount(new ResourceBundle('en_US', __DIR__.'/_files/ResourceBundle'), 2));
}

public static function dataInvalidMinCount()
{
return [
'2 elements while at least 3 expected' => [['Hi', 'There'], 3],
'1 countable while at least 2 expected' => [new Fixtures\OneCountable(), 2],
'2 countable while at least 3 expected' => [new SimpleXMLElement('<a><b /><c /></a>'), 3],
'6 countable while at least 7 expected' => [new ResourceBundle('en_US', __DIR__.'/_files/ResourceBundle'), 7],
];
}

/**
* @dataProvider dataInvalidMinCount
* @expectedException \Assert\AssertionFailedException
* @expectedExceptionCode \Assert\Assertion::INVALID_MIN_COUNT
* @expectedExceptionMessageRegExp /List should have at least \d+ elements, but has \d elements./
*
* @param mixed $countable
* @param int $count
*/
public function testInvalidMinCount($countable, $count)
{
Assertion::minCount($countable, $count);
}

public function testValidMaxCount()
{
$this->assertTrue(Assertion::maxCount(['Hi'], 1));
$this->assertTrue(Assertion::maxCount(['Hi', 'There'], 2));
$this->assertTrue(Assertion::maxCount(new Fixtures\OneCountable(), 1));
$this->assertTrue(Assertion::maxCount(new SimpleXMLElement('<a><b /><c /></a>'), 3));
$this->assertTrue(Assertion::maxCount(new ResourceBundle('en_US', __DIR__.'/_files/ResourceBundle'), 7));
}

public static function dataInvalidMaxCount()
{
return [
'2 elements while at most 1 expected' => [['Hi', 'There'], 1],
'1 countable while at most 0 expected' => [new Fixtures\OneCountable(), 0],
'2 countable while at most 1 expected' => [new SimpleXMLElement('<a><b /><c /></a>'), 1],
'6 countable while at most 5 expected' => [new ResourceBundle('en_US', __DIR__.'/_files/ResourceBundle'), 5],
];
}

/**
* @dataProvider dataInvalidMaxCount
* @expectedException \Assert\AssertionFailedException
* @expectedExceptionCode \Assert\Assertion::INVALID_MAX_COUNT
* @expectedExceptionMessageRegExp /List should have at most \d+ elements, but has \d elements./
*/
public function testInvalidMaxCount($countable, $count)
{
Assertion::maxCount($countable, $count);
}

public function testChoicesNotEmpty()
{
$this->assertTrue(
Expand Down Expand Up @@ -1699,7 +1762,7 @@ public function testInvalidCallable()
public function testValidCallable()
{
$this->assertTrue(Assertion::isCallable('\is_callable'));
$this->assertTrue(Assertion::isCallable(__NAMESPACE__ . '\\Fixtures\\someCallable'));
$this->assertTrue(Assertion::isCallable(__NAMESPACE__.'\\Fixtures\\someCallable'));
$this->assertTrue(Assertion::isCallable([OneCountable::class, 'count']));
$this->assertTrue(
Assertion::isCallable(
Expand Down