8000 NotificationFake error · Issue #49950 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

NotificationFake error #49950

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

Closed
Herbertzz opened this issue Feb 2, 2024 · 2 comments
Closed

NotificationFake error #49950

Herbertzz opened this issue Feb 2, 2024 · 2 comments

Comments

@Herbertzz
Copy link

Laravel Version

10.41.0

PHP Version

8.3

Database Driver & Version

No response

Description

TypeError: get_class(): Argument #1 ($object) must be of type object, string given
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/NotificationFake.php:315
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Testing/Fakes/NotificationFake.php:280
/var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:355
/var/www/html/tests/Feature/NotificationSendTest.php:21
/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:177

Steps To Reproduce

Execute the following test classes

<?php

namespace Tests\Feature;

use App\Notifications\MobileVerifyCode;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;

class NotificationSendTest extends TestCase
{
    use RefreshDatabase;

    public function test_mobile_verify_code()
    {
        Notification::fake();
        $mobiles = ['18800001111', '19911112222'];
        Notification::send($mobiles, new MobileVerifyCode('012345', 5));
        Notification::assertSentTimes(MobileVerifyCode::class, 2);

//        $user = User::factory()->create();
//        Notification::send($user, new MobileVerifyCode('012345', 5));
    }
}
@mateusjunges
Copy link
Contributor

You are getting this issue because you are passing an array of strings to the send method, while it expects an array or \Illuminate\Support\Collection of objects. The $notifiables argument is then passed one by one to the via method (which is typed as object) of your notification by the NotificationSender class:

foreach ($notifiables as $notifiable) {
if (empty($viaChannels = $channels ?: $notification->via($notifiable))) {
continue;
}

When faking notifications using Notification::fake, the \Illuminate\Support\Testing\Fakes\NotificationFake class is used instead of the ChannelManager. Now, when trying to send your notification, laravel will try to store your notification in a in memory array, so you can perform assertions against it later on:

$this->notifications[get_class($notifiable)][$notifiable->getKey()][get_class($notification)][] = [

The line above tries to add the notification you just sent to an array like this:

$array[FQCN of the notifiable][Notifiable primary key][Notification FQCN]

$array[User::class][12345][MobileVerifyCode::class]

Since you are passing an array of strings, that line will throw an exception when calling get_class using a string as parameter.

Possible solution

By reading your example code, I assume you are trying to send on-demand notifications.

If you want to send the MobileVerifyCode notification (which I assume that uses some sms channel) to recipients 18800001111 and 19911112222, you can do this using the Notification::route method:

Notification::fake();
$mobiles = ['18800001111', '19911112222'];

foreach ($mobiles as $mobile) {
    Notification::route('vonage', $mobile)
        ->notify(new MobileVerifyCode('012345', 5));
}
// Or:

Notification::route('vonage', $mobiles[0])
    ->route('vonage', $mobiles[1])
    ->notify(new MobileVerifyCode('012345', 5));
}

@crynobone
Copy link
Member

See above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0