Search code examples
laravelphpunitmailer

Laravel PHPUnit gets "ErrorException: Illegal string offset 'address' " when testnig Maillable hasFrom(...) method


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 = '[email protected]';
$from = '[email protected]';
Mail::to($customer)->queue(new OrderConfirmation('Some Message', 'Some Subject', $from));

Mail::assertQueued(OrderConfirmation::class, function ($mail) {
     return $mail->hasFrom('[email protected]');
});

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

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

Laravel 5.6

Is it a bug or am I doing something wrong?


Solution

  • The reason you're getting this issue is because you're overriding the $from property in the Mailable class.

    You could remove the $from property you have set up in the class and call the from() method from the constructor:

    public $message;
    
    public function __construct($message, $subject, $from)
    {
        $this->message = $message;
        $this->subject = $subject;
        $this->from($from);
    }
    
    public function build()
    {
        return $this->view('emails.orders.confirmation');
    }
    

    Alternatively, you could rename the $from property to something like $fromAddress.

    NB The same was happening with subject as well but since the subject() method is simply there to assign a value to the subject property on the class is wasn't actually causing an issue.