Create basic Event class, create NamedEvent interface

This commit is contained in:
Art4 2025-01-01 07:31:44 +00:00
parent 021ffb519b
commit 8cd3fda857
7 changed files with 63 additions and 28 deletions

33
src/Event/Event.php Normal file
View file

@ -0,0 +1,33 @@
<?php
// Copyright (C) 2010-2024, the Friendica project
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
//
// SPDX-License-Identifier: AGPL-3.0-or-later
declare(strict_types=1);
namespace Friendica\Event;
/**
* One-way Event to inform listener about something happend.
*/
final class Event implements NamedEvent
{
/**
* Friendica is initialized.
*/
public const INIT = 'friendica.init';
private string $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
}

View file

@ -12,7 +12,7 @@ namespace Friendica\Event;
/**
* Allow Event listener to modify HTML.
*/
final class HtmlFilterEvent
final class HtmlFilterEvent implements NamedEvent
{
public const HEAD = 'friendica.html.head';

View file

@ -10,24 +10,9 @@ declare(strict_types=1);
namespace Friendica\Event;
/**
* One-way Event to inform listener about something happend.
* Interface for named events.
*/
final class NamedEvent
interface NamedEvent
{
/**
* Friendica is initialized.
*/
public const INIT = 'friendica.init';
private string $name;
public function __construct(string $name)
{
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
public function getName(): string;
}

View file

@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Friendica\EventSubscriber;
use Friendica\Core\Hook;
use Friendica\Event\Event;
use Friendica\Event\HtmlFilterEvent;
use Friendica\Event\NamedEvent;
@ -29,7 +30,7 @@ final class HookEventBridge implements StaticEventSubscriber
* This maps the new event names to the legacy Hook names.
*/
private static array $eventMapper = [
NamedEvent::INIT => 'init_1',
Event::INIT => 'init_1',
HtmlFilterEvent::HEAD => 'head',
HtmlFilterEvent::FOOTER => 'footer',
HtmlFilterEvent::PAGE_CONTENT_TOP => 'page_content_top',
@ -42,7 +43,7 @@ final class HookEventBridge implements StaticEventSubscriber
public static function getStaticSubscribedEvents(): array
{
return [
NamedEvent::class => 'onNamedEvent',
Event::class => 'onNamedEvent',
HtmlFilterEvent::class => 'onHtmlFilterEvent',
];
}

View file

@ -9,15 +9,23 @@ declare(strict_types=1);
namespace Friendica\Test\Unit\Event;
use Friendica\Event\Event;
use Friendica\Event\NamedEvent;
use PHPUnit\Framework\TestCase;
class NamedEventTest extends TestCase
class EventTest extends TestCase
{
public function testImplementationOfInstances(): void
{
$event = new Event('test');
$this->assertInstanceOf(NamedEvent::class, $event);
}
public static function getPublicConstants(): array
{
return [
[NamedEvent::INIT, 'friendica.init'],
[Event::INIT, 'friendica.init'],
];
}
@ -31,7 +39,7 @@ class NamedEventTest extends TestCase
public function testGetNameReturnsName(): void
{
$event = new NamedEvent('test');
$event = new Event('test');
$this->assertSame('test', $event->getName());
}

View file

@ -10,10 +10,18 @@ declare(strict_types=1);
namespace Friendica\Test\Unit\Event;
use Friendica\Event\HtmlFilterEvent;
use Friendica\Event\NamedEvent;
use PHPUnit\Framework\TestCase;
class HtmlFilterEventTest extends TestCase
{
public function testImplementationOfInstances(): void
{
$event = new HtmlFilterEvent('test', 'original');
$this->assertInstanceOf(NamedEvent::class, $event);
}
public static function getPublicConstants(): array
{
return [

View file

@ -9,8 +9,8 @@ declare(strict_types=1);
namespace Friendica\Test\Unit\EventSubscriber;
use Friendica\Event\Event;
use Friendica\Event\HtmlFilterEvent;
use Friendica\Event\NamedEvent;
use Friendica\EventSubscriber\HookEventBridge;
use Friendica\EventSubscriber\StaticEventSubscriber;
use PHPUnit\Framework\TestCase;
@ -28,7 +28,7 @@ class HookEventBridgeTest extends TestCase
public function testGetStaticSubscribedEventsReturnsStaticMethods(): void
{
$expected = [
NamedEvent::class => 'onNamedEvent',
Event::class => 'onNamedEvent',
HtmlFilterEvent::class => 'onHtmlFilterEvent',
];
@ -54,7 +54,7 @@ class HookEventBridgeTest extends TestCase
{
return [
['test', 'test'],
[NamedEvent::INIT, 'init_1'],
[Event::INIT, 'init_1'],
];
}
@ -63,7 +63,7 @@ class HookEventBridgeTest extends TestCase
*/
public function testOnNamedEventCallsHook($name, $expected): void
{
$event = new NamedEvent($name);
$event = new Event($name);
$reflectionProperty = new \ReflectionProperty(HookEventBridge::class, 'mockedCallHook');
$reflectionProperty->setAccessible(true);