diff --git a/README.md b/README.md index 43e9e464..204c9f39 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/bin/generate_method_docs.php b/bin/generate_method_docs.php index 849556e7..a5a18358 100644 --- a/bin/generate_method_docs.php +++ b/bin/generate_method_docs.php @@ -11,6 +11,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + require_once __DIR__.'/../vendor/autoload.php'; require_once __DIR__.'/MethodDocGenerator.php'; diff --git a/lib/Assert/Assertion.php b/lib/Assert/Assertion.php index 2e493a62..5a48344f 100644 --- a/lib/Assert/Assertion.php +++ b/lib/Assert/Assertion.php @@ -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. @@ -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. @@ -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. @@ -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. * @@ -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 * @@ -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 * @@ -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 * diff --git a/lib/Assert/AssertionChain.php b/lib/Assert/AssertionChain.php index 5d48377a..eae54132 100644 --- a/lib/Assert/AssertionChain.php +++ b/lib/Assert/AssertionChain.php @@ -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. diff --git a/lib/Assert/LazyAssertion.php b/lib/Assert/LazyAssertion.php index 0d780750..e2b272d0 100644 --- a/lib/Assert/LazyAssertion.php +++ b/lib/Assert/LazyAssertion.php @@ -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. diff --git a/tests/Assert/Tests/AssertTest.php b/tests/Assert/Tests/AssertTest.php index 5fa07302..dbd97d3f 100644 --- a/tests/Assert/Tests/AssertTest.php +++ b/tests/Assert/Tests/AssertTest.php @@ -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')); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index ae659cf5..3c231132 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -11,6 +11,7 @@ * obtain it through the world-wide-web, please send an email * to kontakt@beberlei.de so I can send you a copy immediately. */ + $loader = @include __DIR__.'/../vendor/autoload.php'; if (!$loader) { die(<<<'EOT'