diff --git a/src/AttributeEvents.php b/src/AttributeEvents.php index a7df948..a97aca2 100644 --- a/src/AttributeEvents.php +++ b/src/AttributeEvents.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Kleemans\Tests\Fake\OrderStatus; trait AttributeEvents { @@ -28,35 +29,31 @@ private function fireAttributeEvents(): void { foreach ($this->getAttributeEvents() as $change => $event) { [$attribute, $expected] = explode(':', $change); - $value = $this->getAttribute($attribute); // Accessor if ($this->hasAccessor($attribute)) { - if (!$this->isDirtyAccessor($attribute)) { + if (! $this->isDirtyAccessor($attribute)) { continue; // Not changed } - } - - // JSON attribute + } // JSON attribute elseif (Str::contains($attribute, '->')) { [$attribute, $path] = explode('->', $attribute, 2); $path = str_replace('->', '.', $path); - if (!$this->isDirtyNested($attribute, $path)) { + if (! $this->isDirtyNested($attribute, $path)) { continue; // Not changed } $value = Arr::get($this->getAttribute($attribute), $path); - } - - // Regular attribute - elseif (!$this->isDirty($attribute)) { + } // Regular attribute + elseif (! $this->isDirty($attribute)) { continue; // Not changed } if ( $expected === '*' + || $value instanceof \UnitEnum && ($value->name === $expected) || $expected === 'true' && $value === true || $expected === 'false' && $value === false || is_numeric($expected) && Str::contains($expected, '.') && $value === (float) $expected // float @@ -73,7 +70,7 @@ private function syncOriginalAccessors(): void foreach ($this->getAttributeEvents() as $change => $event) { [$attribute] = explode(':', $change); - if (!$this->hasAccessor($attribute)) { + if (! $this->hasAccessor($attribute)) { continue; // Attribute does not have accessor } @@ -88,7 +85,7 @@ private function syncOriginalAccessors(): void public function isDirtyAccessor(string $attribute): bool { - if (!isset($this->originalAccessors[$attribute])) { + if (! isset($this->originalAccessors[$attribute])) { return false; // Attribute does not have a original value saved } @@ -116,7 +113,7 @@ public function isDirtyNested(string $attribute, string $path): bool private function getAttributeEvents(): iterable { foreach ($this->dispatchesEvents as $change => $event) { - if (!Str::contains($change, ':')) { + if (! Str::contains($change, ':')) { continue; // Not an attribute event } diff --git a/tests/AttributeEventsTest.php b/tests/AttributeEventsTest.php index 63d84a3..f24ef55 100644 --- a/tests/AttributeEventsTest.php +++ b/tests/AttributeEventsTest.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Events\Dispatcher; use Illuminate\Support\Testing\Fakes\EventFake; +use Kleemans\Tests\Fake\OrderStatus; use PHPUnit\Framework\TestCase; class AttributeEventsTest extends TestCase @@ -18,6 +19,7 @@ class AttributeEventsTest extends TestCase Fake\Events\OrderDeleted::class, Fake\Events\OrderNoteUpdated::class, Fake\Events\OrderShipped::class, + Fake\Events\EnumOrderShipped::class, Fake\Events\OrderCanceled::class, Fake\Events\OrderReturned::class, Fake\Events\OrderMadeFree::class, @@ -391,6 +393,19 @@ public function it_works_with_nested_json_fields() $this->dispatcher->assertDispatched(Fake\Events\InvoiceDownloaded::class); } + /** @test */ + public function it_works_with_enumerated_casts() + { + $order = new Fake\EnumOrder(); + $order->status = OrderStatus::PROCESSING; + $order->save(); + + $order->status = OrderStatus::SHIPPED; + $order->save(); + + $this->dispatcher->assertDispatched(Fake\Events\EnumOrderShipped::class); + } + // Setup methods private function initEventDispatcher() diff --git a/tests/Fake/EnumOrder.php b/tests/Fake/EnumOrder.php new file mode 100644 index 0000000..8e8dfed --- /dev/null +++ b/tests/Fake/EnumOrder.php @@ -0,0 +1,37 @@ + OrderStatus::PROCESSING, + 'shipping_address' => '', + 'billing_address' => '', + 'note' => '', + 'total' => 0.00, + 'paid_amount' => 0.00, + 'discount_percentage' => 0, + 'tax_free' => false, + 'payment_gateway' => 'credit_card', + 'meta' => '{}', + ]; + + protected $guarded = []; + + protected $casts = [ + 'status' => OrderStatus::class, + ]; + + protected $dispatchesEvents = [ + 'status:SHIPPED' => Events\EnumOrderShipped::class, + ]; +} diff --git a/tests/Fake/Events/EnumOrderShipped.php b/tests/Fake/Events/EnumOrderShipped.php new file mode 100644 index 0000000..a5c2207 --- /dev/null +++ b/tests/Fake/Events/EnumOrderShipped.php @@ -0,0 +1,13 @@ +