Skip to content

Commit

Permalink
port passing entity to events to latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
cnizzardini committed Feb 18, 2024
1 parent 68d1ee1 commit 5a4c94d
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 28 deletions.
5 changes: 4 additions & 1 deletion src/Event/BaseEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace AuditStash\Event;

use AuditStash\EventInterface;
use Cake\Datasource\EntityInterface;
use DateTime;
use ReturnTypeWillChange;

Expand Down Expand Up @@ -38,13 +39,15 @@ abstract class BaseEvent implements EventInterface
* @param string $source The name of the source (table)
* @param array|null $changed The array of changes that got detected for the entity
* @param array|null $original The original values the entity had before it got changed
* @param \Cake\Datasource\EntityInterface|null $entity The entity being changed
*/
public function __construct(
string $transactionId,
mixed $id,
string $source,
?array $changed,
?array $original
?array $original,
?EntityInterface $entity
) {
$this->transactionId = $transactionId;
$this->id = $id;
Expand Down
17 changes: 17 additions & 0 deletions src/Event/BaseEventTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace AuditStash\Event;

use Cake\Datasource\EntityInterface;

/**
* Implements most of the methods of the EventInterface.
*/
Expand Down Expand Up @@ -50,6 +52,13 @@ trait BaseEventTrait
*/
protected array $meta = [];

/**
* The entity being changed.
*
* @var \Cake\Datasource\EntityInterface|null
*/
protected ?EntityInterface $entity = null;

/**
* Returns the global transaction id in which this event is contained.
*
Expand Down Expand Up @@ -135,4 +144,12 @@ public function setMetaInfo(array $meta): void
{
$this->meta = $meta;
}

/**
* @return \Cake\Datasource\EntityInterface|null
*/
public function getEntity(): ?EntityInterface
{
return $this->entity;
}
}
1 change: 1 addition & 0 deletions src/Event/SerializableEventTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ protected function basicSerialize(): array
'parent_source' => $this->parentSource,
'@timestamp' => $this->timestamp,
'meta' => $this->meta,
'entity' => $this->entity,
];
}
}
3 changes: 2 additions & 1 deletion src/EventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public function create(array $data): EventInterface
$data['primary_key'],
$data['source'],
$data['changed'],
$data['original']
$data['original'],
null
);
} else {
$event = new $map[$data['type']](
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Behavior/AuditLogBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function afterSave(
$auditEvent = $entity->isNew() ? AuditCreateEvent::class : AuditUpdateEvent::class;

$transaction = $options['_auditTransaction'];
$auditEvent = new $auditEvent($transaction, $primary, $this->_table->getTable(), $changed, $original);
$auditEvent = new $auditEvent($transaction, $primary, $this->_table->getTable(), $changed, $original, $entity);

if (!empty($options['_sourceTable'])) {
$auditEvent->setParentSourceName($options['_sourceTable']->getTable());
Expand Down
9 changes: 5 additions & 4 deletions tests/TestCase/Event/SerializeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use AuditStash\Event\AuditDeleteEvent;
use AuditStash\Event\AuditUpdateEvent;
use AuditStash\EventFactory;
use Cake\ORM\Entity;
use Cake\TestSuite\TestCase;

class SerializeTest extends TestCase
Expand All @@ -18,7 +19,7 @@ class SerializeTest extends TestCase
*/
public function testSerializeCreate()
{
$event = new AuditCreateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar']);
$event = new AuditCreateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar'], new Entity());
$event->setMetaInfo(['extra' => 'info']);
$serialized = serialize($event);
$this->assertEquals($event, unserialize($serialized));
Expand All @@ -31,7 +32,7 @@ public function testSerializeCreate()
*/
public function testSerializeUpdate()
{
$event = new AuditUpdateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar']);
$event = new AuditUpdateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar'], new Entity());
$event->setMetaInfo(['extra' => 'info']);
$serialized = serialize($event);
$this->assertEquals($event, unserialize($serialized));
Expand All @@ -58,7 +59,7 @@ public function testSerializeDelete()
public function testJsonSerializeCreate()
{
$factory = new EventFactory();
$event = new AuditCreateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar']);
$event = new AuditCreateEvent('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 All @@ -73,7 +74,7 @@ public function testJsonSerializeCreate()
public function testJsonSerializeUpdate()
{
$factory = new EventFactory();
$event = new AuditUpdateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar']);
$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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use AuditStash\Persister\ElasticSearchPersister;
use Cake\Datasource\ConnectionManager;
use Cake\I18n\DateTime;
use Cake\ORM\Entity;
use Cake\TestSuite\TestCase;

class ElasticSearchPersisterIntegrationTest extends TestCase
Expand Down Expand Up @@ -49,7 +50,7 @@ public function testLogSingleCreateEvent()
'published' => 'Y',
];

$events[] = new AuditCreateEvent('1234', 50, 'articles', $data, $data);
$events[] = new AuditCreateEvent('1234', 50, 'articles', $data, $data, new Entity());
$persister->logEvents($events);
$client->getIndex('article')->refresh();

Expand Down Expand Up @@ -108,7 +109,7 @@ public function testLogSingleUpdateEvent()
'published' => 'Y',
];

$events[] = new AuditUpdateEvent('1234', 50, 'articles', $changed, $original);
$events[] = new AuditUpdateEvent('1234', 50, 'articles', $changed, $original, new Entity());
$events[0]->setParentSourceName('authors');
$persister->logEvents($events);
$client->getIndex('article')->refresh();
Expand Down Expand Up @@ -194,7 +195,7 @@ public function testLogMultipleEvents()
'id' => 3,
'tag' => 'cakephp',
];
$events[] = new AuditCreateEvent('1234', 4, 'tags', $data, $data);
$events[] = new AuditCreateEvent('1234', 4, 'tags', $data, $data, new Entity());

$original = [
'title' => 'Old article title',
Expand All @@ -204,7 +205,7 @@ public function testLogMultipleEvents()
'title' => 'A new article',
'published' => 'Y',
];
$events[] = new AuditUpdateEvent('1234', 2, 'authors', $changed, $original);
$events[] = new AuditUpdateEvent('1234', 2, 'authors', $changed, $original, new Entity());
$events[] = new AuditDeleteEvent('1234', 50, 'articles');
$events[] = new AuditDeleteEvent('1234', 51, 'articles');

Expand Down Expand Up @@ -243,7 +244,7 @@ public function testPersistingTimeObjects()
'published_date' => new DateTime('2015-04-13 20:20:21'),
];

$events[] = new AuditUpdateEvent('1234', 50, 'articles', $changed, $original);
$events[] = new AuditUpdateEvent('1234', 50, 'articles', $changed, $original, new Entity());
$persister->logEvents($events);
$client->getIndex('article')->refresh();

Expand Down
3 changes: 2 additions & 1 deletion tests/TestCase/Persister/ElasticSearchPersisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use AuditStash\Event\AuditCreateEvent;
use AuditStash\Persister\ElasticSearchPersister;
use Cake\ElasticSearch\Datasource\Connection;
use Cake\ORM\Entity;
use Cake\TestSuite\TestCase;
use Elastica\Bulk\ResponseSet;
use Elastica\Client;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function testLogEvents()
'published' => 'Y',
];

$events[] = new AuditCreateEvent('1234', 50, 'articles', $data, $data);
$events[] = new AuditCreateEvent('1234', 50, 'articles', $data, $data, new Entity());
$clientMock->expects($this->once())->method('addDocuments');
$persister->logEvents($events);
}
Expand Down
30 changes: 15 additions & 15 deletions tests/TestCase/Persister/TablePersisterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function testSetInvalidTable()
*/
public function testSerializeNull()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', null, null);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', null, null, new Entity());
$event->setMetaInfo([]);

$entity = new Entity([
Expand Down Expand Up @@ -129,7 +129,7 @@ public function testSerializeNull()
*/
public function testExtractMetaFields()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity());
$event->setMetaInfo([
'foo' => 'bar',
'baz' => [
Expand Down Expand Up @@ -175,7 +175,7 @@ public function testExtractMetaFields()
*/
public function testExtractAllMetaFields()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity());
$event->setMetaInfo([
'foo' => 'bar',
'baz' => [
Expand Down Expand Up @@ -221,7 +221,7 @@ public function testExtractAllMetaFields()
*/
public function testExtractMetaFieldsDoNotUnset()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity());
$event->setMetaInfo([
'foo' => 'bar',
]);
Expand Down Expand Up @@ -262,7 +262,7 @@ public function testExtractMetaFieldsDoNotUnset()
*/
public function testExtractAllMetaFieldsDoNotUnset()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity());
$event->setMetaInfo([
'foo' => 'bar',
]);
Expand Down Expand Up @@ -301,7 +301,7 @@ public function testExtractAllMetaFieldsDoNotUnset()
*/
public function testErrorLogging()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity());

/** @var \AuditStash\Persister\TablePersister|\PHPUnit\Framework\MockObject\MockObject $TablePersister */
$TablePersister = $this
Expand Down Expand Up @@ -366,7 +366,7 @@ function ($event, EntityInterface $entity) {
}
);

$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity());
$TablePersister->logEvents([$event]);
}

Expand All @@ -375,7 +375,7 @@ function ($event, EntityInterface $entity) {
*/
public function testCompoundPrimaryKeyExtractDefault()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], [], new Entity());

$entity = new Entity([
'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96',
Expand Down Expand Up @@ -408,7 +408,7 @@ public function testCompoundPrimaryKeyExtractDefault()
*/
public function testPrimaryKeyExtractRaw()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity());

$entity = new Entity([
'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96',
Expand Down Expand Up @@ -442,7 +442,7 @@ public function testPrimaryKeyExtractRaw()
*/
public function testCompoundPrimaryKeyExtractRaw()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], [], new Entity());

$entity = new Entity([
'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96',
Expand Down Expand Up @@ -478,7 +478,7 @@ public function testCompoundPrimaryKeyExtractRaw()
*/
public function testPrimaryKeyExtractProperties()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity());

$entity = new Entity([
'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96',
Expand Down Expand Up @@ -512,7 +512,7 @@ public function testPrimaryKeyExtractProperties()
*/
public function testCompoundPrimaryKeyExtractProperties()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], [], new Entity());

$entity = new Entity([
'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96',
Expand Down Expand Up @@ -548,7 +548,7 @@ public function testCompoundPrimaryKeyExtractProperties()
*/
public function testPrimaryKeyExtractSerialized()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 'pk', 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 'pk', 'source', [], [], new Entity());

$entity = new Entity([
'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96',
Expand Down Expand Up @@ -584,7 +584,7 @@ public function testPrimaryKeyExtractSerialized()
*/
public function testCompoundPrimaryKeyExtractSerialized()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], [], new Entity());

$entity = new Entity([
'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96',
Expand Down Expand Up @@ -620,7 +620,7 @@ public function testCompoundPrimaryKeyExtractSerialized()
*/
public function testDoNotSerializeFields()
{
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []);
$event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity());
$event->setMetaInfo([
'foo' => 'bar',
]);
Expand Down

0 comments on commit 5a4c94d

Please sign in to comment.