php-swiftmailerHow to mock SwiftMailer?
Mocking SwiftMailer can be done using a library such as Mockery. Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework.
Example code
$mock = \Mockery::mock('Swift_Mailer');
$mock->shouldReceive('send')
->once()
->andReturn(true);
Output example
true
Code explanation
$mock = \Mockery::mock('Swift_Mailer');- creates a mock object of the Swift_Mailer class$mock->shouldReceive('send')- sets up an expectation that thesendmethod will be called->once()- sets the expectation that thesendmethod will be called only once->andReturn(true)- sets the expectation that thesendmethod will returntrue
Helpful links
More of Php Swiftmailer
- How to configure Swiftmailer for SMTP without authentication?
- How to use SMTP with Swiftmailer?
- How to use TLS 1.2 with Swiftmailer?
- How to set timeout with Swiftmailer?
- How to set the reply to address in Swiftmailer?
- How to set the port for Swiftmailer?
- How to get the message ID in SwiftMailer?
- How to use Swiftmailer with SendGrid?
- How to send multiple attachments with SwiftMailer?
See more codes...