Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a not switch on Assert class #265

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/Assert/AssertionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,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 @@ -197,6 +204,10 @@ public function __call($methodName, $args): AssertionChain
}
}

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

if ($this->all) {
$methodName = 'all'.$methodName;
}
Expand All @@ -218,6 +229,18 @@ public function all(): AssertionChain
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
5 changes: 5 additions & 0 deletions tests/Assert/Tests/AssertionChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public function testThatAssertionChainSkipAssertionsOnValidNull()
$this->assertInstanceOf(AssertionChain::class, Assert::that(null)->nullOr()->integer()->eq(10));
}

public function testThatAssertionChainDoesANegativeAssert()
{
$this->assertInstanceOf(AssertionChain::class, Assert::that(1)->not()->eq(10));
}

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