Refactor
This commit is contained in:
parent
b8c3f6d9a7
commit
8c354f2754
3 changed files with 20 additions and 21 deletions
|
@ -1,5 +1,5 @@
|
||||||
services:
|
services:
|
||||||
Drupal\opd_presentations\PresentationCounter:
|
Drupal\opd_presentations\CountGivenPresentations:
|
||||||
autowire: true
|
autowire: true
|
||||||
Drupal\opd_presentations\Repository\PresentationNodeRepository:
|
Drupal\opd_presentations\Repository\PresentationNodeRepository:
|
||||||
autowire: true
|
autowire: true
|
||||||
|
|
|
@ -6,12 +6,12 @@ namespace Drupal\opd_presentations;
|
||||||
|
|
||||||
use Drupal\opd_presentations\Repository\PresentationRepositoryInterface;
|
use Drupal\opd_presentations\Repository\PresentationRepositoryInterface;
|
||||||
|
|
||||||
final class PresentationCounter {
|
final class CountGivenPresentations {
|
||||||
|
|
||||||
public function __construct(private PresentationRepositoryInterface $presentationRepository) {
|
public function __construct(private PresentationRepositoryInterface $presentationRepository) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getPastCount(): int {
|
public function __invoke(): int {
|
||||||
$presentations = $this->presentationRepository->getPublished();
|
$presentations = $this->presentationRepository->getPublished();
|
||||||
|
|
||||||
return array_reduce(
|
return array_reduce(
|
|
@ -6,54 +6,55 @@ namespace Drupal\Tests\opd_presentations;
|
||||||
|
|
||||||
use Drupal\Tests\RandomGeneratorTrait;
|
use Drupal\Tests\RandomGeneratorTrait;
|
||||||
use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait;
|
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\Events;
|
||||||
use Drupal\opd_presentations\PresentationCounter;
|
|
||||||
use Drupal\opd_presentations\Repository\PresentationRepositoryInterface;
|
|
||||||
use weitzman\DrupalTestTraits\ExistingSiteBase;
|
use weitzman\DrupalTestTraits\ExistingSiteBase;
|
||||||
|
|
||||||
final class PresentationCounterTest extends ExistingSiteBase {
|
final class CountGivenPresentationsTest extends ExistingSiteBase {
|
||||||
|
|
||||||
use PresentationCreationTrait;
|
use PresentationCreationTrait;
|
||||||
use RandomGeneratorTrait;
|
use RandomGeneratorTrait;
|
||||||
|
|
||||||
public function test_it_counts_events(): void {
|
public function test_it_counts_events(): void {
|
||||||
$counter = $this->container->get(PresentationCounter::class);
|
$action = $this->container->get(CountGivenPresentations::class);
|
||||||
assert($counter instanceof PresentationCounter);
|
assert($action instanceof CountGivenPresentations);
|
||||||
|
|
||||||
$this->createPresentation(
|
$this->createPresentation(
|
||||||
Events::fromDateStrings('yesterday'),
|
Events::fromDateStrings('yesterday'),
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertGreaterThanOrEqual(
|
$this->assertGreaterThanOrEqual(
|
||||||
actual: $counter->getPastCount(),
|
actual: $action(),
|
||||||
expected: 1,
|
expected: 1,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_it_only_counts_published_events(): void {
|
public function test_it_only_counts_published_events(): void {
|
||||||
$counter = $this->container->get(PresentationCounter::class);
|
$action = $this->container->get(CountGivenPresentations::class);
|
||||||
assert($counter instanceof PresentationCounter);
|
assert($action instanceof CountGivenPresentations);
|
||||||
|
|
||||||
$count = $counter->getPastCount();
|
// Get the existing presentation count (including existing nodes).
|
||||||
|
$originalCount = $action();
|
||||||
|
|
||||||
$this->createPresentation(
|
$this->createPresentation(
|
||||||
events: Events::fromDateStrings('yesterday'),
|
events: Events::fromDateStrings('yesterday'),
|
||||||
isPublished: FALSE,
|
isPublished: FALSE,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Ensure the count has only increased by one, even though an unpublished
|
||||||
|
// presentation was created.
|
||||||
$this->assertSame(
|
$this->assertSame(
|
||||||
actual: $counter->getPastCount(),
|
actual: $action(),
|
||||||
expected: $count,
|
expected: $originalCount,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_it_only_counts_past_events(): void {
|
public function test_it_only_counts_past_events(): void {
|
||||||
$counter = $this->container->get(PresentationCounter::class);
|
$action = $this->container->get(CountGivenPresentations::class);
|
||||||
assert($counter instanceof PresentationCounter);
|
assert($action instanceof CountGivenPresentations);
|
||||||
|
|
||||||
// Get the existing presentation count (including existing nodes).
|
// Get the existing presentation count (including existing nodes).
|
||||||
$originalCount = $counter->getPastCount();
|
$originalCount = $action();
|
||||||
|
|
||||||
$this->assertGreaterThanOrEqual(
|
$this->assertGreaterThanOrEqual(
|
||||||
actual: $originalCount,
|
actual: $originalCount,
|
||||||
|
@ -64,11 +65,9 @@ final class PresentationCounterTest extends ExistingSiteBase {
|
||||||
Events::fromDateStrings('tomorrow', 'yesterday'),
|
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.
|
// Ensure the count has only increased by one, even though a future and past event were created.
|
||||||
$this->assertSame(
|
$this->assertSame(
|
||||||
actual: $counter->getPastCount(),
|
actual: $action(),
|
||||||
expected: $originalCount + 1,
|
expected: $originalCount + 1,
|
||||||
);
|
);
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue