Skip to content

Commit 2f0045c

Browse files
aidan-caseyGummibeer
authored andcommitted
add model assertion that two models are related
1 parent 504c3e0 commit 2f0045c

File tree

4 files changed

+101
-20
lines changed

4 files changed

+101
-20
lines changed

src/Laravel/ModelAssertions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Astrotomic\PhpunitAssertions\Laravel;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7+
use Illuminate\Database\Eloquent\Relations\Relation;
68
use Illuminate\Support\Facades\DB;
79
use Illuminate\Testing\Constraints\HasInDatabase;
810
use PHPUnit\Framework\Assert as PHPUnit;
@@ -33,4 +35,14 @@ public static function assertSame(Model $expected, $actual): void
3335
PHPUnit::assertSame($expected->exists, $actual->exists);
3436
PHPUnit::assertTrue($expected->is($actual));
3537
}
38+
39+
public static function assertRelated(Model $model, $actual, string $relation)
40+
{
41+
PHPUnit::assertInstanceOf(Model::class, $actual);
42+
PHPUnit::assertTrue(method_exists($model, $relation));
43+
PHPUnit::assertInstanceOf(Relation::class, $model->$relation());
44+
45+
$related = $model->$relation()->whereKey($actual->getKey())->first();
46+
self::assertSame($actual, $related);
47+
}
3648
}

tests/Laravel/ModelAssertionsTest.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace Astrotomic\PhpunitAssertions\Tests\Laravel;
44

55
use Astrotomic\PhpunitAssertions\Laravel\ModelAssertions;
6+
use Astrotomic\PhpunitAssertions\Tests\Laravel\Models\Comment;
7+
use Astrotomic\PhpunitAssertions\Tests\Laravel\Models\Post;
68
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\Relations\HasMany;
710
use Illuminate\Database\Schema\Blueprint;
811
use Illuminate\Support\Facades\Schema;
912

@@ -13,11 +16,8 @@ protected function setUp(): void
1316
{
1417
parent::setUp();
1518

16-
Schema::create('posts', static function (Blueprint $table): void {
17-
$table->increments('id');
18-
$table->string('title');
19-
$table->timestamps();
20-
});
19+
Post::migrate();
20+
Comment::migrate();
2121
}
2222

2323
/**
@@ -26,15 +26,11 @@ protected function setUp(): void
2626
*/
2727
public function it_can_validate_exists(): void
2828
{
29-
$model = new class extends Model {
30-
protected $table = 'posts';
31-
protected $guarded = [];
32-
};
29+
$post = Post::create([
30+
'title' => self::randomString(),
31+
]);
3332

34-
$model->title = self::randomString();
35-
$model->save();
36-
37-
ModelAssertions::assertExists($model);
33+
ModelAssertions::assertExists($post);
3834
}
3935

4036
/**
@@ -43,14 +39,29 @@ public function it_can_validate_exists(): void
4339
*/
4440
public function it_can_validate_same(): void
4541
{
46-
$model = new class extends Model {
47-
protected $table = 'posts';
48-
protected $guarded = [];
49-
};
42+
$post = Post::create([
43+
'title' => self::randomString(),
44+
]);
45+
46+
ModelAssertions::assertSame($post, Post::first());
47+
}
48+
49+
/**
50+
* @test
51+
* @dataProvider hundredTimes
52+
*/
53+
public function it_can_validate_related(): void
54+
{
55+
$post = Post::create([
56+
'title' => self::randomString(),
57+
]);
5058

51-
$model->title = self::randomString();
52-
$model->save();
59+
$comment = Comment::create([
60+
'message' => self::randomString(),
61+
'post_id' => $post->getKey(),
62+
]);
5363

54-
ModelAssertions::assertSame($model, $model->query()->first());
64+
ModelAssertions::assertRelated($post, $comment, 'comments');
65+
ModelAssertions::assertRelated($comment, $post, 'post');
5566
}
5667
}

tests/Laravel/Models/Comment.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions\Tests\Laravel\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7+
use Illuminate\Database\Eloquent\Relations\HasMany;
8+
use Illuminate\Database\Schema\Blueprint;
9+
use Illuminate\Support\Facades\Schema;
10+
11+
class Comment extends Model
12+
{
13+
protected $table = 'comments';
14+
protected $guarded = [];
15+
16+
public static function migrate(): void
17+
{
18+
Schema::create((new self)->table, static function (Blueprint $table): void {
19+
$table->increments('id');
20+
$table->text('message');
21+
$table->foreignId('post_id')->constrained('posts');
22+
$table->timestamps();
23+
});
24+
}
25+
26+
public function post(): BelongsTo
27+
{
28+
return $this->belongsTo(Post::class);
29+
}
30+
}

tests/Laravel/Models/Post.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Astrotomic\PhpunitAssertions\Tests\Laravel\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\HasMany;
7+
use Illuminate\Database\Schema\Blueprint;
8+
use Illuminate\Support\Facades\Schema;
9+
10+
class Post extends Model
11+
{
12+
protected $table = 'posts';
13+
protected $guarded = [];
14+
15+
public static function migrate(): void
16+
{
17+
Schema::create((new self)->table, static function (Blueprint $table): void {
18+
$table->increments('id');
19+
$table->string('title');
20+
$table->timestamps();
21+
});
22+
}
23+
24+
public function comments(): HasMany
25+
{
26+
return $this->hasMany(Comment::class);
27+
}
28+
}

0 commit comments

Comments
 (0)