1010
1111trait ModelAssertions
1212{
13+ /**
14+ * @param string|\Illuminate\Database\Eloquent\Model $table
15+ * @param array $data
16+ * @param string|null $connection
17+ */
1318 public static function assertExists ($ table , array $ data = [], ?string $ connection = null ): void
1419 {
1520 if ($ table instanceof Model) {
@@ -28,14 +33,23 @@ public static function assertExists($table, array $data = [], ?string $connectio
2833 );
2934 }
3035
36+ /**
37+ * @param \Illuminate\Database\Eloquent\Model $expected
38+ * @param \Illuminate\Database\Eloquent\Model|mixed $actual
39+ */
3140 public static function assertSame (Model $ expected , $ actual ): void
3241 {
3342 PHPUnit::assertInstanceOf (get_class ($ expected ), $ actual );
3443 PHPUnit::assertSame ($ expected ->exists , $ actual ->exists );
3544 PHPUnit::assertTrue ($ expected ->is ($ actual ));
3645 }
3746
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 )
3953 {
4054 PHPUnit::assertInstanceOf (Model::class, $ actual );
4155 PHPUnit::assertTrue (method_exists ($ model , $ relation ));
@@ -44,4 +58,30 @@ public static function assertRelated(Model $model, $actual, string $relation)
4458 $ related = $ model ->$ relation ()->whereKey ($ actual ->getKey ())->first ();
4559 self ::assertSame ($ actual , $ related );
4660 }
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+ }
4787}
0 commit comments