From e3be5e4bf4b71251450346acbbba544bf7518f21 Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Sun, 22 Mar 2020 11:18:41 +0100 Subject: [PATCH] Convert DateTime to string value (#176) * Convert DateTime to string value Instead of showing `Expected a value less than DateTime. Got: DateTime` it will now show `Expected a value less than DateTime: "1999-01-01T00:00:00+00:00". Got: DateTime: "2020-01-01T00:00:00+00:00" ` which is much easier to understand. --- src/Assert.php | 6 ++++++ tests/AssertTest.php | 2 ++ 2 files changed, 8 insertions(+) diff --git a/src/Assert.php b/src/Assert.php index cf0548a5..337349ba 100644 --- a/src/Assert.php +++ b/src/Assert.php @@ -15,6 +15,8 @@ use BadMethodCallException; use Closure; use Countable; +use DateTime; +use DateTimeImmutable; use Exception; use InvalidArgumentException; use ResourceBundle; @@ -2099,6 +2101,10 @@ protected static function valueToString($value) return \get_class($value).': '.self::valueToString($value->__toString()); } + if ($value instanceof DateTime || $value instanceof DateTimeImmutable) { + return \get_class($value).': '.self::valueToString($value->format('c')); + } + return \get_class($value); } diff --git a/tests/AssertTest.php b/tests/AssertTest.php index ee850229..a12a846c 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -677,6 +677,8 @@ public function getStringConversions() array('eq', array(array(1), array(2)), 'Expected a value equal to array. Got: array'), array('eq', array(new ArrayIterator(array()), new stdClass()), 'Expected a value equal to stdClass. Got: ArrayIterator'), array('eq', array(1, self::getResource()), 'Expected a value equal to resource. Got: 1'), + + array('lessThan', array(new \DateTime('2020-01-01 00:00:00'), new \DateTime('1999-01-01 00:00:00')), 'Expected a value less than DateTime: "1999-01-01T00:00:00+00:00". Got: DateTime: "2020-01-01T00:00:00+00:00"'), ); }