Skip to content

Commit bdce6c0

Browse files
authored
IBX-9060: Added TypedNotificationRenderer interface for notification type labeling (#610)
* IBX-9060: Add TypedNotificationRenderer interface for notification type labeling * IBX-9060: Renamed TypedNotificationRenderer to TypedNotificationRendererInterface * IBX-9060: Added PHPUnit test for Registry::getTypeLabels() method
1 parent e45fcea commit bdce6c0

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

src/lib/Notification/Renderer/Registry.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ public function hasRenderer(string $alias): bool
4141
{
4242
return isset($this->registry[$alias]);
4343
}
44+
45+
/**
46+
* @return array<string, string>
47+
*/
48+
public function getTypeLabels(): array
49+
{
50+
$labels = [];
51+
foreach ($this->registry as $type => $renderer) {
52+
if ($renderer instanceof TypedNotificationRendererInterface) {
53+
$labels[$type] = $renderer->getTypeLabel();
54+
}
55+
}
56+
ksort($labels);
57+
58+
return $labels;
59+
}
4460
}
4561

4662
class_alias(Registry::class, 'eZ\Publish\Core\Notification\Renderer\Registry');
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Core\Notification\Renderer;
10+
11+
interface TypedNotificationRendererInterface
12+
{
13+
public function getTypeLabel(): string;
14+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Core\Notification\Renderer;
10+
11+
use Ibexa\Contracts\Core\Repository\Values\Notification\Notification;
12+
use Ibexa\Core\Notification\Renderer\NotificationRenderer;
13+
use Ibexa\Core\Notification\Renderer\Registry;
14+
use Ibexa\Core\Notification\Renderer\TypedNotificationRendererInterface;
15+
use PHPUnit\Framework\TestCase;
16+
17+
final class RegistryTest extends TestCase
18+
{
19+
public function testGetTypeLabelsReturnsSortedLabelsFromTypedRenderers(): void
20+
{
21+
$typedRendererA = new class() implements NotificationRenderer, TypedNotificationRendererInterface {
22+
public function render(Notification $notification): string
23+
{
24+
return 'Rendered A';
25+
}
26+
27+
public function generateUrl(Notification $notification): ?string
28+
{
29+
return null;
30+
}
31+
32+
public function getTypeLabel(): string
33+
{
34+
return 'Label A';
35+
}
36+
};
37+
38+
$typedRendererB = new class() implements NotificationRenderer, TypedNotificationRendererInterface {
39+
public function render(Notification $notification): string
40+
{
41+
return 'Rendered B';
42+
}
43+
44+
public function generateUrl(Notification $notification): ?string
45+
{
46+
return null;
47+
}
48+
49+
public function getTypeLabel(): string
50+
{
51+
return 'Label B';
52+
}
53+
};
54+
55+
$nonTypedRenderer = $this->createMock(NotificationRenderer::class);
56+
57+
$registry = new Registry();
58+
$registry->addRenderer('z_type', $typedRendererB);
59+
$registry->addRenderer('a_type', $typedRendererA);
60+
$registry->addRenderer('x_type', $nonTypedRenderer);
61+
62+
$expected = [
63+
'a_type' => 'Label A',
64+
'z_type' => 'Label B',
65+
];
66+
67+
$this->assertSame($expected, $registry->getTypeLabels());
68+
}
69+
}

0 commit comments

Comments
 (0)