Return an empty Collection if there are no posts

Return an empty Collection if there are no related posts for the given
post.

References 
This commit is contained in:
Oliver Davies 2020-10-07 13:15:12 +01:00
parent 059e237600
commit ae0024f335
2 changed files with 32 additions and 0 deletions
web/modules/custom/blog/tests/src/Kernel

View file

@ -33,6 +33,25 @@ final class RelatedPostsTest extends PostTestBase {
$this->assertSame('Post B', $relatedPosts->first()->label());
}
/** @test */
public function it_returns_an_empty_collection_if_there_are_no_related_posts(): void {
$postA = $this->postFactory
->setTitle('Drupal 8 post')
->withTags(['Drupal 8'])
->create();
$postA->save();
$postB = $this->postFactory
->setTitle('Drupal 9 post')
->withTags(['Drupal 9'])
->create();
$postB->save();
$relatedPosts = $this->relatedPostsRepository->getFor($postA);
$this->assertEmpty($relatedPosts);
}
protected function setUp() {
parent::setUp();