Files
myep/tests/Dashboard/StuckBookingDraftsWidgetProviderTest.php
T

94 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\Dashboard;
use App\Dashboard\Widget\StuckBookingDraftsWidgetProvider;
use App\Entity\BookingEditDraft;
use App\Entity\User;
use App\Repository\BookingEditDraftRepository;
use App\Security\Role;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class StuckBookingDraftsWidgetProviderTest extends TestCase
{
public function testAsksForDraftsOlderThanAWeekAndRequiresCustomerExpert(): void
{
$repository = $this->createMock(BookingEditDraftRepository::class);
$repository->expects($this->once())
->method('findStuck')
->with(7, 10)
->willReturn([])
;
$provider = new StuckBookingDraftsWidgetProvider($repository, $this->urlGenerator());
$this->assertSame(Role::CUSTOMER_EXPERT, $provider->getRequiredRole());
$this->assertNotNull($provider->build());
}
public function testSaysSoWhenNothingIsStuck(): void
{
$widget = $this->provider([])->build();
$this->assertNotNull($widget);
$this->assertSame([], $widget->entries);
$this->assertSame('Keine festhängenden Buchungsänderungen.', $widget->emptyText);
}
public function testLabelNamesTheBookingTheCustomerAndHowLongItHasBeenStuck(): void
{
$widget = $this->provider([$this->draft(111564, '[email protected]', 61)])->build();
$this->assertNotNull($widget);
$this->assertStringContainsString('Vorgang 111564', $widget->entries[0]->label);
$this->assertStringContainsString('[email protected]', $widget->entries[0]->label);
$this->assertStringContainsString('seit 61 Tagen', $widget->entries[0]->label);
}
public function testEntriesLinkToTheDraft(): void
{
$widget = $this->provider([$this->draft(111564, '[email protected]', 8)])->build();
$this->assertNotNull($widget);
$this->assertNotNull($widget->entries[0]->url);
$this->assertSame('Alle Buchungsentwürfe', $widget->actionLabel);
}
/**
* @param BookingEditDraft[] $drafts
*/
private function provider(array $drafts): StuckBookingDraftsWidgetProvider
{
$repository = $this->createStub(BookingEditDraftRepository::class);
$repository->method('findStuck')->willReturn($drafts);
return new StuckBookingDraftsWidgetProvider($repository, $this->urlGenerator());
}
private function urlGenerator(): UrlGeneratorInterface
{
$urlGenerator = $this->createStub(UrlGeneratorInterface::class);
$urlGenerator->method('generate')->willReturnCallback(
static fn (string $route): string => '/'.str_replace('_', '/', substr($route, \strlen('app_'))),
);
return $urlGenerator;
}
private function draft(int $bookingNumber, string $email, int $ageDays): BookingEditDraft
{
$draft = new BookingEditDraft(new User($email), 98787, new \DateTimeImmutable('+60 days'), []);
$draft->setBookingNumber($bookingNumber);
// createdAt is stamped by the constructor and has no setter - the age is the whole point of
// this widget, so it is set directly rather than asserted away.
$createdAt = new \ReflectionProperty(BookingEditDraft::class, 'createdAt');
$createdAt->setValue($draft, new \DateTimeImmutable(sprintf('-%d days', $ageDays)));
return $draft;
}
}