Add toHaveProperties

This commit is contained in:
Esteban
2021-09-03 04:26:57 -04:00
parent 60c0636523
commit 4331b2aaf6
2 changed files with 40 additions and 0 deletions

View File

@ -386,6 +386,20 @@ final class Expectation
return $this;
}
/**
* Asserts that the value contains the provided properties $names.
*
* @param array<string> $names
*/
public function toHaveProperties(array $names): Expectation
{
foreach ($names as $name) {
$this->toHaveProperty($name);
}
return $this;
}
/**
* Asserts that two variables have the same value.
*

View File

@ -0,0 +1,26 @@
<?php
use PHPUnit\Framework\ExpectationFailedException;
test('pass', function () {
$object = new stdClass();
$object->name = 'Jhon';
$object->age = 21;
expect($object)->toHaveProperties(['name', 'age']);
});
test('failures', function () {
$object = new stdClass();
$object->name = 'Jhon';
expect($object)->toHaveProperties(['name', 'age']);
})->throws(ExpectationFailedException::class);
test('not failures', function () {
$object = new stdClass();
$object->name = 'Jhon';
$object->age = 21;
expect($object)->not->toHaveProperties(['name', 'age']);
})->throws(ExpectationFailedException::class);