Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce notFalse() #274

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ Assertion::notContains(mixed $string, string $needle);
Assertion::notEmpty(mixed $value);
Assertion::notEmptyKey(mixed $value, string|int $key);
Assertion::notEq(mixed $value1, mixed $value2);
Assertion::notFalse(mixed $value);
Assertion::notInArray(mixed $value, array $choices);
Assertion::notIsInstanceOf(mixed $value, string $className);
Assertion::notNull(mixed $value);
Expand Down
1 change: 1 addition & 0 deletions bin/generate_method_docs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/MethodDocGenerator.php';

Expand Down
32 changes: 29 additions & 3 deletions lib/Assert/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
* @method static bool allNotEmpty(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is not empty for all values.
* @method static bool allNotEmptyKey(mixed $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty for all values.
* @method static bool allNotEq(mixed $value1, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using == ) for all values.
* @method static bool allNotFalse(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is not false for all values.
* @method static bool allNotInArray(mixed $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices for all values.
* @method static bool allNotIsInstanceOf(mixed $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name for all values.
* @method static bool allNotNull(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is not null for all values.
Expand Down Expand Up @@ -170,6 +171,7 @@
* @method static bool nullOrNotEmpty(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is not empty or that the value is null.
* @method static bool nullOrNotEmptyKey(mixed $value, string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty or that the value is null.
* @method static bool nullOrNotEq(mixed $value1, mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using == ) or that the value is null.
* @method static bool nullOrNotFalse(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is not false or that the value is null.
* @method static bool nullOrNotInArray(mixed $value, array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices or that the value is null.
* @method static bool nullOrNotIsInstanceOf(mixed $value, string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name or that the value is null.
* @method static bool nullOrNotNull(mixed $value, string|callable $message = null, string $propertyPath = null) Assert that value is not null or that the value is null.
Expand Down Expand Up @@ -276,6 +278,7 @@ class Assertion
const INVALID_MIN_COUNT = 227;
const INVALID_MAX_COUNT = 228;
const INVALID_STRING_NOT_CONTAINS = 229;
const INVALID_NOT_FALSE = 230;

/**
* Exception to throw when an assertion failed.
Expand Down Expand Up @@ -649,6 +652,29 @@ public static function notNull($value, $message = null, $propertyPath = null)
return true;
}

/**
* Assert that value is not false.
*
* @param mixed $value
* @param string|callable|null $message
* @param string|null $propertyPath
*
* @return bool
*/
public static function notFalse($value, $message = null, $propertyPath = null)
{
if (false === $value) {
$message = \sprintf(
static::generateMessage($message ?: 'Value "%s" is false, but non false value was expected.'),
static::stringify($value)
);

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

return true;
}

/**
* Assert that value is a string.
*
Expand Down Expand Up @@ -2428,7 +2454,7 @@ public static function satisfy($value, $callback, $message = null, $propertyPath
* (using input_filter/FILTER_VALIDATE_IP).
*
* @param string $value
* @param null|int $flag
* @param int|null $flag
* @param string|callable|null $message
* @param string|null $propertyPath
*
Expand All @@ -2455,7 +2481,7 @@ public static function ip($value, $flag = null, $message = null, $propertyPath =
* (using input_filter/FILTER_VALIDATE_IP).
*
* @param string $value
* @param null|int $flag
* @param int|null $flag
* @param string|callable|null $message
* @param string|null $propertyPath
*
Expand All @@ -2475,7 +2501,7 @@ public static function ipv4($value, $flag = null, $message = null, $propertyPath
* (using input_filter/FILTER_VALIDATE_IP).
*
* @param string $value
* @param null|int $flag
* @param int|null $flag
* @param string|callable|null $message
* @param string|null $propertyPath
*
Expand Down
1 change: 1 addition & 0 deletions lib/Assert/AssertionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
* @method AssertionChain notEmpty(string|callable $message = null, string $propertyPath = null) Assert that value is not empty.
* @method AssertionChain notEmptyKey(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty.
* @method AssertionChain notEq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using == ).
* @method AssertionChain notFalse(string|callable $message = null, string $propertyPath = null) Assert that value is not false.
* @method AssertionChain notInArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices.
* @method AssertionChain notIsInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name.
* @method AssertionChain notNull(string|callable $message = null, string $propertyPath = null) Assert that value is not null.
Expand Down
1 change: 1 addition & 0 deletions lib/Assert/LazyAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
* @method LazyAssertion notEmpty(string|callable $message = null, string $propertyPath = null) Assert that value is not empty.
* @method LazyAssertion notEmptyKey(string|int $key, string|callable $message = null, string $propertyPath = null) Assert that key exists in an array/array-accessible object and its value is not empty.
* @method LazyAssertion notEq(mixed $value2, string|callable $message = null, string $propertyPath = null) Assert that two values are not equal (using == ).
* @method LazyAssertion notFalse(string|callable $message = null, string $propertyPath = null) Assert that value is not false.
* @method LazyAssertion notInArray(array $choices, string|callable $message = null, string $propertyPath = null) Assert that value is not in array of choices.
* @method LazyAssertion notIsInstanceOf(string $className, string|callable $message = null, string $propertyPath = null) Assert that value is not instance of given class-name.
* @method LazyAssertion notNull(string|callable $message = null, string $propertyPath = null) Assert that value is not null.
Expand Down
19 changes: 19 additions & 0 deletions tests/Assert/Tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,25 @@ public function testInvalidNotNull()
Assertion::notNull(null);
}

public function testNotFalse()
{
$this->assertTrue(Assertion::notFalse('1'));
$this->assertTrue(Assertion::notFalse(1));
$this->assertTrue(Assertion::notFalse(0));
$this->assertTrue(Assertion::notFalse([]));
$this->assertTrue(Assertion::notFalse(true));
$this->assertTrue(Assertion::notFalse(null));
}

/**
* @expectedException \Assert\AssertionFailedException
* @expectedExceptionCode \Assert\Assertion::INVALID_NOT_FALSE
*/
public function testInvalidNotFalse()
{
Assertion::notFalse(false);
}

public function testString()
{
$this->assertTrue(Assertion::string('test-string'));
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* obtain it through the world-wide-web, please send an email
* to [email protected] so I can send you a copy immediately.
*/

$loader = @include __DIR__.'/../vendor/autoload.php';
if (!$loader) {
die(<<<'EOT'
Expand Down