Skip to content

Commit b2a6406

Browse files
committed
fix: content create migration not being created (only update)
1 parent b155a5b commit b2a6406

3 files changed

Lines changed: 47 additions & 54 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
fail-fast: false
1515
matrix:
1616
php:
17+
- "8.2"
1718
- "8.3"
1819
- "8.4"
1920
- "8.5"

src/EventListener/ContentListener.php

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
namespace vardumper\IbexaAutomaticMigrationsBundle\EventListener;
66

77
use Ibexa\Contracts\Core\Repository\Events\Content\BeforeDeleteContentEvent;
8-
use Ibexa\Contracts\Core\Repository\Events\Content\CreateContentEvent;
9-
use Ibexa\Contracts\Core\Repository\Events\Content\UpdateContentEvent;
8+
use Ibexa\Contracts\Core\Repository\Events\Content\PublishVersionEvent;
109
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
1110
use Psr\Log\LoggerInterface;
1211
use Symfony\Component\DependencyInjection\Attribute\Autowire;
@@ -42,8 +41,8 @@ public function __construct(
4241
}
4342
}
4443

45-
#[AsEventListener(CreateContentEvent::class)]
46-
public function onCreated(CreateContentEvent $event): void
44+
#[AsEventListener(PublishVersionEvent::class)]
45+
public function onPublished(PublishVersionEvent $event): void
4746
{
4847
if (!$this->settingsService->isEnabled()) {
4948
return;
@@ -58,26 +57,8 @@ public function onCreated(CreateContentEvent $event): void
5857
$this->logger->info('Skipping content migration generation in CLI');
5958
return;
6059
}
61-
$this->generateMigration($content->contentInfo, 'create');
62-
}
63-
64-
#[AsEventListener(UpdateContentEvent::class)]
65-
public function onUpdated(UpdateContentEvent $event): void
66-
{
67-
if (!$this->settingsService->isEnabled()) {
68-
return;
69-
}
70-
71-
$content = $event->getContent();
72-
73-
if (!$this->settingsService->isTypeEnabled('content')) {
74-
return;
75-
}
76-
if ($this->isCli) {
77-
$this->logger->info('Skipping content migration generation in CLI');
78-
return;
79-
}
80-
$this->generateMigration($content->contentInfo, 'update');
60+
$mode = $event->getVersionInfo()->getVersionNo() === 1 ? 'create' : 'update';
61+
$this->generateMigration($content->contentInfo, $mode);
8162
}
8263

8364
#[AsEventListener(BeforeDeleteContentEvent::class)]

tests/Unit/EventListener/ContentListenerTest.php

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
declare(strict_types=1);
44

55
use Ibexa\Contracts\Core\Repository\Events\Content\BeforeDeleteContentEvent;
6-
use Ibexa\Contracts\Core\Repository\Events\Content\CreateContentEvent;
7-
use Ibexa\Contracts\Core\Repository\Events\Content\UpdateContentEvent;
6+
use Ibexa\Contracts\Core\Repository\Events\Content\PublishVersionEvent;
87
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
9-
use Ibexa\Contracts\Core\Repository\Values\Content\ContentCreateStruct;
108
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
11-
use Ibexa\Contracts\Core\Repository\Values\Content\ContentUpdateStruct;
129
use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo;
1310
use Psr\Log\NullLogger;
1411
use vardumper\IbexaAutomaticMigrationsBundle\EventListener\ContentListener;
@@ -31,12 +28,19 @@
3128
'contentInfo' => $contentInfo,
3229
default => null,
3330
});
34-
$createStruct = $this->createStub(ContentCreateStruct::class);
35-
$versionInfo = $this->createStub(VersionInfo::class);
36-
$updateStruct = $this->createStub(ContentUpdateStruct::class);
3731

38-
$this->createEvent = new CreateContentEvent($content, $createStruct, [], null);
39-
$this->updateEvent = new UpdateContentEvent($content, $versionInfo, $updateStruct, null);
32+
$versionInfoV1 = $this->getMockBuilder(VersionInfo::class)
33+
->disableOriginalConstructor()
34+
->getMock();
35+
$versionInfoV1->method('getVersionNo')->willReturn(1);
36+
37+
$versionInfoV2 = $this->getMockBuilder(VersionInfo::class)
38+
->disableOriginalConstructor()
39+
->getMock();
40+
$versionInfoV2->method('getVersionNo')->willReturn(2);
41+
42+
$this->publishCreateEvent = new PublishVersionEvent($content, $versionInfoV1, []);
43+
$this->publishUpdateEvent = new PublishVersionEvent($content, $versionInfoV2, []);
4044
$this->deleteEvent = new BeforeDeleteContentEvent($contentInfo);
4145
});
4246

@@ -49,20 +53,20 @@
4953
expect(is_dir($this->tmpDir . '/src/MigrationsDefinitions'))->toBeTrue();
5054
});
5155

52-
it('onCreated returns early when not enabled (APP_ENV=testing)', function () {
56+
it('onPublished (create) returns early when not enabled (APP_ENV=testing)', function () {
5357
// SettingsService::isEnabled() returns false when APP_ENV != 'dev'
54-
expect(fn () => $this->listener->onCreated($this->createEvent))->not->toThrow(\Throwable::class);
58+
expect(fn () => $this->listener->onPublished($this->publishCreateEvent))->not->toThrow(\Throwable::class);
5559
});
5660

57-
it('onUpdated returns early when not enabled', function () {
58-
expect(fn () => $this->listener->onUpdated($this->updateEvent))->not->toThrow(\Throwable::class);
61+
it('onPublished (update) returns early when not enabled', function () {
62+
expect(fn () => $this->listener->onPublished($this->publishUpdateEvent))->not->toThrow(\Throwable::class);
5963
});
6064

6165
it('onBeforeDeleted returns early when not enabled', function () {
6266
expect(fn () => $this->listener->onBeforeDeleted($this->deleteEvent))->not->toThrow(\Throwable::class);
6367
});
6468

65-
it('onCreated returns early when content type disabled in dev env', function () {
69+
it('onPublished returns early when content type disabled in dev env', function () {
6670
$previous = $_SERVER['APP_ENV'] ?? null;
6771
$_SERVER['APP_ENV'] = 'dev';
6872
try {
@@ -71,7 +75,7 @@
7175
makeSettingsService($this->tmpDir, true, ['content' => false]),
7276
$this->tmpDir
7377
);
74-
expect(fn () => $listener->onCreated($this->createEvent))->not->toThrow(\Throwable::class);
78+
expect(fn () => $listener->onPublished($this->publishCreateEvent))->not->toThrow(\Throwable::class);
7579
} finally {
7680
if ($previous === null) {
7781
unset($_SERVER['APP_ENV']);
@@ -81,12 +85,12 @@
8185
}
8286
});
8387

84-
it('onCreated reaches generateMigration in dev env', function () {
85-
withEnv('dev', fn () => expect(fn () => $this->listener->onCreated($this->createEvent))->not->toThrow(\Throwable::class));
88+
it('onPublished (create) reaches generateMigration in dev env', function () {
89+
withEnv('dev', fn () => expect(fn () => $this->listener->onPublished($this->publishCreateEvent))->not->toThrow(\Throwable::class));
8690
});
8791

88-
it('onUpdated reaches generateMigration in dev env', function () {
89-
withEnv('dev', fn () => expect(fn () => $this->listener->onUpdated($this->updateEvent))->not->toThrow(\Throwable::class));
92+
it('onPublished (update) reaches generateMigration in dev env', function () {
93+
withEnv('dev', fn () => expect(fn () => $this->listener->onPublished($this->publishUpdateEvent))->not->toThrow(\Throwable::class));
9094
});
9195

9296
it('onBeforeDeleted reaches generateMigration in dev env', function () {
@@ -105,42 +109,49 @@
105109
'contentInfo' => $contentInfo,
106110
default => null,
107111
});
108-
$createStruct = $this->createStub(ContentCreateStruct::class);
109-
$versionInfo = $this->createStub(VersionInfo::class);
110-
$updateStruct = $this->createStub(ContentUpdateStruct::class);
111112

112-
$this->createEvent = new CreateContentEvent($content, $createStruct, [], null);
113-
$this->updateEvent = new UpdateContentEvent($content, $versionInfo, $updateStruct, null);
113+
$versionInfoV1 = $this->getMockBuilder(VersionInfo::class)
114+
->disableOriginalConstructor()
115+
->getMock();
116+
$versionInfoV1->method('getVersionNo')->willReturn(1);
117+
118+
$versionInfoV2 = $this->getMockBuilder(VersionInfo::class)
119+
->disableOriginalConstructor()
120+
->getMock();
121+
$versionInfoV2->method('getVersionNo')->willReturn(2);
122+
123+
$this->publishCreateEvent = new PublishVersionEvent($content, $versionInfoV1, []);
124+
$this->publishUpdateEvent = new PublishVersionEvent($content, $versionInfoV2, []);
114125
$this->deleteEvent = new BeforeDeleteContentEvent($contentInfo);
115126
});
116127

117128
afterEach(function () {
118129
removeTmpDir($this->tmpDir);
119130
});
120131

121-
it('onCreated handles successful runner branch', function () {
132+
it('onPublished (create) handles successful runner branch', function () {
122133
$listener = withTestingEnv(fn () => new ContentListener(
123134
new NullLogger(),
124135
makeSettingsService($this->tmpDir, true, ['content' => true]),
125136
$this->tmpDir,
126137
makeFakeRunner(0)
127138
));
128139

129-
withEnv('dev', fn () => expect(fn () => $listener->onCreated($this->createEvent))->not->toThrow(\Throwable::class));
140+
withEnv('dev', fn () => expect(fn () => $listener->onPublished($this->publishCreateEvent))->not->toThrow(\Throwable::class));
130141
});
131142

132-
it('onUpdated handles failed runner branch', function () {
143+
it('onPublished (update) handles failed runner branch', function () {
133144
$listener = withTestingEnv(fn () => new ContentListener(
134145
new NullLogger(),
135146
makeSettingsService($this->tmpDir, true, ['content' => true]),
136147
$this->tmpDir,
137148
makeFakeRunner(1, '', 'boom')
138149
));
139150

140-
withEnv('dev', fn () => expect(fn () => $listener->onUpdated($this->updateEvent))->not->toThrow(\Throwable::class));
151+
withEnv('dev', fn () => expect(fn () => $listener->onPublished($this->publishUpdateEvent))->not->toThrow(\Throwable::class));
141152
});
142153

143-
it('onCreated – forced ibexa mode – exercises ibexa generation branch', function () {
154+
it('onPublished (create) – forced ibexa mode – exercises ibexa generation branch', function () {
144155
$listener = withTestingEnv(fn () => new ContentListener(
145156
new NullLogger(),
146157
makeSettingsService($this->tmpDir, true, ['content' => true]),
@@ -149,6 +160,6 @@
149160
));
150161
setPrivateProperty($listener, 'mode', 'ibexa');
151162

152-
withEnv('dev', fn () => expect(fn () => $listener->onCreated($this->createEvent))->not->toThrow(\Throwable::class));
163+
withEnv('dev', fn () => expect(fn () => $listener->onPublished($this->publishCreateEvent))->not->toThrow(\Throwable::class));
153164
});
154165
});

0 commit comments

Comments
 (0)