Refactor to use a PostBuilder
This commit is contained in:
parent
f7487a6bbe
commit
c687deaf5c
43
web/modules/custom/example/src/Builder/PostBuilder.php
Normal file
43
web/modules/custom/example/src/Builder/PostBuilder.php
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\example\Builder;
|
||||||
|
|
||||||
|
use Drupal\Core\Datetime\DrupalDateTime;
|
||||||
|
use Drupal\node\Entity\Node;
|
||||||
|
use Drupal\node\NodeInterface;
|
||||||
|
|
||||||
|
final class PostBuilder {
|
||||||
|
|
||||||
|
private ?DrupalDateTime $created;
|
||||||
|
|
||||||
|
private string $title;
|
||||||
|
|
||||||
|
public static function create(): self {
|
||||||
|
return new self();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setCreatedDate(string $time = 'now') {
|
||||||
|
$this->created = new DrupalDateTime($time);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setTitle(string $title) {
|
||||||
|
$this->title = $title;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPost(): NodeInterface {
|
||||||
|
$post = Node::create([
|
||||||
|
'created' => $this?->created->getTimestamp(),
|
||||||
|
'title' => $this->title,
|
||||||
|
'type' => 'post',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$post->save();
|
||||||
|
|
||||||
|
return $post;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,37 +2,31 @@
|
||||||
|
|
||||||
namespace Drupal\Tests\example\Kernel;
|
namespace Drupal\Tests\example\Kernel;
|
||||||
|
|
||||||
use Drupal\Core\Datetime\DrupalDateTime;
|
use Drupal\example\Builder\PostBuilder;
|
||||||
use Drupal\example\Repository\PostNodeRepository;
|
use Drupal\example\Repository\PostNodeRepository;
|
||||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||||
use Drupal\node\NodeInterface;
|
use Drupal\node\NodeInterface;
|
||||||
use Drupal\Tests\node\Traits\NodeCreationTrait;
|
|
||||||
|
|
||||||
class PostNodeRepositoryTest extends EntityKernelTestBase {
|
class PostNodeRepositoryTest extends EntityKernelTestBase {
|
||||||
|
|
||||||
use NodeCreationTrait;
|
|
||||||
|
|
||||||
protected static $modules = ['node', 'example'];
|
protected static $modules = ['node', 'example'];
|
||||||
|
|
||||||
public function testPostsAreReturnedByCreatedDate(): void {
|
public function testPostsAreReturnedByCreatedDate(): void {
|
||||||
// Arrange.
|
// Arrange.
|
||||||
$this->createNode([
|
PostBuilder::create()
|
||||||
'title' => 'Post one',
|
->setCreatedDate('-1 week')
|
||||||
'created' => (new DrupalDateTime('-1 week'))->getTimestamp(),
|
->setTitle('Post one')
|
||||||
'type' => 'post',
|
->getPost();
|
||||||
]);
|
|
||||||
|
|
||||||
$this->createNode([
|
PostBuilder::create()
|
||||||
'title' => 'Post two',
|
->setCreatedDate('-8 days')
|
||||||
'created' => (new DrupalDateTime('-8 days'))->getTimestamp(),
|
->setTitle('Post two')
|
||||||
'type' => 'post',
|
->getPost();
|
||||||
]);
|
|
||||||
|
|
||||||
$this->createNode([
|
PostBuilder::create()
|
||||||
'title' => 'Post three',
|
->setCreatedDate('yesterday')
|
||||||
'created' => (new DrupalDateTime('yesterday'))->getTimestamp(),
|
->setTitle('Post three')
|
||||||
'type' => 'post',
|
->getPost();
|
||||||
]);
|
|
||||||
|
|
||||||
// Act.
|
// Act.
|
||||||
$postRepository = $this->container->get(PostNodeRepository::class);
|
$postRepository = $this->container->get(PostNodeRepository::class);
|
||||||
|
|
Loading…
Reference in a new issue