Skip to content

Commit 9931983

Browse files
committed
Add a not switch on Assert class
1 parent e4f4a40 commit 9931983

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/Assert/AssertionChain.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ class AssertionChain
126126
*/
127127
private $all = false;
128128

129+
/**
130+
* Perform a negative assertion
131+
*
132+
* @var bool
133+
*/
134+
private $not = false;
135+
129136
/** @var string|Assertion Class to use for assertion calls */
130137
private $assertionClassName = 'Assert\Assertion';
131138

@@ -174,6 +181,10 @@ public function __call($methodName, $args)
174181
}
175182
}
176183

184+
if ($this->not) {
185+
$methodName = 'not'.$methodName;
186+
}
187+
177188
if ($this->all) {
178189
$methodName = 'all'.$methodName;
179190
}
@@ -195,6 +206,18 @@ public function all()
195206
return $this;
196207
}
197208

209+
/**
210+
* Switch chain into negative mode
211+
*
212+
* @return \Assert\AssertionChain
213+
*/
214+
public function not()
215+
{
216+
$this->not = true;
217+
218+
return $this;
219+
}
220+
198221
/**
199222
* Switch chain into mode allowing nulls, ignoring further assertions.
200223
*

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());

0 commit comments

Comments
 (0)