Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\URL;
use Illuminate\Testing\LoggedExceptionCollection;
use Illuminate\Testing\ParallelTestingServiceProvider;
use Illuminate\Testing\TestingServiceProvider;
use Illuminate\Validation\ValidationException;

class FoundationServiceProvider extends AggregateServiceProvider
Expand All @@ -20,6 +21,7 @@ class FoundationServiceProvider extends AggregateServiceProvider
protected $providers = [
FormRequestServiceProvider::class,
ParallelTestingServiceProvider::class,
TestingServiceProvider::class,
];

/**
Expand Down
44 changes: 44 additions & 0 deletions src/Illuminate/Testing/Comparators/ModelComparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Illuminate\Testing\Comparators;

use Illuminate\Database\Eloquent\Model;
use SebastianBergmann\Comparator\Comparator;
use SebastianBergmann\Comparator\ComparisonFailure;

class ModelComparator extends Comparator
{
/**
* Checks if the two values are allowed to be compared with this comparator.
*
* @param mixed $expected
* @param mixed $actual
* @return bool
*/
public function accepts($expected, $actual): bool
{
return $expected instanceof Model && $actual instanceof Model;
}

/**
* Asserts that expected and actual are the same model.
*
* @param \Illuminate\Database\Eloquent\Model $expected
* @param \Illuminate\Database\Eloquent\Model $actual
* @param float $delta
* @param bool $canonicalize
* @param bool $ignoreCase
* @return void
*/
public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = false, $ignoreCase = false): void
{
if (! $expected->is($actual)) {
throw new ComparisonFailure(
$expected,
$actual,
"{$expected->getMorphClass()}::{$expected->getKey()}",
"{$actual->getMorphClass()}::{$actual->getKey()}",
);
}
}
}
21 changes: 21 additions & 0 deletions src/Illuminate/Testing/TestingServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Illuminate\Testing;

use Illuminate\Support\ServiceProvider;
use Illuminate\Testing\Comparators\ModelComparator;
use SebastianBergmann\Comparator\Factory;

class TestingServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register(): void
{
$comparatorFactory = Factory::getInstance();
$comparatorFactory->register(new ModelComparator());
}
}
80 changes: 80 additions & 0 deletions tests/Testing/Comparators/ModelComparatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Illuminate\Tests\Testing\Comparators;

use Illuminate\Database\Eloquent\Model;
use Orchestra\Testbench\TestCase;

class ModelComparatorTest extends TestCase
{
public function testIsEqual()
{
$modelA = new TestModel([
'id' => 100,
]);
$modelB = new TestModel([
'id' => 100,
]);

$this->assertEquals($modelA, $modelB);
}

public function testIgnoresProperties()
{
$modelA = new TestModel([
'id' => 100,
'text' => 'good',
]);
$modelB = new TestModel([
'id' => 100,
'text' => 'bad',
]);

$this->assertEquals($modelA, $modelB);
}

public function testIsNotEqualIfDifferentKey()
{
$modelA = new TestModel([
'id' => 100,
]);
$modelB = new TestModel([
'id' => 200,
]);

$this->assertNotEquals($modelA, $modelB);
}

public function testIsNotEqualIfDifferentTable()
{
$modelA = new TestModel([
'id' => 100,
]);
$modelA->setTable('table_a');
$modelB = new TestModel([
'id' => 100,
]);
$modelB->setTable('table_b');

$this->assertNotEquals($modelA, $modelB);
}

public function testIsNotEqualIfDifferentConnection()
{
$modelA = new TestModel([
'id' => 100,
]);
$modelA->setConnection('good');
$modelB = new TestModel([
'id' => 100,
]);
$modelA->setConnection('bad');

$this->assertNotEquals($modelA, $modelB);
}
}

class TestModel extends Model
{
protected $guarded = [];
}