From 2f0045c3e6ee6abf5cc637a2802a0928a33c8d41 Mon Sep 17 00:00:00 2001 From: Aidan Casey <6686277+aidan-casey@users.noreply.github.com> Date: Sat, 16 Jan 2021 18:13:06 +0100 Subject: [PATCH 1/6] add model assertion that two models are related --- src/Laravel/ModelAssertions.php | 12 +++++++ tests/Laravel/ModelAssertionsTest.php | 51 ++++++++++++++++----------- tests/Laravel/Models/Comment.php | 30 ++++++++++++++++ tests/Laravel/Models/Post.php | 28 +++++++++++++++ 4 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 tests/Laravel/Models/Comment.php create mode 100644 tests/Laravel/Models/Post.php diff --git a/src/Laravel/ModelAssertions.php b/src/Laravel/ModelAssertions.php index 160408a..f614bc3 100644 --- a/src/Laravel/ModelAssertions.php +++ b/src/Laravel/ModelAssertions.php @@ -3,6 +3,8 @@ namespace Astrotomic\PhpunitAssertions\Laravel; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Facades\DB; use Illuminate\Testing\Constraints\HasInDatabase; use PHPUnit\Framework\Assert as PHPUnit; @@ -33,4 +35,14 @@ public static function assertSame(Model $expected, $actual): void PHPUnit::assertSame($expected->exists, $actual->exists); PHPUnit::assertTrue($expected->is($actual)); } + + public static function assertRelated(Model $model, $actual, string $relation) + { + PHPUnit::assertInstanceOf(Model::class, $actual); + PHPUnit::assertTrue(method_exists($model, $relation)); + PHPUnit::assertInstanceOf(Relation::class, $model->$relation()); + + $related = $model->$relation()->whereKey($actual->getKey())->first(); + self::assertSame($actual, $related); + } } diff --git a/tests/Laravel/ModelAssertionsTest.php b/tests/Laravel/ModelAssertionsTest.php index 6130adf..b4af5c1 100644 --- a/tests/Laravel/ModelAssertionsTest.php +++ b/tests/Laravel/ModelAssertionsTest.php @@ -3,7 +3,10 @@ namespace Astrotomic\PhpunitAssertions\Tests\Laravel; use Astrotomic\PhpunitAssertions\Laravel\ModelAssertions; +use Astrotomic\PhpunitAssertions\Tests\Laravel\Models\Comment; +use Astrotomic\PhpunitAssertions\Tests\Laravel\Models\Post; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; @@ -13,11 +16,8 @@ protected function setUp(): void { parent::setUp(); - Schema::create('posts', static function (Blueprint $table): void { - $table->increments('id'); - $table->string('title'); - $table->timestamps(); - }); + Post::migrate(); + Comment::migrate(); } /** @@ -26,15 +26,11 @@ protected function setUp(): void */ public function it_can_validate_exists(): void { - $model = new class extends Model { - protected $table = 'posts'; - protected $guarded = []; - }; + $post = Post::create([ + 'title' => self::randomString(), + ]); - $model->title = self::randomString(); - $model->save(); - - ModelAssertions::assertExists($model); + ModelAssertions::assertExists($post); } /** @@ -43,14 +39,29 @@ public function it_can_validate_exists(): void */ public function it_can_validate_same(): void { - $model = new class extends Model { - protected $table = 'posts'; - protected $guarded = []; - }; + $post = Post::create([ + 'title' => self::randomString(), + ]); + + ModelAssertions::assertSame($post, Post::first()); + } + + /** + * @test + * @dataProvider hundredTimes + */ + public function it_can_validate_related(): void + { + $post = Post::create([ + 'title' => self::randomString(), + ]); - $model->title = self::randomString(); - $model->save(); + $comment = Comment::create([ + 'message' => self::randomString(), + 'post_id' => $post->getKey(), + ]); - ModelAssertions::assertSame($model, $model->query()->first()); + ModelAssertions::assertRelated($post, $comment, 'comments'); + ModelAssertions::assertRelated($comment, $post, 'post'); } } diff --git a/tests/Laravel/Models/Comment.php b/tests/Laravel/Models/Comment.php new file mode 100644 index 0000000..de4cf12 --- /dev/null +++ b/tests/Laravel/Models/Comment.php @@ -0,0 +1,30 @@ +table, static function (Blueprint $table): void { + $table->increments('id'); + $table->text('message'); + $table->foreignId('post_id')->constrained('posts'); + $table->timestamps(); + }); + } + + public function post(): BelongsTo + { + return $this->belongsTo(Post::class); + } +} \ No newline at end of file diff --git a/tests/Laravel/Models/Post.php b/tests/Laravel/Models/Post.php new file mode 100644 index 0000000..be4c2b0 --- /dev/null +++ b/tests/Laravel/Models/Post.php @@ -0,0 +1,28 @@ +table, static function (Blueprint $table): void { + $table->increments('id'); + $table->string('title'); + $table->timestamps(); + }); + } + + public function comments(): HasMany + { + return $this->hasMany(Comment::class); + } +} \ No newline at end of file From de15e8ba4b47db349907ea5cdd770ebf9419d887 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Sat, 16 Jan 2021 18:14:10 +0100 Subject: [PATCH 2/6] fix php cs --- src/Laravel/ModelAssertions.php | 1 - tests/Laravel/ModelAssertionsTest.php | 4 ---- tests/Laravel/Models/Comment.php | 3 +-- tests/Laravel/Models/Post.php | 2 +- 4 files changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Laravel/ModelAssertions.php b/src/Laravel/ModelAssertions.php index f614bc3..a69abfd 100644 --- a/src/Laravel/ModelAssertions.php +++ b/src/Laravel/ModelAssertions.php @@ -3,7 +3,6 @@ namespace Astrotomic\PhpunitAssertions\Laravel; use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Facades\DB; use Illuminate\Testing\Constraints\HasInDatabase; diff --git a/tests/Laravel/ModelAssertionsTest.php b/tests/Laravel/ModelAssertionsTest.php index b4af5c1..95be2d3 100644 --- a/tests/Laravel/ModelAssertionsTest.php +++ b/tests/Laravel/ModelAssertionsTest.php @@ -5,10 +5,6 @@ use Astrotomic\PhpunitAssertions\Laravel\ModelAssertions; use Astrotomic\PhpunitAssertions\Tests\Laravel\Models\Comment; use Astrotomic\PhpunitAssertions\Tests\Laravel\Models\Post; -use Illuminate\Database\Eloquent\Model; -use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Database\Schema\Blueprint; -use Illuminate\Support\Facades\Schema; final class ModelAssertionsTest extends TestCase { diff --git a/tests/Laravel/Models/Comment.php b/tests/Laravel/Models/Comment.php index de4cf12..27ba3eb 100644 --- a/tests/Laravel/Models/Comment.php +++ b/tests/Laravel/Models/Comment.php @@ -4,7 +4,6 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; -use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; @@ -27,4 +26,4 @@ public function post(): BelongsTo { return $this->belongsTo(Post::class); } -} \ No newline at end of file +} diff --git a/tests/Laravel/Models/Post.php b/tests/Laravel/Models/Post.php index be4c2b0..bb9fdbb 100644 --- a/tests/Laravel/Models/Post.php +++ b/tests/Laravel/Models/Post.php @@ -25,4 +25,4 @@ public function comments(): HasMany { return $this->hasMany(Comment::class); } -} \ No newline at end of file +} From 4d96c3f61edd6d3579a4a90cf4dd6aa394828b34 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 2 Mar 2021 13:47:20 +0100 Subject: [PATCH 3/6] add assertRelationship assertion --- README.md | 2 ++ src/Laravel/ModelAssertions.php | 42 ++++++++++++++++++++++++++- tests/Laravel/ModelAssertionsTest.php | 30 +++++++++++++++++-- 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b74a423..e60cb1c 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,8 @@ This will prevent any method name conflicts with core, your custom or other trai ```php \Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertExists($model); \Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertSame($model, \App\Models\User::first()); +\Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertRelated($post, $comment, 'comments'); +\Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertRelationship($post, \App\Models\Comment::class, 'comments', \Illuminate\Database\Eloquent\Relations\HasMany::class); ``` ### Blade diff --git a/src/Laravel/ModelAssertions.php b/src/Laravel/ModelAssertions.php index a69abfd..bcb6746 100644 --- a/src/Laravel/ModelAssertions.php +++ b/src/Laravel/ModelAssertions.php @@ -10,6 +10,11 @@ trait ModelAssertions { + /** + * @param string|\Illuminate\Database\Eloquent\Model $table + * @param array $data + * @param string|null $connection + */ public static function assertExists($table, array $data = [], ?string $connection = null): void { if ($table instanceof Model) { @@ -28,6 +33,10 @@ public static function assertExists($table, array $data = [], ?string $connectio ); } + /** + * @param \Illuminate\Database\Eloquent\Model $expected + * @param \Illuminate\Database\Eloquent\Model|mixed $actual + */ public static function assertSame(Model $expected, $actual): void { PHPUnit::assertInstanceOf(get_class($expected), $actual); @@ -35,7 +44,12 @@ public static function assertSame(Model $expected, $actual): void PHPUnit::assertTrue($expected->is($actual)); } - public static function assertRelated(Model $model, $actual, string $relation) + /** + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $relation + * @param \Illuminate\Database\Eloquent\Model|mixed $actual + */ + public static function assertRelated(Model $model, string $relation, $actual) { PHPUnit::assertInstanceOf(Model::class, $actual); PHPUnit::assertTrue(method_exists($model, $relation)); @@ -44,4 +58,30 @@ public static function assertRelated(Model $model, $actual, string $relation) $related = $model->$relation()->whereKey($actual->getKey())->first(); self::assertSame($actual, $related); } + + /** + * @param \Illuminate\Database\Eloquent\Model $model + * @param string $relation + * @param string|\Illuminate\Database\Eloquent\Model|mixed $actual + * @param string|null $type + */ + public static function assertRelationship(Model $model, string $relation, $actual, ?string $type = null) + { + PHPUnit::assertTrue(method_exists($model, $relation)); + PHPUnit::assertInstanceOf(Relation::class, $model->$relation()); + + if($type) { + PHPUnit::assertInstanceOf($type, $model->$relation()); + } + + $related = $model->$relation()->getRelated(); + PHPUnit::assertInstanceOf(Model::class, $related); + + if(is_string($actual)) { + PHPUnit::assertInstanceOf($actual, $related); + } else { + PHPUnit::assertInstanceOf(get_class($actual), $related); + self::assertRelated($model, $relation, $actual); + } + } } diff --git a/tests/Laravel/ModelAssertionsTest.php b/tests/Laravel/ModelAssertionsTest.php index 95be2d3..4cfa820 100644 --- a/tests/Laravel/ModelAssertionsTest.php +++ b/tests/Laravel/ModelAssertionsTest.php @@ -5,6 +5,8 @@ use Astrotomic\PhpunitAssertions\Laravel\ModelAssertions; use Astrotomic\PhpunitAssertions\Tests\Laravel\Models\Comment; use Astrotomic\PhpunitAssertions\Tests\Laravel\Models\Post; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; final class ModelAssertionsTest extends TestCase { @@ -57,7 +59,31 @@ public function it_can_validate_related(): void 'post_id' => $post->getKey(), ]); - ModelAssertions::assertRelated($post, $comment, 'comments'); - ModelAssertions::assertRelated($comment, $post, 'post'); + ModelAssertions::assertRelated($post, 'comments', $comment); + ModelAssertions::assertRelated($comment, 'post', $post); + } + + /** + * @test + * @dataProvider hundredTimes + */ + public function it_can_validate_relationship(): void + { + $post = Post::create([ + 'title' => self::randomString(), + ]); + + $comment = Comment::create([ + 'message' => self::randomString(), + 'post_id' => $post->getKey(), + ]); + + ModelAssertions::assertRelationship($post, 'comments', Comment::class); + ModelAssertions::assertRelationship($post, 'comments', Comment::class, HasMany::class); + ModelAssertions::assertRelationship($post, 'comments', $comment, HasMany::class); + + ModelAssertions::assertRelationship($comment, 'post', Post::class); + ModelAssertions::assertRelationship($comment, 'post', Post::class, BelongsTo::class); + ModelAssertions::assertRelationship($comment, 'post', $post, BelongsTo::class); } } From 11f4b0b2ba73b1846fb5de2ba83eccc8033d7a82 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 2 Mar 2021 13:47:49 +0100 Subject: [PATCH 4/6] fix php cs --- src/Laravel/ModelAssertions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Laravel/ModelAssertions.php b/src/Laravel/ModelAssertions.php index bcb6746..4ec2327 100644 --- a/src/Laravel/ModelAssertions.php +++ b/src/Laravel/ModelAssertions.php @@ -70,14 +70,14 @@ public static function assertRelationship(Model $model, string $relation, $actua PHPUnit::assertTrue(method_exists($model, $relation)); PHPUnit::assertInstanceOf(Relation::class, $model->$relation()); - if($type) { + if ($type) { PHPUnit::assertInstanceOf($type, $model->$relation()); } $related = $model->$relation()->getRelated(); PHPUnit::assertInstanceOf(Model::class, $related); - if(is_string($actual)) { + if (is_string($actual)) { PHPUnit::assertInstanceOf($actual, $related); } else { PHPUnit::assertInstanceOf(get_class($actual), $related); From cf247152300adf41ca3aab513c7cc6ddf68778db Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 2 Mar 2021 14:16:12 +0100 Subject: [PATCH 5/6] merge both new assertions to assertRelated --- README.md | 2 +- src/Laravel/ModelAssertions.php | 22 +++++-------------- tests/Laravel/ModelAssertionsTest.php | 31 +++++++-------------------- 3 files changed, 14 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index e60cb1c..01167a9 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ This will prevent any method name conflicts with core, your custom or other trai \Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertExists($model); \Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertSame($model, \App\Models\User::first()); \Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertRelated($post, $comment, 'comments'); -\Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertRelationship($post, \App\Models\Comment::class, 'comments', \Illuminate\Database\Eloquent\Relations\HasMany::class); +\Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertRelated($post, \App\Models\Comment::class, 'comments', \Illuminate\Database\Eloquent\Relations\HasMany::class); ``` ### Blade diff --git a/src/Laravel/ModelAssertions.php b/src/Laravel/ModelAssertions.php index 4ec2327..7d648d4 100644 --- a/src/Laravel/ModelAssertions.php +++ b/src/Laravel/ModelAssertions.php @@ -44,28 +44,13 @@ public static function assertSame(Model $expected, $actual): void PHPUnit::assertTrue($expected->is($actual)); } - /** - * @param \Illuminate\Database\Eloquent\Model $model - * @param string $relation - * @param \Illuminate\Database\Eloquent\Model|mixed $actual - */ - public static function assertRelated(Model $model, string $relation, $actual) - { - PHPUnit::assertInstanceOf(Model::class, $actual); - PHPUnit::assertTrue(method_exists($model, $relation)); - PHPUnit::assertInstanceOf(Relation::class, $model->$relation()); - - $related = $model->$relation()->whereKey($actual->getKey())->first(); - self::assertSame($actual, $related); - } - /** * @param \Illuminate\Database\Eloquent\Model $model * @param string $relation * @param string|\Illuminate\Database\Eloquent\Model|mixed $actual * @param string|null $type */ - public static function assertRelationship(Model $model, string $relation, $actual, ?string $type = null) + public static function assertRelated(Model $model, string $relation, $actual, ?string $type = null) { PHPUnit::assertTrue(method_exists($model, $relation)); PHPUnit::assertInstanceOf(Relation::class, $model->$relation()); @@ -81,7 +66,10 @@ public static function assertRelationship(Model $model, string $relation, $actua PHPUnit::assertInstanceOf($actual, $related); } else { PHPUnit::assertInstanceOf(get_class($actual), $related); - self::assertRelated($model, $relation, $actual); + self::assertSame( + $actual, + $model->$relation()->whereKey($actual->getKey())->first() + ); } } } diff --git a/tests/Laravel/ModelAssertionsTest.php b/tests/Laravel/ModelAssertionsTest.php index 4cfa820..28e4781 100644 --- a/tests/Laravel/ModelAssertionsTest.php +++ b/tests/Laravel/ModelAssertionsTest.php @@ -48,7 +48,7 @@ public function it_can_validate_same(): void * @test * @dataProvider hundredTimes */ - public function it_can_validate_related(): void + public function it_can_validate_relationship(): void { $post = Post::create([ 'title' => self::randomString(), @@ -59,31 +59,16 @@ public function it_can_validate_related(): void 'post_id' => $post->getKey(), ]); + ModelAssertions::assertRelated($post, 'comments', Comment::class); ModelAssertions::assertRelated($post, 'comments', $comment); - ModelAssertions::assertRelated($comment, 'post', $post); - } - - /** - * @test - * @dataProvider hundredTimes - */ - public function it_can_validate_relationship(): void - { - $post = Post::create([ - 'title' => self::randomString(), - ]); - $comment = Comment::create([ - 'message' => self::randomString(), - 'post_id' => $post->getKey(), - ]); + ModelAssertions::assertRelated($post, 'comments', Comment::class, HasMany::class); + ModelAssertions::assertRelated($post, 'comments', $comment, HasMany::class); - ModelAssertions::assertRelationship($post, 'comments', Comment::class); - ModelAssertions::assertRelationship($post, 'comments', Comment::class, HasMany::class); - ModelAssertions::assertRelationship($post, 'comments', $comment, HasMany::class); + ModelAssertions::assertRelated($comment, 'post', Post::class); + ModelAssertions::assertRelated($comment, 'post', $post); - ModelAssertions::assertRelationship($comment, 'post', Post::class); - ModelAssertions::assertRelationship($comment, 'post', Post::class, BelongsTo::class); - ModelAssertions::assertRelationship($comment, 'post', $post, BelongsTo::class); + ModelAssertions::assertRelated($comment, 'post', Post::class, BelongsTo::class); + ModelAssertions::assertRelated($comment, 'post', $post, BelongsTo::class); } } From 52bb5d9b1c59955407b2569d2852fab78a402017 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Wed, 10 Mar 2021 11:38:14 +0100 Subject: [PATCH 6/6] fix readme usage argument order --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 01167a9..c2e6a53 100644 --- a/README.md +++ b/README.md @@ -135,8 +135,13 @@ This will prevent any method name conflicts with core, your custom or other trai ```php \Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertExists($model); \Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertSame($model, \App\Models\User::first()); -\Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertRelated($post, $comment, 'comments'); -\Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertRelated($post, \App\Models\Comment::class, 'comments', \Illuminate\Database\Eloquent\Relations\HasMany::class); +\Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertRelated($post, 'comments', $comment); +\Astrotomic\PhpunitAssertions\Laravel\ModelAssertions::assertRelated( + $post, + 'comments', + \App\Models\Comment::class, + \Illuminate\Database\Eloquent\Relations\HasMany::class +); ``` ### Blade