33namespace Astrotomic \PhpunitAssertions \Tests \Laravel ;
44
55use Astrotomic \PhpunitAssertions \Laravel \ModelAssertions ;
6- use Illuminate \Database \Eloquent \Model ;
7- use Illuminate \Database \Schema \Blueprint ;
8- use Illuminate \Support \Facades \Schema ;
6+ use Astrotomic \PhpunitAssertions \Tests \Laravel \Models \Comment ;
7+ use Astrotomic \PhpunitAssertions \Tests \Laravel \Models \Post ;
8+ use Illuminate \Database \Eloquent \Relations \BelongsTo ;
9+ use Illuminate \Database \Eloquent \Relations \HasMany ;
910
1011final class ModelAssertionsTest extends TestCase
1112{
1213 protected function setUp (): void
1314 {
1415 parent ::setUp ();
1516
16- Schema::create ('posts ' , static function (Blueprint $ table ): void {
17- $ table ->increments ('id ' );
18- $ table ->string ('title ' );
19- $ table ->timestamps ();
20- });
17+ Post::migrate ();
18+ Comment::migrate ();
2119 }
2220
2321 /**
@@ -26,15 +24,11 @@ protected function setUp(): void
2624 */
2725 public function it_can_validate_exists (): void
2826 {
29- $ model = new class extends Model {
30- protected $ table = 'posts ' ;
31- protected $ guarded = [];
32- };
27+ $ post = Post::create ([
28+ 'title ' => self ::randomString (),
29+ ]);
3330
34- $ model ->title = self ::randomString ();
35- $ model ->save ();
36-
37- ModelAssertions::assertExists ($ model );
31+ ModelAssertions::assertExists ($ post );
3832 }
3933
4034 /**
@@ -43,14 +37,38 @@ public function it_can_validate_exists(): void
4337 */
4438 public function it_can_validate_same (): void
4539 {
46- $ model = new class extends Model {
47- protected $ table = 'posts ' ;
48- protected $ guarded = [];
49- };
40+ $ post = Post::create ([
41+ 'title ' => self ::randomString (),
42+ ]);
43+
44+ ModelAssertions::assertSame ($ post , Post::first ());
45+ }
46+
47+ /**
48+ * @test
49+ * @dataProvider hundredTimes
50+ */
51+ public function it_can_validate_relationship (): void
52+ {
53+ $ post = Post::create ([
54+ 'title ' => self ::randomString (),
55+ ]);
56+
57+ $ comment = Comment::create ([
58+ 'message ' => self ::randomString (),
59+ 'post_id ' => $ post ->getKey (),
60+ ]);
61+
62+ ModelAssertions::assertRelated ($ post , 'comments ' , Comment::class);
63+ ModelAssertions::assertRelated ($ post , 'comments ' , $ comment );
64+
65+ ModelAssertions::assertRelated ($ post , 'comments ' , Comment::class, HasMany::class);
66+ ModelAssertions::assertRelated ($ post , 'comments ' , $ comment , HasMany::class);
5067
51- $ model -> title = self :: randomString ( );
52- $ model -> save ( );
68+ ModelAssertions:: assertRelated ( $ comment , ' post ' , Post::class );
69+ ModelAssertions:: assertRelated ( $ comment , ' post ' , $ post );
5370
54- ModelAssertions::assertSame ($ model , $ model ->query ()->first ());
71+ ModelAssertions::assertRelated ($ comment , 'post ' , Post::class, BelongsTo::class);
72+ ModelAssertions::assertRelated ($ comment , 'post ' , $ post , BelongsTo::class);
5573 }
5674}
0 commit comments