| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Kinglozzer\SilverStripeMailgunner\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Kinglozzer\SilverStripeMailgunner\Mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Email; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class MailerTest extends \SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @param object &$object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @param string $methodName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @param array $parameters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected function invokeMethod(&$object, $methodName, array $parameters = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $reflection = new \ReflectionClass(get_class($object)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $method = $reflection->getMethod($methodName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $method->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         return $method->invokeArgs($object, $parameters); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Simple test to check that registering the mailer as an Injector service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * will make it the default mailer used by Email | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function testMailerRegistrationWithEmail() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         Injector::nest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         Injector::inst()->registerService(new Mailer, 'Mailer'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->assertInstanceOf('Kinglozzer\SilverStripeMailgunner\Mailer', Email::mailer()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         Injector::unnest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->assertNotInstanceOf('Kinglozzer\SilverStripeMailgunner\Mailer', Email::mailer()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function testSetGetMailgunClient() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $mailer = new Mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $mockClient = $this->getMock('Mailgun\Mailgun'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->assertInstanceOf('Mailgun\Mailgun', $mailer->getMailgunClient()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $mailer->setMailgunClient($mockClient); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->assertSame($mockClient, $mailer->getMailgunClient()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     protected function getMockEmail() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             '"Foo Smith" <[email protected]>', // to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             '"Baz Smith" <[email protected]>', // from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'Important question', // subject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             '<p>How much foo could a foo bar baz if a baz bam could bar foo?</p>', // html content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             'How much foo could a foo bar baz if a baz bam could bar foo?', // plain text content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     'filename' => 'filename.jpg', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     'contents' => 'abcdefg' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ], // attachments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 'X-Custom-Header' => 'foo', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 'Cc' => '[email protected]', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 'Bcc' => '[email protected]' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ] // headers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 77 |  | View Code Duplication |     public function testSendPlain() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $mailer = $this->getMock('Kinglozzer\SilverStripeMailgunner\Mailer', ['sendMessage']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             ->method('sendMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $this->equalTo($to), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 $this->equalTo($from), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 $this->equalTo($subject), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 $this->equalTo(''), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 $this->equalTo($plainContent), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $this->equalTo($attachments), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 $this->equalTo($headers) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $mailer->sendPlain($to, $from, $subject, $plainContent, $attachments, $headers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 97 |  | View Code Duplication |     public function testSendHTML() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $mailer = $this->getMock('Kinglozzer\SilverStripeMailgunner\Mailer', ['sendMessage']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             ->method('sendMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $this->equalTo($to), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 $this->equalTo($from), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 $this->equalTo($subject), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 $this->equalTo($content), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |                 $this->equalTo($plainContent), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 $this->equalTo($attachments), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $this->equalTo($headers) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $mailer->sendHTML($to, $from, $subject, $content, $attachments, $headers, $plainContent); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function testSendMessage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $domain = 'http://testdomain.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         Config::inst()->update('Kinglozzer\SilverStripeMailgunner\Mailer', 'api_domain', $domain); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $preparedattachments = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             ['filePath' => '/foo/bar/baz', 'remoteName' => 'image.jpg'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $messageBuilder = $this->getMock('Mailgun\Messages\MessageBuilder', ['getMessage', 'getFiles']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         // We expect that sendMessage() will fetch the full message text from the builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             ->method('getMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             ->will($this->returnValue('test message')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             ->method('getFiles') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             ->will($this->returnValue('attachedfiles')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $client = $this->getMock('Mailgun\Mailgun', ['MessageBuilder', 'sendMessage']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         // We expect that sendMessage() will fetch the message builder from the Mailgun client, and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         // we use this point to inject our mock message builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $client->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             ->method('MessageBuilder') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             ->will($this->returnValue($messageBuilder)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         // We expect that Mailer::sendMessage() will trigger Mailgun::sendMessage() with the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         // domain set in config, and the prepared message and attachments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |          $client->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             ->method('sendMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 $this->equalTo($domain), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 $this->equalTo('test message'), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 $this->equalTo('attachedfiles') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $mailer = $this->getMock( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             'Kinglozzer\SilverStripeMailgunner\Mailer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             ['getMailgunClient', 'buildMessage', 'prepareAttachments', 'closeTempFileHandles'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         // We inject our mock Mailgun client while asserting that sendMessage() does request it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |             ->method('getMailgunClient') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             ->will($this->returnValue($client)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         // We've got attachments, so we assert that sendMessage() passes them off to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         // prepareAttachments() and specify a mock "prepared" return value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             ->method('prepareAttachments') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             ->with($this->equalTo($attachments)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             ->will($this->returnValue($preparedattachments)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         // We expect that sendMessage() will pass everything off to the buildMessage() method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             ->method('buildMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 $this->equalTo($messageBuilder), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                 $this->equalTo($to), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                 $this->equalTo($from), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                 $this->equalTo($subject), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                 $this->equalTo($content), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                 $this->equalTo($plainContent), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |                 $this->equalTo($preparedattachments), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                 $this->equalTo($headers) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |         // Assert that the mailer attempts to close any remaining open file handles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             ->method('closeTempFileHandles'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         // Let's go! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         $this->invokeMethod( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             $mailer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             'sendMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             [$to, $from, $subject, $content, $plainContent, $attachments, $headers] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     public function testSendMessageBatch() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         $domain = 'http://testdomain.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         Config::inst()->update('Kinglozzer\SilverStripeMailgunner\Mailer', 'api_domain', $domain); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         $preparedattachments = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |             ['filePath' => '/foo/bar/baz', 'remoteName' => 'image.jpg'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |          $messageBuilder = $this->getMockBuilder('Mailgun\Messages\BatchMessage') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |             ->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |             ->setMethods(['finalize']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         // We expect that finalize() will be called to send any remaining messages in the queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |             ->method('finalize'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |         $client = $this->getMock('Mailgun\Mailgun', ['BatchMessage']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         // We expect that sendMessage() will fetch the message builder from the Mailgun client, and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         // we use this point to inject our mock message builder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         $client->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |             ->method('BatchMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |             ->with($this->equalTo($domain)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |             ->will($this->returnValue($messageBuilder)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         $mailer = $this->getMock( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |             'Kinglozzer\SilverStripeMailgunner\Mailer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |             ['getMailgunClient', 'buildMessage', 'prepareAttachments', 'closeTempFileHandles'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |         // We inject our mock Mailgun client while asserting that sendMessage() does request it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |             ->method('getMailgunClient') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |             ->will($this->returnValue($client)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |         // We've got attachments, so we assert that sendMessage() passes them off to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |         // prepareAttachments() and specify a mock "prepared" return value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |             ->method('prepareAttachments') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |             ->with($this->equalTo($attachments)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |             ->will($this->returnValue($preparedattachments)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |         // We expect that sendMessage() will pass everything off to the buildMessage() method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |             ->method('buildMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |             ->with( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |                 $this->equalTo($messageBuilder), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |                 $this->equalTo($to), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |                 $this->equalTo($from), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |                 $this->equalTo($subject), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |                 $this->equalTo($content), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |                 $this->equalTo($plainContent), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |                 $this->equalTo($preparedattachments), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |                 $this->equalTo($headers) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |         // Assert that the mailer attempts to close any remaining open file handles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |             ->method('closeTempFileHandles'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |         // Special header to flag that we want to send a "batch message" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |         $headers['X-Mailgunner-Batch-Message'] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         // Let's go! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |         $this->invokeMethod( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |             $mailer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |             'sendMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |             [$to, $from, $subject, $content, $plainContent, $attachments, $headers] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |      * @expectedException Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |     public function testSendMessageExceptionClosesHandles() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |         list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |         $client = $this->getMock('Mailgun\Mailgun', ['sendMessage']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |         // Make our mock client trigger an exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |         $client->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |             ->method('sendMessage') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |             ->will($this->throwException(new \Exception)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |         $mailer = $this->getMock( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |             'Kinglozzer\SilverStripeMailgunner\Mailer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |             ['getMailgunClient', 'closeTempFileHandles'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |         // Inject our mock Mailgun client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |             ->method('getMailgunClient') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |             ->will($this->returnValue($client)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |         // Assert that the exception that the client throws triggers closing open file handles | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |         $mailer->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |             ->method('closeTempFileHandles'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |         $this->invokeMethod( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |             $mailer, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |             'sendMessage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |             [$to, $from, $subject, $content, $plainContent, $attachments, $headers] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 290 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 291 |  |  |     public function testBuildMessage() | 
            
                                                                        
                            
            
                                    
            
            
                | 292 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 293 |  |  |         list($to, $from, $subject, $content, $plainContent, $attachments, $headers) = $this->getMockEmail(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 294 |  |  |         // Mock "prepared" attachment | 
            
                                                                        
                            
            
                                    
            
            
                | 295 |  |  |         $attachments = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 296 |  |  |             ['filePath' => '/foo/bar/baz', 'remoteName' => 'image.jpg'] | 
            
                                                                        
                            
            
                                    
            
            
                | 297 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 298 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 299 |  |  |         $messageBuilder = $this->getMock('Mailgun\Messages\MessageBuilder'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 300 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 301 |  |  |             ->method('addToRecipient') | 
            
                                                                        
                            
            
                                    
            
            
                | 302 |  |  |             ->with( | 
            
                                                                        
                            
            
                                    
            
            
                | 303 |  |  |                 $this->equalTo('[email protected]'), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 304 |  |  |                 $this->equalTo(['full_name' => 'Foo Smith']) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 305 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 306 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 307 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 308 |  |  |             ->method('setFromAddress') | 
            
                                                                        
                            
            
                                    
            
            
                | 309 |  |  |             ->with( | 
            
                                                                        
                            
            
                                    
            
            
                | 310 |  |  |                 $this->equalTo('[email protected]'), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 311 |  |  |                 $this->equalTo(['full_name' => 'Baz Smith']) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 312 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 313 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 314 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 315 |  |  |             ->method('setSubject') | 
            
                                                                        
                            
            
                                    
            
            
                | 316 |  |  |             ->with($this->equalTo($subject)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 317 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 318 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 319 |  |  |             ->method('setHtmlBody') | 
            
                                                                        
                            
            
                                    
            
            
                | 320 |  |  |             ->with($this->equalTo($content)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 321 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 322 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 323 |  |  |             ->method('setTextBody') | 
            
                                                                        
                            
            
                                    
            
            
                | 324 |  |  |             ->with($this->equalTo($plainContent)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 325 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 326 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 327 |  |  |             ->method('addAttachment') | 
            
                                                                        
                            
            
                                    
            
            
                | 328 |  |  |             ->with( | 
            
                                                                        
                            
            
                                    
            
            
                | 329 |  |  |                 $this->equalTo('/foo/bar/baz'), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 330 |  |  |                 $this->equalTo('image.jpg') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 331 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 332 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 333 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 334 |  |  |             ->method('addCcRecipient') | 
            
                                                                        
                            
            
                                    
            
            
                | 335 |  |  |             ->with($this->equalTo($headers['Cc'])); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 336 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 337 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 338 |  |  |             ->method('addBccRecipient') | 
            
                                                                        
                            
            
                                    
            
            
                | 339 |  |  |             ->with($this->equalTo($headers['Bcc'])); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 340 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 341 |  |  |         $messageBuilder->expects($this->once()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 342 |  |  |             ->method('addCustomHeader') | 
            
                                                                        
                            
            
                                    
            
            
                | 343 |  |  |             ->with( | 
            
                                                                        
                            
            
                                    
            
            
                | 344 |  |  |                 $this->equalTo('X-Custom-Header'), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 345 |  |  |                 $this->equalTo('foo') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 346 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 347 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 348 |  |  |         $this->invokeMethod( | 
            
                                                                        
                            
            
                                    
            
            
                | 349 |  |  |             new Mailer, | 
            
                                                                        
                            
            
                                    
            
            
                | 350 |  |  |             'buildMessage', | 
            
                                                                        
                            
            
                                    
            
            
                | 351 |  |  |             [$messageBuilder, $to, $from, $subject, $content, $plainContent, $attachments, $headers] | 
            
                                                                        
                            
            
                                    
            
            
                | 352 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 353 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |     public function testParseAddresses() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |         $mailer = new Mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |         $parsed = $this->invokeMethod($mailer, 'parseAddresses', ['[email protected]']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |         $this->assertEquals(['[email protected]' => ''], $parsed); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |         $parsed = $this->invokeMethod($mailer, 'parseAddresses', ['Joe Bloggs <[email protected]>']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |         $this->assertEquals(['[email protected]' => 'Joe Bloggs'], $parsed); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |         $parsed = $this->invokeMethod($mailer, 'parseAddresses', ['[email protected], [email protected]']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |         $this->assertEquals(['[email protected]' => '', '[email protected]' => ''], $parsed); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |         // Test all the different formats | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |         $raw = '"Joe Bloggs"<[email protected]>; John Smith <[email protected]>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |         $raw .= ', \'James\'<[email protected]>; [email protected],<[email protected]>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |         $expected = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |             '[email protected]' => 'Joe Bloggs', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |             '[email protected]' => 'John Smith', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |             '[email protected]' => 'James', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |             '[email protected]' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |             '[email protected]' => '' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |         $actual = $this->invokeMethod($mailer, 'parseAddresses', [$raw]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |         $this->assertEquals($expected, $actual); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |     public function testPrepareAttachments() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |         $attachments = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |             ['filename' => 'test1.jpg', 'contents' => 'abcdefg'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |             ['filename' => 'test2.jpg', 'contents' => 'hijklmn'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |         $expected = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |             ['filePath' => 'tmp/test1.jpg', 'remoteName' => 'test1.jpg'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |             ['filePath' => 'tmp/test2.jpg', 'remoteName' => 'test2.jpg'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |         $mailer = $this->getMock('Kinglozzer\SilverStripeMailgunner\Mailer', ['writeToTempFile']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |         $mailer->expects($this->at(0)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |             ->method('writeToTempFile') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |             ->with($this->equalTo('abcdefg')) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |             ->will($this->returnValue('tmp/test1.jpg')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |         $mailer->expects($this->at(1)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |             ->method('writeToTempFile') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |             ->with($this->equalTo('hijklmn')) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |             ->will($this->returnValue('tmp/test2.jpg')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |         $prepared =  $this->invokeMethod($mailer, 'prepareAttachments', [$attachments]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |         $this->assertEquals($expected, $prepared); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |     public function testWriteToTempFile() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |         $contents = 'test file contents'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  |         $mailer = new Mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  |         $tempFile = $this->invokeMethod($mailer, 'writeToTempFile', [$contents]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  |         $this->assertEquals($contents, file_get_contents($tempFile)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  |         // Assert that the stream and temp file path are stored | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  |         $reflection = new \ReflectionClass(get_class($mailer)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  |         $property = $reflection->getProperty('tempFileHandles'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  |         $property->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  |         $fileHandles = $property->getValue($mailer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 |  |  |         $this->assertNotEmpty($fileHandles); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 |  |  |         // Test the contents of the stream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 |  |  |         $handle = $fileHandles[0]['handle']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  |         rewind($handle); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  |         $this->assertEquals($contents, fread($handle, filesize($fileHandles[0]['path']))); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 |  |  |         $this->assertEquals($tempFile, $fileHandles[0]['path']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  |     public function testCloseTempFileHandles() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 432 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 433 |  |  |         $mailer = new Mailer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 434 |  |  |         $tempFile = tempnam(sys_get_temp_dir(), 'SS_MG_TESTS_TMP'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 435 |  |  |         $fileHandle = fopen($tempFile, 'w'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 436 |  |  |         fwrite($fileHandle, 'test data'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 437 |  |  |         $handleData = ['handle' => $fileHandle, 'path' => $tempFile]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 438 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 439 |  |  |         $reflection = new \ReflectionClass(get_class($mailer)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 |  |  |         $property = $reflection->getProperty('tempFileHandles'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 |  |  |         $property->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 442 |  |  |         $fileHandles = $property->setValue($mailer, [$handleData]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 443 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 444 |  |  |         $this->invokeMethod($mailer, 'closeTempFileHandles'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 445 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 446 |  |  |         $this->assertEmpty($property->getValue($mailer)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 |  |  |         $this->assertFalse(file_exists($tempFile)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 |  |  |         $this->assertEquals('Unknown', get_resource_type($fileHandle)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 450 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 451 |  |  |  | 
            
                        
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: