|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Notifications; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Casts\AsStringable; |
| 6 | +use Illuminate\Database\Eloquent\Concerns\HasUuids; |
| 7 | +use Illuminate\Database\Schema\Blueprint; |
| 8 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 9 | +use Illuminate\Notifications\Notifiable; |
| 10 | +use Illuminate\Support\Facades\Notification; |
| 11 | +use Illuminate\Support\Facades\Schema; |
| 12 | +use Orchestra\Testbench\Attributes\DefineDatabase; |
| 13 | +use Orchestra\Testbench\Attributes\WithMigration; |
| 14 | +use Orchestra\Testbench\TestCase; |
| 15 | + |
| 16 | +#[WithMigration('laravel', 'notifications')] |
| 17 | +class DatabaseNotificationTest extends TestCase |
| 18 | +{ |
| 19 | + use RefreshDatabase; |
| 20 | + |
| 21 | + #[DefineDatabase('defineDatabaseAndConvertUserIdToUuid')] |
| 22 | + public function testAssertSentToWhenNotifiableHasStringableKey() |
| 23 | + { |
| 24 | + Notification::fake(); |
| 25 | + |
| 26 | + $user = UuidUserFactoryStub::new()->create(); |
| 27 | + |
| 28 | + $user->notify(new NotificationStub); |
| 29 | + |
| 30 | + Notification::assertSentTo($user, NotificationStub::class, function ($notification, $channels, $notifiable) use ($user) { |
| 31 | + return $notifiable === $user; |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Define database and convert User's ID to UUID. |
| 37 | + * |
| 38 | + * @param \Illuminate\Foundation\Application $app |
| 39 | + * @return void |
| 40 | + */ |
| 41 | + protected function defineDatabaseAndConvertUserIdToUuid($app): void |
| 42 | + { |
| 43 | + Schema::table('users', function (Blueprint $table) { |
| 44 | + $table->uuid('id')->change(); |
| 45 | + }); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +class UuidUserFactoryStub extends \Orchestra\Testbench\Factories\UserFactory |
| 50 | +{ |
| 51 | + protected $model = UuidUserStub::class; |
| 52 | +} |
| 53 | + |
| 54 | +class UuidUserStub extends \Illuminate\Foundation\Auth\User |
| 55 | +{ |
| 56 | + use HasUuids, Notifiable; |
| 57 | + |
| 58 | + protected $table = 'users'; |
| 59 | + |
| 60 | + #[\Override] |
| 61 | + public function casts() |
| 62 | + { |
| 63 | + return array_merge(parent::casts(), ['id' => AsStringable::class]); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +class NotificationStub extends \Illuminate\Notifications\Notification |
| 68 | +{ |
| 69 | + public function via($notifiable) |
| 70 | + { |
| 71 | + return ['mail']; |
| 72 | + } |
| 73 | +} |
0 commit comments