Add lesson 4

- Add initial `PostNodeRepository`
- Move logic to Repository
- Inject `EntityTypeManagerInterface`
- Add `PostNodeRepositoryTest`
- Add failing test method
- Failing test: non-existent service
- The "node" entity type does not exist.
- Create posts
- Add assertions based on the `created` date
- Sort nodes before returning them
- Return values to reset array keys
This commit is contained in:
Oliver Davies 2024-01-13 22:49:20 +00:00
parent 5121502d7f
commit 75b17fd565
4 changed files with 101 additions and 2 deletions

View file

@ -0,0 +1,4 @@
services:
Drupal\example\Repository\PostNodeRepository:
arguments:
- '@entity_type.manager'

View file

@ -3,12 +3,18 @@
namespace Drupal\example\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\example\Repository\PostNodeRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BlogPageController extends ControllerBase {
public function __construct(
private PostNodeRepository $postNodeRepository,
) {
}
public function __invoke(): array {
$nodeStorage = $this->entityTypeManager()->getStorage('node');
$nodes = $nodeStorage->loadMultiple();
$nodes = $this->postNodeRepository->findAll();
$build = [];
$build['content']['#theme'] = 'item_list';
@ -19,4 +25,10 @@ class BlogPageController extends ControllerBase {
return $build;
}
public static function create(ContainerInterface $container): self {
return new self(
$container->get(PostNodeRepository::class),
);
}
}

View file

@ -0,0 +1,29 @@
<?php
namespace Drupal\example\Repository;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\node\NodeInterface;
final class PostNodeRepository {
public function __construct(
private EntityTypeManagerInterface $entityTypeManager,
) {
}
/**
* @return array<int, NodeInterface>
*/
public function findAll(): array {
$nodeStorage = $this->entityTypeManager->getStorage('node');
$nodes = $nodeStorage->loadMultiple();
uasort($nodes, function (NodeInterface $a, NodeInterface $b): int {
return $a->getCreatedTime() <=> $b->getCreatedTime();
});
return array_values($nodes);
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace Drupal\Tests\example\Kernel;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\example\Repository\PostNodeRepository;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\NodeInterface;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class PostNodeRepositoryTest extends EntityKernelTestBase {
use NodeCreationTrait;
protected static $modules = ['node', 'example'];
public function testPostsAreReturnedByCreatedDate(): void {
// Arrange.
$this->createNode([
'title' => 'Post one',
'created' => (new DrupalDateTime('-1 week'))->getTimestamp(),
'type' => 'post',
]);
$this->createNode([
'title' => 'Post two',
'created' => (new DrupalDateTime('-8 days'))->getTimestamp(),
'type' => 'post',
]);
$this->createNode([
'title' => 'Post three',
'created' => (new DrupalDateTime('yesterday'))->getTimestamp(),
'type' => 'post',
]);
// Act.
$postRepository = $this->container->get(PostNodeRepository::class);
assert($postRepository instanceof PostNodeRepository);
$nodes = $postRepository->findAll();
// Assert.
self::assertCount(3, $nodes);
self::assertSame(
['Post two', 'Post one', 'Post three'],
array_map(
fn (NodeInterface $node) => $node->label(),
$nodes
)
);
}
}