Adds toMatchObject

This commit is contained in:
Nuno Maduro
2020-09-15 21:53:25 +02:00
parent 9b5f664f00
commit 4e184b2f90
3 changed files with 62 additions and 3 deletions

View File

@ -170,11 +170,20 @@ final class Expectation
/**
* Asserts that the value contains the property $name.
*
* @param mixed $value
*/
public function toHaveProperty(string $name): Expectation
public function toHaveProperty(string $name, $value = null): Expectation
{
$this->toBeObject();
Assert::assertTrue(property_exists($this->value, $name));
if (func_num_args() > 1) {
/* @phpstan-ignore-next-line */
Assert::assertEquals($value, $this->value->{$name});
}
return $this;
}
@ -460,6 +469,21 @@ final class Expectation
return $this;
}
/**
* Asserts that the value object matches a subset
* of the properties of an given object.
*
* @param array<string, mixed>|object $object
*/
public function toMatchObject($object): Expectation
{
foreach ((array) $object as $property => $value) {
$this->toHaveProperty($property, $value);
}
return $this;
}
/**
* Dynamically calls methods on the class without any arguments.
*