diff --git a/src/Illuminate/Database/Eloquent/Attributes/ObservedBy.php b/src/Illuminate/Database/Eloquent/Attributes/ObservedBy.php new file mode 100644 index 000000000000..600174146f94 --- /dev/null +++ b/src/Illuminate/Database/Eloquent/Attributes/ObservedBy.php @@ -0,0 +1,19 @@ +getAttributes(ObservedBy::class)) + ->map(fn ($attribute) => $attribute->getArguments()) + ->flatten() + ->all(); + } + /** * Register observers with the model. * 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; diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index 395629b4f9b1..1966e5550bee 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\ObservedBy; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\ArrayObject; use Illuminate\Database\Eloquent\Casts\AsArrayObject; @@ -1893,6 +1894,26 @@ 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 +3355,18 @@ public function uniqueIds() } } +#[ObservedBy(EloquentTestObserverStub::class)] +class EloquentModelWithObserveAttributeStub extends EloquentModelStub +{ + // +} + +#[ObservedBy([EloquentTestObserverStub::class])] +class EloquentModelWithObserveAttributeUsingArrayStub extends EloquentModelStub +{ + // +} + class EloquentModelSavingEventStub { //