Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
Merge a2260f3 into 8042958
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigopedra committed Sep 5, 2019
2 parents 8042958 + a2260f3 commit abc0792
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -27,6 +27,7 @@
"illuminate/events": "^6.0",
"illuminate/support": "^6.0",
"league/flysystem": "^1.0.45",
"spatie/laravel-schemaless-attributes": "^1.6",
"symfony/finder": "^4.2",
"symfony/property-access": "^4.0",
"symfony/serializer": "^4.0"
Expand Down
11 changes: 11 additions & 0 deletions src/Models/EloquentStoredEvent.php
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Spatie\EventProjector\ShouldBeStored;
use Spatie\SchemalessAttributes\SchemalessAttributes;

class EloquentStoredEvent extends Model
{
Expand Down Expand Up @@ -37,6 +38,16 @@ public function getEventAttribute(): ShouldBeStored
return $this->toStoredEvent()->event;
}

public function getMetaDataAttribute(): SchemalessAttributes
{
return SchemalessAttributes::createForModel($this, 'meta_data');
}

public function scopeWithMetaDataAttributes(): Builder
{
return SchemalessAttributes::scopeWithSchemalessAttributes('meta_data');
}

public function scopeStartingFrom(Builder $query, int $storedEventId): void
{
$query->where('id', '>=', $storedEventId);
Expand Down
23 changes: 23 additions & 0 deletions tests/Models/StoredEventTest.php
Expand Up @@ -58,6 +58,29 @@ public function it_will_store_the_alias_when_a_classname_is_found_in_the_event_c
$this->assertDatabaseHas('stored_events', ['event_class' => 'money_added']);
}

/** @test * */
public function it_allows_to_modify_metadata_with_offset_set_in_eloquent_model()
{
EloquentStoredEvent::creating(function (EloquentStoredEvent $event) {
$event->meta_data['ip'] = '127.0.0.1';
});

$this->setConfig('event-projector.event_class_map', [
'money_added' => MoneyAddedEvent::class,
]);

$this->fireEvents();

$instance = EloquentStoredEvent::first();
$payload = (new EloquentStoredEvent())->getConnection()->raw("CAST('{\"ip\": \"127.0.0.1\"}' AS JSON)");

$this->assertArrayHasKey('ip', $instance->meta_data->toArray());
$this->assertSame('127.0.0.1', $instance->meta_data['ip']);
$this->assertDatabaseHas('stored_events', ['meta_data' => $payload]);

EloquentStoredEvent::flushEventListeners();
}

public function fireEvents(int $number = 1, string $className = MoneyAddedEvent::class)
{
foreach (range(1, $number) as $i) {
Expand Down

0 comments on commit abc0792

Please sign in to comment.