From a5302480edbe50530c7cf5b31c0288a4ba7deedc Mon Sep 17 00:00:00 2001 From: Oliver Nybroe Date: Mon, 16 Aug 2021 09:22:23 +0200 Subject: [PATCH 1/3] Add a phpunit comparator for models --- .../Providers/FoundationServiceProvider.php | 2 + .../Testing/Comparators/ModelComparator.php | 31 ++++++++ .../Testing/TestingServiceProvider.php | 22 ++++++ .../Comparators/ModelComparatorTest.php | 79 +++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 src/Illuminate/Testing/Comparators/ModelComparator.php create mode 100644 src/Illuminate/Testing/TestingServiceProvider.php create mode 100644 tests/Testing/Comparators/ModelComparatorTest.php diff --git a/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php b/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php index bb69c8850456..550a5c4f7387 100644 --- a/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php +++ b/src/Illuminate/Foundation/Providers/FoundationServiceProvider.php @@ -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 @@ -20,6 +21,7 @@ class FoundationServiceProvider extends AggregateServiceProvider protected $providers = [ FormRequestServiceProvider::class, ParallelTestingServiceProvider::class, + TestingServiceProvider::class, ]; /** diff --git a/src/Illuminate/Testing/Comparators/ModelComparator.php b/src/Illuminate/Testing/Comparators/ModelComparator.php new file mode 100644 index 000000000000..c94e0934ccfe --- /dev/null +++ b/src/Illuminate/Testing/Comparators/ModelComparator.php @@ -0,0 +1,31 @@ +is($actual)) { + throw new ComparisonFailure( + $expected, + $actual, + "{$expected->getMorphClass()}::{$expected->getKey()}", + "{$actual->getMorphClass()}::{$actual->getKey()}", + ); + } + } +} diff --git a/src/Illuminate/Testing/TestingServiceProvider.php b/src/Illuminate/Testing/TestingServiceProvider.php new file mode 100644 index 000000000000..050c41559a2c --- /dev/null +++ b/src/Illuminate/Testing/TestingServiceProvider.php @@ -0,0 +1,22 @@ +register(new ModelComparator()); + } +} diff --git a/tests/Testing/Comparators/ModelComparatorTest.php b/tests/Testing/Comparators/ModelComparatorTest.php new file mode 100644 index 000000000000..f64673ef1302 --- /dev/null +++ b/tests/Testing/Comparators/ModelComparatorTest.php @@ -0,0 +1,79 @@ + 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 = []; +} From e576b17242df09996af581599c9a6d5c044386bd Mon Sep 17 00:00:00 2001 From: Oliver Nybroe Date: Mon, 16 Aug 2021 11:23:25 +0200 Subject: [PATCH 2/3] Fix styling issues --- .../Testing/Comparators/ModelComparator.php | 17 +++++++++++++++-- .../Testing/TestingServiceProvider.php | 3 +-- .../Testing/Comparators/ModelComparatorTest.php | 3 ++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Testing/Comparators/ModelComparator.php b/src/Illuminate/Testing/Comparators/ModelComparator.php index c94e0934ccfe..dea3356f9fa1 100644 --- a/src/Illuminate/Testing/Comparators/ModelComparator.php +++ b/src/Illuminate/Testing/Comparators/ModelComparator.php @@ -8,14 +8,27 @@ 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; } /** - * @param Model $expected - * @param Model $actual + * 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 { diff --git a/src/Illuminate/Testing/TestingServiceProvider.php b/src/Illuminate/Testing/TestingServiceProvider.php index 050c41559a2c..9ee199d023d4 100644 --- a/src/Illuminate/Testing/TestingServiceProvider.php +++ b/src/Illuminate/Testing/TestingServiceProvider.php @@ -2,7 +2,6 @@ namespace Illuminate\Testing; -use Illuminate\Contracts\Support\DeferrableProvider; use Illuminate\Support\ServiceProvider; use Illuminate\Testing\Comparators\ModelComparator; use SebastianBergmann\Comparator\Factory; @@ -14,7 +13,7 @@ class TestingServiceProvider extends ServiceProvider * * @return void */ - public function register() + public function register(): void { $comparatorFactory = Factory::getInstance(); $comparatorFactory->register(new ModelComparator()); diff --git a/tests/Testing/Comparators/ModelComparatorTest.php b/tests/Testing/Comparators/ModelComparatorTest.php index f64673ef1302..273f46cca867 100644 --- a/tests/Testing/Comparators/ModelComparatorTest.php +++ b/tests/Testing/Comparators/ModelComparatorTest.php @@ -74,6 +74,7 @@ public function testIsNotEqualIfDifferentConnection() } } -class TestModel extends Model { +class TestModel extends Model +{ protected $guarded = []; } From ea4aacbe0e40d364fecd3d41c150ab9672984a0f Mon Sep 17 00:00:00 2001 From: Oliver Nybroe Date: Mon, 16 Aug 2021 11:31:35 +0200 Subject: [PATCH 3/3] Fix docblock spaces --- src/Illuminate/Testing/Comparators/ModelComparator.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Testing/Comparators/ModelComparator.php b/src/Illuminate/Testing/Comparators/ModelComparator.php index dea3356f9fa1..ce4fa669b39e 100644 --- a/src/Illuminate/Testing/Comparators/ModelComparator.php +++ b/src/Illuminate/Testing/Comparators/ModelComparator.php @@ -23,11 +23,11 @@ public function accepts($expected, $actual): bool /** * 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 + * @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