Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions Attribute/SubscribeDoctrineEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,14 @@
*
* @author Roni Saha <roni@xiidea.net>
*/

#[\Attribute(\Attribute::TARGET_CLASS)]
/* @final */ class SubscribeDoctrineEvents
class SubscribeDoctrineEvents
{
public $events = array();
public array $events = [];

public function __construct(array $values)
public function __construct(array|string $values)
{
if (isset($values['value'])) {
$values['events'] = $values['value'];
}
if (!isset($values['events'])) {
return;
}

$this->events = is_array($values['events']) ? $values['events'] : array_map('trim', explode(',', $values['events']));
$this->events = is_array($values) ? $values : array_map('trim', explode(',', $values));

$this->events = array_filter($this->events);
}
Expand Down
28 changes: 9 additions & 19 deletions Tests/Attribute/SubscribeDoctrineEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,36 @@ class SubscribeDoctrineEventsTest extends TestCase
{
public function testConstructWithoutData()
{
$annotation = new SubscribeDoctrineEvents(array());
$annotation = new SubscribeDoctrineEvents([]);

$this->assertTrue(is_array($annotation->events));
$this->assertEmpty($annotation->events);
}

public function testConstructWithInvalidData()
{
$data = array(
'unknown' => 'foo',
'array' => array('bar' => 'bar'),
);

$annotation = new SubscribeDoctrineEvents($data);

$this->assertTrue(is_array($annotation->events));
$annotation = new SubscribeDoctrineEvents("");
$this->assertEmpty($annotation->events);
}

public function testConstructWithValue()

public function testConstructWithStringValue()
{
$data = array('value' => 'updated,created');
$data = 'created,updated';

$annotation = new SubscribeDoctrineEvents($data);

$this->assertTrue(is_array($annotation->events));
$this->assertNotEmpty($annotation->events);

$this->assertEquals(explode(',', $data['value']), $annotation->events);
$this->assertEquals(explode(',', $data), $annotation->events);
}

public function testConstructWithEvent()
public function testConstructWithArrayValue()
{
$data = array('events' => 'updated,created');
$data = ['created', 'updated'];

$annotation = new SubscribeDoctrineEvents($data);

$this->assertTrue(is_array($annotation->events));
$this->assertNotEmpty($annotation->events);

$this->assertEquals(explode(',', $data['events']), $annotation->events);
$this->assertEquals($data, $annotation->events);
}
}
8 changes: 4 additions & 4 deletions Tests/Fixtures/ODM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class UnitOfWork implements PropertyChangedListener
/**
* Collect information about a property change.
*
* @param object $sender the object on which the property changed
* @param object $sender the object on which the property changed
* @param string $propertyName the name of the property that changed
* @param mixed $oldValue the old value of the property that changed
* @param mixed $newValue the new value of the property that changed
* @param mixed $oldValue the old value of the property that changed
* @param mixed $newValue the new value of the property that changed
*/
public function propertyChanged($sender, $propertyName, $oldValue, $newValue)
public function propertyChanged($sender, $propertyName, $oldValue, $newValue): void
{
}

Expand Down
9 changes: 1 addition & 8 deletions Tests/Fixtures/ORM/Movie.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,12 @@
#[ORM\Entity]
class Movie
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/

#[ORM\Id]
#[ORM\Column(type: 'integer')]
#[ORM\GeneratedValue(strategy: "AUTO")]
protected $id;

/**
* @ORM\Column(type="string")
*/
#[ORM\Column(type: 'string')]
protected $name;

Expand Down
8 changes: 4 additions & 4 deletions Tests/Fixtures/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class UnitOfWork implements PropertyChangedListener
/**
* Collect information about a property change.
*
* @param object $sender the object on which the property changed
* @param object $sender the object on which the property changed
* @param string $propertyName the name of the property that changed
* @param mixed $oldValue the old value of the property that changed
* @param mixed $newValue the new value of the property that changed
* @param mixed $oldValue the old value of the property that changed
* @param mixed $newValue the new value of the property that changed
*/
public function propertyChanged($sender, $propertyName, $oldValue, $newValue)
public function propertyChanged($sender, $propertyName, $oldValue, $newValue): void
{
}

Expand Down
76 changes: 33 additions & 43 deletions Tests/Subscriber/DoctrineSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@

use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Xiidea\EasyAuditBundle\Subscriber\DoctrineSubscriber;
use Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\DummyEntity;
use Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie;
use Doctrine\Persistence\ObjectManager;

class DoctrineSubscriberTest extends TestCase
{
/** @var MockObject */
private $dispatcher;


/** @var MockObject */
private $entityManager;

Expand All @@ -43,86 +42,77 @@ public function setUp(): void
}


public function testCreateEventForAttributedEntity()
{
$subscriber = new DoctrineSubscriber(array());

$this->invokeCreatedEventCall($subscriber);
}

public function testCreateEventForEntityNotConfiguredToTrack()
{
$subscriber = new DoctrineSubscriber(array());
$this->invokeCreatedEventCall($subscriber, new DummyEntity());
}

public function testCreateEventForEntityConfiguredToTrack()
{
$subscriber = new DoctrineSubscriber(
array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array('created'))
);

$this->invokeCreatedEventCall($subscriber);
}

public function testCreateEventForEntityConfiguredToTrackAllEvents()
public function testEntityConfigureToTrackForAttributedEntity()
{
$subscriber = new DoctrineSubscriber(array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array()));
$subscriber = new DoctrineSubscriber([]);

$this->invokeCreatedEventCall($subscriber);
$subscriber->setDispatcher($this->dispatcher);
$subscriber->postPersist(new LifecycleEventArgs(new Movie(), $this->entityManager));
$subscriber->postUpdate(new LifecycleEventArgs(new Movie(), $this->entityManager));
$subscriber->preRemove(new LifecycleEventArgs(new Movie(), $this->entityManager));
$subscriber->postRemove(new LifecycleEventArgs(new Movie(), $this->entityManager));
$this->assertTrue(true);
}

public function testUpdateEventForEntityNotConfiguredToTrack()
public function testEntityNotConfiguredToTrack()
{
$subscriber = new DoctrineSubscriber(array());
$this->invokeUpdatedEventCall($subscriber, new DummyEntity());
$subscriber = new DoctrineSubscriber([]);
$subscriber->setDispatcher($this->dispatcher);
$subscriber->postPersist(new LifecycleEventArgs(new DummyEntity(), $this->entityManager));
$this->assertTrue(true);
}

public function testRemovedEventForEntityNotConfiguredToTrack()
{
$subscriber = new DoctrineSubscriber(array());
$subscriber = new DoctrineSubscriber([]);
$this->invokeDeletedEventCall($subscriber);
$this->assertTrue(true);
}

public function testRemovedEventForEntityConfiguredToTrackAllEvent()
{
$this->mockMetaData();
$subscriber = new DoctrineSubscriber(array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array()));
$subscriber = new DoctrineSubscriber(['Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => []]);
$this->invokeDeletedEventCall($subscriber);
$this->assertTrue(true);
}


/**
* @param DoctrineSubscriber $subscriber
* @return void
*/
private function invokeCreatedEventCall($subscriber, $entity = null)
public function testCreatedEventTrackEntityViaYaml(): void
{
$subscriber = new DoctrineSubscriber([DummyEntity::class => ['created']]);
$subscriber->setDispatcher($this->dispatcher);
$subscriber->postPersist(new LifecycleEventArgs($entity ?? new Movie(), $this->entityManager));
$dummyClass = new DummyEntity();
$subscriber->preRemove(new LifecycleEventArgs($dummyClass, $this->entityManager));
$subscriber->postRemove(new LifecycleEventArgs($dummyClass, $this->entityManager));
$this->assertTrue(true);
}

/**
* @param DoctrineSubscriber $subscriber
*/
private function invokeUpdatedEventCall($subscriber, $entity = null)

public function testAnyEventToTrackConfigureViaYaml(): void
{
$subscriber = new DoctrineSubscriber([DummyEntity::class => []]);
$subscriber->setDispatcher($this->dispatcher);

$subscriber->postUpdate(new LifecycleEventArgs($entity ?? new Movie(), $this->entityManager));
$dummyClass = new DummyEntity();
$subscriber->preRemove(new LifecycleEventArgs($dummyClass, $this->entityManager));
$subscriber->postRemove(new LifecycleEventArgs($dummyClass, $this->entityManager));
$this->assertTrue(true);
}



/**
* @param DoctrineSubscriber $subscriber
*/
private function invokeDeletedEventCall($subscriber)
{
$subscriber->setDispatcher($this->dispatcher);

$movie = new Movie();
$subscriber->preRemove(new LifecycleEventArgs($movie, $this->entityManager));
$subscriber->postRemove(new LifecycleEventArgs($movie, $this->entityManager));
$this->assertTrue(true);
}

private function mockMetaData($data = ['id' => 1])
Expand Down