Skip to content

Commit

Permalink
Rework event system.
Browse files Browse the repository at this point in the history
  • Loading branch information
Syndesi committed Aug 14, 2023
1 parent c4c0c0e commit 34c3de1
Show file tree
Hide file tree
Showing 15 changed files with 487 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"guzzlehttp/guzzle": "^7.5",
"infection/infection": "^0.27",
"phpbench/phpbench": "^1.2",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "^1.6",
"phpunit/php-code-coverage": "^9.2",
"phpunit/phpunit": "^9.5",
Expand Down
124 changes: 122 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\EventSystem\ElementPropertyChange\Event;

use App\Contract\ElementInterface;
use App\Contract\EventInterface;
use App\Trait\StoppableEventTrait;

class ElementPropertyChangeEvent implements EventInterface
{
use StoppableEventTrait;

/**
* @param array<string, mixed> $changedProperties
*/
public function __construct(
private string $type,
private ?ElementInterface $element,
private array $changedProperties
) {
}

public function getType(): string
{
return $this->type;
}

public function getElement(): ?ElementInterface
{
return $this->element;
}

/**
* @return array<string, mixed>
*/
public function getChangedProperties(): array
{
return $this->changedProperties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\EventSystem\ElementPropertyChange\EventListener;

use App\EventSystem\ElementPropertyChange\Event\ElementPropertyChangeEvent;

class CreatedElementPropertyChangeEventListener
{
public function __construct()
{
}

public function onElementPropertyChangeEvent(ElementPropertyChangeEvent $event): void
{
if (!array_key_exists('created', $event->getChangedProperties())) {
return;
}
throw new \Exception("Setting the property 'created' is forbidden.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\EventSystem\ElementPropertyChange\EventListener;

use App\EventSystem\ElementPropertyChange\Event\ElementPropertyChangeEvent;

class IdElementPropertyChangeEventListener
{
public function __construct()
{
}

public function onElementPropertyChangeEvent(ElementPropertyChangeEvent $event): void
{
if (!array_key_exists('id', $event->getChangedProperties())) {
return;
}
throw new \Exception("Setting the property 'id' is forbidden.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\EventSystem\ElementPropertyChange\EventListener;

use App\EventSystem\ElementPropertyChange\Event\ElementPropertyChangeEvent;

class TokenElementPropertyChangeEventListener
{
public function __construct()
{
}

public function onElementPropertyChangeEvent(ElementPropertyChangeEvent $event): void
{
if ('Token' !== $event->getType()) {
return;
}
if (array_key_exists('token', $event->getChangedProperties())) {
throw new \Exception("Setting the property 'token' is forbidden.");
}
if (array_key_exists('_tokenHash', $event->getChangedProperties())) {
throw new \Exception("Setting the property '_tokenHash' is forbidden.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\EventSystem\ElementPropertyChange\EventListener;

use App\EventSystem\ElementPropertyChange\Event\ElementPropertyChangeEvent;

class UpdatedElementPropertyChangeEventListener
{
public function __construct()
{
}

public function onElementPropertyChangeEvent(ElementPropertyChangeEvent $event): void
{
if (!array_key_exists('updated', $event->getChangedProperties())) {
return;
}
throw new \Exception("Setting the property 'updated' is forbidden.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\EventSystem\ElementPropertyChange\EventListener;

use App\EventSystem\ElementPropertyChange\Event\ElementPropertyChangeEvent;
use EmberNexusBundle\Service\EmberNexusConfiguration;

class UserElementPropertyChangeEventListener
{
public function __construct(
private EmberNexusConfiguration $emberNexusConfiguration
) {
}

public function onElementPropertyChangeEvent(ElementPropertyChangeEvent $event): void
{
if ('User' !== $event->getType()) {
return;
}
if (array_key_exists('_passwordHash', $event->getChangedProperties())) {
throw new \Exception("Setting the property '_passwordHash' is forbidden.");
}
if (array_key_exists($this->emberNexusConfiguration->getRegisterUniqueIdentifier(), $event->getChangedProperties())) {
throw new \Exception(sprintf("Setting the property '%s' is forbidden.", $this->emberNexusConfiguration->getRegisterUniqueIdentifier()));
}
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Tests\UnitTests\EventSystem\ElementPropertyChange\Event;

use App\EventSystem\ElementPropertyChange\Event\ElementPropertyChangeEvent;
use App\Type\NodeElement;
use PHPUnit\Framework\TestCase;

class ElementPropertyChangeEventTest extends TestCase
{
public function testEventReturnsCorrectWithoutElementData(): void
{
$event = new ElementPropertyChangeEvent('type', null, ['a' => 'A']);
$this->assertSame('type', $event->getType());
$this->assertNull($event->getElement());
$changedProperties = $event->getChangedProperties();
$this->assertIsArray($changedProperties);
/**
* @var $changedProperties array<string, mixed>
*/
$this->assertSame('A', $changedProperties['a']);
}

public function testEventReturnsCorrectWithElementData(): void
{
$element = new NodeElement();
$element->setLabel('Element');
$event = new ElementPropertyChangeEvent('type', $element, ['b' => 'B']);
$this->assertSame('type', $event->getType());
$this->assertSame($element, $event->getElement());
$changedProperties = $event->getChangedProperties();
$this->assertIsArray($changedProperties);
/**
* @var $changedProperties array<string, mixed>
*/
$this->assertSame('B', $changedProperties['b']);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Tests\UnitTests\EventSystem\ElementPropertyChange\EventListener;

use App\EventSystem\ElementPropertyChange\Event\ElementPropertyChangeEvent;
use App\EventSystem\ElementPropertyChange\EventListener\CreatedElementPropertyChangeEventListener;
use PHPUnit\Framework\TestCase;

class CreatedElementPropertyChangeEventListenerTest extends TestCase
{
public function testElementsWithoutCreatedPropertyAreIgnored(): void
{
self::expectNotToPerformAssertions();
$event = new ElementPropertyChangeEvent('Test', null, ['name' => 'Test']);
$eventListener = new CreatedElementPropertyChangeEventListener();
$eventListener->onElementPropertyChangeEvent($event);
}

public function testElementWithCreatedPropertyTriggersException(): void
{
$event = new ElementPropertyChangeEvent('Test', null, ['created' => true]);
$eventListener = new CreatedElementPropertyChangeEventListener();
$this->expectException(\Exception::class);
$eventListener->onElementPropertyChangeEvent($event);
}
}
Loading

0 comments on commit 34c3de1

Please sign in to comment.