Skip to content

Commit

Permalink
Add a not switch on Assert class
Browse files Browse the repository at this point in the history
  • Loading branch information
Taluu committed Dec 9, 2018
1 parent e4f4a40 commit 9931983
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/Assert/AssertionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ class AssertionChain
*/
private $all = false;

/**
* Perform a negative assertion
*
* @var bool
*/
private $not = false;

/** @var string|Assertion Class to use for assertion calls */
private $assertionClassName = 'Assert\Assertion';

Expand Down Expand Up @@ -174,6 +181,10 @@ public function __call($methodName, $args)
}
}

if ($this->not) {
$methodName = 'not'.$methodName;
}

if ($this->all) {
$methodName = 'all'.$methodName;
}
Expand All @@ -195,6 +206,18 @@ public function all()
return $this;
}

/**
* Switch chain into negative mode
*
* @return \Assert\AssertionChain
*/
public function not()
{
$this->not = true;

return $this;
}

/**
* Switch chain into mode allowing nulls, ignoring further assertions.
*
Expand Down
5 changes: 5 additions & 0 deletions tests/Assert/Tests/AssertionChainFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function testThatAssertionChainFunctionsValidatesAllInputs()
$this->assertInstanceOf(AssertionChain::class, \Assert\that([1, 2, 3])->all()->integer());
}

public function testThatAssertionChainFunctionsValidateNegativeAssertions()
{
$this->assertInstanceOf(AssertionChain::class, \Assert\that('foo')->not()->isInstanceOf(\stdClass::class));
}

public function testAssertionChainFunctionsThatAllShortcut()
{
$this->assertInstanceOf(AssertionChain::class, \Assert\thatAll([1, 2, 3])->integer());
Expand Down

0 comments on commit 9931983

Please sign in to comment.