Skip to content

PHPUnit gets “ErrorException: Illegal string offset 'address' ” when testnig Maillable hasFrom(…) method #25532

@timkolm

Description

@timkolm
  • Laravel Version: 5.6
  • PHP Version: 7.1
  • Database Driver & Version: Mysql 5.7

Description:

Here's my Mailable class:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class OrderConfirmation extends Mailable
{
    use Queueable, SerializesModels;

    public $message;
    public $subject;
    public $from;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($message, $subject, $from)
    {
        $this->message = $message;
        $this->subject = $subject;
        $this->from = $from;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->from($this->from)
            ->subject($this->subject)
            ->view('emails.orders.confirmation');
    }
}

I'm trying to check that mail has certain From address like this:

Mail::fake();
$customer = 'test@test.com';
$from = 'from@test.com';
Mail::to($customer)->queue(new OrderConfirmation('Some Message', 'Some Subject', $from));

Mail::assertQueued(OrderConfirmation::class, function ($mail) {
     return $mail->hasFrom('from@test.com');
});

but it gets "ErrorException: Illegal string offset 'address' "

/var/www/vendor/laravel/framework/src/Illuminate/Mail/Mailable.php:597

Steps To Reproduce:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions