Skip to content

Commit a530248

Browse files
author
Oliver Nybroe
committed
Add a phpunit comparator for models
1 parent f95f3af commit a530248

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

src/Illuminate/Foundation/Providers/FoundationServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\URL;
99
use Illuminate\Testing\LoggedExceptionCollection;
1010
use Illuminate\Testing\ParallelTestingServiceProvider;
11+
use Illuminate\Testing\TestingServiceProvider;
1112
use Illuminate\Validation\ValidationException;
1213

1314
class FoundationServiceProvider extends AggregateServiceProvider
@@ -20,6 +21,7 @@ class FoundationServiceProvider extends AggregateServiceProvider
2021
protected $providers = [
2122
FormRequestServiceProvider::class,
2223
ParallelTestingServiceProvider::class,
24+
TestingServiceProvider::class,
2325
];
2426

2527
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Illuminate\Testing\Comparators;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use SebastianBergmann\Comparator\Comparator;
7+
use SebastianBergmann\Comparator\ComparisonFailure;
8+
9+
class ModelComparator extends Comparator
10+
{
11+
public function accepts($expected, $actual): bool
12+
{
13+
return $expected instanceof Model && $actual instanceof Model;
14+
}
15+
16+
/**
17+
* @param Model $expected
18+
* @param Model $actual
19+
*/
20+
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false): void
21+
{
22+
if (! $expected->is($actual)) {
23+
throw new ComparisonFailure(
24+
$expected,
25+
$actual,
26+
"{$expected->getMorphClass()}::{$expected->getKey()}",
27+
"{$actual->getMorphClass()}::{$actual->getKey()}",
28+
);
29+
}
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Illuminate\Testing;
4+
5+
use Illuminate\Contracts\Support\DeferrableProvider;
6+
use Illuminate\Support\ServiceProvider;
7+
use Illuminate\Testing\Comparators\ModelComparator;
8+
use SebastianBergmann\Comparator\Factory;
9+
10+
class TestingServiceProvider extends ServiceProvider
11+
{
12+
/**
13+
* Register the service provider.
14+
*
15+
* @return void
16+
*/
17+
public function register()
18+
{
19+
$comparatorFactory = Factory::getInstance();
20+
$comparatorFactory->register(new ModelComparator());
21+
}
22+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Testing\Comparators;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Orchestra\Testbench\TestCase;
7+
8+
class ModelComparatorTest extends TestCase
9+
{
10+
public function testIsEqual()
11+
{
12+
$modelA = new TestModel([
13+
'id' => 100,
14+
]);
15+
$modelB = new TestModel([
16+
'id' => 100,
17+
]);
18+
19+
$this->assertEquals($modelA, $modelB);
20+
}
21+
22+
public function testIgnoresProperties()
23+
{
24+
$modelA = new TestModel([
25+
'id' => 100,
26+
'text' => 'good',
27+
]);
28+
$modelB = new TestModel([
29+
'id' => 100,
30+
'text' => 'bad',
31+
]);
32+
33+
$this->assertEquals($modelA, $modelB);
34+
}
35+
36+
public function testIsNotEqualIfDifferentKey()
37+
{
38+
$modelA = new TestModel([
39+
'id' => 100,
40+
]);
41+
$modelB = new TestModel([
42+
'id' => 200,
43+
]);
44+
45+
$this->assertNotEquals($modelA, $modelB);
46+
}
47+
48+
public function testIsNotEqualIfDifferentTable()
49+
{
50+
$modelA = new TestModel([
51+
'id' => 100,
52+
]);
53+
$modelA->setTable('table_a');
54+
$modelB = new TestModel([
55+
'id' => 100,
56+
]);
57+
$modelB->setTable('table_b');
58+
59+
$this->assertNotEquals($modelA, $modelB);
60+
}
61+
62+
public function testIsNotEqualIfDifferentConnection()
63+
{
64+
$modelA = new TestModel([
65+
'id' => 100,
66+
]);
67+
$modelA->setConnection('good');
68+
$modelB = new TestModel([
69+
'id' => 100,
70+
]);
71+
$modelA->setConnection('bad');
72+
73+
$this->assertNotEquals($modelA, $modelB);
74+
}
75+
}
76+
77+
class TestModel extends Model {
78+
protected $guarded = [];
79+
}

0 commit comments

Comments
 (0)