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 set timeout with Swiftmailer?
- How to use SMTP with Swiftmailer?
- How to use TLS 1.2 with Swiftmailer?
- How to get the response code when using Swiftmailer?
- How to use Swiftmailer to send RFC 2822 compliant emails?
- How to configure Swiftmailer for Postfix?
- How to send emails in UTF8 using Swiftmailer?
- How to send emails to multiple recipients with Swiftmailer?
- How to set the port for Swiftmailer?
- How to enable TLS with Swiftmailer?
See more codes...