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 thesend
method will be called->once()
- sets the expectation that thesend
method will be called only once->andReturn(true)
- sets the expectation that thesend
method will returntrue
Helpful links
More of Php Swiftmailer
- How to log emails sent with Swiftmailer?
- How to use SMTP with Swiftmailer?
- How to configure Swiftmailer for SMTP without authentication?
- Swiftmailer usage example
- How to send emails in UTF8 using Swiftmailer?
- How to set timeout with Swiftmailer?
- How to use Swiftmailer transport?
- How to set the 'from' address in Swiftmailer?
- How to use TLS 1.2 with Swiftmailer?
- How to use Swiftmailer to send RFC 2822 compliant emails?
See more codes...