Skip to content

Commit

Permalink
Finishing implementing test for DataObjectEvent flow
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime-rainville committed Nov 7, 2024
1 parent 3fd5c4f commit 4b29b73
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 99 deletions.
19 changes: 19 additions & 0 deletions src/DataObjectEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace ArchiPro\Silverstripe\EventDispatcher;

use ArchiPro\EventDispatcher\ListenerProvider;
use ArchiPro\Silverstripe\EventDispatcher\Event\DataObjectEvent;
use ArchiPro\Silverstripe\EventDispatcher\Event\Operation;
use ArchiPro\Silverstripe\EventDispatcher\Service\EventService;
use Closure;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DataObject;

/**
Expand All @@ -16,6 +20,8 @@
*/
class DataObjectEventListener
{
use Injectable;

/**
* Creates a new DataObject event listener.
*
Expand All @@ -31,6 +37,19 @@ public function __construct(
$this->operations = $operations ?? Operation::cases();
}

/**
* Registers this listener with the given provider.
*
* If no provider is provided, the global EventService will be used.
*/
public function selfRegister(ListenerProvider|EventService $provider = null): void
{
if (empty($provider)) {
$provider = Injector::inst()->get(EventService::class);
}
$provider->addListener(DataObjectEvent::class, $this);
}

/**
* Handles a DataObject event.
*
Expand Down
13 changes: 4 additions & 9 deletions src/Event/DataObjectEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ArchiPro\Silverstripe\EventDispatcher\Event;

use SilverStripe\Core\Extension;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\ORM\DataObject;
use SilverStripe\Security\Member;
Expand Down Expand Up @@ -118,17 +119,11 @@ public function getObject(bool $useVersion = false): ?DataObject
return null;
}

$object = DataObject::get_by_id($this->objectClass, $this->objectID);

// If we want the specific version and the object is versioned
if ($useVersion && $this->version && $object && $object->hasExtension(Versioned::class)) {
/** @var Versioned|DataObject $object */
return $object->Version == $this->version
? $object
: $object->Versions()->byID($this->version);
if (!$useVersion || empty($this->version)) {
return DataObject::get_by_id($this->objectClass, $this->objectID, false);
}

return $object;
return Versioned::get_version($this->objectClass, $this->objectID, $this->version);
}

/**
Expand Down
43 changes: 0 additions & 43 deletions tests/php/Event/AbstractDataObjectEventTest.php

This file was deleted.

42 changes: 0 additions & 42 deletions tests/php/Event/DataObjectVersionEventTest.php

This file was deleted.

7 changes: 2 additions & 5 deletions tests/php/Extension/EventDispatchExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

$service = Injector::inst()->get(EventService::class);

// Add listener that captures events
$service->addListener(DataObjectEvent::class, new DataObjectEventListener(
DataObjectEventListener::create(
function (DataObjectEvent $event) {
static::$events[] = $event;
},
[SimpleDataObject::class, VersionedDataObject::class]
));
)->selfRegister();
}

protected function setUp(): void
Expand Down

0 comments on commit 4b29b73

Please sign in to comment.