Skip to content

Commit

Permalink
Merge pull request #82 from cnizzardini/passing-entity-to-events-fixes
Browse files Browse the repository at this point in the history
Set entity property in BaseEvent
  • Loading branch information
cnizzardini authored Feb 18, 2024
2 parents f098114 + 8d18809 commit 5817566
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
10 changes: 9 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="tests/bootstrap.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
colors="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="tests/bootstrap.php"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd"
cacheDirectory=".phpunit.cache"
displayDetailsOnTestsThatTriggerWarnings="true"
>
<coverage/>
<php>
<ini name="memory_limit" value="-1"/>
Expand Down
1 change: 1 addition & 0 deletions src/Event/BaseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function __construct(
$this->changed = $changed;
$this->original = $original;
$this->timestamp = (new DateTime())->format(DateTime::ATOM);
$this->entity = $entity;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/EventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use AuditStash\Event\AuditCreateEvent;
use AuditStash\Event\AuditDeleteEvent;
use AuditStash\Event\AuditUpdateEvent;
use Cake\ORM\Entity;
use ReflectionObject;

/**
Expand Down Expand Up @@ -35,15 +36,16 @@ public function create(array $data): EventInterface
$data['transaction'],
$data['primary_key'],
$data['source'],
$data['changed'],
$data['original'],
null
array_key_exists('changed', $data) ? $data['changed'] : [],
array_key_exists('original', $data) ? $data['original'] : [],
new Entity()
);
} else {
$event = new $map[$data['type']](
$data['transaction'],
$data['primary_key'],
$data['source']
$data['source'],
null
);
}

Expand Down
18 changes: 16 additions & 2 deletions tests/TestCase/Event/SerializeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ public function testSerializeDelete()
public function testJsonSerializeCreate()
{
$factory = new EventFactory();
$event = new AuditCreateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar'], new Entity());
$event = new AuditCreateEvent(
'123',
50,
'articles',
['title' => 'foo'],
null,
new Entity()
);
$event->setMetaInfo(['extra' => 'info']);
$serialized = json_encode($event);
$result = $factory->create(json_decode($serialized, true));
Expand All @@ -74,7 +81,14 @@ public function testJsonSerializeCreate()
public function testJsonSerializeUpdate()
{
$factory = new EventFactory();
$event = new AuditUpdateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar'], new Entity());
$event = new AuditUpdateEvent(
'123',
50,
'articles',
['title' => 'foo'],
['title' => 'bar'],
new Entity()
);
$event->setMetaInfo(['extra' => 'info']);
$serialized = json_encode($event);
$result = $factory->create(json_decode($serialized, true));
Expand Down

0 comments on commit 5817566

Please sign in to comment.