Refactor to use a factory class
This commit is contained in:
parent
10e4616e01
commit
2617e29dec
6 changed files with 150 additions and 33 deletions
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\opdavies_blog_test\Factory;
|
||||
|
||||
use Assert\Assert;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\opdavies_blog\Entity\Node\Post;
|
||||
use Drupal\taxonomy\Entity\Term;
|
||||
use Drupal\taxonomy\TermInterface;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
final class PostFactory {
|
||||
|
||||
private Collection $tags;
|
||||
|
||||
private string $title = 'This is a test blog post';
|
||||
|
||||
public function __construct() {
|
||||
$this->tags = new Collection();
|
||||
}
|
||||
|
||||
public function create(array $overrides = []): Post {
|
||||
$this->tags->each(function (TermInterface $tag): void {
|
||||
Assert::that($tag->bundle())->same('tags');
|
||||
});
|
||||
|
||||
$values = [
|
||||
'field_tags' => $this->tags->toArray(),
|
||||
'title' => $this->title,
|
||||
'type' => 'post',
|
||||
];
|
||||
|
||||
/** @var Post $post */
|
||||
$post = Node::create($values + $overrides);
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
public function setTitle(string $title): self {
|
||||
Assert::that($title)->notEmpty();
|
||||
|
||||
$this->title = $title;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withTags(array $tags): self {
|
||||
foreach ($tags as $tag) {
|
||||
Assert::that($tag)->notEmpty()->string();
|
||||
|
||||
$this->tags->push(
|
||||
Term::create(['vid' => 'tags', 'name' => $tag])
|
||||
);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
|
@ -6,9 +6,8 @@ namespace Drupal\Tests\custom\Kernel\Entity\Node;
|
|||
|
||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\opdavies_blog\Entity\Node\Post;
|
||||
use Drupal\taxonomy\Entity\Term;
|
||||
use Drupal\opdavies_blog_test\Factory\PostFactory;
|
||||
|
||||
final class PostTest extends EntityKernelTestBase {
|
||||
|
||||
|
@ -28,32 +27,24 @@ final class PostTest extends EntityKernelTestBase {
|
|||
|
||||
/** @test */
|
||||
public function it_can_determine_if_a_post_contains_a_tweet(): void {
|
||||
/** @var Post $post */
|
||||
$post = Node::create(['type' => 'post']);
|
||||
$post = (new PostFactory())->create();
|
||||
$post->save();
|
||||
|
||||
$this->assertFalse($post->hasTweet());
|
||||
|
||||
/** @var Post $post */
|
||||
$post = Node::create([
|
||||
'field_has_tweet' => TRUE,
|
||||
'type' => 'post',
|
||||
]);
|
||||
$post = (new PostFactory())->create(['field_has_tweet' => TRUE]);
|
||||
$post->save();
|
||||
|
||||
$this->assertTrue($post->hasTweet());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_converts_a_post_to_a_tweet(): void {
|
||||
/** @var Post $post */
|
||||
$post = Node::create([
|
||||
'field_tags' => [
|
||||
Term::create(['vid' => 'tags', 'name' => 'Automated testing']),
|
||||
Term::create(['vid' => 'tags', 'name' => 'DDEV']),
|
||||
Term::create(['vid' => 'tags', 'name' => 'Drupal']),
|
||||
Term::create(['vid' => 'tags', 'name' => 'PHP']),
|
||||
],
|
||||
'status' => NodeInterface::PUBLISHED,
|
||||
'title' => 'Creating a custom PHPUnit command for DDEV',
|
||||
'type' => 'post',
|
||||
]);
|
||||
$post = (new PostFactory())
|
||||
->setTitle('Creating a custom PHPUnit command for DDEV')
|
||||
->withTags(['Automated testing', 'DDEV', 'Drupal', 'PHP'])
|
||||
->create();
|
||||
|
||||
$post->save();
|
||||
|
||||
$expected = <<<EOF
|
||||
|
@ -69,17 +60,11 @@ final class PostTest extends EntityKernelTestBase {
|
|||
|
||||
/** @test */
|
||||
public function certain_terms_are_not_added_as_hashtags(): void {
|
||||
/** @var Post $post */
|
||||
$post = Node::create([
|
||||
'field_tags' => [
|
||||
Term::create(['vid' => 'tags', 'name' => 'Drupal']),
|
||||
Term::create(['vid' => 'tags', 'name' => 'Drupal Planet']),
|
||||
Term::create(['vid' => 'tags', 'name' => 'PHP']),
|
||||
],
|
||||
'status' => NodeInterface::PUBLISHED,
|
||||
'title' => 'Drupal Planet should not be added as a hashtag',
|
||||
'type' => 'post',
|
||||
]);
|
||||
$post = (new PostFactory())
|
||||
->setTitle('Drupal Planet should not be added as a hashtag')
|
||||
->withTags(['Drupal', 'Drupal Planet', 'PHP'])
|
||||
->create();
|
||||
|
||||
$post->save();
|
||||
|
||||
$expected = <<<EOF
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue