Skip to content

Commit 7624e83

Browse files
committed
ITT: assertEloquentBelongsTo assertion added.
1 parent 1a5a20b commit 7624e83

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/Asserts/EloquentAsserts.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminated\Testing\Asserts;
44

5+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
56
use Illuminate\Database\Eloquent\Relations\HasMany;
67

78
trait EloquentAsserts
@@ -79,4 +80,21 @@ protected function assertEloquentHasCreateRelationMethod($class, $relation)
7980
$child = $parent->{$createMethod}($childAttributes);
8081
$this->assertEquals($child->fresh()->toArray(), $parent->{$relation}->first()->toArray());
8182
}
83+
84+
protected function assertEloquentBelongsTo($class, $relation)
85+
{
86+
$this->assertMethodExists($class, $relation);
87+
88+
$belongsToRelation = (new $class)->{$relation}();
89+
$this->assertInstanceOf(BelongsTo::class, $belongsToRelation);
90+
91+
$parentModel = $belongsToRelation->getRelated();
92+
$parentKey = $belongsToRelation->getOwnerKey();
93+
$childForeignKey = $belongsToRelation->getForeignKey();
94+
95+
$parent = factory(get_class($parentModel))->create();
96+
$child = factory($class)->create([$childForeignKey => $parent->{$parentKey}]);
97+
98+
$this->assertEquals($parent->toArray(), $child->{$relation}->toArray());
99+
}
82100
}

0 commit comments

Comments
 (0)