Skip to content

Commit 23fee6b

Browse files
committed
fix: fire created event though event configured for update
1 parent 39eaabd commit 23fee6b

File tree

6 files changed

+167
-59
lines changed

6 files changed

+167
-59
lines changed

Attribute/SubscribeDoctrineEvents.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,35 @@
1616
*
1717
* @author Roni Saha <[email protected]>
1818
*/
19-
2019
#[\Attribute(\Attribute::TARGET_CLASS)]
21-
/* @final */ class SubscribeDoctrineEvents
20+
/* @final */
21+
class SubscribeDoctrineEvents
2222
{
23-
public $events = array();
23+
public array $events = [];
2424

25-
public function __construct(array $values)
25+
public function __construct(array|string $values)
2626
{
27-
if (isset($values['value'])) {
28-
$values['events'] = $values['value'];
29-
}
30-
if (!isset($values['events'])) {
27+
$validValues = [
28+
'created',
29+
'updated',
30+
];
31+
$valueValueStr = [
32+
'created,updated',
33+
'created, updated',
34+
'updated,created',
35+
'updated, created',
36+
];
37+
if (!empty($values) && is_string($values) && !in_array($values, $valueValueStr)) {
3138
return;
3239
}
33-
34-
$this->events = is_array($values['events']) ? $values['events'] : array_map('trim', explode(',', $values['events']));
40+
if (!empty($values) && is_array($values)) {
41+
foreach ($values as $value) {
42+
if (!in_array($value, $validValues)) {
43+
return;
44+
}
45+
}
46+
}
47+
$this->events = is_array($values) ? $values : array_map('trim', explode(',', $values));
3548

3649
$this->events = array_filter($this->events);
3750
}

Tests/Attribute/SubscribeDoctrineEventsTest.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ 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);
2525
}
2626

2727
public function testConstructWithInvalidData()
2828
{
29-
$data = array(
29+
$data = [
3030
'unknown' => 'foo',
31-
'array' => array('bar' => 'bar'),
32-
);
31+
'array' => ['bar' => 'bar'],
32+
];
3333

3434
$annotation = new SubscribeDoctrineEvents($data);
3535

@@ -39,25 +39,13 @@ public function testConstructWithInvalidData()
3939

4040
public function testConstructWithValue()
4141
{
42-
$data = array('value' => 'updated,created');
42+
$data = 'created,updated';
4343

4444
$annotation = new SubscribeDoctrineEvents($data);
4545

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

49-
$this->assertEquals(explode(',', $data['value']), $annotation->events);
50-
}
51-
52-
public function testConstructWithEvent()
53-
{
54-
$data = array('events' => 'updated,created');
55-
56-
$annotation = new SubscribeDoctrineEvents($data);
57-
58-
$this->assertTrue(is_array($annotation->events));
59-
$this->assertNotEmpty($annotation->events);
60-
61-
$this->assertEquals(explode(',', $data['events']), $annotation->events);
49+
$this->assertEquals(explode(',', $data), $annotation->events);
6250
}
6351
}

Tests/Fixtures/ORM/Book.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the XiideaEasyAuditBundle package.
5+
*
6+
* (c) Xiidea <http://www.xiidea.net>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Xiidea\EasyAuditBundle\Tests\Fixtures\ORM;
13+
14+
use Doctrine\ORM\Mapping as ORM;
15+
use Xiidea\EasyAuditBundle\Attribute\SubscribeDoctrineEvents;
16+
17+
/**
18+
* @ORM\Entity
19+
*/
20+
#[SubscribeDoctrineEvents(['updated'])]
21+
#[ORM\Entity]
22+
class Book
23+
{
24+
25+
#[ORM\Id]
26+
#[ORM\Column(type: 'integer')]
27+
#[ORM\GeneratedValue(strategy: "AUTO")]
28+
protected $id;
29+
30+
#[ORM\Column(type: 'string')]
31+
protected $name;
32+
33+
public function __construct($id = 1, $name = 'car')
34+
{
35+
$this->id = $id;
36+
$this->name = $name;
37+
}
38+
39+
public function getName()
40+
{
41+
return $this->name;
42+
}
43+
44+
public function setName($name)
45+
{
46+
$this->name = $name;
47+
}
48+
49+
/**
50+
* @return mixed
51+
*/
52+
public function getId()
53+
{
54+
return $this->id;
55+
}
56+
}

Tests/Fixtures/ORM/Machine.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the XiideaEasyAuditBundle package.
5+
*
6+
* (c) Xiidea <http://www.xiidea.net>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Xiidea\EasyAuditBundle\Tests\Fixtures\ORM;
13+
14+
use Doctrine\ORM\Mapping as ORM;
15+
use Xiidea\EasyAuditBundle\Attribute\SubscribeDoctrineEvents;
16+
17+
18+
#[SubscribeDoctrineEvents([])]
19+
#[ORM\Entity]
20+
class Machine
21+
{
22+
23+
#[ORM\Id]
24+
#[ORM\Column(type: 'integer')]
25+
#[ORM\GeneratedValue(strategy: "AUTO")]
26+
protected $id;
27+
28+
#[ORM\Column(type: 'string')]
29+
protected $name;
30+
31+
public function __construct($id = 1, $name = 'car')
32+
{
33+
$this->id = $id;
34+
$this->name = $name;
35+
}
36+
37+
public function getName()
38+
{
39+
return $this->name;
40+
}
41+
42+
public function setName($name)
43+
{
44+
$this->name = $name;
45+
}
46+
47+
/**
48+
* @return mixed
49+
*/
50+
public function getId()
51+
{
52+
return $this->id;
53+
}
54+
}

Tests/Fixtures/ORM/Movie.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,16 @@
1717
/**
1818
* @ORM\Entity
1919
*/
20-
#[SubscribeDoctrineEvents([])]
20+
#[SubscribeDoctrineEvents(['created'])]
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/Subscriber/DoctrineSubscriberTest.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717
use PHPUnit\Framework\MockObject\MockObject;
1818
use PHPUnit\Framework\TestCase;
1919
use Xiidea\EasyAuditBundle\Subscriber\DoctrineSubscriber;
20-
use Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\DummyEntity;
20+
use Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Book;
21+
use Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Machine;
2122
use Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie;
22-
use Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\TestMovie;
2323

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

29-
3029
/** @var MockObject */
3130
private $entityManager;
3231

@@ -46,74 +45,79 @@ public function setUp(): void
4645

4746
public function testCreateEventForAttributedEntity()
4847
{
49-
$subscriber = new DoctrineSubscriber(array());
48+
$subscriber = new DoctrineSubscriber([]);
5049

5150
$this->invokeCreatedEventCall($subscriber);
51+
$this->assertTrue(true);
5252
}
5353

5454
public function testCreateEventForEntityNotConfiguredToTrack()
5555
{
56-
$subscriber = new DoctrineSubscriber(array());
57-
$this->invokeCreatedEventCall($subscriber, new DummyEntity());
56+
$subscriber = new DoctrineSubscriber([]);
57+
$this->invokeCreatedEventCall($subscriber);
58+
$this->assertTrue(true);
5859
}
5960

6061
public function testCreateEventForEntityConfiguredToTrack()
6162
{
62-
$subscriber = new DoctrineSubscriber(
63-
array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array('created'))
64-
);
63+
$subscriber = new DoctrineSubscriber(['Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => ['created']]);
6564

6665
$this->invokeCreatedEventCall($subscriber);
66+
$this->assertTrue(true);
6767
}
6868

6969
public function testCreateEventForEntityConfiguredToTrackAllEvents()
7070
{
71-
$subscriber = new DoctrineSubscriber(array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array()));
71+
$subscriber = new DoctrineSubscriber(['Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => []]);
7272

7373
$this->invokeCreatedEventCall($subscriber);
74-
$this->invokeCreatedEventCall($subscriber, new TestMovie());
74+
$this->assertTrue(true);
7575
}
7676

7777
public function testUpdateEventForEntityNotConfiguredToTrack()
7878
{
79-
$subscriber = new DoctrineSubscriber(array());
80-
$this->invokeUpdatedEventCall($subscriber, new DummyEntity());
81-
$this->invokeUpdatedEventCall($subscriber, new TestMovie());
79+
$subscriber = new DoctrineSubscriber([]);
80+
$this->invokeUpdatedEventCall($subscriber);
81+
$this->assertTrue(true);
8282
}
8383

8484
public function testRemovedEventForEntityNotConfiguredToTrack()
8585
{
86-
$subscriber = new DoctrineSubscriber(array());
86+
$subscriber = new DoctrineSubscriber([]);
8787
$this->invokeDeletedEventCall($subscriber);
88+
$this->assertTrue(true);
8889
}
8990

9091
public function testRemovedEventForEntityConfiguredToTrackAllEvent()
9192
{
9293
$this->mockMetaData();
93-
$subscriber = new DoctrineSubscriber(array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array()));
94+
$subscriber = new DoctrineSubscriber(['Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => []]);
9495
$this->invokeDeletedEventCall($subscriber);
96+
$this->assertTrue(true);
9597
}
9698

9799

98100
/**
99101
* @param DoctrineSubscriber $subscriber
100102
*/
101-
private function invokeCreatedEventCall($subscriber, $entity = null)
103+
private function invokeCreatedEventCall($subscriber)
102104
{
103105
$subscriber->setDispatcher($this->dispatcher);
104-
$subscriber->postPersist(new LifecycleEventArgs($entity ?? new Movie(), $this->entityManager));
106+
107+
$subscriber->postPersist(new LifecycleEventArgs(new Movie(), $this->entityManager));
108+
$subscriber->postPersist(new LifecycleEventArgs(new Machine(), $this->entityManager));
105109
$this->assertTrue(true);
106110
}
107111

108112
/**
109113
* @param DoctrineSubscriber $subscriber
110114
*/
111-
private function invokeUpdatedEventCall($subscriber, $entity = null)
115+
private function invokeUpdatedEventCall($subscriber)
112116
{
113117
$subscriber->setDispatcher($this->dispatcher);
114118

115-
$subscriber->postUpdate(new LifecycleEventArgs($entity ?? new Movie(), $this->entityManager));
116-
$this->assertTrue(true);
119+
$subscriber->postUpdate(new LifecycleEventArgs(new Book(), $this->entityManager));
120+
$subscriber->postUpdate(new LifecycleEventArgs(new Machine(), $this->entityManager));
117121
}
118122

119123
/**
@@ -122,11 +126,11 @@ private function invokeUpdatedEventCall($subscriber, $entity = null)
122126
private function invokeDeletedEventCall($subscriber)
123127
{
124128
$subscriber->setDispatcher($this->dispatcher);
129+
125130
$movie = new Movie();
126131
$subscriber->preRemove(new LifecycleEventArgs($movie, $this->entityManager));
127132
$subscriber->postRemove(new LifecycleEventArgs($movie, $this->entityManager));
128133
$this->assertTrue(true);
129-
$subscriber->postRemove(new LifecycleEventArgs(new TestMovie(), $this->entityManager));
130134
}
131135

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

0 commit comments

Comments
 (0)