Skip to content

Commit

Permalink
Convert DateTime to string value (#176)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
ruudk committed Mar 22, 2020
1 parent efafa74 commit e3be5e4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Assert.php
Expand Up @@ -15,6 +15,8 @@
use BadMethodCallException;
use Closure;
use Countable;
use DateTime;
use DateTimeImmutable;
use Exception;
use InvalidArgumentException;
use ResourceBundle;
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 2 additions & 0 deletions tests/AssertTest.php
Expand Up @@ -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"'),
);
}

Expand Down

0 comments on commit e3be5e4

Please sign in to comment.