Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/lib/Notification/Renderer/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ public function hasRenderer(string $alias): bool
{
return isset($this->registry[$alias]);
}

/**
* @return array<string, string>
*/
public function getTypeLabels(): array
{
$labels = [];
foreach ($this->registry as $type => $renderer) {
if ($renderer instanceof TypedNotificationRendererInterface) {
$labels[$type] = $renderer->getTypeLabel();
}
}
ksort($labels);

return $labels;
}
}

class_alias(Registry::class, 'eZ\Publish\Core\Notification\Renderer\Registry');
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Core\Notification\Renderer;

interface TypedNotificationRendererInterface
{
public function getTypeLabel(): string;
}
69 changes: 69 additions & 0 deletions tests/lib/Notification/Renderer/RegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Core\Notification\Renderer;

use Ibexa\Contracts\Core\Repository\Values\Notification\Notification;
use Ibexa\Core\Notification\Renderer\NotificationRenderer;
use Ibexa\Core\Notification\Renderer\Registry;
use Ibexa\Core\Notification\Renderer\TypedNotificationRendererInterface;
use PHPUnit\Framework\TestCase;

final class RegistryTest extends TestCase
{
public function testGetTypeLabelsReturnsSortedLabelsFromTypedRenderers(): void
{
$typedRendererA = new class() implements NotificationRenderer, TypedNotificationRendererInterface {
public function render(Notification $notification): string
{
return 'Rendered A';
}

public function generateUrl(Notification $notification): ?string
{
return null;
}

public function getTypeLabel(): string
{
return 'Label A';
}
};

$typedRendererB = new class() implements NotificationRenderer, TypedNotificationRendererInterface {
public function render(Notification $notification): string
{
return 'Rendered B';
}

public function generateUrl(Notification $notification): ?string
{
return null;
}

public function getTypeLabel(): string
{
return 'Label B';
}
};

$nonTypedRenderer = $this->createMock(NotificationRenderer::class);

$registry = new Registry();
$registry->addRenderer('z_type', $typedRendererB);
$registry->addRenderer('a_type', $typedRendererA);
$registry->addRenderer('x_type', $nonTypedRenderer);

$expected = [
'a_type' => 'Label A',
'z_type' => 'Label B',
];

$this->assertSame($expected, $registry->getTypeLabels());
}
}
Loading