-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use static factory methods instead of functions in the Assert namespa…
…ce (#184) * Add chaining builder factory as a static class The namespaced methods can not be autoloaded and make customizing assertion and/or exception classes impossible. * Replace references to Assert functions with methods in the Assert class * Allow LazyAssertion to use an alternative exception class * Allow an alternative lazy assertion exception class to be set in a overridden Assert class * Allow AssertionChain to use an alternative Assertion class * Allow an alternative Assertion class to be set in a overridden Assert class * Add tests for custom assertion and exception classes * Ignore AssertionChain::setAssertionClassName when generating docs * Use old setExpectedException method to support builds on PHP < 5.6 * Replace ClassName::class by the FQN in a string literal to support PHP < 5.5 * Convert arrays to traditional syntax to support PHP < 5.4 * Remove class member access on instantiation to support PHP < 5.4 * Reinstate tests for deprecated assertion chain functions * Adjust documentation to reflect both the old and new fluent interfaces * Add psr-4 dev autoload root so test helper classes will be autoloaded * Throw separate exception for non-string class name argument * Move CustomLazyAssertionException to its own file * Add tests for LazyAssertion::setExceptionClass * Move CustomAssertion class to its own file * Throw separate exception for non-string class name argument * Add tests for AssertionChain::setAssertionClassName * Add tests for custom lazy assertion exceptions in try all situations * Use traditional array syntax * Update AssertionChainTest.php Use PHP5.3 compatible array() syntax * Use traditional array syntax
- Loading branch information
Showing
14 changed files
with
558 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
/** | ||
* Assert | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this package in the file LICENSE.txt. | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to kontakt@beberlei.de so I can send you a copy immediately. | ||
*/ | ||
|
||
namespace Assert; | ||
|
||
/** | ||
* AssertionChain factory | ||
*/ | ||
abstract class Assert | ||
{ | ||
/** @var string */ | ||
protected static $lazyAssertionExceptionClass = 'Assert\LazyAssertionException'; | ||
|
||
/** @var string */ | ||
protected static $assertionClass = 'Assert\Assertion'; | ||
|
||
/** | ||
* Start validation on a value, returns {@link AssertionChain} | ||
* | ||
* The invocation of this method starts an assertion chain | ||
* that is happening on the passed value. | ||
* | ||
* @example | ||
* | ||
* Assert::that($value)->notEmpty()->integer(); | ||
* Assert::that($value)->nullOr()->string()->startsWith("Foo"); | ||
* | ||
* The assertion chain can be stateful, that means be careful when you reuse | ||
* it. You should never pass around the chain. | ||
* | ||
* @param mixed $value | ||
* @param string $defaultMessage | ||
* @param string $defaultPropertyPath | ||
* | ||
* @return \Assert\AssertionChain | ||
*/ | ||
public static function that($value, $defaultMessage = null, $defaultPropertyPath = null) | ||
{ | ||
$assertionChain = new AssertionChain($value, $defaultMessage, $defaultPropertyPath); | ||
return $assertionChain | ||
->setAssertionClassName(static::$assertionClass) | ||
; | ||
} | ||
|
||
/** | ||
* Start validation on a set of values, returns {@link AssertionChain} | ||
* | ||
* @param mixed $values | ||
* @param string $defaultMessage | ||
* @param string $defaultPropertyPath | ||
* | ||
* @return \Assert\AssertionChain | ||
*/ | ||
public static function thatAll($values, $defaultMessage = null, $defaultPropertyPath = null) | ||
{ | ||
return static::that($values, $defaultMessage, $defaultPropertyPath)->all(); | ||
} | ||
|
||
/** | ||
* Start validation and allow NULL, returns {@link AssertionChain} | ||
* | ||
* @param mixed $value | ||
* @param string $defaultMessage | ||
* @param string $defaultPropertyPath | ||
* | ||
* @return \Assert\AssertionChain | ||
*/ | ||
public static function thatNullOr($value, $defaultMessage = null, $defaultPropertyPath = null) | ||
{ | ||
return static::that($value, $defaultMessage, $defaultPropertyPath)->nullOr(); | ||
} | ||
|
||
/** | ||
* Create a lazy assertion object. | ||
* | ||
* @return \Assert\LazyAssertion | ||
*/ | ||
public static function lazy() | ||
{ | ||
$lazyAssertion = new LazyAssertion(); | ||
return $lazyAssertion | ||
->setExceptionClass(static::$lazyAssertionExceptionClass) | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.