feat: admin dashboard widgets
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Dashboard;
|
||||
|
||||
use App\Dashboard\Widget\RecentLogEntriesWidgetProvider;
|
||||
use App\Entity\LogEntry;
|
||||
use App\Repository\LogEntryRepository;
|
||||
use App\Security\Role;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
class RecentLogEntriesWidgetProviderTest extends TestCase
|
||||
{
|
||||
public function testEntriesStayDisplayOnly(): void
|
||||
{
|
||||
$widget = $this->provider([$this->logEntry('bpn', 'Verbindung wiederhergestellt')])->build();
|
||||
|
||||
$this->assertNotNull($widget);
|
||||
$this->assertNull($widget->entries[0]->url);
|
||||
}
|
||||
|
||||
public function testHeaderLinksToTheFullLog(): void
|
||||
{
|
||||
$widget = $this->provider([$this->logEntry('bpn', 'Verbindung wiederhergestellt')])->build();
|
||||
|
||||
$this->assertNotNull($widget);
|
||||
$this->assertSame('/admin/log', $widget->actionUrl);
|
||||
$this->assertSame('Alle Logeinträge', $widget->actionLabel);
|
||||
}
|
||||
|
||||
public function testLabelNamesWhenWhereAndWhat(): void
|
||||
{
|
||||
$widget = $this->provider([$this->logEntry('auth', 'Anmeldung fehlgeschlagen')])->build();
|
||||
|
||||
$this->assertNotNull($widget);
|
||||
$this->assertSame('24.08.2026 09:30 auth: Anmeldung fehlgeschlagen', $widget->entries[0]->label);
|
||||
}
|
||||
|
||||
public function testLongMessagesAreCutToFitOneLine(): void
|
||||
{
|
||||
$widget = $this->provider([$this->logEntry('core', str_repeat('a', 80))])->build();
|
||||
|
||||
$this->assertNotNull($widget);
|
||||
$this->assertStringEndsWith(str_repeat('a', 60).'…', $widget->entries[0]->label);
|
||||
}
|
||||
|
||||
public function testAsksForFiveEntriesAndRequiresAdmin(): void
|
||||
{
|
||||
$repository = $this->createMock(LogEntryRepository::class);
|
||||
$repository->expects($this->once())
|
||||
->method('findLatest')
|
||||
->with(5)
|
||||
->willReturn([])
|
||||
;
|
||||
|
||||
$provider = new RecentLogEntriesWidgetProvider($repository, $this->urlGenerator());
|
||||
|
||||
$this->assertSame(Role::ADMIN, $provider->getRequiredRole());
|
||||
$this->assertNotNull($provider->build());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param LogEntry[] $entries
|
||||
*/
|
||||
private function provider(array $entries): RecentLogEntriesWidgetProvider
|
||||
{
|
||||
$repository = $this->createMock(LogEntryRepository::class);
|
||||
$repository->method('findLatest')->willReturn($entries);
|
||||
|
||||
return new RecentLogEntriesWidgetProvider($repository, $this->urlGenerator());
|
||||
}
|
||||
|
||||
private function urlGenerator(): UrlGeneratorInterface
|
||||
{
|
||||
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
|
||||
$urlGenerator->method('generate')->willReturnCallback(
|
||||
static fn (string $route): string => '/'.str_replace('_', '/', substr($route, \strlen('app_'))),
|
||||
);
|
||||
|
||||
return $urlGenerator;
|
||||
}
|
||||
|
||||
private function logEntry(string $channel, string $message): LogEntry
|
||||
{
|
||||
return (new LogEntry($channel, $message))
|
||||
->setCreatedAt(new \DateTimeImmutable('2026-08-24 09:30:00'))
|
||||
;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user