3
3
namespace Astrotomic \PhpunitAssertions \Tests \Laravel ;
4
4
5
5
use 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 ;
9
10
10
11
final class ModelAssertionsTest extends TestCase
11
12
{
12
13
protected function setUp (): void
13
14
{
14
15
parent ::setUp ();
15
16
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 ();
21
19
}
22
20
23
21
/**
@@ -26,15 +24,11 @@ protected function setUp(): void
26
24
*/
27
25
public function it_can_validate_exists (): void
28
26
{
29
- $ model = new class extends Model {
30
- protected $ table = 'posts ' ;
31
- protected $ guarded = [];
32
- };
27
+ $ post = Post::create ([
28
+ 'title ' => self ::randomString (),
29
+ ]);
33
30
34
- $ model ->title = self ::randomString ();
35
- $ model ->save ();
36
-
37
- ModelAssertions::assertExists ($ model );
31
+ ModelAssertions::assertExists ($ post );
38
32
}
39
33
40
34
/**
@@ -43,14 +37,38 @@ public function it_can_validate_exists(): void
43
37
*/
44
38
public function it_can_validate_same (): void
45
39
{
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);
50
67
51
- $ model -> title = self :: randomString ( );
52
- $ model -> save ( );
68
+ ModelAssertions:: assertRelated ( $ comment , ' post ' , Post::class );
69
+ ModelAssertions:: assertRelated ( $ comment , ' post ' , $ post );
53
70
54
- ModelAssertions::assertSame ($ model , $ model ->query ()->first ());
71
+ ModelAssertions::assertRelated ($ comment , 'post ' , Post::class, BelongsTo::class);
72
+ ModelAssertions::assertRelated ($ comment , 'post ' , $ post , BelongsTo::class);
55
73
}
56
74
}
0 commit comments