ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SwiftMailerHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog\Handler;
13 
14 use Monolog\Logger;
16 
18 {
20  private $mailer;
21 
22  public function setUp()
23  {
24  $this->mailer = $this
25  ->getMockBuilder('Swift_Mailer')
26  ->disableOriginalConstructor()
27  ->getMock();
28  }
29 
31  {
32  $this->mailer->expects($this->never())
33  ->method('send');
34 
35  $callback = function () {
36  throw new \RuntimeException('Swift_Message creation callback should not have been called in this test');
37  };
38  $handler = new SwiftMailerHandler($this->mailer, $callback);
39 
40  $records = array(
41  $this->getRecord(Logger::DEBUG),
42  $this->getRecord(Logger::INFO),
43  );
44  $handler->handleBatch($records);
45  }
46 
48  {
49  // Wire Mailer to expect a specific Swift_Message with a customized Subject
50  $expectedMessage = new \Swift_Message();
51  $this->mailer->expects($this->once())
52  ->method('send')
53  ->with($this->callback(function ($value) use ($expectedMessage) {
54  return $value instanceof \Swift_Message
55  && $value->getSubject() === 'Emergency'
56  && $value === $expectedMessage;
57  }));
58 
59  // Callback dynamically changes subject based on number of logged records
60  $callback = function ($content, array $records) use ($expectedMessage) {
61  $subject = count($records) > 0 ? 'Emergency' : 'Normal';
62  $expectedMessage->setSubject($subject);
63 
64  return $expectedMessage;
65  };
66  $handler = new SwiftMailerHandler($this->mailer, $callback);
67 
68  // Logging 1 record makes this an Emergency
69  $records = array(
71  );
72  $handler->handleBatch($records);
73  }
74 
75  public function testMessageSubjectFormatting()
76  {
77  // Wire Mailer to expect a specific Swift_Message with a customized Subject
78  $messageTemplate = new \Swift_Message();
79  $messageTemplate->setSubject('Alert: %level_name% %message%');
80  $receivedMessage = null;
81 
82  $this->mailer->expects($this->once())
83  ->method('send')
84  ->with($this->callback(function ($value) use (&$receivedMessage) {
85  $receivedMessage = $value;
86  return true;
87  }));
88 
89  $handler = new SwiftMailerHandler($this->mailer, $messageTemplate);
90 
91  $records = array(
93  );
94  $handler->handleBatch($records);
95 
96  $this->assertEquals('Alert: EMERGENCY test', $receivedMessage->getSubject());
97  }
98 
99  public function testMessageHaveUniqueId()
100  {
101  $messageTemplate = \Swift_Message::newInstance();
102  $handler = new SwiftMailerHandler($this->mailer, $messageTemplate);
103 
104  $method = new \ReflectionMethod('Monolog\Handler\SwiftMailerHandler', 'buildMessage');
105  $method->setAccessible(true);
106  $method->invokeArgs($handler, array($messageTemplate, array()));
107 
108  $builtMessage1 = $method->invoke($handler, $messageTemplate, array());
109  $builtMessage2 = $method->invoke($handler, $messageTemplate, array());
110 
111  $this->assertFalse($builtMessage1->getId() === $builtMessage2->getId(), 'Two different messages have the same id');
112  }
113 }
const DEBUG
Detailed debug information.
Definition: Logger.php:32
$records
Definition: simple_test.php:22
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
const EMERGENCY
Urgent alert.
Definition: Logger.php:77
Create styles array
The data for the language used.
$handler
SwiftMailerHandler uses Swift_Mailer to send the emails.
const INFO
Interesting events.
Definition: Logger.php:39