From 4331b2aaf6d9dd03e07588556a5895425a7fce82 Mon Sep 17 00:00:00 2001 From: Esteban Date: Fri, 3 Sep 2021 04:26:57 -0400 Subject: [PATCH] Add toHaveProperties --- src/Expectation.php | 14 ++++++++++++ tests/Features/Expect/toHaveProperties.php | 26 ++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 tests/Features/Expect/toHaveProperties.php diff --git a/src/Expectation.php b/src/Expectation.php index 6e5f6030..02d27c28 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -386,6 +386,20 @@ final class Expectation return $this; } + /** + * Asserts that the value contains the provided properties $names. + * + * @param array $names + */ + public function toHaveProperties(array $names): Expectation + { + foreach ($names as $name) { + $this->toHaveProperty($name); + } + + return $this; + } + /** * Asserts that two variables have the same value. * diff --git a/tests/Features/Expect/toHaveProperties.php b/tests/Features/Expect/toHaveProperties.php new file mode 100644 index 00000000..ad3da1b0 --- /dev/null +++ b/tests/Features/Expect/toHaveProperties.php @@ -0,0 +1,26 @@ +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);