Skip to content

Commit

Permalink
Fix event firing for extended models
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Jan 3, 2025
1 parent 53605c6 commit 38acff8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/core/src/Base/Traits/HasModelExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,23 @@ public static function observe($classes): void
$instance->registerObserver($class);
}
}

/**
* Fire the given event for the model.
*/
protected function fireModelEvent($event, $halt = true): mixed
{
// Fire the actual models events
$result = parent::fireModelEvent($event, $halt);

$lunarClass = str_replace('Contracts\\', '', ModelManifest::guessContractClass(static::class));

if ($lunarClass == static::class) {
return $result;
}

return static::$dispatcher->{($halt ? 'until' : 'dispatch')}(
"eloquent.{$event}: ".$lunarClass, $this
);
}
}
4 changes: 4 additions & 0 deletions packages/core/src/LunarServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
use Lunar\Models\Language;
use Lunar\Models\Order;
use Lunar\Models\OrderLine;
use Lunar\Models\Product;
use Lunar\Models\ProductOption;
use Lunar\Models\ProductOptionValue;
use Lunar\Models\ProductVariant;
Expand All @@ -83,6 +84,7 @@
use Lunar\Observers\MediaObserver;
use Lunar\Observers\OrderLineObserver;
use Lunar\Observers\OrderObserver;
use Lunar\Observers\ProductObserver;
use Lunar\Observers\ProductOptionObserver;
use Lunar\Observers\ProductOptionValueObserver;
use Lunar\Observers\ProductVariantObserver;
Expand Down Expand Up @@ -296,6 +298,8 @@ protected function registerStateListeners()
*/
protected function registerObservers(): void
{
Product::observe(ProductObserver::class);

Channel::observe(ChannelObserver::class);
CustomerGroup::observe(CustomerGroupObserver::class);
Language::observe(LanguageObserver::class);
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/Observers/ProductObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Lunar\Observers;

use Lunar\Models\Product;

class ProductObserver
{
/**
* Handle the ProductVariant "deleted" event.
*
* @return void
*/
public function deleting(Product $product): void
{
$product->variants()->delete();
}

public function restored(Product $product): void
{
$product->variants()->restore();
}
}
25 changes: 25 additions & 0 deletions tests/core/Unit/Base/Traits/HasModelExtendingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,28 @@ function () {
->and(Product::morphName())
->toBe('product');
});

test('core model events are triggered with extended models', function () {
\Illuminate\Support\Facades\Event::fake();

$product = \Lunar\Tests\Core\Stubs\Models\Product::factory()->create();

$product->delete();

\Illuminate\Support\Facades\Event::assertDispatched(
'eloquent.deleted: ' . Product::class
);

\Lunar\Facades\ModelManifest::replace(
\Lunar\Models\Contracts\Product::class,
\Lunar\Tests\Core\Stubs\Models\CustomProduct::class
);

$product = \Lunar\Tests\Core\Stubs\Models\CustomProduct::factory()->create();

$product->delete();

\Illuminate\Support\Facades\Event::assertDispatched(
'eloquent.deleted: ' . Product::class
);
});

0 comments on commit 38acff8

Please sign in to comment.