This commit is contained in:
Oliver Davies 2025-06-21 12:52:11 +01:00
parent b8c3f6d9a7
commit 8c354f2754
3 changed files with 20 additions and 21 deletions

View file

@ -1,5 +1,5 @@
services:
Drupal\opd_presentations\PresentationCounter:
Drupal\opd_presentations\CountGivenPresentations:
autowire: true
Drupal\opd_presentations\Repository\PresentationNodeRepository:
autowire: true

View file

@ -6,12 +6,12 @@ namespace Drupal\opd_presentations;
use Drupal\opd_presentations\Repository\PresentationRepositoryInterface;
final class PresentationCounter {
final class CountGivenPresentations {
public function __construct(private PresentationRepositoryInterface $presentationRepository) {
}
public function getPastCount(): int {
public function __invoke(): int {
$presentations = $this->presentationRepository->getPublished();
return array_reduce(

View file

@ -6,54 +6,55 @@ namespace Drupal\Tests\opd_presentations;
use Drupal\Tests\RandomGeneratorTrait;
use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait;
use Drupal\opd_presentations\Date;
use Drupal\opd_presentations\CountGivenPresentations;
use Drupal\opd_presentations\Events;
use Drupal\opd_presentations\PresentationCounter;
use Drupal\opd_presentations\Repository\PresentationRepositoryInterface;
use weitzman\DrupalTestTraits\ExistingSiteBase;
final class PresentationCounterTest extends ExistingSiteBase {
final class CountGivenPresentationsTest extends ExistingSiteBase {
use PresentationCreationTrait;
use RandomGeneratorTrait;
public function test_it_counts_events(): void {
$counter = $this->container->get(PresentationCounter::class);
assert($counter instanceof PresentationCounter);
$action = $this->container->get(CountGivenPresentations::class);
assert($action instanceof CountGivenPresentations);
$this->createPresentation(
Events::fromDateStrings('yesterday'),
);
$this->assertGreaterThanOrEqual(
actual: $counter->getPastCount(),
actual: $action(),
expected: 1,
);
}
public function test_it_only_counts_published_events(): void {
$counter = $this->container->get(PresentationCounter::class);
assert($counter instanceof PresentationCounter);
$action = $this->container->get(CountGivenPresentations::class);
assert($action instanceof CountGivenPresentations);
$count = $counter->getPastCount();
// Get the existing presentation count (including existing nodes).
$originalCount = $action();
$this->createPresentation(
events: Events::fromDateStrings('yesterday'),
isPublished: FALSE,
);
// Ensure the count has only increased by one, even though an unpublished
// presentation was created.
$this->assertSame(
actual: $counter->getPastCount(),
expected: $count,
actual: $action(),
expected: $originalCount,
);
}
public function test_it_only_counts_past_events(): void {
$counter = $this->container->get(PresentationCounter::class);
assert($counter instanceof PresentationCounter);
$action = $this->container->get(CountGivenPresentations::class);
assert($action instanceof CountGivenPresentations);
// Get the existing presentation count (including existing nodes).
$originalCount = $counter->getPastCount();
$originalCount = $action();
$this->assertGreaterThanOrEqual(
actual: $originalCount,
@ -64,11 +65,9 @@ final class PresentationCounterTest extends ExistingSiteBase {
Events::fromDateStrings('tomorrow', 'yesterday'),
);
$counter = $this->container->get(PresentationCounter::class);
// Ensure the count has only increased by one, even though a future and past event were created.
$this->assertSame(
actual: $counter->getPastCount(),
actual: $action(),
expected: $originalCount + 1,
);
}