10
10
11
11
trait ModelAssertions
12
12
{
13
+ /**
14
+ * @param string|\Illuminate\Database\Eloquent\Model $table
15
+ * @param array $data
16
+ * @param string|null $connection
17
+ */
13
18
public static function assertExists ($ table , array $ data = [], ?string $ connection = null ): void
14
19
{
15
20
if ($ table instanceof Model) {
@@ -28,14 +33,23 @@ public static function assertExists($table, array $data = [], ?string $connectio
28
33
);
29
34
}
30
35
36
+ /**
37
+ * @param \Illuminate\Database\Eloquent\Model $expected
38
+ * @param \Illuminate\Database\Eloquent\Model|mixed $actual
39
+ */
31
40
public static function assertSame (Model $ expected , $ actual ): void
32
41
{
33
42
PHPUnit::assertInstanceOf (get_class ($ expected ), $ actual );
34
43
PHPUnit::assertSame ($ expected ->exists , $ actual ->exists );
35
44
PHPUnit::assertTrue ($ expected ->is ($ actual ));
36
45
}
37
46
38
- public static function assertRelated (Model $ model , $ actual , string $ relation )
47
+ /**
48
+ * @param \Illuminate\Database\Eloquent\Model $model
49
+ * @param string $relation
50
+ * @param \Illuminate\Database\Eloquent\Model|mixed $actual
51
+ */
52
+ public static function assertRelated (Model $ model , string $ relation , $ actual )
39
53
{
40
54
PHPUnit::assertInstanceOf (Model::class, $ actual );
41
55
PHPUnit::assertTrue (method_exists ($ model , $ relation ));
@@ -44,4 +58,30 @@ public static function assertRelated(Model $model, $actual, string $relation)
44
58
$ related = $ model ->$ relation ()->whereKey ($ actual ->getKey ())->first ();
45
59
self ::assertSame ($ actual , $ related );
46
60
}
61
+
62
+ /**
63
+ * @param \Illuminate\Database\Eloquent\Model $model
64
+ * @param string $relation
65
+ * @param string|\Illuminate\Database\Eloquent\Model|mixed $actual
66
+ * @param string|null $type
67
+ */
68
+ public static function assertRelationship (Model $ model , string $ relation , $ actual , ?string $ type = null )
69
+ {
70
+ PHPUnit::assertTrue (method_exists ($ model , $ relation ));
71
+ PHPUnit::assertInstanceOf (Relation::class, $ model ->$ relation ());
72
+
73
+ if ($ type ) {
74
+ PHPUnit::assertInstanceOf ($ type , $ model ->$ relation ());
75
+ }
76
+
77
+ $ related = $ model ->$ relation ()->getRelated ();
78
+ PHPUnit::assertInstanceOf (Model::class, $ related );
79
+
80
+ if (is_string ($ actual )) {
81
+ PHPUnit::assertInstanceOf ($ actual , $ related );
82
+ } else {
83
+ PHPUnit::assertInstanceOf (get_class ($ actual ), $ related );
84
+ self ::assertRelated ($ model , $ relation , $ actual );
85
+ }
86
+ }
47
87
}
0 commit comments