-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make LazyAssertion open for extension
- Loading branch information
1 parent
ad2c8a7
commit 49aef74
Showing
8 changed files
with
245 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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 [email protected] so I can send you a copy immediately. | ||
*/ | ||
|
||
namespace Assert\Tests; | ||
|
||
use Assert\LazyAssertionException; | ||
use Assert\Tests\Fixtures\ContextAware\Assert; | ||
use Assert\Tests\Fixtures\ContextAware\InvalidArgumentException; | ||
use Assert\Tests\Fixtures\ContextAware\LazyAssertion; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ContextAwareLazyAssertion extends TestCase | ||
{ | ||
public function testContextIsPassedThroughToExceptions() | ||
{ | ||
$context = ['method' => __FUNCTION__, 'class' => __CLASS__]; | ||
/** @var LazyAssertion $assertion */ | ||
$assertion = Assert::lazy(); | ||
$assertion->tryAll() | ||
->that(true, '', null, $context) | ||
->false() | ||
->null(); | ||
|
||
try { | ||
$assertion->verifyNow(); | ||
} catch (LazyAssertionException $e) { | ||
self::assertCount(2, $e->getErrorExceptions()); | ||
|
||
foreach ($e->getErrorExceptions() as $error) { | ||
self::assertInstanceOf(InvalidArgumentException::class, $error); | ||
self::assertSame($context, $error->getContext()); | ||
} | ||
} | ||
} | ||
} |
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,33 @@ | ||
<?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 [email protected] so I can send you a copy immediately. | ||
*/ | ||
|
||
namespace Assert\Tests\Fixtures\ContextAware; | ||
|
||
use Assert\Assert as BaseAssert; | ||
|
||
class Assert extends BaseAssert | ||
{ | ||
/** @var string */ | ||
protected static $assertionClass = Assertion::class; | ||
|
||
/** @var string */ | ||
protected static $lazyAssertionClass = LazyAssertion::class; | ||
|
||
public static function that($value, $defaultMessage = null, $defaultPropertyPath = null, array $context = []) | ||
{ | ||
$assertionChain = new AssertionChain($value, $defaultMessage, $defaultPropertyPath, $context); | ||
|
||
return $assertionChain->setAssertionClassName(static::$assertionClass); | ||
} | ||
} |
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,23 @@ | ||
<?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 [email protected] so I can send you a copy immediately. | ||
*/ | ||
|
||
namespace Assert\Tests\Fixtures\ContextAware; | ||
|
||
use Assert\Assertion as BaseAssertion; | ||
|
||
class Assertion extends BaseAssertion | ||
{ | ||
/** @var string */ | ||
protected static $exceptionClass = InvalidArgumentException::class; | ||
} |
34 changes: 34 additions & 0 deletions
34
tests/Assert/Tests/Fixtures/ContextAware/AssertionChain.php
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,34 @@ | ||
<?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 [email protected] so I can send you a copy immediately. | ||
*/ | ||
|
||
namespace Assert\Tests\Fixtures\ContextAware; | ||
|
||
use Assert\AssertionChain as BaseAssertionChain; | ||
|
||
class AssertionChain extends BaseAssertionChain | ||
{ | ||
/** @var array */ | ||
private $context; | ||
|
||
public function __construct($value, $defaultMessage = null, $defaultPropertyPath = null, array $context = []) | ||
{ | ||
parent::__construct($value, $defaultMessage, $defaultPropertyPath); | ||
$this->context = $context; | ||
} | ||
|
||
public function getContext(): array | ||
{ | ||
return $this->context; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/Assert/Tests/Fixtures/ContextAware/InvalidArgumentException.php
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,35 @@ | ||
<?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 [email protected] so I can send you a copy immediately. | ||
*/ | ||
|
||
namespace Assert\Tests\Fixtures\ContextAware; | ||
|
||
use Assert\InvalidArgumentException as BaseInvalidArgumentException; | ||
|
||
class InvalidArgumentException extends BaseInvalidArgumentException | ||
{ | ||
/** @var array */ | ||
private $context; | ||
|
||
public function getContext(): array | ||
{ | ||
return $this->context; | ||
} | ||
|
||
public function setContext(array $context): self | ||
{ | ||
$this->context = $context; | ||
|
||
return $this; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
tests/Assert/Tests/Fixtures/ContextAware/LazyAssertion.php
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,53 @@ | ||
<?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 [email protected] so I can send you a copy immediately. | ||
*/ | ||
|
||
namespace Assert\Tests\Fixtures\ContextAware; | ||
|
||
use Assert\LazyAssertion as BaseLazyAssertion; | ||
|
||
class LazyAssertion extends BaseLazyAssertion | ||
{ | ||
/** @var string The class to use as AssertionChain factory */ | ||
protected $assertClass = Assert::class; | ||
/** @var AssertionChain */ | ||
protected $currentChain; | ||
|
||
public function that($value, $propertyPath, $defaultMessage = null, array $context = []) | ||
{ | ||
$this->currentChainFailed = false; | ||
$this->thisChainTryAll = false; | ||
$assertClass = $this->assertClass; | ||
$this->currentChain = $assertClass::that($value, $defaultMessage, $propertyPath, $context); | ||
|
||
return $this; | ||
} | ||
|
||
public function __call($method, $args) | ||
{ | ||
$errorKeys = \array_keys($this->errors); | ||
|
||
parent::__call($method, $args); | ||
|
||
$additions = \array_diff(\array_keys($this->errors), $errorKeys); | ||
|
||
foreach ($additions as $addition) { | ||
$error = $this->errors[$addition]; | ||
if ($error instanceof InvalidArgumentException) { | ||
$this->errors[$addition] = $error->setContext($this->currentChain->getContext()); | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
} |