Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Fix using AsStringable cast on Notifiable's key #54818

Merged
merged 3 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Illuminate/Support/Testing/Fakes/NotificationFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public function hasSent($notifiable, $notification)
*/
protected function notificationsFor($notifiable, $notification)
{
return $this->notifications[get_class($notifiable)][$notifiable->getKey()][$notification] ?? [];
return $this->notifications[get_class($notifiable)][(string) $notifiable->getKey()][$notification] ?? [];
}

/**
Expand Down Expand Up @@ -326,7 +326,7 @@ public function sendNow($notifiables, $notification, ?array $channels = null)
continue;
}

$this->notifications[get_class($notifiable)][$notifiable->getKey()][get_class($notification)][] = [
$this->notifications[get_class($notifiable)][(string) $notifiable->getKey()][get_class($notification)][] = [
'notification' => $this->serializeAndRestore && $notification instanceof ShouldQueue
? $this->serializeAndRestoreNotification($notification)
: $notification,
Expand Down
73 changes: 73 additions & 0 deletions tests/Integration/Notifications/DatabaseNotificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Illuminate\Tests\Integration\Notifications;

use Illuminate\Database\Eloquent\Casts\AsStringable;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Schema;
use Orchestra\Testbench\Attributes\DefineDatabase;
use Orchestra\Testbench\Attributes\WithMigration;
use Orchestra\Testbench\TestCase;

#[WithMigration('laravel', 'notifications')]
class DatabaseNotificationTest extends TestCase
{
use RefreshDatabase;

#[DefineDatabase('defineDatabaseAndConvertUserIdToUuid')]
public function testAssertSentToWhenNotifiableHasStringableKey()
{
Notification::fake();

$user = UuidUserFactoryStub::new()->create();

$user->notify(new NotificationStub);

Notification::assertSentTo($user, NotificationStub::class, function ($notification, $channels, $notifiable) use ($user) {
return $notifiable === $user;
});
}

/**
* Define database and convert User's ID to UUID.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function defineDatabaseAndConvertUserIdToUuid($app): void
{
Schema::table('users', function (Blueprint $table) {
$table->uuid('id')->change();
});
}
}

class UuidUserFactoryStub extends \Orchestra\Testbench\Factories\UserFactory
{
protected $model = UuidUserStub::class;
}

class UuidUserStub extends \Illuminate\Foundation\Auth\User
{
use HasUuids, Notifiable;

protected $table = 'users';

#[\Override]
public function casts()
{
return array_merge(parent::casts(), ['id' => AsStringable::class]);
}
}

class NotificationStub extends \Illuminate\Notifications\Notification
{
public function via($notifiable)
{
return ['mail'];
}
}
Loading