Skip to content

Commit f8e95d1

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

File tree

5 files changed

+141
-42
lines changed

5 files changed

+141
-42
lines changed

Attribute/SubscribeDoctrineEvents.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,14 @@
1616
*
1717
* @author Roni Saha <[email protected]>
1818
*/
19-
2019
#[\Attribute(\Attribute::TARGET_CLASS)]
2120
/* @final */ 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/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)