-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEloquentModelComparator.php
94 lines (81 loc) · 3.07 KB
/
EloquentModelComparator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php declare(strict_types = 1);
namespace LastDragon_ru\LaraASP\Testing\Comparators;
use Illuminate\Database\Eloquent\Model;
use Override;
use SebastianBergmann\Comparator\ComparisonFailure;
use SebastianBergmann\Comparator\ObjectComparator;
use function substr_replace;
/**
* Compares two Eloquent Models.
*
* The problem is models after creating from the factory and selecting from
* the database may have different types for the same properties. For example,
* `factory()->create()` will set `key` as `int`, but `select` will set it to
* `string` and (strict) comparison will fail. This comparator normalizes
* properties types before comparison.
*
* @see https://github.com/laravel/ideas/issues/1914
*/
class EloquentModelComparator extends ObjectComparator {
#[Override]
public function accepts(mixed $expected, mixed $actual): bool {
return $expected instanceof Model
&& $actual instanceof Model;
}
/**
* @param array<array-key, mixed> $processed
*/
#[Override]
public function assertEquals(
mixed $expected,
mixed $actual,
float $delta = 0.0,
bool $canonicalize = false,
bool $ignoreCase = false,
array &$processed = [],
): void {
// If classes different we just call parent to fail
if (!($actual instanceof Model) || !($expected instanceof Model) || $actual::class !== $expected::class) {
parent::assertEquals($expected, $actual, $delta, $canonicalize, $ignoreCase, $processed);
return;
}
// Normalize properties and compare
$normalizedExpected = $this->normalize($expected);
$normalizedActual = $this->normalize($actual);
try {
parent::assertEquals(
$normalizedExpected,
$normalizedActual,
$delta,
$canonicalize,
$ignoreCase,
$processed,
);
} catch (ComparisonFailure $e) {
throw new ComparisonFailure(
expected : $expected,
actual : $actual,
expectedAsString: substr_replace($e->getExpectedAsString(), $expected::class.' Model', 0, 5),
actualAsString : substr_replace($e->getActualAsString(), $actual::class.' Model', 0, 5),
message : 'Failed asserting that two models are equal.',
);
}
}
protected function normalize(Model $model): Model {
// We don't want update original model
$model = clone $model;
// Normalize attributes
foreach ($model->getAttributes() as $key => $value) {
// Just for the ... phpstan.
$key = (string) $key;
// This will force Laravel to convert properties into the right types.
$model->setAttribute($key, $model->getAttribute($key));
// We also need sync `original`
if ($model->isClean($key)) {
$model->syncOriginalAttribute($key);
}
}
// Return
return $model;
}
}