Within the `PostFactory` class, ensure that when using the `withTags` method and creating tag terms that an existing term doesn't already exist for a given name before trying to create it. With the previous implementation, there would be multiple terms if the PostFactory was used multiple times with the same tag name. Given that `PostFactory` now has a dependency on `EntityTypeManger`, this has been added as a service within `opdavies_blog_test` and needs to be resolved from the container before trying to use it within a test. This commit also updates the usages in `PostTest` so that those tests continue to work and pass. References #3
43 lines
1.1 KiB
PHP
43 lines
1.1 KiB
PHP
<?php
|
|
|
|
// phpcs:disable Drupal.Commenting.DocComment, Drupal.NamingConventions.ValidFunctionName
|
|
|
|
namespace Drupal\Tests\opdavies_blog\Kernel;
|
|
|
|
use Drupal\opdavies_blog\Repository\RelatedPostsRepository;
|
|
use Drupal\opdavies_blog_test\Factory\PostFactory;
|
|
|
|
final class RelatedPostsTest extends PostTestBase {
|
|
|
|
private PostFactory $postFactory;
|
|
|
|
private RelatedPostsRepository $relatedPostsRepository;
|
|
|
|
/** @test */
|
|
public function it_returns_related_posts(): void {
|
|
$postA = $this->postFactory
|
|
->setTitle('Post A')
|
|
->withTags(['Drupal 8'])
|
|
->create();
|
|
$postA->save();
|
|
|
|
$postB = $this->postFactory
|
|
->setTitle('Post B')
|
|
->withTags(['Drupal 8'])
|
|
->create();
|
|
$postB->save();
|
|
|
|
$relatedPosts = $this->relatedPostsRepository->getFor($postA);
|
|
|
|
$this->assertCount(1, $relatedPosts);
|
|
$this->assertSame('Post B', $relatedPosts->first()->label());
|
|
}
|
|
|
|
protected function setUp() {
|
|
parent::setUp();
|
|
|
|
$this->postFactory = $this->container->get(PostFactory::class);
|
|
$this->relatedPostsRepository = $this->container->get(RelatedPostsRepository::class);
|
|
}
|
|
|
|
}
|