Skip to content

Commit 3d05515

Browse files
committed
feat: add SyncEntitiesSubscriber to sync entities with migrations
1 parent 5fff111 commit 3d05515

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/Listeners/SyncEntities.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CalebDW\SqlEntities\Listeners;
6+
7+
use CalebDW\SqlEntities\EntityManager;
8+
use Illuminate\Database\Events\MigrationsEnded;
9+
use Illuminate\Database\Events\MigrationsStarted;
10+
11+
class SyncEntities
12+
{
13+
public function __construct(
14+
protected EntityManager $manager,
15+
) {
16+
}
17+
18+
public function handleStarted(MigrationsStarted $event): void
19+
{
20+
if ($event->method !== 'up') {
21+
return;
22+
}
23+
24+
if ($event->options['pretend'] ?? false) {
25+
return;
26+
}
27+
28+
$this->manager->dropAll();
29+
}
30+
31+
public function handleEnded(MigrationsEnded $event): void
32+
{
33+
if ($event->method !== 'up') {
34+
return;
35+
}
36+
37+
if ($event->options['pretend'] ?? false) {
38+
return;
39+
}
40+
41+
$this->manager->createAll();
42+
}
43+
44+
/**
45+
* @codeCoverageIgnore
46+
* @return array<string, string>
47+
*/
48+
public function subscribe(): array
49+
{
50+
return [
51+
MigrationsStarted::class => 'handleStarted',
52+
MigrationsEnded::class => 'handleEnded',
53+
];
54+
}
55+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use CalebDW\SqlEntities\EntityManager;
6+
use CalebDW\SqlEntities\Listeners\SyncEntities;
7+
use Illuminate\Database\Events\MigrationsEnded;
8+
use Illuminate\Database\Events\MigrationsStarted;
9+
use Mockery;
10+
11+
beforeEach(function () {
12+
test()->manager = Mockery::mock(EntityManager::class);
13+
test()->listener = new SyncEntities(test()->manager);
14+
});
15+
16+
afterEach(function () {
17+
Mockery::close();
18+
});
19+
20+
describe('started', function () {
21+
it('does nothing if the method is not "up"', function () {
22+
test()->manager->shouldNotReceive('dropAll');
23+
test()->listener->handleStarted(
24+
new MigrationsStarted(method: 'down'),
25+
);
26+
});
27+
it('does nothing if the pretend option is true', function () {
28+
test()->manager->shouldNotReceive('dropAll');
29+
test()->listener->handleStarted(
30+
new MigrationsStarted(method: 'up', options: ['pretend' => true]),
31+
);
32+
});
33+
it('drops all entities', function () {
34+
test()->manager
35+
->shouldReceive('dropAll')
36+
->once();
37+
38+
test()->listener->handleStarted(
39+
new MigrationsStarted(method: 'up'),
40+
);
41+
});
42+
});
43+
44+
describe('ended', function () {
45+
it('does nothing if the method is not "up"', function () {
46+
test()->manager->shouldNotReceive('createAll');
47+
test()->listener->handleEnded(
48+
new MigrationsEnded(method: 'down'),
49+
);
50+
});
51+
it('does nothing if the pretend option is true', function () {
52+
test()->manager->shouldNotReceive('createAll');
53+
test()->listener->handleEnded(
54+
new MigrationsEnded(method: 'up', options: ['pretend' => true]),
55+
);
56+
});
57+
it('creates all entities', function () {
58+
test()->manager
59+
->shouldReceive('createAll')
60+
->once();
61+
62+
test()->listener->handleEnded(
63+
new MigrationsEnded(method: 'up'),
64+
);
65+
});
66+
});

0 commit comments

Comments
 (0)