Skip to content

Commit 324c96c

Browse files
committed
ITT: assertEloquentHasCreateManyRelationsMethod assertion added.
1 parent 0763bfa commit 324c96c

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Asserts/EloquentAsserts.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,29 @@ protected function assertEloquentHasCreateRelationMethod($class, $relation)
8484
$this->assertEquals($child->fresh()->toArray(), $parent->{$relation}->first()->toArray());
8585
}
8686

87+
protected function assertEloquentHasCreateManyRelationsMethod($class, $relation)
88+
{
89+
$this->assertMethodExists($class, $relation);
90+
91+
$hasManyRelation = (new $class)->{$relation}();
92+
$this->assertInstanceOf(HasMany::class, $hasManyRelation);
93+
94+
$createManyMethod = 'createMany' . title_case($relation);
95+
$this->assertMethodExists($class, $createManyMethod);
96+
97+
$childModel = $hasManyRelation->getRelated();
98+
$childKey = $childModel->getKeyName();
99+
100+
$parent = factory($class)->create();
101+
$children = $parent->{$createManyMethod}(
102+
factory(get_class($childModel), 3)
103+
->make()
104+
->toArray()
105+
);
106+
107+
$this->assertCollectionsEqual($children, $parent->{$relation}, $childKey);
108+
}
109+
87110
protected function assertEloquentBelongsTo($class, $relation)
88111
{
89112
$this->assertMethodExists($class, $relation);

tests/TestingTools/Asserts/EloquentAssertsTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ public function it_has_eloquent_has_create_relation_method_assertion()
5656
$this->assertEloquentHasCreateRelationMethod(Post::class, 'comments');
5757
}
5858

59+
/** @test */
60+
public function it_has_eloquent_has_create_many_relations_method_assertion()
61+
{
62+
$this->assertEloquentHasCreateManyRelationsMethod(Post::class, 'comments');
63+
}
64+
5965
/** @test */
6066
public function it_has_eloquent_belongs_to_assertion()
6167
{

tests/TestingTools/fixture/app/Post.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@ public function createComment(array $attributes)
1818
{
1919
return $this->comments()->create($attributes);
2020
}
21+
22+
public function createManyComments(array $comments)
23+
{
24+
return $this->comments()->createMany($comments);
25+
}
2126
}

0 commit comments

Comments
 (0)