Skip to content

Commit

Permalink
Merge pull request #61 from bbatsche/negative-integer-asserts
Browse files Browse the repository at this point in the history
Additional Negative Assertions
  • Loading branch information
bbatsche authored Dec 1, 2021
2 parents ce3c1fc + e3ead04 commit 6b4f1dc
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 10 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ verify($numericValue)->is()->lessOrEqualTo($max);
verify($numericValue)->is()->finite();
verify($numericValue)->is()->infinite();
verify($numericValue)->is()->nan();
// Note: nan() does not support negative assertions


// String Values
Expand Down Expand Up @@ -212,7 +211,6 @@ verify('ClassName')->has()->staticAttribute('attributeName');

// JSON and XML
verify($jsonValue)->is()->json();
// Note: json() does not support negative assertions

verify($jsonValue)->is()->equalToJsonString('{"json": "string"}');
verify($jsonValue)->is()->equalToJsonFile('/path/to/file.json');
Expand Down
8 changes: 4 additions & 4 deletions src/Verify.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public function finite(): self
if ($this->modifierCondition) {
a::assertFinite($this->getActualValue(), $this->description);
} else {
a::assertInfinite($this->getActualValue(), $this->description);
a::assertThat($this->getActualValue(), a::logicalOr(a::isInfinite(), a::isNan()), $this->description);
}

return $this;
Expand Down Expand Up @@ -638,7 +638,7 @@ public function infinite(): self
if ($this->modifierCondition) {
a::assertInfinite($this->getActualValue(), $this->description);
} else {
a::assertFinite($this->getActualValue(), $this->description);
a::assertThat($this->getActualValue(), a::logicalOr(a::isNan(), a::isFinite()), $this->description);
}

return $this;
Expand Down Expand Up @@ -738,7 +738,7 @@ public function json(): self
if ($this->modifierCondition) {
a::assertJson($this->getActualValue(), $this->description);
} else {
throw new BadMethodCallException(__METHOD__ . ' does not support negative condition.');
a::assertThat($this->getActualValue(), a::logicalNot(a::isJson()), $this->description);
}

return $this;
Expand Down Expand Up @@ -880,7 +880,7 @@ public function nan(): self
if ($this->modifierCondition) {
a::assertNan($this->getActualValue(), $this->description);
} else {
throw new BadMethodCallException(__METHOD__ . ' does not support negative condition.');
a::assertThat($this->getActualValue(), a::logicalOr(a::isFinite(), a::isInfinite()), $this->description);
}

return $this;
Expand Down
91 changes: 87 additions & 4 deletions test/VerifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,40 @@ public function invalidSubjects(): array
];
}

/**
* Methods that use assertThat(logicalOr()) in order to perform negative assertions.
*/
public function logicalOrMethods(): array
{
return [[
'finite',
[
'isNan' => 'is nan',
'isInfinite' => 'is infinite',
],
], [
'infinite',
[
'isNan' => 'is nan',
'isFinite' => 'is finite',
],
], [
'nan',
[
'isFinite' => 'is finite',
'isInfinite' => 'is infinite',
],
]];
}

/**
* Verify methods that cannot be used with a negative condition.
*/
public function methodsWithoutNegativeCondition(): array
{
return [
['equalToXmlStructure', new DOMElement('foo')],
['json'],
['subset'],
['nan'],
];
}

Expand Down Expand Up @@ -226,9 +250,7 @@ public function noParamMethods(): array
[false, 'callable', 'assertIsNotCallable'],
[false, 'empty', 'assertNotEmpty'],
[false, 'false', 'assertNotFalse'],
[false, 'finite', 'assertInfinite'],
[false, 'float', 'assertIsNotFloat'],
[false, 'infinite', 'assertFinite'],
[false, 'int', 'assertIsNotInt'],
[false, 'iterable', 'assertIsNotIterable'],
[false, 'null', 'assertNotNull'],
Expand Down Expand Up @@ -312,6 +334,41 @@ public function testAttribute(bool $modifierCondition, $actualValue, string $ass
$this->assertSame($this->subject, $this->subject->attribute('attribute_name'));
}

/**
* Test methods that use logicalOr in order to do inverse assertions.
*
* @param array<string, string> $methods
*
* @dataProvider logicalOrMethods
* @runInSeparateProcess
*
* @return void
*/
public function testCompositeLogicMethods(string $verifyMethod, array $methods)
{
PHPMockery::mock('BeBat\\Verify', 'method_exists')->andReturn(true);

$this->setModifierCondition(false);

foreach ($methods as $methodName => $result) {
$this->mockAssert->shouldReceive($methodName)
->with()
->once()
->andReturn($result);
}

$this->mockAssert->shouldReceive('logicalOr')
->withArgs(static function ($argument) use ($methods): bool {
return \in_array($argument, $methods, true);
})->once()
->andReturn('logical or');
$this->mockAssert->shouldReceive('assertThat')
->with('actual value', 'logical or', 'some message')
->once();

$this->assertSame($this->subject, $this->subject->{$verifyMethod}());
}

/**
* Test exception for invalid conjunction.
*
Expand Down Expand Up @@ -778,6 +835,32 @@ public function testMissingAttributeThrowsException($subject, string $exceptionM
$this->subject->attributeNamed('some_attribute')->true();
}

/**
* Test asserting that value is not JSON using assertThat(logicalNot()).
*
* @return void
*/
public function testNotJson()
{
PHPMockery::mock('BeBat\\Verify', 'method_exists')->andReturn(true);

$this->setModifierCondition(false);

$this->mockAssert->shouldReceive('isJson')
->with()
->once()
->andReturn('is json');
$this->mockAssert->shouldReceive('logicalNot')
->with('is json')
->once()
->andReturn('logical not');
$this->mockAssert->shouldReceive('assertThat')
->with('actual value', 'logical not', 'some message')
->once();

$this->assertSame($this->subject, $this->subject->json());
}

/**
* Test validation of subject when trying to read an attribute.
*
Expand Down

0 comments on commit 6b4f1dc

Please sign in to comment.