4 * Class CRM_Utils_Mail_FilteredPearMailerTest
7 class CRM_Utils_Mail_FilteredPearMailerTest
extends CiviUnitTestCase
{
9 public function testFilter() {
10 $mock = new class() extends \Mail
{
13 public function send($recipients, $headers, $body) {
14 $this->buf
['recipients'] = $recipients;
15 $this->buf
['headers'] = $headers;
16 $this->buf
['body'] = $body;
17 return 'all the fruits in the basket';
22 $fm = new CRM_Utils_Mail_FilteredPearMailer('mock', [], $mock);
23 $fm->addFilter('1000_apple', function ($mailer, &$recipients, &$headers, &$body) {
24 $body .= ' with apples!';
26 $fm->addFilter('1000_banana', function ($mailer, &$recipients, &$headers, &$body) {
27 $headers['Banana'] = 'Cavendish';
29 $r = $fm->send(['recip'], ['Subject' => 'Fruit loops'], 'body');
31 $this->assertEquals('Fruit loops', $mock->buf
['headers']['Subject']);
32 $this->assertEquals('Cavendish', $mock->buf
['headers']['Banana']);
33 $this->assertEquals('body with apples!', $mock->buf
['body']);
34 $this->assertEquals('all the fruits in the basket', $r);
37 public function testFilter_shortCircuit() {
38 $mock = new class() extends \Mail
{
40 public function send($recipients, $headers, $body) {
41 return 'all the fruits in the basket';
46 $fm = new CRM_Utils_Mail_FilteredPearMailer('mock', [], $mock);
47 $fm->addFilter('1000_short_circuit', function ($mailer, &$recipients, &$headers, &$body) {
48 return 'the triumph of veggies over fruits';
50 $r = $fm->send(['recip'], ['Subject' => 'Fruit loops'], 'body');
51 $this->assertEquals('the triumph of veggies over fruits', $r);