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

Commit

Permalink
Merge pull request #34 from elbgoods/bugfix/compare-model-datetime
Browse files Browse the repository at this point in the history
allow to compare model date-time attributes
  • Loading branch information
Gummibeer authored Sep 14, 2020
2 parents 59faf28 + d2b1e1b commit 3cbe200
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 3cbe200

Please sign in to comment.