From d2b1e1b59374765b4756d2e5ba3d0dc52ae5075e Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 14 Sep 2020 11:12:57 +0200 Subject: [PATCH] allow to compare model date-time attributes --- src/PHPUnit/Assertions/ModelAssertions.php | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/PHPUnit/Assertions/ModelAssertions.php b/src/PHPUnit/Assertions/ModelAssertions.php index 824e7cf..7b228c8 100644 --- a/src/PHPUnit/Assertions/ModelAssertions.php +++ b/src/PHPUnit/Assertions/ModelAssertions.php @@ -2,6 +2,8 @@ namespace Elbgoods\CiTestTools\PHPUnit\Assertions; +use Carbon\Carbon; +use DateTimeInterface; use Illuminate\Database\Eloquent\Model; use PHPUnit\Framework\Assert as PHPUnit; @@ -23,11 +25,20 @@ public static function assertEqualsModel(Model $expected, $actual, ?string $mess } foreach (array_diff($expected->getFillable(), $expected->getHidden()) as $attribute) { - PHPUnit::assertEquals( - $expected->getAttribute($attribute), - data_get($actual, $attribute), - $message ?? "Failed to assert that attribute \"{$attribute}\" equals expected value." - ); + $expectedValue = $expected->getAttribute($attribute); + + if ($expectedValue instanceof DateTimeInterface) { + PHPUnit::assertTrue( + Carbon::instance($expectedValue)->isSameAs(Carbon::ISO8601, data_get($actual, $attribute)), + $message ?? "Failed to assert that attribute \"{$attribute}\" equals expected value." + ); + } else { + PHPUnit::assertEquals( + $expectedValue, + data_get($actual, $attribute), + $message ?? "Failed to assert that attribute \"{$attribute}\" equals expected value." + ); + } } }