Skip to content

Commit 37b43b3

Browse files
committed
cleanup
1 parent 5121a29 commit 37b43b3

File tree

4 files changed

+51
-54
lines changed

4 files changed

+51
-54
lines changed

tests/DiscoversEventHandlersTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@
1414
use Spatie\EventSourcing\Tests\TestClasses\AutoDiscoverEventHandlers\TestReactor;
1515
use function PHPUnit\Framework\assertEqualsCanonicalizing;
1616

17-
beforeEach(function () {
18-
$this->getDiscoveryBasePath = function (): string
19-
{
20-
return realpath($this->pathToTests().'/../');
21-
};
22-
});
17+
function getDiscoveryBasePath(): string
18+
{
19+
return realpath(test()->pathToTests().'/../');
20+
}
2321

2422
test('it can get all classes that have event handlers', function () {
2523
/** @var \Spatie\EventSourcing\Projectionist $projectionist */
2624
$projectionist = app(Projectionist::class);
2725

2826
$pathToComposerJson = __DIR__.'/../composer.json';
2927

30-
$getDiscoveryBasePath = $this->getDiscoveryBasePath;
31-
3228
(new DiscoverEventHandlers())
3329
->within([__DIR__.'/TestClasses/AutoDiscoverEventHandlers'])
34-
->useBasePath($getDiscoveryBasePath())
30+
->useBasePath(getDiscoveryBasePath())
3531
->useRootNamespace('Spatie\EventSourcing\\')
3632
->ignoringFiles(Composer::getAutoloadedFiles($pathToComposerJson))
3733

tests/Models/StoredEventTest.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,27 @@
2222
use function PHPUnit\Framework\assertSame;
2323
use function PHPUnit\Framework\assertTrue;
2424

25+
function fireEvents(int $number = 1, string $className = MoneyAddedEvent::class)
26+
{
27+
foreach (range(1, $number) as $i) {
28+
event(new $className(test()->account, 1234));
29+
}
30+
}
31+
2532
beforeEach(function () {
2633
Projectionist::addProjector(new BalanceProjector());
2734

2835
$this->account = Account::create();
29-
30-
$this->fireEvents = function (int $number = 1, string $className = MoneyAddedEvent::class) {
31-
foreach (range(1, $number) as $i) {
32-
event(new $className($this->account, 1234));
33-
}
34-
};
3536
});
3637

3738
test('it has a scope to get all events starting from given id', function () {
38-
($this->fireEvents)(4);
39+
fireEvents(4);
3940

4041
assertEquals([3, 4], EloquentStoredEvent::startingFrom(3)->pluck('id')->toArray());
4142
});
4243

4344
test('it will throw a human readable exception when the event couldnt be deserialized', function () {
44-
($this->fireEvents)();
45+
fireEvents();
4546

4647
// sneakily change the stored event class
4748
EloquentStoredEvent::first()->update(['event_class' => 'NonExistingClass']);
@@ -54,7 +55,7 @@
5455
'money_added' => MoneyAddedEvent::class,
5556
]);
5657

57-
($this->fireEvents)();
58+
fireEvents();
5859

5960
assertEquals(MoneyAddedEvent::class, EloquentStoredEvent::first()->toStoredEvent()->event_class);
6061
$this->assertDatabaseHas('stored_events', ['event_class' => 'money_added']);
@@ -69,7 +70,7 @@
6970
'money_added' => MoneyAddedEvent::class,
7071
]);
7172

72-
($this->fireEvents)();
73+
fireEvents();
7374

7475
$instance = EloquentStoredEvent::withMetaDataAttributes('ip', '127.0.0.1')->first();
7576

@@ -81,7 +82,7 @@
8182
});
8283

8384
test('it can handle an encoded string as event properties', function () {
84-
($this->fireEvents)();
85+
fireEvents();
8586

8687
$eloquentEvent = EloquentStoredEvent::first();
8788

@@ -99,7 +100,7 @@
99100
});
100101

101102
test('it encodes the event properties itself when its an array', function () {
102-
($this->fireEvents)();
103+
fireEvents();
103104

104105
$eloquentEvent = EloquentStoredEvent::first();
105106

@@ -119,7 +120,7 @@
119120
});
120121

121122
test('it exposes the aggregate version', function () {
122-
($this->fireEvents)();
123+
fireEvents();
123124

124125
$eloquentEvent = EloquentStoredEvent::first();
125126

@@ -191,7 +192,7 @@
191192

192193
Carbon::setTestNow($now);
193194

194-
($this->fireEvents)();
195+
fireEvents();
195196

196197
/** @var \Spatie\EventSourcing\StoredEvents\Models\EloquentStoredEvent $eloquentEvent */
197198
$eloquentEvent = EloquentStoredEvent::first();
@@ -202,7 +203,7 @@
202203
});
203204

204205
test('the stored event id is set', function () {
205-
($this->fireEvents)();
206+
fireEvents();
206207

207208
/** @var \Spatie\EventSourcing\StoredEvents\Models\EloquentStoredEvent $eloquentEvent */
208209
$eloquentEvent = EloquentStoredEvent::first();

tests/ProjectionTest.php

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@
1010
use function PHPUnit\Framework\assertFalse;
1111
use function PHPUnit\Framework\assertTrue;
1212

13-
beforeEach(function () {
14-
$this->createProjection = function (): ProjectionModel
15-
{
16-
ProjectionModel::new()->writeable()->create([
17-
'uuid' => 'test-uuid',
18-
'field' => 'original',
19-
]);
13+
function createProjection(): ProjectionModel
14+
{
15+
ProjectionModel::new()->writeable()->create([
16+
'uuid' => 'test-uuid',
17+
'field' => 'original',
18+
]);
2019

21-
return ProjectionModel::find('test-uuid');
22-
};
20+
return ProjectionModel::find('test-uuid');
21+
}
2322

24-
$this->assertNothingChanged = function (): void
25-
{
26-
$this->assertDatabaseHas((new ProjectionModel())->getTable(), [
27-
'uuid' => 'test-uuid',
28-
'field' => 'original',
29-
]);
30-
};
23+
function assertNothingChanged(): void
24+
{
25+
test()->assertDatabaseHas((new ProjectionModel())->getTable(), [
26+
'uuid' => 'test-uuid',
27+
'field' => 'original',
28+
]);
29+
}
3130

31+
beforeEach(function () {
3232
Schema::dropIfExists('projection_models');
3333

3434
Schema::create('projection_models', function (Blueprint $table): void {
@@ -55,15 +55,15 @@
5555
});
5656

5757
test('save', function () {
58-
$projection = ($this->createProjection)();
58+
$projection = createProjection();
5959

6060
expect(function () use ($projection) {
6161
$projection->field = 'changed';
6262

6363
$projection->save();
6464
})->toThrow(ReadonlyProjection::class);
6565

66-
($this->assertNothingChanged)();
66+
assertNothingChanged();
6767

6868
$projection->field = 'changed';
6969

@@ -73,15 +73,15 @@
7373
});
7474

7575
test('update', function () {
76-
$projection = ($this->createProjection)();
76+
$projection = createProjection();
7777

7878
expect(function () use ($projection) {
7979
$projection->update([
8080
'field' => 'changed',
8181
]);
8282
})->toThrow(ReadonlyProjection::class);
8383

84-
($this->assertNothingChanged)();
84+
assertNothingChanged();
8585

8686
$projection->writeable()->update([
8787
'field' => 'changed',
@@ -91,39 +91,39 @@
9191
});
9292

9393
test('delete', function () {
94-
$projection = ($this->createProjection)();
94+
$projection = createProjection();
9595

9696
expect(fn() => $projection->delete())->toThrow(ReadonlyProjection::class);
9797

98-
($this->assertNothingChanged)();
98+
assertNothingChanged();
9999

100100
$projection->writeable()->delete();
101101

102102
assertEquals(0, ProjectionModel::all()->count());
103103
});
104104

105105
test('force delete', function () {
106-
$projection = ($this->createProjection)();
106+
$projection = createProjection();
107107

108108
expect(fn () => $projection->forceDelete())->toThrow(ReadonlyProjection::class);
109109

110-
($this->assertNothingChanged)();
110+
assertNothingChanged();
111111

112112
$projection->writeable()->forceDelete();
113113

114114
assertEquals(0, ProjectionModel::all()->count());
115115
});
116116

117117
test('force fill', function () {
118-
$projection = ($this->createProjection)();
118+
$projection = createProjection();
119119

120120
expect(function () use ($projection) {
121121
$projection->forceFill([
122122
'field' => 'changed',
123123
])->save();
124124
})->toThrow(ReadonlyProjection::class);
125125

126-
($this->assertNothingChanged)();
126+
assertNothingChanged();
127127

128128
$projection->writeable()->forceFill([
129129
'field' => 'changed',
@@ -133,23 +133,23 @@
133133
});
134134

135135
test('is writeable is reset on refresh', function () {
136-
$projection = ($this->createProjection)();
136+
$projection = createProjection();
137137

138138
$projection = $projection->writeable();
139139

140140
assertFalse($projection->refresh()->isWriteable());
141141
});
142142

143143
test('is writeable is reset on fresh', function () {
144-
$projection = ($this->createProjection)();
144+
$projection = createProjection();
145145

146146
$projection = $projection->writeable();
147147

148148
assertFalse($projection->fresh()->isWriteable());
149149
});
150150

151151
test('read', function () {
152-
($this->createProjection)();
152+
createProjection();
153153

154154
assertEquals(1, ProjectionModel::all()->count());
155155
});

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function setConfig(string $name, $value)
8282
(new EventSourcingServiceProvider($this->app))->register();
8383
}
8484

85-
protected function pathToTests(): string
85+
public function pathToTests(): string
8686
{
8787
return __DIR__;
8888
}

0 commit comments

Comments
 (0)