Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
allow to compare model date-time attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gummibeer committed Sep 14, 2020
1 parent 59faf28 commit d2b1e1b
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/PHPUnit/Assertions/ModelAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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."
);
}
}
}

Expand Down

0 comments on commit d2b1e1b

Please sign in to comment.