Laravel NotificationFake.php sendNow在notifiable->id是uuid时导致非法偏移类型错误

5vf7fwbs  于 2023-04-10  发布在  PHP
关注(0)|答案(2)|浏览(100)

我正在使用Laravel 9,Vue 3和Inertia。我创建了以下测试:

public function test_send_notification(): void
    {
        Notification::fake();

        $this->seed(OccupationSeeder::class);
        $responder = Responder::factory()->create();

        $responder->notify(new InviteResponder());

        Notification::assertSentTo($responder, InviteResponder::class);
    }

当我运行测试时,我得到TypeError:偏移类型非法
执行时失败

$this->notifications[get_class($notifiable)][$notifiable->getKey()][get_class($notification)][] = [
                'notification' => $notification,
                'channels' => $notifiableChannels,
                'notifiable' => $notifiable,
                'locale' => $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
                    if ($notifiable instanceof HasLocalePreference) {
                        return $notifiable->preferredLocale();
                    }
                }),
            ];

in NotificationFake.php sendNow()
这是我的Responder模型的一部分

class Responder extends Model
{
    use HasFactory, Notifiable;

    protected $primaryKey = 'uuid';
    protected $keyType = 'string';
    public $incrementing = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'uuid',
        'diagnostic_event_id',
        'school_id',
        'user_id',
        'occupation_id',
        'years',
        'status'
    ];

    /**
     * Autogenerate uuid
     *
     * @return void
     */
    protected static function boot()
    {
        parent::boot();

        static::creating(function($model) {
            // Automatically create an uuid when creating a new responder
            $model->setAttribute($model->getKeyName(), Str::uuid());
        });
    }

这是ResponderFactory

class ResponderFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        return [
            'uuid' => $this->faker->uuid,
            'diagnostic_event_id' => DiagnosticEvent::factory()->lazy(),
            'school_id' => School::factory()->lazy(),
            'user_id' => User::factory()->lazy(),
            'occupation_id' => $this->faker->numberBetween(1,13),
            'years' => $this->faker->numberBetween(1,5),
            'status' => 'created',
            'created_at' => now(),
            'updated_at' => now()
        ];
    }
}

当我在程序中调用$responder-〉notify(...)时,我没有得到任何错误。
下面是我的ResponderController invite()函数:

public function invite(Request $request): RedirectResponse
    {
        $this->authorize('create', Responder::class);

        $responders = Responder::where('diagnostic_event_id', $request->diagnostic_event_id)->get();
        $questions = Question::all();

        DB::transaction( function() use ($request, $responders, $questions) {
            foreach ($responders as $responder) {
                // Skip if responder already notified
                if ($responder->status === 'created') {
                    // Create null answers for the responder if not already exists
                    foreach ($questions as $question) {
                        Answer::create([
                            'diagnostic_event_id' => $request->diagnostic_event_id,
                            'responder_uuid' => $responder->uuid,
                            'question_id' => $question->id,
                            'score' => null
                        ]);
                    }

                    // Send notification to responder
                    $responder->notify(new InviteResponder());

                    // Update responder status
                    $responder->update(['status' => 'sent']);
                }
            }
        });
        return redirect()->route('responder.select',
            ['event_id' => $request->diagnostic_event_id]);
    }

我的测试有什么问题,我可以做些什么来解决这个问题?

h4cxqtbf

h4cxqtbf1#

你可能想把你的faker改成这样:

'uuid' => Str::ulid()->toBase32()

或者简单地说:

'uuid' => $this->newModel()->newUniqueId()
h6my8fg2

h6my8fg22#

如果是user(User.php)模型应该是这样的。
看到应用的(字符串)类型!

public static function booted()
    {
        static::creating(function(User $user){
            $user->uuid = (string) Str::uuid();
        });
    }

相关问题