From eaa1c94dae9c678d41d56f1ccd73ed146b2bbcf3 Mon Sep 17 00:00:00 2001 From: emargareten <2771355@gmail.com> Date: Thu, 25 Jan 2024 23:05:49 +0200 Subject: [PATCH 1/4] Introduce Observe attribute for models --- .../Database/Eloquent/Attributes/Observe.php | 19 +++++++++++ .../Database/Eloquent/Concerns/HasEvents.php | 27 +++++++++++++++ tests/Database/DatabaseEloquentModelTest.php | 34 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/Illuminate/Database/Eloquent/Attributes/Observe.php diff --git a/src/Illuminate/Database/Eloquent/Attributes/Observe.php b/src/Illuminate/Database/Eloquent/Attributes/Observe.php new file mode 100644 index 000000000000..fa4377c543ae --- /dev/null +++ b/src/Illuminate/Database/Eloquent/Attributes/Observe.php @@ -0,0 +1,19 @@ +getAttributes(Observe::class)) + ->map(fn ($attribute) => $attribute->getArguments()) + ->flatten() + ->all(); + } + /** * Register observers with the model. * diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index 395629b4f9b1..efa8c653c8d0 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -15,6 +15,7 @@ use Illuminate\Database\Connection; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\ConnectionResolverInterface as Resolver; +use Illuminate\Database\Eloquent\Attributes\Observe; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\ArrayObject; use Illuminate\Database\Eloquent\Casts\AsArrayObject; @@ -1893,6 +1894,27 @@ public function testModelObserversCanBeAttachedToModelsThroughAnArray() EloquentModelStub::flushEventListeners(); } + + public function testModelObserversCanBeAttachedToModelsWithStringUsingAttribute() + { + EloquentModelWithObserveAttributeStub::setEventDispatcher($events = m::mock(Dispatcher::class)); + $events->shouldReceive('dispatch'); + $events->shouldReceive('listen')->once()->with('eloquent.creating: Illuminate\Tests\Database\EloquentModelWithObserveAttributeStub', EloquentTestObserverStub::class.'@creating'); + $events->shouldReceive('listen')->once()->with('eloquent.saved: Illuminate\Tests\Database\EloquentModelWithObserveAttributeStub', EloquentTestObserverStub::class.'@saved'); + $events->shouldReceive('forget'); + EloquentModelWithObserveAttributeStub::flushEventListeners(); + } + + public function testModelObserversCanBeAttachedToModelsThroughAnArrayUsingAttribute() + { + EloquentModelWithObserveAttributeUsingArrayStub::setEventDispatcher($events = m::mock(Dispatcher::class)); + $events->shouldReceive('dispatch'); + $events->shouldReceive('listen')->once()->with('eloquent.creating: Illuminate\Tests\Database\EloquentModelWithObserveAttributeUsingArrayStub', EloquentTestObserverStub::class.'@creating'); + $events->shouldReceive('listen')->once()->with('eloquent.saved: Illuminate\Tests\Database\EloquentModelWithObserveAttributeUsingArrayStub', EloquentTestObserverStub::class.'@saved'); + $events->shouldReceive('forget'); + EloquentModelWithObserveAttributeUsingArrayStub::flushEventListeners(); + } + public function testThrowExceptionOnAttachingNotExistsModelObserverWithString() { $this->expectException(InvalidArgumentException::class); @@ -3334,6 +3356,18 @@ public function uniqueIds() } } +#[Observe(EloquentTestObserverStub::class)] +class EloquentModelWithObserveAttributeStub extends EloquentModelStub +{ + // +} + +#[Observe([EloquentTestObserverStub::class])] +class EloquentModelWithObserveAttributeUsingArrayStub extends EloquentModelStub +{ + // +} + class EloquentModelSavingEventStub { // From f6f40f3d7045ec8d000450d18f874e9c3b0c31a1 Mon Sep 17 00:00:00 2001 From: emargareten <2771355@gmail.com> Date: Thu, 25 Jan 2024 23:06:01 +0200 Subject: [PATCH 2/4] fix tests --- ...rsSendEmailVerificationNotificationHandleFunctionTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/Auth/AuthListenersSendEmailVerificationNotificationHandleFunctionTest.php b/tests/Auth/AuthListenersSendEmailVerificationNotificationHandleFunctionTest.php index 2b888f59f07e..9ef72a9fd726 100644 --- a/tests/Auth/AuthListenersSendEmailVerificationNotificationHandleFunctionTest.php +++ b/tests/Auth/AuthListenersSendEmailVerificationNotificationHandleFunctionTest.php @@ -6,6 +6,7 @@ use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User; +use Mockery as m; use PHPUnit\Framework\TestCase; class AuthListenersSendEmailVerificationNotificationHandleFunctionTest extends TestCase @@ -29,8 +30,8 @@ public function testWillExecuted() */ public function testUserIsNotInstanceOfMustVerifyEmail() { - $user = $this->getMockBuilder(User::class)->getMock(); - $user->expects($this->never())->method('sendEmailVerificationNotification'); + $user = m::mock(User::class); + $user->shouldNotReceive('sendEmailVerificationNotification'); $listener = new SendEmailVerificationNotification; From 99f3cc1d1841d1c1b869fddcb06881b5e6b9a400 Mon Sep 17 00:00:00 2001 From: Eliezer Margareten <46111162+emargareten@users.noreply.github.com> Date: Fri, 26 Jan 2024 00:50:15 +0200 Subject: [PATCH 3/4] fix styling --- tests/Database/DatabaseEloquentModelTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index efa8c653c8d0..c720e4e92837 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -1894,7 +1894,6 @@ public function testModelObserversCanBeAttachedToModelsThroughAnArray() EloquentModelStub::flushEventListeners(); } - public function testModelObserversCanBeAttachedToModelsWithStringUsingAttribute() { EloquentModelWithObserveAttributeStub::setEventDispatcher($events = m::mock(Dispatcher::class)); From a9562beefe7758f916d55f95e965511d7778f09f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 9 Feb 2024 11:11:30 -0600 Subject: [PATCH 4/4] rename file --- .../Eloquent/Attributes/{Observe.php => ObservedBy.php} | 2 +- src/Illuminate/Database/Eloquent/Concerns/HasEvents.php | 4 ++-- tests/Database/DatabaseEloquentModelTest.php | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) rename src/Illuminate/Database/Eloquent/Attributes/{Observe.php => ObservedBy.php} (95%) diff --git a/src/Illuminate/Database/Eloquent/Attributes/Observe.php b/src/Illuminate/Database/Eloquent/Attributes/ObservedBy.php similarity index 95% rename from src/Illuminate/Database/Eloquent/Attributes/Observe.php rename to src/Illuminate/Database/Eloquent/Attributes/ObservedBy.php index fa4377c543ae..600174146f94 100644 --- a/src/Illuminate/Database/Eloquent/Attributes/Observe.php +++ b/src/Illuminate/Database/Eloquent/Attributes/ObservedBy.php @@ -5,7 +5,7 @@ use Attribute; #[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)] -class Observe +class ObservedBy { /** * Create a new attribute instance. diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php b/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php index b5437ae80151..0730dcb10971 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php @@ -3,7 +3,7 @@ namespace Illuminate\Database\Eloquent\Concerns; use Illuminate\Contracts\Events\Dispatcher; -use Illuminate\Database\Eloquent\Attributes\Observe; +use Illuminate\Database\Eloquent\Attributes\ObservedBy; use Illuminate\Events\NullDispatcher; use Illuminate\Support\Arr; use InvalidArgumentException; @@ -48,7 +48,7 @@ public static function resolveObserveAttributes() { $reflectionClass = new ReflectionClass(static::class); - return collect($reflectionClass->getAttributes(Observe::class)) + return collect($reflectionClass->getAttributes(ObservedBy::class)) ->map(fn ($attribute) => $attribute->getArguments()) ->flatten() ->all(); diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index c720e4e92837..1966e5550bee 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -15,7 +15,7 @@ use Illuminate\Database\Connection; use Illuminate\Database\ConnectionResolverInterface; use Illuminate\Database\ConnectionResolverInterface as Resolver; -use Illuminate\Database\Eloquent\Attributes\Observe; +use Illuminate\Database\Eloquent\Attributes\ObservedBy; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\ArrayObject; use Illuminate\Database\Eloquent\Casts\AsArrayObject; @@ -3355,13 +3355,13 @@ public function uniqueIds() } } -#[Observe(EloquentTestObserverStub::class)] +#[ObservedBy(EloquentTestObserverStub::class)] class EloquentModelWithObserveAttributeStub extends EloquentModelStub { // } -#[Observe([EloquentTestObserverStub::class])] +#[ObservedBy([EloquentTestObserverStub::class])] class EloquentModelWithObserveAttributeUsingArrayStub extends EloquentModelStub { //