diff --git a/modules/opd_presentations/opd_presentations.services.yml b/modules/opd_presentations/opd_presentations.services.yml index 15f161ce8..d2528f660 100644 --- a/modules/opd_presentations/opd_presentations.services.yml +++ b/modules/opd_presentations/opd_presentations.services.yml @@ -1,5 +1,5 @@ services: - Drupal\opd_presentations\PresentationCounter: + Drupal\opd_presentations\CountGivenPresentations: autowire: true Drupal\opd_presentations\Repository\PresentationNodeRepository: autowire: true diff --git a/modules/opd_presentations/src/PresentationCounter.php b/modules/opd_presentations/src/CountGivenPresentations.php similarity index 87% rename from modules/opd_presentations/src/PresentationCounter.php rename to modules/opd_presentations/src/CountGivenPresentations.php index 2e7a176b3..9b47a4445 100644 --- a/modules/opd_presentations/src/PresentationCounter.php +++ b/modules/opd_presentations/src/CountGivenPresentations.php @@ -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( diff --git a/modules/opd_presentations/tests/src/Functional/PresentationCounterTest.php b/modules/opd_presentations/tests/src/Functional/CountGivenPresentationsTest.php similarity index 58% rename from modules/opd_presentations/tests/src/Functional/PresentationCounterTest.php rename to modules/opd_presentations/tests/src/Functional/CountGivenPresentationsTest.php index ac8c3acd3..116c55724 100644 --- a/modules/opd_presentations/tests/src/Functional/PresentationCounterTest.php +++ b/modules/opd_presentations/tests/src/Functional/CountGivenPresentationsTest.php @@ -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, ); }