Skip to content

Commit

Permalink
New attampt to provide context
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyvdSluijs committed Jul 4, 2020
1 parent 4487922 commit d6d2e8d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
use Assert\Tests\Fixtures\ContextAware\LazyAssertion;
use PHPUnit\Framework\TestCase;

class ContextAwareLazyAssertion extends TestCase
class ContextAwareLazyAssertionTest extends TestCase
{
public function testContextIsPassedThroughToExceptions()
{
$context = ['method' => __FUNCTION__, 'class' => __CLASS__];
/** @var LazyAssertion $assertion */
$assertion = Assert::lazy();
$assertion->tryAll()
->that(true, '', null, $context)
->that(true, '', null)
->withContext($context)
->false()
->null();

Expand Down
6 changes: 4 additions & 2 deletions tests/Assert/Tests/Fixtures/ContextAware/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
namespace Assert\Tests\Fixtures\ContextAware;

use Assert\Assert as BaseAssert;
use Assert\AssertionChain as BaseAssertionChain;


class Assert extends BaseAssert
{
Expand All @@ -24,9 +26,9 @@ class Assert extends BaseAssert
/** @var string */
protected static $lazyAssertionClass = LazyAssertion::class;

public static function that($value, $defaultMessage = null, $defaultPropertyPath = null, array $context = [])
public static function that($value, $defaultMessage = null, string $defaultPropertyPath = null): BaseAssertionChain
{
$assertionChain = new AssertionChain($value, $defaultMessage, $defaultPropertyPath, $context);
$assertionChain = new AssertionChain($value, $defaultMessage, $defaultPropertyPath);

return $assertionChain->setAssertionClassName(static::$assertionClass);
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Assert/Tests/Fixtures/ContextAware/AssertionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ class AssertionChain extends BaseAssertionChain
/** @var array */
private $context;

public function __construct($value, $defaultMessage = null, $defaultPropertyPath = null, array $context = [])
public function setContext(array $context)
{
parent::__construct($value, $defaultMessage, $defaultPropertyPath);
$this->context = $context;
}

Expand Down
15 changes: 6 additions & 9 deletions tests/Assert/Tests/Fixtures/ContextAware/LazyAssertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@

class LazyAssertion extends BaseLazyAssertion
{
/** @var string The class to use as AssertionChain factory */
protected $assertClass = Assert::class;
/** @var AssertionChain */
protected $currentChain;
public function __construct()
{
$this->assertClass = Assert::class;
}

public function that($value, $propertyPath, $defaultMessage = null, array $context = [])
public function withContext(array $context)
{
$this->currentChainFailed = false;
$this->thisChainTryAll = false;
$assertClass = $this->assertClass;
$this->currentChain = $assertClass::that($value, $defaultMessage, $propertyPath, $context);
$this->currentChain->setContext($context);

return $this;
}
Expand Down

0 comments on commit d6d2e8d

Please sign in to comment.