Skip to content

Commit 9ddcc30

Browse files
committed
ITT: assertEloquentHasCreateRelationMethod assertion added.
1 parent 7b66aae commit 9ddcc30

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/Asserts/EloquentAsserts.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,21 @@ protected function assertEloquentHasMany($class, $relation)
6262

6363
$this->assertCollectionsEqual($children, $parent->{$relation}, $childKey);
6464
}
65+
66+
protected function assertEloquentHasCreateRelationMethod($class, $relation)
67+
{
68+
$this->assertMethodExists($class, $relation);
69+
70+
$parent = factory($class)->create();
71+
$hasManyRelation = $parent->{$relation}();
72+
$this->assertInstanceOf(HasMany::class, $hasManyRelation);
73+
74+
$createMethod = 'create' . title_case(str_singular($relation));
75+
$this->assertMethodExists($class, $createMethod);
76+
77+
$childModel = $hasManyRelation->getRelated();
78+
$childAttributes = factory(get_class($childModel))->make()->toArray();
79+
$child = $parent->{$createMethod}($childAttributes);
80+
$this->assertEquals($child->fresh()->toArray(), $parent->{$relation}->first()->toArray());
81+
}
6582
}

0 commit comments

Comments
 (0)