Skip to content

Commit 01ed0bc

Browse files
committed
Add a not switch on Assert class
1 parent 54d254b commit 01ed0bc

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

lib/Assert/AssertionChain.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ class AssertionChain
142142
*/
143143
private $all = false;
144144

145+
/**
146+
* Perform a negative assertion.
147+
*
148+
* @var bool
149+
*/
150+
private $not = false;
151+
145152
/** @var string|Assertion Class to use for assertion calls */
146153
private $assertionClassName = 'Assert\Assertion';
147154

@@ -197,6 +204,10 @@ public function __call($methodName, $args): AssertionChain
197204
}
198205
}
199206

207+
if ($this->not) {
208+
$methodName = 'not'.$methodName;
209+
}
210+
200211
if ($this->all) {
201212
$methodName = 'all'.$methodName;
202213
}
@@ -218,6 +229,18 @@ public function all(): AssertionChain
218229
return $this;
219230
}
220231

232+
/**
233+
* Switch chain into negative mode.
234+
*
235+
* @return \Assert\AssertionChain
236+
*/
237+
public function not()
238+
{
239+
$this->not = true;
240+
241+
return $this;
242+
}
243+
221244
/**
222245
* Switch chain into mode allowing nulls, ignoring further assertions.
223246
*

tests/Assert/Tests/AssertionChainFunctionsTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public function testThatAssertionChainFunctionsValidatesAllInputs()
5151
$this->assertInstanceOf(AssertionChain::class, \Assert\that([1, 2, 3])->all()->integer());
5252
}
5353

54+
public function testThatAssertionChainFunctionsValidateNegativeAssertions()
55+
{
56+
$this->assertInstanceOf(AssertionChain::class, \Assert\that('foo')->not()->isInstanceOf(\stdClass::class));
57+
}
58+
5459
public function testAssertionChainFunctionsThatAllShortcut()
5560
{
5661
$this->assertInstanceOf(AssertionChain::class, \Assert\thatAll([1, 2, 3])->integer());

tests/Assert/Tests/AssertionChainTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public function testThatAssertionChainSkipAssertionsOnValidNull()
4545
$this->assertInstanceOf(AssertionChain::class, Assert::that(null)->nullOr()->integer()->eq(10));
4646
}
4747

48+
public function testThatAssertionChainDoesANegativeAssert()
49+
{
50+
$this->assertInstanceOf(AssertionChain::class, Assert::that(1)->not()->eq(10));
51+
}
52+
4853
public function testThatAssertionChainValidatesAllInputs()
4954
{
5055
$this->assertInstanceOf(AssertionChain::class, Assert::that([1, 2, 3])->all()->integer());

0 commit comments

Comments
 (0)