Remove the hard-coded talk count

This commit is contained in:
Oliver Davies 2024-11-20 13:23:17 +00:00
parent cdd7a466d2
commit 0db961b741
4 changed files with 20 additions and 3 deletions

View file

@ -1,4 +1,5 @@
services: services:
Drupal\opdavies_talks\Controller\TalksPageController: Drupal\opdavies_talks\Controller\TalksPageController:
autowire: true autowire: true
Drupal\opdavies_talks\Repository\TalkNodeRepository: [] Drupal\opdavies_talks\Repository\TalkNodeRepository:
autowire: true

View file

@ -4,10 +4,17 @@ declare(strict_types=1);
namespace Drupal\opdavies_talks\Repository; namespace Drupal\opdavies_talks\Repository;
use Drupal\Core\Entity\EntityTypeManagerInterface;
final class TalkNodeRepository { final class TalkNodeRepository {
public function __construct(private EntityTypeManagerInterface $entityTypeManager) {
}
public function getCount(): int { public function getCount(): int {
return 2; $talks = $this->entityTypeManager->getStorage('node')->loadByProperties();
return count($talks);
} }
} }

View file

@ -19,6 +19,9 @@ class TalksPageTest extends BrowserTestBase {
} }
public function test_it_displays_the_talk_count(): void { public function test_it_displays_the_talk_count(): void {
$this->createNode(['type' => 'talk']);
$this->createNode(['type' => 'talk']);
$this->drupalGet('/talks'); $this->drupalGet('/talks');
$session = $this->assertSession(); $session = $this->assertSession();

View file

@ -3,13 +3,19 @@
namespace Drupal\Tests\opdavies_talks\Kernel\Repository; namespace Drupal\Tests\opdavies_talks\Kernel\Repository;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\opdavies_talks\Repository\TalkNodeRepository; use Drupal\opdavies_talks\Repository\TalkNodeRepository;
final class TalkNodeRepositoryTest extends EntityKernelTestBase { final class TalkNodeRepositoryTest extends EntityKernelTestBase {
protected static $modules = ['opdavies_talks']; protected static $modules = ['node', 'opdavies_talks'];
use NodeCreationTrait;
public function test_it_returns_a_count(): void { public function test_it_returns_a_count(): void {
$this->createNode(['type' => 'talk']);
$this->createNode(['type' => 'talk']);
$talkRepository = $this->container->get(TalkNodeRepository::class); $talkRepository = $this->container->get(TalkNodeRepository::class);
self::assertSame(2, $talkRepository->getCount()); self::assertSame(2, $talkRepository->getCount());