php - Unittesting Laravel 5 Mail using Mock -
is there way test mail in laravel 5? tried legit mock example see on internet seems works on laravel 4. current code below.
$mock = mockery::mock('swift_mailer'); $this->app['mailer']->setswiftmailer($mock); ...some more codes here... $mock->shouldreceive('send')->once() ->andreturnusing(function($msg) { $this->assertequals('my subject', $msg->getsubject()); $this->assertequals('foo@bar.com', $msg->getto()); $this->assertcontains('some string', $msg->getbody()); });
this contents of apiclient.php, last line line 155, indicated in stack trace.
mail::queue('emails.error', [ 'error_message' => $error_message, 'request' => $request, 'stack_trace' => $stack_trace ], function ($message) use ($error_message) { $message->to(env('mail_to_email'), env('mail_to_name'))->subject("[project error] " . $error_message); });
below stack trace
method mockery_0__vendor_swift_mailer::gettransport() not exist on mock object /users/bon/webserver/project/vendor/laravel/framework/src/illuminate/mail/mailer.php:285 /users/bon/webserver/project/vendor/laravel/framework/src/illuminate/mail/mailer.php:285 /users/bon/webserver/project/vendor/laravel/framework/src/illuminate/mail/mailer.php:150 /users/bon/webserver/project/vendor/laravel/framework/src/illuminate/mail/mailer.php:255 /users/bon/webserver/project/vendor/laravel/framework/src/illuminate/queue/jobs/job.php:126 /users/bon/webserver/project/vendor/laravel/framework/src/illuminate/queue/jobs/syncjob.php:42 /users/bon/webserver/project/vendor/laravel/framework/src/illuminate/queue/syncqueue.php:25 /users/bon/webserver/project/vendor/laravel/framework/src/illuminate/mail/mailer.php:184 /users/bon/webserver/project/vendor/laravel/framework/src/illuminate/support/facades/facade.php:216 /users/bon/webserver/project/app/libraries/apiclient.php:155 /users/bon/webserver/project/app/libraries/apiclient.php:155 /users/bon/webserver/project/app/libraries/apiclient.php:174 /users/bon/webserver/project/tests/unit_tests/apiclientunittest.php:43
in addition, adding use mockery;
gets following error.
php warning: use statement non-compound name 'mockery' has no effect in /users/bon/webserver/project/tests/unit_tests/apiclientunittest.php on line 9
this frustrates me hours point i'm asking here on so. it's weird laravel doesn't have direct support testing mails when unittesting when decided upgrade version 5.
cost me better part of afternoon worked - passed in closure , gave mockery object
code being tested:
$subject = "the subject"; mail::send('emails.emailtemplate', ['user' => $user ], function( $mail ) use ($user, $subject){ $mail -> to( $user -> email) -> subject( $subject ); });
test worked:
$subject = "the subject"; $user = factory(app\models\user::class) -> create(); mail::shouldreceive('send') -> once() -> with( 'emails.emailtemplate', m::on( function( $data ){ $this -> assertarrayhaskey( 'user', $data ); return true; }), m::on( function(\closure $closure) use ($user, $subject){ $mock = m::mock('illuminate\mailer\message'); $mock -> shouldreceive('to') -> once() -> with( $user -> email ) -> andreturn( $mock ); //simulate chaining $mock -> shouldreceive('subject') -> once() -> with($subject); $closure($mock); return true; }) );
Comments
Post a Comment