Skip to content

Commit afc7fe9

Browse files
authored
Merge pull request #63 from saifulferoz/bugfix/event-configuration-not-working
fix: configured event not working
2 parents 2a977f1 + e5cf820 commit afc7fe9

File tree

6 files changed

+55
-90
lines changed

6 files changed

+55
-90
lines changed

Attribute/SubscribeDoctrineEvents.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,14 @@
1616
*
1717
* @author Roni Saha <roni@xiidea.net>
1818
*/
19-
2019
#[\Attribute(\Attribute::TARGET_CLASS)]
21-
/* @final */ class SubscribeDoctrineEvents
20+
class SubscribeDoctrineEvents
2221
{
23-
public $events = array();
22+
public array $events = [];
2423

25-
public function __construct(array $values)
24+
public function __construct(array|string $values)
2625
{
27-
if (isset($values['value'])) {
28-
$values['events'] = $values['value'];
29-
}
30-
if (!isset($values['events'])) {
31-
return;
32-
}
33-
34-
$this->events = is_array($values['events']) ? $values['events'] : array_map('trim', explode(',', $values['events']));
26+
$this->events = is_array($values) ? $values : array_map('trim', explode(',', $values));
3527

3628
$this->events = array_filter($this->events);
3729
}

Tests/Attribute/SubscribeDoctrineEventsTest.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,46 +18,36 @@ class SubscribeDoctrineEventsTest extends TestCase
1818
{
1919
public function testConstructWithoutData()
2020
{
21-
$annotation = new SubscribeDoctrineEvents(array());
21+
$annotation = new SubscribeDoctrineEvents([]);
2222

2323
$this->assertTrue(is_array($annotation->events));
2424
$this->assertEmpty($annotation->events);
25-
}
26-
27-
public function testConstructWithInvalidData()
28-
{
29-
$data = array(
30-
'unknown' => 'foo',
31-
'array' => array('bar' => 'bar'),
32-
);
33-
34-
$annotation = new SubscribeDoctrineEvents($data);
35-
36-
$this->assertTrue(is_array($annotation->events));
25+
$annotation = new SubscribeDoctrineEvents("");
3726
$this->assertEmpty($annotation->events);
3827
}
3928

40-
public function testConstructWithValue()
29+
30+
public function testConstructWithStringValue()
4131
{
42-
$data = array('value' => 'updated,created');
32+
$data = 'created,updated';
4333

4434
$annotation = new SubscribeDoctrineEvents($data);
4535

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

49-
$this->assertEquals(explode(',', $data['value']), $annotation->events);
39+
$this->assertEquals(explode(',', $data), $annotation->events);
5040
}
5141

52-
public function testConstructWithEvent()
42+
public function testConstructWithArrayValue()
5343
{
54-
$data = array('events' => 'updated,created');
44+
$data = ['created', 'updated'];
5545

5646
$annotation = new SubscribeDoctrineEvents($data);
5747

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

61-
$this->assertEquals(explode(',', $data['events']), $annotation->events);
51+
$this->assertEquals($data, $annotation->events);
6252
}
6353
}

Tests/Fixtures/ODM/UnitOfWork.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class UnitOfWork implements PropertyChangedListener
1010
/**
1111
* Collect information about a property change.
1212
*
13-
* @param object $sender the object on which the property changed
13+
* @param object $sender the object on which the property changed
1414
* @param string $propertyName the name of the property that changed
15-
* @param mixed $oldValue the old value of the property that changed
16-
* @param mixed $newValue the new value of the property that changed
15+
* @param mixed $oldValue the old value of the property that changed
16+
* @param mixed $newValue the new value of the property that changed
1717
*/
18-
public function propertyChanged($sender, $propertyName, $oldValue, $newValue)
18+
public function propertyChanged($sender, $propertyName, $oldValue, $newValue): void
1919
{
2020
}
2121

Tests/Fixtures/ORM/Movie.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,12 @@
2121
#[ORM\Entity]
2222
class Movie
2323
{
24-
/**
25-
* @ORM\Id
26-
* @ORM\Column(type="integer")
27-
* @ORM\GeneratedValue(strategy="AUTO")
28-
*/
24+
2925
#[ORM\Id]
3026
#[ORM\Column(type: 'integer')]
3127
#[ORM\GeneratedValue(strategy: "AUTO")]
3228
protected $id;
3329

34-
/**
35-
* @ORM\Column(type="string")
36-
*/
3730
#[ORM\Column(type: 'string')]
3831
protected $name;
3932

Tests/Fixtures/ORM/UnitOfWork.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class UnitOfWork implements PropertyChangedListener
1010
/**
1111
* Collect information about a property change.
1212
*
13-
* @param object $sender the object on which the property changed
13+
* @param object $sender the object on which the property changed
1414
* @param string $propertyName the name of the property that changed
15-
* @param mixed $oldValue the old value of the property that changed
16-
* @param mixed $newValue the new value of the property that changed
15+
* @param mixed $oldValue the old value of the property that changed
16+
* @param mixed $newValue the new value of the property that changed
1717
*/
18-
public function propertyChanged($sender, $propertyName, $oldValue, $newValue)
18+
public function propertyChanged($sender, $propertyName, $oldValue, $newValue): void
1919
{
2020
}
2121

Tests/Subscriber/DoctrineSubscriberTest.php

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@
1313

1414
use Doctrine\Persistence\Event\LifecycleEventArgs;
1515
use Doctrine\Persistence\Mapping\ClassMetadata;
16+
use Doctrine\Persistence\ObjectManager;
1617
use PHPUnit\Framework\MockObject\MockObject;
1718
use PHPUnit\Framework\TestCase;
1819
use Xiidea\EasyAuditBundle\Subscriber\DoctrineSubscriber;
1920
use Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\DummyEntity;
2021
use Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie;
21-
use Doctrine\Persistence\ObjectManager;
2222

2323
class DoctrineSubscriberTest extends TestCase
2424
{
2525
/** @var MockObject */
2626
private $dispatcher;
2727

28-
2928
/** @var MockObject */
3029
private $entityManager;
3130

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

4544

46-
public function testCreateEventForAttributedEntity()
47-
{
48-
$subscriber = new DoctrineSubscriber(array());
49-
50-
$this->invokeCreatedEventCall($subscriber);
51-
}
52-
53-
public function testCreateEventForEntityNotConfiguredToTrack()
54-
{
55-
$subscriber = new DoctrineSubscriber(array());
56-
$this->invokeCreatedEventCall($subscriber, new DummyEntity());
57-
}
58-
59-
public function testCreateEventForEntityConfiguredToTrack()
60-
{
61-
$subscriber = new DoctrineSubscriber(
62-
array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array('created'))
63-
);
64-
65-
$this->invokeCreatedEventCall($subscriber);
66-
}
67-
68-
public function testCreateEventForEntityConfiguredToTrackAllEvents()
45+
public function testEntityConfigureToTrackForAttributedEntity()
6946
{
70-
$subscriber = new DoctrineSubscriber(array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array()));
47+
$subscriber = new DoctrineSubscriber([]);
7148

72-
$this->invokeCreatedEventCall($subscriber);
49+
$subscriber->setDispatcher($this->dispatcher);
50+
$subscriber->postPersist(new LifecycleEventArgs(new Movie(), $this->entityManager));
51+
$subscriber->postUpdate(new LifecycleEventArgs(new Movie(), $this->entityManager));
52+
$subscriber->preRemove(new LifecycleEventArgs(new Movie(), $this->entityManager));
53+
$subscriber->postRemove(new LifecycleEventArgs(new Movie(), $this->entityManager));
54+
$this->assertTrue(true);
7355
}
7456

75-
public function testUpdateEventForEntityNotConfiguredToTrack()
57+
public function testEntityNotConfiguredToTrack()
7658
{
77-
$subscriber = new DoctrineSubscriber(array());
78-
$this->invokeUpdatedEventCall($subscriber, new DummyEntity());
59+
$subscriber = new DoctrineSubscriber([]);
60+
$subscriber->setDispatcher($this->dispatcher);
61+
$subscriber->postPersist(new LifecycleEventArgs(new DummyEntity(), $this->entityManager));
62+
$this->assertTrue(true);
7963
}
8064

8165
public function testRemovedEventForEntityNotConfiguredToTrack()
8266
{
83-
$subscriber = new DoctrineSubscriber(array());
67+
$subscriber = new DoctrineSubscriber([]);
8468
$this->invokeDeletedEventCall($subscriber);
69+
$this->assertTrue(true);
8570
}
8671

8772
public function testRemovedEventForEntityConfiguredToTrackAllEvent()
8873
{
8974
$this->mockMetaData();
90-
$subscriber = new DoctrineSubscriber(array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array()));
75+
$subscriber = new DoctrineSubscriber(['Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => []]);
9176
$this->invokeDeletedEventCall($subscriber);
77+
$this->assertTrue(true);
9278
}
9379

94-
9580
/**
96-
* @param DoctrineSubscriber $subscriber
81+
* @return void
9782
*/
98-
private function invokeCreatedEventCall($subscriber, $entity = null)
83+
public function testCreatedEventTrackEntityViaYaml(): void
9984
{
85+
$subscriber = new DoctrineSubscriber([DummyEntity::class => ['created']]);
10086
$subscriber->setDispatcher($this->dispatcher);
101-
$subscriber->postPersist(new LifecycleEventArgs($entity ?? new Movie(), $this->entityManager));
87+
$dummyClass = new DummyEntity();
88+
$subscriber->preRemove(new LifecycleEventArgs($dummyClass, $this->entityManager));
89+
$subscriber->postRemove(new LifecycleEventArgs($dummyClass, $this->entityManager));
10290
$this->assertTrue(true);
10391
}
10492

105-
/**
106-
* @param DoctrineSubscriber $subscriber
107-
*/
108-
private function invokeUpdatedEventCall($subscriber, $entity = null)
93+
94+
public function testAnyEventToTrackConfigureViaYaml(): void
10995
{
96+
$subscriber = new DoctrineSubscriber([DummyEntity::class => []]);
11097
$subscriber->setDispatcher($this->dispatcher);
111-
112-
$subscriber->postUpdate(new LifecycleEventArgs($entity ?? new Movie(), $this->entityManager));
98+
$dummyClass = new DummyEntity();
99+
$subscriber->preRemove(new LifecycleEventArgs($dummyClass, $this->entityManager));
100+
$subscriber->postRemove(new LifecycleEventArgs($dummyClass, $this->entityManager));
113101
$this->assertTrue(true);
114102
}
115103

104+
105+
116106
/**
117107
* @param DoctrineSubscriber $subscriber
118108
*/
119109
private function invokeDeletedEventCall($subscriber)
120110
{
121111
$subscriber->setDispatcher($this->dispatcher);
112+
122113
$movie = new Movie();
123114
$subscriber->preRemove(new LifecycleEventArgs($movie, $this->entityManager));
124115
$subscriber->postRemove(new LifecycleEventArgs($movie, $this->entityManager));
125-
$this->assertTrue(true);
126116
}
127117

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

0 commit comments

Comments
 (0)