Skip to content

Commit ff0c3d2

Browse files
committed
Move to newer service configuration for 5.4+ services
1 parent c8b1b8c commit ff0c3d2

File tree

11 files changed

+239
-51
lines changed

11 files changed

+239
-51
lines changed

config/services.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
44

5+
use Doctrine\ORM\Events;
56
use DualMedia\DoctrineEventConverterBundle\EventSubscriber\DispatchingSubscriber;
67
use DualMedia\DoctrineEventConverterBundle\Proxy\Generator;
78
use DualMedia\DoctrineEventConverterBundle\Service\DelayableEventDispatcher;
@@ -27,12 +28,35 @@
2728

2829
$services->set(VerifierService::class);
2930

30-
$services->set(DispatchingSubscriber::class)
31+
$def = $services->set(DispatchingSubscriber::class)
3132
->arg('$eventService', new Reference(EventService::class))
3233
->arg('$subEventService', new Reference(SubEventService::class))
3334
->arg('$verifierService', new Reference(VerifierService::class))
3435
->arg('$dispatcher', new Reference(DelayableEventDispatcher::class))
35-
->tag('doctrine.event_subscriber');
36+
->tag('doctrine.event_listener', [
37+
'event' => Events::prePersist,
38+
])
39+
->tag('doctrine.event_listener', [
40+
'event' => Events::postPersist,
41+
])
42+
->tag('doctrine.event_listener', [
43+
'event' => Events::preUpdate,
44+
])
45+
->tag('doctrine.event_listener', [
46+
'event' => Events::postUpdate,
47+
])
48+
->tag('doctrine.event_listener', [
49+
'event' => Events::preRemove,
50+
])
51+
->tag('doctrine.event_listener', [
52+
'event' => Events::postRemove,
53+
])
54+
->tag('doctrine.event_listener', [
55+
'event' => Events::preFlush,
56+
])
57+
->tag('doctrine.event_listener', [
58+
'event' => Events::postFlush,
59+
]);
3660

3761
$services->set(Generator::class)
3862
->arg(0, '%kernel.cache_dir%/dm-smd-event-distributor-bundle')

src/DependencyInjection/CompilerPass/EventDetectionCompilerPass.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,10 @@ public function process(
265265
$output = [];
266266
krsort($subEventConstruct, SORT_NUMERIC); // sort by priorities (200 -> 0 -> -200)
267267

268-
foreach ($subEventConstruct as $data) {
269-
$output[] = $data;
268+
foreach ($subEventConstruct as $prioritySortedList) {
269+
foreach ($prioritySortedList as $data) {
270+
$output[] = $data;
271+
}
270272
}
271273

272274
$subEventService->setArgument('$entries', $output);

src/EventSubscriber/DispatchingSubscriber.php

+2-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace DualMedia\DoctrineEventConverterBundle\EventSubscriber;
44

5-
use Doctrine\Common\EventSubscriber;
65
use Doctrine\Common\Util\ClassUtils;
76
use Doctrine\ORM\Event\PostFlushEventArgs;
87
use Doctrine\ORM\Event\PostPersistEventArgs;
@@ -22,7 +21,7 @@
2221
use DualMedia\DoctrineEventConverterBundle\Service\SubEventService;
2322
use DualMedia\DoctrineEventConverterBundle\Service\VerifierService;
2423

25-
class DispatchingSubscriber implements EventSubscriber
24+
class DispatchingSubscriber
2625
{
2726
private bool $preFlush = false;
2827

@@ -48,25 +47,9 @@ public function __construct(
4847
) {
4948
}
5049

51-
public function getSubscribedEvents(): array
52-
{
53-
return [
54-
Events::prePersist,
55-
Events::postPersist,
56-
Events::preUpdate,
57-
Events::postUpdate,
58-
Events::preRemove,
59-
Events::postRemove,
60-
Events::preFlush,
61-
Events::postFlush,
62-
];
63-
}
64-
6550
public function prePersist(
6651
PrePersistEventArgs $args
6752
): void {
68-
echo 'DISPATCHED!!!!!!!!!!!';
69-
7053
if ($args->getObject() instanceof EntityInterface) {
7154
$this->process(Events::prePersist, $args->getObject());
7255
}
@@ -196,7 +179,7 @@ private function subEvents(
196179
$subEvent->setEntity($entity)
197180
->setChanges(array_intersect_key(
198181
$event->getChanges(),
199-
$model->fieldList
182+
$model->fields
200183
)) // save only fields that the event requested, ignore rest
201184
->setEventType($event->getEventType());
202185

src/Model/SubEvent.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ class SubEvent
1616
{
1717
/**
1818
* @param bool $allMode If all the fields must be meeting the requirements of the event
19-
* @param array<string, null|array{0?: mixed, 1?: mixed}> $fieldList The fields that must be changed, null means that any change is required, 0 and 1 indexes match before/after
19+
* @param array<string, null|array{0?: mixed, 1?: mixed}> $fields The fields that must be changed, null means that any change is required, 0 and 1 indexes match before/after
2020
* @param array<string, mixed> $requirements Required field states for this event to fire
2121
* @param list<string> $types Event types in which this event may be triggered
2222
*/
2323
public function __construct(
2424
public readonly bool $allMode,
25-
public readonly array $fieldList,
25+
public readonly array $fields,
2626
public readonly array $requirements,
2727
public readonly array $types,
2828
public readonly bool $afterFlush,

src/Service/SubEventService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(
3434
[$eventClass, $entities, $allMode, $fieldList, $requirements, $types, $afterFlush] = $entry;
3535

3636
foreach ($entities as $entity) {
37-
if (!array_key_exists($entity, $this->events[$entity])) {
37+
if (!array_key_exists($entity, $this->events)) {
3838
$this->events[$entity] = [];
3939
}
4040

src/Service/VerifierService.php

+30-20
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(
1515
}
1616

1717
/**
18-
* @param array<string, array<int, mixed>> $changes
18+
* @param array<string, array{0: mixed, 1: mixed}> $changes
1919
*/
2020
public function validate(
2121
array $changes,
@@ -49,48 +49,58 @@ public function validateRequirements(
4949
}
5050

5151
/**
52-
* @param array<string, array<int, mixed>> $changes
52+
* @param array<string, array{0: mixed, 1: mixed}> $changes
5353
*/
5454
public function validateFields(
5555
array $changes,
5656
SubEvent $model,
5757
string $type
5858
): bool {
5959
if (!in_array($type, [Events::postUpdate, Events::preUpdate], true)) {
60-
return false;
60+
return true;
6161
}
6262

63-
if ($model->allMode && count(array_diff_key($model->fieldList, $changes))) { // Event contains keys that haven't changed
63+
if ($model->allMode && count(array_diff_key($model->fields, $changes))) { // Event contains keys that haven't changed
6464
return false;
65-
} elseif (!$model->allMode && !count(array_intersect_key($changes, $model->fieldList))) { // Event doesn't contain any of the required keys
65+
} elseif (!$model->allMode && !count(array_intersect_key($changes, $model->fields))) { // Event doesn't contain any of the required keys
6666
return false;
6767
}
6868

6969
$validFields = [];
7070

7171
foreach ($changes as $field => $fields) {
72-
if (!array_key_exists($field, $model->fieldList)) {
73-
continue;
74-
} elseif (null === ($modelWantedState = $model->fieldList[$field])) {
75-
// if you set null instead of setting null for key 0 you're dumb and #wontfix
76-
$validFields[$field] = true;
72+
if (!array_key_exists($field, $model->fields)) {
7773
continue;
7874
}
7975

80-
$count = count($modelWantedState);
81-
82-
if (1 === $count) {
83-
$existingCounter = array_key_exists(0, $modelWantedState) ? 0 : 1;
84-
$validFields[$field] = $this->equals($fields[$existingCounter], $modelWantedState[$existingCounter]); // @phpstan-ignore-line
85-
} elseif (2 === $count) {
86-
/** @var array{0: mixed, 1: mixed} $modelWantedState */
87-
$validFields[$field] = $this->equals($fields[0], $modelWantedState[0]) && $this->equals($fields[1], $modelWantedState[1]);
88-
}
76+
$validFields[$field] = null === ($modelWantedState = $model->fields[$field])
77+
|| $this->validateField($fields, $modelWantedState);
8978
}
9079

9180
$reduced = array_reduce($validFields, fn ($carry, $data) => $carry + ((int)$data));
9281

93-
return !$model->allMode ? $reduced > 0 : $reduced === count($model->fieldList);
82+
return !$model->allMode ? $reduced > 0 : $reduced === count($model->fields);
83+
}
84+
85+
/**
86+
* @param array{0: mixed, 1: mixed} $changes
87+
* @param array{0?: mixed, 1?: mixed} $wantedState
88+
*/
89+
public function validateField(
90+
array $changes,
91+
array $wantedState
92+
): bool {
93+
$count = count($wantedState);
94+
95+
if (1 === $count) {
96+
$existingCounter = array_key_exists(0, $wantedState) ? 0 : 1;
97+
return $this->equals($changes[$existingCounter], $wantedState[$existingCounter]); // @phpstan-ignore-line
98+
} elseif (2 === $count) {
99+
/** @var array{0: mixed, 1: mixed} $wantedState */
100+
return $this->equals($changes[0], $wantedState[0]) && $this->equals($changes[1], $wantedState[1]);
101+
}
102+
103+
return false;
94104
}
95105

96106
/**

tests/Fixtures/Enum/BackedIntEnum.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace DualMedia\DoctrineEventConverterBundle\Tests\Fixtures\Enum;
4+
5+
enum BackedIntEnum: int
6+
{
7+
case Is5 = 5;
8+
}

tests/Model/ChangeTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace DualMedia\DoctrineEventConverterBundle\Tests\Model;
44

55
use DualMedia\DoctrineEventConverterBundle\Model\Change;
6-
use DualMedia\DoctrineEventConverterBundle\Tests\KernelTestCase;
6+
use PHPUnit\Framework\TestCase;
77

8-
class ChangeTest extends KernelTestCase
8+
class ChangeTest extends TestCase
99
{
10-
public function test()
10+
public function test(): void
1111
{
1212
$change = new Change('status', 1, 2);
1313

tests/Service/EventServiceTest.php

+31-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace DualMedia\DoctrineEventConverterBundle\Tests\Service;
44

5+
use Doctrine\ORM\Events;
56
use DualMedia\DoctrineEventConverterBundle\Service\EventService;
7+
use DualMedia\DoctrineEventConverterBundle\Tests\Fixtures\Entity\ComplexEntity;
8+
use DualMedia\DoctrineEventConverterBundle\Tests\Fixtures\Entity\Item;
9+
use DualMedia\DoctrineEventConverterBundle\Tests\Fixtures\Event\ComplexEntityEvent;
610
use PHPUnit\Framework\TestCase;
711
use Pkly\ServiceMockHelperTrait;
812

@@ -15,12 +19,37 @@ class EventServiceTest extends TestCase
1519
protected function setUp(): void
1620
{
1721
$this->service = $this->createRealMockedServiceInstance(EventService::class, [
18-
'entries' => [],
22+
'entries' => [
23+
[
24+
ComplexEntityEvent::class,
25+
[ComplexEntity::class],
26+
Events::prePersist,
27+
true,
28+
],
29+
],
1930
]);
2031
}
2132

2233
public function test(): void
2334
{
24-
$this->assertTrue(true);
35+
$this->assertNotEmpty(
36+
$events = $this->service->get(Events::prePersist, ComplexEntity::class),
37+
'There should be exactly 1 event for specified inputs'
38+
);
39+
$this->assertCount(1, $events, 'There should be exactly 1 event for specified inputs');
40+
41+
$this->assertEquals(
42+
ComplexEntityEvent::class,
43+
$events[0]->eventClass
44+
);
45+
$this->assertTrue($events[0]->afterFlush);
46+
}
47+
48+
public function testNotFound(): void
49+
{
50+
$this->assertEmpty(
51+
$this->service->get(Events::postRemove, Item::class),
52+
'No events should be returned from service'
53+
);
2554
}
2655
}

tests/Service/SubEventServiceTest.php

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace DualMedia\DoctrineEventConverterBundle\Tests\Service;
4+
5+
use Doctrine\ORM\Events;
6+
use DualMedia\DoctrineEventConverterBundle\Service\SubEventService;
7+
use DualMedia\DoctrineEventConverterBundle\Tests\Fixtures\Entity\ComplexEntity;
8+
use DualMedia\DoctrineEventConverterBundle\Tests\Fixtures\Entity\Item;
9+
use DualMedia\DoctrineEventConverterBundle\Tests\Fixtures\Event\ComplexEntityEvent;
10+
use PHPUnit\Framework\TestCase;
11+
use Pkly\ServiceMockHelperTrait;
12+
13+
class SubEventServiceTest extends TestCase
14+
{
15+
use ServiceMockHelperTrait;
16+
17+
private SubEventService $service;
18+
19+
protected function setUp(): void
20+
{
21+
$this->service = $this->createRealMockedServiceInstance(SubEventService::class, [
22+
'entries' => [
23+
[
24+
ComplexEntityEvent::class,
25+
[ComplexEntity::class],
26+
false,
27+
[
28+
'stuff' => null,
29+
],
30+
[
31+
'requirement' => 42,
32+
],
33+
[
34+
Events::prePersist,
35+
],
36+
true,
37+
],
38+
],
39+
]);
40+
}
41+
42+
public function test(): void
43+
{
44+
$this->assertNotEmpty(
45+
$events = $this->service->get(ComplexEntity::class),
46+
'There should be exactly 1 event for specified inputs'
47+
);
48+
$this->assertCount(1, $events, 'There should be exactly 1 event for specified inputs');
49+
50+
$this->assertArrayHasKey(
51+
ComplexEntityEvent::class,
52+
$events
53+
);
54+
$this->assertCount(1, $events[ComplexEntityEvent::class]);
55+
$event = $events[ComplexEntityEvent::class][0];
56+
57+
$this->assertFalse(
58+
$event->allMode
59+
);
60+
$this->assertEquals([
61+
'stuff' => null,
62+
], $event->fields);
63+
$this->assertEquals([
64+
'requirement' => 42,
65+
], $event->requirements);
66+
$this->assertEquals([
67+
Events::prePersist,
68+
], $event->types);
69+
$this->assertTrue(
70+
$event->afterFlush
71+
);
72+
}
73+
74+
public function testEmpty(): void
75+
{
76+
$this->assertEmpty(
77+
$this->service->get(Item::class),
78+
'There should be sub events for specified entity'
79+
);
80+
}
81+
}

0 commit comments

Comments
 (0)