feat(feedback-on-to-match): uses contextual messages inside expectations

This commit is contained in:
Nuno Maduro
2020-11-27 21:52:44 +01:00
parent f82bb56d89
commit c71490b472

View File

@ -6,6 +6,7 @@ namespace Pest;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use PHPUnit\Framework\Constraint\Constraint; use PHPUnit\Framework\Constraint\Constraint;
use SebastianBergmann\Exporter\Exporter;
/** /**
* @internal * @internal
@ -23,6 +24,15 @@ final class Expectation
*/ */
public $value; public $value;
/**
* The exporter instance, if any.
*
* @readonly
*
* @var Exporter|null
*/
private $exporter;
/** /**
* Creates a new expectation. * Creates a new expectation.
* *
@ -508,8 +518,24 @@ final class Expectation
*/ */
public function toMatchArray($array): Expectation public function toMatchArray($array): Expectation
{ {
foreach ($array as $property => $value) { if (is_object($this->value) && method_exists($this->value, 'toArray')) {
$this->toHaveKey($property, $value); $valueAsArray = $this->value->toArray();
} else {
$valueAsArray = (array) $this->value;
}
foreach ($array as $key => $value) {
Assert::assertArrayHasKey($key, $valueAsArray);
Assert::assertEquals(
$value,
$valueAsArray[$key],
sprintf(
'Failed asserting that an array has a key %s with the value %s.',
$this->export($key),
$this->export($valueAsArray[$key]),
),
);
} }
return $this; return $this;
@ -524,7 +550,19 @@ final class Expectation
public function toMatchObject($object): Expectation public function toMatchObject($object): Expectation
{ {
foreach ((array) $object as $property => $value) { foreach ((array) $object as $property => $value) {
$this->toHaveProperty($property, $value); Assert::assertTrue(property_exists($this->value, $property));
/* @phpstan-ignore-next-line */
$propertyValue = $this->value->{$property};
Assert::assertEquals(
$value,
$propertyValue,
sprintf(
'Failed asserting that an object has a property %s with the value %s.',
$this->export($property),
$this->export($propertyValue),
),
);
} }
return $this; return $this;
@ -550,6 +588,20 @@ final class Expectation
return $this; return $this;
} }
/**
* Exports the given value.
*
* @param mixed $value
*/
private function export($value): string
{
if ($this->exporter === null) {
$this->exporter = new Exporter();
}
return $this->exporter->export($value);
}
/** /**
* Dynamically calls methods on the class without any arguments. * Dynamically calls methods on the class without any arguments.
* *