-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
487 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
src/EventSystem/ElementPropertyChange/Event/ElementPropertyChangeEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...tSystem/ElementPropertyChange/EventListener/CreatedElementPropertyChangeEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/EventSystem/ElementPropertyChange/EventListener/IdElementPropertyChangeEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...entSystem/ElementPropertyChange/EventListener/TokenElementPropertyChangeEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...tSystem/ElementPropertyChange/EventListener/UpdatedElementPropertyChangeEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ventSystem/ElementPropertyChange/EventListener/UserElementPropertyChangeEventListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
38 changes: 38 additions & 0 deletions
38
tests/UnitTests/EventSystem/ElementPropertyChange/Event/ElementPropertyChangeEventTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...tem/ElementPropertyChange/EventListener/CreatedElementPropertyChangeEventListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.