From c71490b4724672abbc716a79b6cfdb75942bdf1d Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Fri, 27 Nov 2020 21:52:44 +0100 Subject: [PATCH] feat(feedback-on-to-match): uses contextual messages inside expectations --- src/Expectation.php | 58 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/src/Expectation.php b/src/Expectation.php index 68d0790d..0946c00f 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -6,6 +6,7 @@ namespace Pest; use PHPUnit\Framework\Assert; use PHPUnit\Framework\Constraint\Constraint; +use SebastianBergmann\Exporter\Exporter; /** * @internal @@ -23,6 +24,15 @@ final class Expectation */ public $value; + /** + * The exporter instance, if any. + * + * @readonly + * + * @var Exporter|null + */ + private $exporter; + /** * Creates a new expectation. * @@ -508,8 +518,24 @@ final class Expectation */ public function toMatchArray($array): Expectation { - foreach ($array as $property => $value) { - $this->toHaveKey($property, $value); + if (is_object($this->value) && method_exists($this->value, 'toArray')) { + $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; @@ -524,7 +550,19 @@ final class Expectation public function toMatchObject($object): Expectation { 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; @@ -550,6 +588,20 @@ final class Expectation 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. *