mirror of
https://github.com/pestphp/pest.git
synced 2026-03-11 10:17:23 +01:00
Adds toMatchObject
This commit is contained in:
@ -170,11 +170,20 @@ final class Expectation
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts that the value contains the property $name.
|
* 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));
|
Assert::assertTrue(property_exists($this->value, $name));
|
||||||
|
|
||||||
|
if (func_num_args() > 1) {
|
||||||
|
/* @phpstan-ignore-next-line */
|
||||||
|
Assert::assertEquals($value, $this->value->{$name});
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,6 +469,21 @@ final class Expectation
|
|||||||
return $this;
|
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.
|
* Dynamically calls methods on the class without any arguments.
|
||||||
*
|
*
|
||||||
|
|||||||
@ -2,11 +2,15 @@
|
|||||||
|
|
||||||
use PHPUnit\Framework\ExpectationFailedException;
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
$obj = new stdClass();
|
$obj = new stdClass();
|
||||||
$obj->foo = 'bar';
|
$obj->foo = 'bar';
|
||||||
|
$obj->fooNull = null;
|
||||||
|
|
||||||
test('pass', function () use ($obj) {
|
test('pass', function () use ($obj) {
|
||||||
expect($obj)->toHaveProperty('foo');
|
expect($obj)->toHaveProperty('foo');
|
||||||
|
expect($obj)->toHaveProperty('foo', 'bar');
|
||||||
|
expect($obj)->toHaveProperty('fooNull');
|
||||||
|
expect($obj)->toHaveProperty('fooNull', null);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('failures', function () use ($obj) {
|
test('failures', function () use ($obj) {
|
||||||
|
|||||||
31
tests/Expect/toMatchObject.php
Normal file
31
tests/Expect/toMatchObject.php
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->user = (object) [
|
||||||
|
'id' => 1,
|
||||||
|
'name' => 'Nuno',
|
||||||
|
'email' => 'enunomaduro@gmail.com',
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
test('pass', function () {
|
||||||
|
expect($this->user)->toMatchObject([
|
||||||
|
'name' => 'Nuno',
|
||||||
|
'email' => 'enunomaduro@gmail.com',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('failures', function () {
|
||||||
|
expect($this->user)->toMatchObject([
|
||||||
|
'name' => 'Not the same name',
|
||||||
|
'email' => 'enunomaduro@gmail.com',
|
||||||
|
]);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
|
|
||||||
|
test('not failures', function () {
|
||||||
|
expect($this->user)->not->toMatchObject([
|
||||||
|
'id' => 1,
|
||||||
|
]);
|
||||||
|
})->throws(ExpectationFailedException::class);
|
||||||
Reference in New Issue
Block a user