diff --git a/web/modules/custom/blog/tests/modules/blog_test/opdavies_blog_test.services.yml b/web/modules/custom/blog/tests/modules/blog_test/opdavies_blog_test.services.yml index 27bfd9c..3213152 100644 --- a/web/modules/custom/blog/tests/modules/blog_test/opdavies_blog_test.services.yml +++ b/web/modules/custom/blog/tests/modules/blog_test/opdavies_blog_test.services.yml @@ -1,3 +1,6 @@ services: Drupal\opdavies_blog\Service\PostPusher\PostPusher: alias: Drupal\opdavies_blog\Service\PostPusher\NullPostPusher + + Drupal\opdavies_blog_test\Factory\PostFactory: + autowire: true diff --git a/web/modules/custom/blog/tests/modules/blog_test/src/Factory/PostFactory.php b/web/modules/custom/blog/tests/modules/blog_test/src/Factory/PostFactory.php index 903a109..c9549e8 100644 --- a/web/modules/custom/blog/tests/modules/blog_test/src/Factory/PostFactory.php +++ b/web/modules/custom/blog/tests/modules/blog_test/src/Factory/PostFactory.php @@ -5,6 +5,9 @@ declare(strict_types=1); namespace Drupal\opdavies_blog_test\Factory; use Assert\Assert; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\node\Entity\Node; use Drupal\opdavies_blog\Entity\Node\Post; use Drupal\taxonomy\Entity\Term; @@ -13,11 +16,17 @@ use Tightenco\Collect\Support\Collection; final class PostFactory { + private EntityStorageInterface $termStorage; + private Collection $tags; private string $title = 'This is a test blog post'; - public function __construct() { + public function __construct( + EntityTypeManagerInterface $entityTypeManager + ) { + $this->termStorage = $entityTypeManager->getStorage('taxonomy_term'); + $this->tags = new Collection(); } @@ -47,15 +56,25 @@ final class PostFactory { } public function withTags(array $tags): self { + $this->tags = new Collection(); + foreach ($tags as $tag) { Assert::that($tag)->notEmpty()->string(); - $this->tags->push( - Term::create(['vid' => 'tags', 'name' => $tag]) - ); + $this->tags->push($this->createOrReferenceTag($tag)); } return $this; } + private function createOrReferenceTag(string $tag): EntityInterface { + $existingTags = $this->termStorage->loadByProperties(['name' => $tag]); + + if ($existingTags) { + return reset($existingTags); + } + + return Term::create(['vid' => 'tags', 'name' => $tag]); + } + } diff --git a/web/modules/custom/blog/tests/src/Kernel/Entity/Node/PostTest.php b/web/modules/custom/blog/tests/src/Kernel/Entity/Node/PostTest.php index f9b22fb..f108eed 100644 --- a/web/modules/custom/blog/tests/src/Kernel/Entity/Node/PostTest.php +++ b/web/modules/custom/blog/tests/src/Kernel/Entity/Node/PostTest.php @@ -24,14 +24,16 @@ final class PostTest extends EntityKernelTestBase { 'opdavies_blog_test', ]; + private PostFactory $postFactory; + /** @test */ public function it_can_determine_if_a_post_contains_a_tweet(): void { - $post = (new PostFactory())->create(); + $post = $this->postFactory->create(); $post->save(); $this->assertFalse($post->hasTweet()); - $post = (new PostFactory())->create([Post::FIELD_HAS_TWEET => TRUE]); + $post = $this->postFactory->create([Post::FIELD_HAS_TWEET => TRUE]); $post->save(); $this->assertTrue($post->hasTweet()); @@ -39,7 +41,7 @@ final class PostTest extends EntityKernelTestBase { /** @test */ public function it_converts_a_post_to_a_tweet(): void { - $post = (new PostFactory()) + $post = $this->postFactory ->setTitle('Creating a custom PHPUnit command for DDEV') ->withTags(['Automated testing', 'DDEV', 'Drupal', 'Drupal 8', 'PHP']) ->create(); @@ -59,7 +61,7 @@ final class PostTest extends EntityKernelTestBase { /** @test */ public function certain_terms_are_not_added_as_hashtags(): void { - $post = (new PostFactory()) + $post = $this->postFactory ->setTitle('Drupal Planet should not be added as a hashtag') ->withTags(['Drupal', 'Drupal Planet', 'PHP']) ->create(); @@ -80,6 +82,8 @@ final class PostTest extends EntityKernelTestBase { protected function setUp() { parent::setUp(); + $this->postFactory = $this->container->get(PostFactory::class); + $this->installEntitySchema('taxonomy_term'); $this->installConfig(['opdavies_blog_test']); diff --git a/web/modules/custom/blog/tests/src/Kernel/RelatedPostsTest.php b/web/modules/custom/blog/tests/src/Kernel/RelatedPostsTest.php index 02555f8..6c5c6a5 100644 --- a/web/modules/custom/blog/tests/src/Kernel/RelatedPostsTest.php +++ b/web/modules/custom/blog/tests/src/Kernel/RelatedPostsTest.php @@ -9,16 +9,20 @@ 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 = (new PostFactory())->setTitle('Post A') + $postA = $this->postFactory + ->setTitle('Post A') ->withTags(['Drupal 8']) ->create(); $postA->save(); - $postB = (new PostFactory())->setTitle('Post B') + $postB = $this->postFactory + ->setTitle('Post B') ->withTags(['Drupal 8']) ->create(); $postB->save(); @@ -32,6 +36,7 @@ final class RelatedPostsTest extends PostTestBase { protected function setUp() { parent::setUp(); + $this->postFactory = $this->container->get(PostFactory::class); $this->relatedPostsRepository = $this->container->get(RelatedPostsRepository::class); }