OSDN Git Service

Добавлена возможность изменения количества записей, выводимых на странице предметов...
[invent/invent.git] / vendor / swiftmailer / swiftmailer / tests / unit / Swift / Transport / AbstractSmtpTest.php
1 <?php
2
3 abstract class Swift_Transport_AbstractSmtpTest extends \SwiftMailerTestCase
4 {
5     abstract protected function getTransport($buf);
6
7     public function testStartAccepts220ServiceGreeting()
8     {
9         /* -- RFC 2821, 4.2.
10
11      Greeting = "220 " Domain [ SP text ] CRLF
12
13      -- RFC 2822, 4.3.2.
14
15      CONNECTION ESTABLISHMENT
16          S: 220
17          E: 554
18         */
19
20         $buf = $this->getBuffer();
21         $smtp = $this->getTransport($buf);
22         $buf->shouldReceive('initialize')
23             ->once();
24         $buf->shouldReceive('readLine')
25             ->once()
26             ->with(0)
27             ->andReturn("220 some.server.tld bleh\r\n");
28
29         $this->finishBuffer($buf);
30         try {
31             $this->assertFalse($smtp->isStarted(), '%s: SMTP should begin non-started');
32             $smtp->start();
33             $this->assertTrue($smtp->isStarted(), '%s: start() should have started connection');
34         } catch (Exception $e) {
35             $this->fail('220 is a valid SMTP greeting and should be accepted');
36         }
37     }
38
39     public function testBadGreetingCausesException()
40     {
41         $buf = $this->getBuffer();
42         $smtp = $this->getTransport($buf);
43         $buf->shouldReceive('initialize')
44             ->once();
45         $buf->shouldReceive('readLine')
46             ->once()
47             ->with(0)
48             ->andReturn("554 I'm busy\r\n");
49         $this->finishBuffer($buf);
50         try {
51             $this->assertFalse($smtp->isStarted(), '%s: SMTP should begin non-started');
52             $smtp->start();
53             $this->fail('554 greeting indicates an error and should cause an exception');
54         } catch (Swift_TransportException $e) {
55             $this->assertFalse($smtp->isStarted(), '%s: start() should have failed');
56         }
57     }
58
59     public function testStartSendsHeloToInitiate()
60     {
61         /* -- RFC 2821, 3.2.
62
63             3.2 Client Initiation
64
65          Once the server has sent the welcoming message and the client has
66          received it, the client normally sends the EHLO command to the
67          server, indicating the client's identity.  In addition to opening the
68          session, use of EHLO indicates that the client is able to process
69          service extensions and requests that the server provide a list of the
70          extensions it supports.  Older SMTP systems which are unable to
71          support service extensions and contemporary clients which do not
72          require service extensions in the mail session being initiated, MAY
73          use HELO instead of EHLO.  Servers MUST NOT return the extended
74          EHLO-style response to a HELO command.  For a particular connection
75          attempt, if the server returns a "command not recognized" response to
76          EHLO, the client SHOULD be able to fall back and send HELO.
77
78          In the EHLO command the host sending the command identifies itself;
79          the command may be interpreted as saying "Hello, I am <domain>" (and,
80          in the case of EHLO, "and I support service extension requests").
81
82        -- RFC 2281, 4.1.1.1.
83
84        ehlo            = "EHLO" SP Domain CRLF
85        helo            = "HELO" SP Domain CRLF
86
87        -- RFC 2821, 4.3.2.
88
89        EHLO or HELO
90            S: 250
91            E: 504, 550
92
93      */
94
95         $buf = $this->getBuffer();
96         $smtp = $this->getTransport($buf);
97
98         $buf->shouldReceive('initialize')
99             ->once();
100         $buf->shouldReceive('readLine')
101             ->once()
102             ->with(0)
103             ->andReturn("220 some.server.tld bleh\r\n");
104         $buf->shouldReceive('write')
105             ->once()
106             ->with('~^HELO example.org\r\n$~D')
107             ->andReturn(1);
108         $buf->shouldReceive('readLine')
109             ->once()
110             ->with(1)
111             ->andReturn('250 ServerName'."\r\n");
112
113         $this->finishBuffer($buf);
114         try {
115             $smtp->start();
116         } catch (Exception $e) {
117             $this->fail('Starting SMTP should send HELO and accept 250 response');
118         }
119     }
120
121     public function testInvalidHeloResponseCausesException()
122     {
123         $buf = $this->getBuffer();
124         $smtp = $this->getTransport($buf);
125
126         $buf->shouldReceive('initialize')
127             ->once();
128         $buf->shouldReceive('readLine')
129             ->once()
130             ->with(0)
131             ->andReturn("220 some.server.tld bleh\r\n");
132         $buf->shouldReceive('write')
133             ->once()
134             ->with('~^HELO example.org\r\n$~D')
135             ->andReturn(1);
136         $buf->shouldReceive('readLine')
137             ->once()
138             ->with(1)
139             ->andReturn('504 WTF'."\r\n");
140
141         $this->finishBuffer($buf);
142         try {
143             $this->assertFalse($smtp->isStarted(), '%s: SMTP should begin non-started');
144             $smtp->start();
145             $this->fail('Non 250 HELO response should raise Exception');
146         } catch (Swift_TransportException $e) {
147             $this->assertFalse($smtp->isStarted(), '%s: SMTP start() should have failed');
148         }
149     }
150
151     public function testDomainNameIsPlacedInHelo()
152     {
153         /* -- RFC 2821, 4.1.4.
154
155        The SMTP client MUST, if possible, ensure that the domain parameter
156        to the EHLO command is a valid principal host name (not a CNAME or MX
157        name) for its host.  If this is not possible (e.g., when the client's
158        address is dynamically assigned and the client does not have an
159        obvious name), an address literal SHOULD be substituted for the
160        domain name and supplemental information provided that will assist in
161        identifying the client.
162         */
163
164         $buf = $this->getBuffer();
165         $smtp = $this->getTransport($buf);
166
167         $buf->shouldReceive('initialize')
168             ->once();
169         $buf->shouldReceive('readLine')
170             ->once()
171             ->with(0)
172             ->andReturn("220 some.server.tld bleh\r\n");
173         $buf->shouldReceive('write')
174             ->once()
175             ->with("HELO mydomain.com\r\n")
176             ->andReturn(1);
177         $buf->shouldReceive('readLine')
178             ->once()
179             ->with(1)
180             ->andReturn('250 ServerName'."\r\n");
181
182         $this->finishBuffer($buf);
183         $smtp->setLocalDomain('mydomain.com');
184         $smtp->start();
185     }
186
187     public function testSuccessfulMailCommand()
188     {
189         /* -- RFC 2821, 3.3.
190
191         There are three steps to SMTP mail transactions.  The transaction
192         starts with a MAIL command which gives the sender identification.
193
194         .....
195
196         The first step in the procedure is the MAIL command.
197
198             MAIL FROM:<reverse-path> [SP <mail-parameters> ] <CRLF>
199
200         -- RFC 2821, 4.1.1.2.
201
202         Syntax:
203
204             "MAIL FROM:" ("<>" / Reverse-Path)
205                        [SP Mail-parameters] CRLF
206         -- RFC 2821, 4.1.2.
207
208         Reverse-path = Path
209             Forward-path = Path
210             Path = "<" [ A-d-l ":" ] Mailbox ">"
211             A-d-l = At-domain *( "," A-d-l )
212                         ; Note that this form, the so-called "source route",
213                         ; MUST BE accepted, SHOULD NOT be generated, and SHOULD be
214                         ; ignored.
215             At-domain = "@" domain
216
217         -- RFC 2821, 4.3.2.
218
219         MAIL
220             S: 250
221             E: 552, 451, 452, 550, 553, 503
222         */
223
224         $buf = $this->getBuffer();
225         $smtp = $this->getTransport($buf);
226         $message = $this->createMessage();
227         $message->shouldReceive('getFrom')
228                 ->once()
229                 ->andReturn(['me@domain.com' => 'Me']);
230         $message->shouldReceive('getTo')
231                 ->once()
232                 ->andReturn(['foo@bar' => null]);
233         $buf->shouldReceive('initialize')
234             ->once();
235         $buf->shouldReceive('write')
236             ->once()
237             ->with("MAIL FROM:<me@domain.com>\r\n")
238             ->andReturn(1);
239         $buf->shouldReceive('readLine')
240             ->once()
241             ->with(1)
242             ->andReturn("250 OK\r\n");
243
244         $this->finishBuffer($buf);
245         try {
246             $smtp->start();
247             $smtp->send($message);
248         } catch (Exception $e) {
249             $this->fail('MAIL FROM should accept a 250 response');
250         }
251     }
252
253     public function testInvalidResponseCodeFromMailCausesException()
254     {
255         $buf = $this->getBuffer();
256         $smtp = $this->getTransport($buf);
257         $message = $this->createMessage();
258
259         $message->shouldReceive('getFrom')
260                 ->once()
261                 ->andReturn(['me@domain.com' => 'Me']);
262         $message->shouldReceive('getTo')
263                 ->once()
264                 ->andReturn(['foo@bar' => null]);
265         $buf->shouldReceive('write')
266             ->once()
267             ->with("MAIL FROM:<me@domain.com>\r\n")
268             ->andReturn(1);
269         $buf->shouldReceive('readLine')
270             ->once()
271             ->with(1)
272             ->andReturn('553 Bad'."\r\n");
273
274         $this->finishBuffer($buf);
275         try {
276             $smtp->start();
277             $smtp->send($message);
278             $this->fail('MAIL FROM should accept a 250 response');
279         } catch (Swift_TransportException $e) {
280         }
281     }
282
283     public function testSenderIsPreferredOverFrom()
284     {
285         $buf = $this->getBuffer();
286         $smtp = $this->getTransport($buf);
287         $message = $this->createMessage();
288
289         $message->shouldReceive('getFrom')
290                 ->once()
291                 ->andReturn(['me@domain.com' => 'Me']);
292         $message->shouldReceive('getSender')
293                 ->once()
294                 ->andReturn(['another@domain.com' => 'Someone']);
295         $message->shouldReceive('getTo')
296                 ->once()
297                 ->andReturn(['foo@bar' => null]);
298         $buf->shouldReceive('write')
299             ->once()
300             ->with("MAIL FROM:<another@domain.com>\r\n")
301             ->andReturn(1);
302         $buf->shouldReceive('readLine')
303             ->once()
304             ->with(1)
305             ->andReturn('250 OK'."\r\n");
306
307         $this->finishBuffer($buf);
308         $smtp->start();
309         $smtp->send($message);
310     }
311
312     public function testReturnPathIsPreferredOverSender()
313     {
314         $buf = $this->getBuffer();
315         $smtp = $this->getTransport($buf);
316         $message = $this->createMessage();
317
318         $message->shouldReceive('getFrom')
319                 ->once()
320                 ->andReturn(['me@domain.com' => 'Me']);
321         $message->shouldReceive('getSender')
322                 ->once()
323                 ->andReturn(['another@domain.com' => 'Someone']);
324         $message->shouldReceive('getReturnPath')
325                 ->once()
326                 ->andReturn('more@domain.com');
327         $message->shouldReceive('getTo')
328                 ->once()
329                 ->andReturn(['foo@bar' => null]);
330         $buf->shouldReceive('write')
331             ->once()
332             ->with("MAIL FROM:<more@domain.com>\r\n")
333             ->andReturn(1);
334         $buf->shouldReceive('readLine')
335             ->once()
336             ->with(1)
337             ->andReturn('250 OK'."\r\n");
338
339         $this->finishBuffer($buf);
340         $smtp->start();
341         $smtp->send($message);
342     }
343
344     public function testSuccessfulRcptCommandWith250Response()
345     {
346         /* -- RFC 2821, 3.3.
347
348      The second step in the procedure is the RCPT command.
349
350             RCPT TO:<forward-path> [ SP <rcpt-parameters> ] <CRLF>
351
352      The first or only argument to this command includes a forward-path
353      (normally a mailbox and domain, always surrounded by "<" and ">"
354      brackets) identifying one recipient.  If accepted, the SMTP server
355      returns a 250 OK reply and stores the forward-path.  If the recipient
356      is known not to be a deliverable address, the SMTP server returns a
357      550 reply, typically with a string such as "no such user - " and the
358      mailbox name (other circumstances and reply codes are possible).
359      This step of the procedure can be repeated any number of times.
360
361         -- RFC 2821, 4.1.1.3.
362
363         This command is used to identify an individual recipient of the mail
364         data; multiple recipients are specified by multiple use of this
365         command.  The argument field contains a forward-path and may contain
366         optional parameters.
367
368         The forward-path normally consists of the required destination
369         mailbox.  Sending systems SHOULD not generate the optional list of
370         hosts known as a source route.
371
372         .......
373
374         "RCPT TO:" ("<Postmaster@" domain ">" / "<Postmaster>" / Forward-Path)
375                                         [SP Rcpt-parameters] CRLF
376
377         -- RFC 2821, 4.2.2.
378
379             250 Requested mail action okay, completed
380             251 User not local; will forward to <forward-path>
381          (See section 3.4)
382             252 Cannot VRFY user, but will accept message and attempt
383                     delivery
384
385         -- RFC 2821, 4.3.2.
386
387         RCPT
388             S: 250, 251 (but see section 3.4 for discussion of 251 and 551)
389             E: 550, 551, 552, 553, 450, 451, 452, 503, 550
390         */
391
392         //We'll treat 252 as accepted since it isn't really a failure
393
394         $buf = $this->getBuffer();
395         $smtp = $this->getTransport($buf);
396         $message = $this->createMessage();
397
398         $message->shouldReceive('getFrom')
399                 ->once()
400                 ->andReturn(['me@domain.com' => 'Me']);
401         $message->shouldReceive('getTo')
402                 ->once()
403                 ->andReturn(['foo@bar' => null]);
404         $buf->shouldReceive('write')
405             ->once()
406             ->with("MAIL FROM:<me@domain.com>\r\n")
407             ->andReturn(1);
408         $buf->shouldReceive('readLine')
409             ->once()
410             ->with(1)
411             ->andReturn('250 OK'."\r\n");
412         $buf->shouldReceive('write')
413             ->once()
414             ->with("RCPT TO:<foo@bar>\r\n")
415             ->andReturn(2);
416         $buf->shouldReceive('readLine')
417             ->once()
418             ->with(2)
419             ->andReturn('250 OK'."\r\n");
420
421         $this->finishBuffer($buf);
422         try {
423             $smtp->start();
424             $smtp->send($message);
425         } catch (Exception $e) {
426             $this->fail('RCPT TO should accept a 250 response');
427         }
428     }
429
430     public function testUtf8AddressWithIdnEncoder()
431     {
432         $buf = $this->getBuffer();
433         $smtp = $this->getTransport($buf);
434         $message = $this->createMessage();
435
436         $message->shouldReceive('getFrom')
437                 ->once()
438                 ->andReturn(['me@dömain.com' => 'Me']);
439         $message->shouldReceive('getTo')
440                 ->once()
441                 ->andReturn(['foo@bär' => null]);
442         $buf->shouldReceive('write')
443             ->once()
444             ->with("MAIL FROM:<me@xn--dmain-jua.com>\r\n")
445             ->andReturn(1);
446         $buf->shouldReceive('write')
447             ->once()
448             ->with("RCPT TO:<foo@xn--br-via>\r\n")
449             ->andReturn(1);
450         $buf->shouldReceive('readLine')
451             ->once()
452             ->with(1)
453             ->andReturn('250 OK'."\r\n");
454
455         $this->finishBuffer($buf);
456         $smtp->start();
457         $smtp->send($message);
458     }
459
460     public function testUtf8AddressWithUtf8Encoder()
461     {
462         $buf = $this->getBuffer();
463         $smtp = $this->getTransport($buf, null, new Swift_AddressEncoder_Utf8AddressEncoder());
464         $message = $this->createMessage();
465
466         $message->shouldReceive('getFrom')
467                 ->once()
468                 ->andReturn(['më@dömain.com' => 'Me']);
469         $message->shouldReceive('getTo')
470                 ->once()
471                 ->andReturn(['föö@bär' => null]);
472         $buf->shouldReceive('write')
473             ->once()
474             ->with("MAIL FROM:<më@dömain.com>\r\n")
475             ->andReturn(1);
476         $buf->shouldReceive('write')
477             ->once()
478             ->with("RCPT TO:<föö@bär>\r\n")
479             ->andReturn(1);
480         $buf->shouldReceive('readLine')
481             ->once()
482             ->with(1)
483             ->andReturn('250 OK'."\r\n");
484
485         $this->finishBuffer($buf);
486         $smtp->start();
487         $smtp->send($message);
488     }
489
490     public function testNonEncodableSenderCausesException()
491     {
492         $buf = $this->getBuffer();
493         $smtp = $this->getTransport($buf);
494         $message = $this->createMessage();
495
496         $message->shouldReceive('getFrom')
497                 ->once()
498                 ->andReturn(['më@domain.com' => 'Me']);
499         $message->shouldReceive('getTo')
500                 ->once()
501                 ->andReturn(['foo@bar' => null]);
502
503         $this->finishBuffer($buf);
504         try {
505             $smtp->start();
506             $smtp->send($message);
507             $this->fail('më@domain.com cannot be encoded (not observed)');
508         } catch (Swift_AddressEncoderException $e) {
509             $this->assertEquals('më@domain.com', $e->getAddress());
510         }
511     }
512
513     public function testMailFromCommandIsOnlySentOncePerMessage()
514     {
515         $buf = $this->getBuffer();
516         $smtp = $this->getTransport($buf);
517         $message = $this->createMessage();
518
519         $message->shouldReceive('getFrom')
520                 ->once()
521                 ->andReturn(['me@domain.com' => 'Me']);
522         $message->shouldReceive('getTo')
523                 ->once()
524                 ->andReturn(['foo@bar' => null]);
525         $buf->shouldReceive('write')
526             ->once()
527             ->with("MAIL FROM:<me@domain.com>\r\n")
528             ->andReturn(1);
529         $buf->shouldReceive('readLine')
530             ->once()
531             ->with(1)
532             ->andReturn('250 OK'."\r\n");
533         $buf->shouldReceive('write')
534             ->once()
535             ->with("RCPT TO:<foo@bar>\r\n")
536             ->andReturn(2);
537         $buf->shouldReceive('readLine')
538             ->once()
539             ->with(2)
540             ->andReturn('250 OK'."\r\n");
541         $buf->shouldReceive('write')
542             ->never()
543             ->with("MAIL FROM:<me@domain.com>\r\n");
544
545         $this->finishBuffer($buf);
546         $smtp->start();
547         $smtp->send($message);
548     }
549
550     public function testMultipleRecipientsSendsMultipleRcpt()
551     {
552         $buf = $this->getBuffer();
553         $smtp = $this->getTransport($buf);
554         $message = $this->createMessage();
555
556         $message->shouldReceive('getFrom')
557                 ->once()
558                 ->andReturn(['me@domain.com' => 'Me']);
559         $message->shouldReceive('getTo')
560                 ->once()
561                 ->andReturn([
562                     'foo@bar' => null,
563                     'zip@button' => 'Zip Button',
564                     'test@domain' => 'Test user',
565                     'tëst@domain' => 'Test user',
566                 ]);
567         $buf->shouldReceive('write')
568             ->once()
569             ->with("RCPT TO:<foo@bar>\r\n")
570             ->andReturn(1);
571         $buf->shouldReceive('readLine')
572             ->once()
573             ->with(1)
574             ->andReturn('250 OK'."\r\n");
575         $buf->shouldReceive('write')
576             ->once()
577             ->with("RCPT TO:<zip@button>\r\n")
578             ->andReturn(2);
579         $buf->shouldReceive('readLine')
580             ->once()
581             ->with(2)
582             ->andReturn('250 OK'."\r\n");
583         $buf->shouldReceive('write')
584             ->once()
585             ->with("RCPT TO:<test@domain>\r\n")
586             ->andReturn(3);
587         $buf->shouldReceive('readLine')
588             ->once()
589             ->with(3)
590             ->andReturn('250 OK'."\r\n");
591
592         $this->finishBuffer($buf);
593         $smtp->start();
594         $smtp->send($message);
595     }
596
597     public function testCcRecipientsSendsMultipleRcpt()
598     {
599         $buf = $this->getBuffer();
600         $smtp = $this->getTransport($buf);
601         $message = $this->createMessage();
602
603         $message->shouldReceive('getFrom')
604                 ->once()
605                 ->andReturn(['me@domain.com' => 'Me']);
606         $message->shouldReceive('getTo')
607                 ->once()
608                 ->andReturn(['foo@bar' => null]);
609         $message->shouldReceive('getCc')
610                 ->once()
611                 ->andReturn([
612                     'zip@button' => 'Zip Button',
613                     'test@domain' => 'Test user',
614                 ]);
615         $buf->shouldReceive('write')
616             ->once()
617             ->with("RCPT TO:<foo@bar>\r\n")
618             ->andReturn(1);
619         $buf->shouldReceive('readLine')
620             ->once()
621             ->with(1)
622             ->andReturn('250 OK'."\r\n");
623         $buf->shouldReceive('write')
624             ->once()
625             ->with("RCPT TO:<zip@button>\r\n")
626             ->andReturn(2);
627         $buf->shouldReceive('readLine')
628             ->once()
629             ->with(2)
630             ->andReturn('250 OK'."\r\n");
631         $buf->shouldReceive('write')
632             ->once()
633             ->with("RCPT TO:<test@domain>\r\n")
634             ->andReturn(3);
635         $buf->shouldReceive('readLine')
636             ->once()
637             ->with(3)
638             ->andReturn('250 OK'."\r\n");
639
640         $this->finishBuffer($buf);
641         $smtp->start();
642         $smtp->send($message);
643     }
644
645     public function testSendReturnsNumberOfSuccessfulRecipients()
646     {
647         $buf = $this->getBuffer();
648         $smtp = $this->getTransport($buf);
649         $message = $this->createMessage();
650
651         $message->shouldReceive('getFrom')
652                 ->once()
653                 ->andReturn(['me@domain.com' => 'Me']);
654         $message->shouldReceive('getTo')
655                 ->once()
656                 ->andReturn(['foo@bar' => null]);
657         $message->shouldReceive('getCc')
658                 ->once()
659                 ->andReturn([
660                     'zip@button' => 'Zip Button',
661                     'test@domain' => 'Test user',
662                 ]);
663         $buf->shouldReceive('write')
664             ->once()
665             ->with("RCPT TO:<foo@bar>\r\n")
666             ->andReturn(1);
667         $buf->shouldReceive('readLine')
668             ->once()
669             ->with(1)
670             ->andReturn('250 OK'."\r\n");
671         $buf->shouldReceive('write')
672             ->once()
673             ->with("RCPT TO:<zip@button>\r\n")
674             ->andReturn(2);
675         $buf->shouldReceive('readLine')
676             ->once()
677             ->with(2)
678             ->andReturn('501 Nobody here'."\r\n");
679         $buf->shouldReceive('write')
680             ->once()
681             ->with("RCPT TO:<test@domain>\r\n")
682             ->andReturn(3);
683         $buf->shouldReceive('readLine')
684             ->once()
685             ->with(3)
686             ->andReturn('250 OK'."\r\n");
687
688         $this->finishBuffer($buf);
689         $smtp->start();
690         $this->assertEquals(2, $smtp->send($message),
691             '%s: 1 of 3 recipients failed so 2 should be returned'
692             );
693     }
694
695     public function testRsetIsSentIfNoSuccessfulRecipients()
696     {
697         /* --RFC 2821, 4.1.1.5.
698
699         This command specifies that the current mail transaction will be
700         aborted.  Any stored sender, recipients, and mail data MUST be
701         discarded, and all buffers and state tables cleared.  The receiver
702         MUST send a "250 OK" reply to a RSET command with no arguments.  A
703         reset command may be issued by the client at any time.
704
705         -- RFC 2821, 4.3.2.
706
707         RSET
708             S: 250
709         */
710
711         $buf = $this->getBuffer();
712         $smtp = $this->getTransport($buf);
713         $message = $this->createMessage();
714
715         $message->shouldReceive('getFrom')
716                 ->once()
717                 ->andReturn(['me@domain.com' => 'Me']);
718         $message->shouldReceive('getTo')
719                 ->once()
720                 ->andReturn(['foo@bar' => null]);
721         $buf->shouldReceive('write')
722             ->once()
723             ->with("RCPT TO:<foo@bar>\r\n")
724             ->andReturn(1);
725         $buf->shouldReceive('readLine')
726             ->once()
727             ->with(1)
728             ->andReturn('503 Bad'."\r\n");
729         $buf->shouldReceive('write')
730             ->once()
731             ->with("RSET\r\n")
732             ->andReturn(2);
733         $buf->shouldReceive('readLine')
734             ->once()
735             ->with(2)
736             ->andReturn('250 OK'."\r\n");
737
738         $this->finishBuffer($buf);
739         $smtp->start();
740         $this->assertEquals(0, $smtp->send($message),
741             '%s: 1 of 1 recipients failed so 0 should be returned'
742             );
743     }
744
745     public function testSuccessfulDataCommand()
746     {
747         /* -- RFC 2821, 3.3.
748
749         The third step in the procedure is the DATA command (or some
750         alternative specified in a service extension).
751
752                     DATA <CRLF>
753
754         If accepted, the SMTP server returns a 354 Intermediate reply and
755         considers all succeeding lines up to but not including the end of
756         mail data indicator to be the message text.
757
758         -- RFC 2821, 4.1.1.4.
759
760         The receiver normally sends a 354 response to DATA, and then treats
761         the lines (strings ending in <CRLF> sequences, as described in
762         section 2.3.7) following the command as mail data from the sender.
763         This command causes the mail data to be appended to the mail data
764         buffer.  The mail data may contain any of the 128 ASCII character
765         codes, although experience has indicated that use of control
766         characters other than SP, HT, CR, and LF may cause problems and
767         SHOULD be avoided when possible.
768
769         -- RFC 2821, 4.3.2.
770
771         DATA
772             I: 354 -> data -> S: 250
773                                                 E: 552, 554, 451, 452
774             E: 451, 554, 503
775         */
776
777         $buf = $this->getBuffer();
778         $smtp = $this->getTransport($buf);
779         $message = $this->createMessage();
780
781         $message->shouldReceive('getFrom')
782                 ->once()
783                 ->andReturn(['me@domain.com' => 'Me']);
784         $message->shouldReceive('getTo')
785                 ->once()
786                 ->andReturn(['foo@bar' => null]);
787         $buf->shouldReceive('write')
788             ->once()
789             ->with("DATA\r\n")
790             ->andReturn(1);
791         $buf->shouldReceive('readLine')
792             ->once()
793             ->with(1)
794             ->andReturn('354 Go ahead'."\r\n");
795
796         $this->finishBuffer($buf);
797         try {
798             $smtp->start();
799             $smtp->send($message);
800         } catch (Exception $e) {
801             $this->fail('354 is the expected response to DATA');
802         }
803     }
804
805     public function testBadDataResponseCausesException()
806     {
807         $buf = $this->getBuffer();
808         $smtp = $this->getTransport($buf);
809         $message = $this->createMessage();
810
811         $message->shouldReceive('getFrom')
812                 ->once()
813                 ->andReturn(['me@domain.com' => 'Me']);
814         $message->shouldReceive('getTo')
815                 ->once()
816                 ->andReturn(['foo@bar' => null]);
817         $buf->shouldReceive('write')
818             ->once()
819             ->with("DATA\r\n")
820             ->andReturn(1);
821         $buf->shouldReceive('readLine')
822             ->once()
823             ->with(1)
824             ->andReturn('451 Bad'."\r\n");
825
826         $this->finishBuffer($buf);
827         try {
828             $smtp->start();
829             $smtp->send($message);
830             $this->fail('354 is the expected response to DATA (not observed)');
831         } catch (Swift_TransportException $e) {
832         }
833     }
834
835     public function testMessageIsStreamedToBufferForData()
836     {
837         $buf = $this->getBuffer();
838         $smtp = $this->getTransport($buf);
839         $message = $this->createMessage();
840
841         $message->shouldReceive('getFrom')
842                 ->once()
843                 ->andReturn(['me@domain.com' => 'Me']);
844         $message->shouldReceive('getTo')
845                 ->once()
846                 ->andReturn(['foo@bar' => null]);
847         $buf->shouldReceive('write')
848             ->once()
849             ->with("DATA\r\n")
850             ->andReturn(1);
851         $buf->shouldReceive('readLine')
852             ->once()
853             ->with(1)
854             ->andReturn('354 OK'."\r\n");
855         $buf->shouldReceive('write')
856             ->once()
857             ->with("\r\n.\r\n")
858             ->andReturn(2);
859         $buf->shouldReceive('readLine')
860             ->once()
861             ->with(2)
862             ->andReturn('250 OK'."\r\n");
863
864         $this->finishBuffer($buf);
865         $smtp->start();
866         $smtp->send($message);
867     }
868
869     public function testBadResponseAfterDataTransmissionCausesException()
870     {
871         $buf = $this->getBuffer();
872         $smtp = $this->getTransport($buf);
873         $message = $this->createMessage();
874
875         $message->shouldReceive('getFrom')
876                 ->once()
877                 ->andReturn(['me@domain.com' => 'Me']);
878         $message->shouldReceive('getTo')
879                 ->once()
880                 ->andReturn(['foo@bar' => null]);
881         $buf->shouldReceive('write')
882             ->once()
883             ->with("DATA\r\n")
884             ->andReturn(1);
885         $buf->shouldReceive('readLine')
886             ->once()
887             ->with(1)
888             ->andReturn('354 OK'."\r\n");
889         $buf->shouldReceive('write')
890             ->once()
891             ->with("\r\n.\r\n")
892             ->andReturn(2);
893         $buf->shouldReceive('readLine')
894             ->once()
895             ->with(2)
896             ->andReturn('554 Error'."\r\n");
897
898         $this->finishBuffer($buf);
899         try {
900             $smtp->start();
901             $smtp->send($message);
902             $this->fail('250 is the expected response after a DATA transmission (not observed)');
903         } catch (Swift_TransportException $e) {
904         }
905     }
906
907     public function testBccRecipientsAreRemovedFromHeaders()
908     {
909         /* -- RFC 2821, 7.2.
910
911      Addresses that do not appear in the message headers may appear in the
912      RCPT commands to an SMTP server for a number of reasons.  The two
913      most common involve the use of a mailing address as a "list exploder"
914      (a single address that resolves into multiple addresses) and the
915      appearance of "blind copies".  Especially when more than one RCPT
916      command is present, and in order to avoid defeating some of the
917      purpose of these mechanisms, SMTP clients and servers SHOULD NOT copy
918      the full set of RCPT command arguments into the headers, either as
919      part of trace headers or as informational or private-extension
920      headers.  Since this rule is often violated in practice, and cannot
921      be enforced, sending SMTP systems that are aware of "bcc" use MAY
922      find it helpful to send each blind copy as a separate message
923      transaction containing only a single RCPT command.
924      */
925
926         $buf = $this->getBuffer();
927         $smtp = $this->getTransport($buf);
928         $message = $this->createMessage();
929         $message->shouldReceive('getFrom')
930                 ->zeroOrMoreTimes()
931                 ->andReturn(['me@domain.com' => 'Me']);
932         $message->shouldReceive('getTo')
933                 ->zeroOrMoreTimes()
934                 ->andReturn(['foo@bar' => null]);
935         $message->shouldReceive('getBcc')
936                 ->zeroOrMoreTimes()
937                 ->andReturn([
938                     'zip@button' => 'Zip Button',
939                     'test@domain' => 'Test user',
940                 ]);
941         $message->shouldReceive('setBcc')
942                 ->once()
943                 ->with([]);
944         $message->shouldReceive('setBcc')
945                 ->zeroOrMoreTimes();
946
947         $this->finishBuffer($buf);
948         $smtp->start();
949         $smtp->send($message);
950     }
951
952     public function testEachBccRecipientIsSentASeparateMessage()
953     {
954         $buf = $this->getBuffer();
955         $smtp = $this->getTransport($buf);
956         $message = $this->createMessage();
957
958         $message->shouldReceive('getFrom')
959                 ->zeroOrMoreTimes()
960                 ->andReturn(['me@domain.com' => 'Me']);
961         $message->shouldReceive('getTo')
962                 ->zeroOrMoreTimes()
963                 ->andReturn(['foo@bar' => null]);
964         $message->shouldReceive('getBcc')
965                 ->zeroOrMoreTimes()
966                 ->andReturn([
967                     'zip@button' => 'Zip Button',
968                     'test@domain' => 'Test user',
969                 ]);
970         $message->shouldReceive('setBcc')
971                 ->atLeast()->once()
972                 ->with([]);
973         $message->shouldReceive('setBcc')
974                 ->once()
975                 ->with(['zip@button' => 'Zip Button']);
976         $message->shouldReceive('setBcc')
977                 ->once()
978                 ->with(['test@domain' => 'Test user']);
979         $message->shouldReceive('setBcc')
980                 ->atLeast()->once()
981                 ->with([
982                     'zip@button' => 'Zip Button',
983                     'test@domain' => 'Test user',
984                 ]);
985
986         $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(1);
987         $buf->shouldReceive('readLine')->once()->with(1)->andReturn("250 OK\r\n");
988         $buf->shouldReceive('write')->once()->with("RCPT TO:<foo@bar>\r\n")->andReturn(2);
989         $buf->shouldReceive('readLine')->once()->with(2)->andReturn("250 OK\r\n");
990         $buf->shouldReceive('write')->once()->with("DATA\r\n")->andReturn(3);
991         $buf->shouldReceive('readLine')->once()->with(3)->andReturn("354 OK\r\n");
992         $buf->shouldReceive('write')->once()->with("\r\n.\r\n")->andReturn(4);
993         $buf->shouldReceive('readLine')->once()->with(4)->andReturn("250 OK\r\n");
994
995         $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(5);
996         $buf->shouldReceive('readLine')->once()->with(5)->andReturn("250 OK\r\n");
997         $buf->shouldReceive('write')->once()->with("RCPT TO:<zip@button>\r\n")->andReturn(6);
998         $buf->shouldReceive('readLine')->once()->with(6)->andReturn("250 OK\r\n");
999         $buf->shouldReceive('write')->once()->with("DATA\r\n")->andReturn(7);
1000         $buf->shouldReceive('readLine')->once()->with(7)->andReturn("354 OK\r\n");
1001         $buf->shouldReceive('write')->once()->with("\r\n.\r\n")->andReturn(8);
1002         $buf->shouldReceive('readLine')->once()->with(8)->andReturn("250 OK\r\n");
1003
1004         $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(9);
1005         $buf->shouldReceive('readLine')->once()->with(9)->andReturn("250 OK\r\n");
1006         $buf->shouldReceive('write')->once()->with("RCPT TO:<test@domain>\r\n")->andReturn(10);
1007         $buf->shouldReceive('readLine')->once()->with(10)->andReturn("250 OK\r\n");
1008         $buf->shouldReceive('write')->once()->with("DATA\r\n")->andReturn(11);
1009         $buf->shouldReceive('readLine')->once()->with(11)->andReturn("354 OK\r\n");
1010         $buf->shouldReceive('write')->once()->with("\r\n.\r\n")->andReturn(12);
1011         $buf->shouldReceive('readLine')->once()->with(12)->andReturn("250 OK\r\n");
1012
1013         $this->finishBuffer($buf);
1014         $smtp->start();
1015         $this->assertEquals(3, $smtp->send($message));
1016     }
1017
1018     public function testMessageStateIsRestoredOnFailure()
1019     {
1020         $buf = $this->getBuffer();
1021         $smtp = $this->getTransport($buf);
1022         $message = $this->createMessage();
1023
1024         $message->shouldReceive('getFrom')
1025                 ->zeroOrMoreTimes()
1026                 ->andReturn(['me@domain.com' => 'Me']);
1027         $message->shouldReceive('getTo')
1028                 ->zeroOrMoreTimes()
1029                 ->andReturn(['foo@bar' => null]);
1030         $message->shouldReceive('getBcc')
1031                 ->zeroOrMoreTimes()
1032                 ->andReturn([
1033                     'zip@button' => 'Zip Button',
1034                     'test@domain' => 'Test user',
1035                 ]);
1036         $message->shouldReceive('setBcc')
1037                 ->once()
1038                 ->with([]);
1039         $message->shouldReceive('setBcc')
1040                 ->once()
1041                 ->with([
1042                     'zip@button' => 'Zip Button',
1043                     'test@domain' => 'Test user',
1044                 ]);
1045         $buf->shouldReceive('write')
1046             ->once()
1047             ->with("MAIL FROM:<me@domain.com>\r\n")
1048             ->andReturn(1);
1049         $buf->shouldReceive('readLine')
1050             ->once()
1051             ->with(1)
1052             ->andReturn("250 OK\r\n");
1053         $buf->shouldReceive('write')
1054             ->once()
1055             ->with("RCPT TO:<foo@bar>\r\n")
1056             ->andReturn(2);
1057         $buf->shouldReceive('readLine')
1058             ->once()
1059             ->with(2)
1060             ->andReturn("250 OK\r\n");
1061         $buf->shouldReceive('write')
1062             ->once()
1063             ->with("DATA\r\n")
1064             ->andReturn(3);
1065         $buf->shouldReceive('readLine')
1066             ->once()
1067             ->with(3)
1068             ->andReturn("451 No\r\n");
1069
1070         $this->finishBuffer($buf);
1071
1072         $smtp->start();
1073         try {
1074             $smtp->send($message);
1075             $this->fail('A bad response was given so exception is expected');
1076         } catch (Swift_TransportException $e) {
1077         }
1078     }
1079
1080     public function testStopSendsQuitCommand()
1081     {
1082         /* -- RFC 2821, 4.1.1.10.
1083
1084         This command specifies that the receiver MUST send an OK reply, and
1085         then close the transmission channel.
1086
1087         The receiver MUST NOT intentionally close the transmission channel
1088         until it receives and replies to a QUIT command (even if there was an
1089         error).  The sender MUST NOT intentionally close the transmission
1090         channel until it sends a QUIT command and SHOULD wait until it
1091         receives the reply (even if there was an error response to a previous
1092         command).  If the connection is closed prematurely due to violations
1093         of the above or system or network failure, the server MUST cancel any
1094         pending transaction, but not undo any previously completed
1095         transaction, and generally MUST act as if the command or transaction
1096         in progress had received a temporary error (i.e., a 4yz response).
1097
1098         The QUIT command may be issued at any time.
1099
1100         Syntax:
1101             "QUIT" CRLF
1102         */
1103
1104         $buf = $this->getBuffer();
1105         $smtp = $this->getTransport($buf);
1106         $message = $this->createMessage();
1107         $buf->shouldReceive('initialize')
1108             ->once();
1109         $buf->shouldReceive('write')
1110             ->once()
1111             ->with("QUIT\r\n")
1112             ->andReturn(1);
1113         $buf->shouldReceive('readLine')
1114             ->once()
1115             ->with(1)
1116             ->andReturn("221 Bye\r\n");
1117         $buf->shouldReceive('terminate')
1118             ->once();
1119
1120         $this->finishBuffer($buf);
1121
1122         $this->assertFalse($smtp->isStarted());
1123         $smtp->start();
1124         $this->assertTrue($smtp->isStarted());
1125         $smtp->stop();
1126         $this->assertFalse($smtp->isStarted());
1127     }
1128
1129     public function testBufferCanBeFetched()
1130     {
1131         $buf = $this->getBuffer();
1132         $smtp = $this->getTransport($buf);
1133         $ref = $smtp->getBuffer();
1134         $this->assertEquals($buf, $ref);
1135     }
1136
1137     public function testBufferCanBeWrittenToUsingExecuteCommand()
1138     {
1139         $buf = $this->getBuffer();
1140         $smtp = $this->getTransport($buf);
1141         $message = $this->createMessage();
1142         $buf->shouldReceive('write')
1143             ->zeroOrMoreTimes()
1144             ->with("FOO\r\n")
1145             ->andReturn(1);
1146         $buf->shouldReceive('readLine')
1147             ->zeroOrMoreTimes()
1148             ->with(1)
1149             ->andReturn("250 OK\r\n");
1150
1151         $res = $smtp->executeCommand("FOO\r\n");
1152         $this->assertEquals("250 OK\r\n", $res);
1153     }
1154
1155     public function testResponseCodesAreValidated()
1156     {
1157         $buf = $this->getBuffer();
1158         $smtp = $this->getTransport($buf);
1159         $message = $this->createMessage();
1160         $buf->shouldReceive('write')
1161             ->zeroOrMoreTimes()
1162             ->with("FOO\r\n")
1163             ->andReturn(1);
1164         $buf->shouldReceive('readLine')
1165             ->zeroOrMoreTimes()
1166             ->with(1)
1167             ->andReturn("551 Not ok\r\n");
1168
1169         try {
1170             $smtp->executeCommand("FOO\r\n", [250, 251]);
1171             $this->fail('A 250 or 251 response was needed but 551 was returned.');
1172         } catch (Swift_TransportException $e) {
1173         }
1174     }
1175
1176     public function testFailedRecipientsCanBeCollectedByReference()
1177     {
1178         $buf = $this->getBuffer();
1179         $smtp = $this->getTransport($buf);
1180         $message = $this->createMessage();
1181
1182         $message->shouldReceive('getFrom')
1183                 ->zeroOrMoreTimes()
1184                 ->andReturn(['me@domain.com' => 'Me']);
1185         $message->shouldReceive('getTo')
1186                 ->zeroOrMoreTimes()
1187                 ->andReturn(['foo@bar' => null]);
1188         $message->shouldReceive('getBcc')
1189                 ->zeroOrMoreTimes()
1190                 ->andReturn([
1191                     'zip@button' => 'Zip Button',
1192                     'test@domain' => 'Test user',
1193                 ]);
1194         $message->shouldReceive('setBcc')
1195                 ->atLeast()->once()
1196                 ->with([]);
1197         $message->shouldReceive('setBcc')
1198                 ->once()
1199                 ->with(['zip@button' => 'Zip Button']);
1200         $message->shouldReceive('setBcc')
1201                 ->once()
1202                 ->with(['test@domain' => 'Test user']);
1203         $message->shouldReceive('setBcc')
1204                 ->atLeast()->once()
1205                 ->with([
1206                     'zip@button' => 'Zip Button',
1207                     'test@domain' => 'Test user',
1208                 ]);
1209
1210         $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(1);
1211         $buf->shouldReceive('readLine')->once()->with(1)->andReturn("250 OK\r\n");
1212         $buf->shouldReceive('write')->once()->with("RCPT TO:<foo@bar>\r\n")->andReturn(2);
1213         $buf->shouldReceive('readLine')->once()->with(2)->andReturn("250 OK\r\n");
1214         $buf->shouldReceive('write')->once()->with("DATA\r\n")->andReturn(3);
1215         $buf->shouldReceive('readLine')->once()->with(3)->andReturn("354 OK\r\n");
1216         $buf->shouldReceive('write')->once()->with("\r\n.\r\n")->andReturn(4);
1217         $buf->shouldReceive('readLine')->once()->with(4)->andReturn("250 OK\r\n");
1218
1219         $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(5);
1220         $buf->shouldReceive('readLine')->once()->with(5)->andReturn("250 OK\r\n");
1221         $buf->shouldReceive('write')->once()->with("RCPT TO:<zip@button>\r\n")->andReturn(6);
1222         $buf->shouldReceive('readLine')->once()->with(6)->andReturn("500 Bad\r\n");
1223         $buf->shouldReceive('write')->once()->with("RSET\r\n")->andReturn(7);
1224         $buf->shouldReceive('readLine')->once()->with(7)->andReturn("250 OK\r\n");
1225
1226         $buf->shouldReceive('write')->once()->with("MAIL FROM:<me@domain.com>\r\n")->andReturn(9);
1227         $buf->shouldReceive('readLine')->once()->with(9)->andReturn("250 OK\r\n");
1228         $buf->shouldReceive('write')->once()->with("RCPT TO:<test@domain>\r\n")->andReturn(10);
1229         $buf->shouldReceive('readLine')->once()->with(10)->andReturn("500 Bad\r\n");
1230         $buf->shouldReceive('write')->once()->with("RSET\r\n")->andReturn(11);
1231         $buf->shouldReceive('readLine')->once()->with(11)->andReturn("250 OK\r\n");
1232
1233         $this->finishBuffer($buf);
1234         $smtp->start();
1235         $this->assertEquals(1, $smtp->send($message, $failures));
1236         $this->assertEquals(['zip@button', 'test@domain'], $failures,
1237             '%s: Failures should be caught in an array'
1238             );
1239     }
1240
1241     public function testSendingRegeneratesMessageId()
1242     {
1243         $buf = $this->getBuffer();
1244         $smtp = $this->getTransport($buf);
1245         $message = $this->createMessage();
1246         $message->shouldReceive('getFrom')
1247                 ->zeroOrMoreTimes()
1248                 ->andReturn(['me@domain.com' => 'Me']);
1249         $message->shouldReceive('getTo')
1250                 ->zeroOrMoreTimes()
1251                 ->andReturn(['foo@bar' => null]);
1252         $message->shouldReceive('generateId')
1253                 ->once();
1254
1255         $this->finishBuffer($buf);
1256         $smtp->start();
1257         $smtp->send($message);
1258     }
1259
1260     public function testPing()
1261     {
1262         $buf = $this->getBuffer();
1263         $smtp = $this->getTransport($buf);
1264
1265         $buf->shouldReceive('initialize')
1266             ->once();
1267         $buf->shouldReceive('readLine')
1268             ->once()
1269             ->with(0)
1270             ->andReturn("220 some.server.tld bleh\r\n");
1271         $buf->shouldReceive('write')
1272             ->once()
1273             ->with('~^NOOP\r\n$~D')
1274             ->andReturn(1);
1275         $buf->shouldReceive('readLine')
1276             ->once()
1277             ->with(1)
1278             ->andReturn('250 OK'."\r\n");
1279
1280         $this->finishBuffer($buf);
1281         $this->assertTrue($smtp->ping());
1282     }
1283
1284     public function testPingOnDeadConnection()
1285     {
1286         $buf = $this->getBuffer();
1287         $smtp = $this->getTransport($buf);
1288
1289         $buf->shouldReceive('initialize')
1290             ->once();
1291         $buf->shouldReceive('readLine')
1292             ->once()
1293             ->with(0)
1294             ->andReturn("220 some.server.tld bleh\r\n");
1295         $buf->shouldReceive('write')
1296             ->once()
1297             ->with('~^NOOP\r\n$~D')
1298             ->andThrow('Swift_TransportException');
1299
1300         $this->finishBuffer($buf);
1301         $smtp->start();
1302         $this->assertTrue($smtp->isStarted());
1303         $this->assertFalse($smtp->ping());
1304         $this->assertFalse($smtp->isStarted());
1305     }
1306
1307     public function testSetLocalDomain()
1308     {
1309         $buf = $this->getBuffer();
1310         $smtp = $this->getTransport($buf);
1311
1312         $smtp->setLocalDomain('example.com');
1313         $this->assertEquals('example.com', $smtp->getLocalDomain());
1314
1315         $smtp->setLocalDomain('192.168.0.1');
1316         $this->assertEquals('[192.168.0.1]', $smtp->getLocalDomain());
1317
1318         $smtp->setLocalDomain('[192.168.0.1]');
1319         $this->assertEquals('[192.168.0.1]', $smtp->getLocalDomain());
1320
1321         $smtp->setLocalDomain('fd00::');
1322         $this->assertEquals('[IPv6:fd00::]', $smtp->getLocalDomain());
1323
1324         $smtp->setLocalDomain('[IPv6:fd00::]');
1325         $this->assertEquals('[IPv6:fd00::]', $smtp->getLocalDomain());
1326     }
1327
1328     protected function getBuffer()
1329     {
1330         return $this->getMockery('Swift_Transport_IoBuffer')->shouldIgnoreMissing();
1331     }
1332
1333     protected function createMessage()
1334     {
1335         return $this->getMockery('Swift_Mime_SimpleMessage')->shouldIgnoreMissing();
1336     }
1337
1338     protected function finishBuffer($buf)
1339     {
1340         $buf->shouldReceive('readLine')
1341             ->zeroOrMoreTimes()
1342             ->with(0)
1343             ->andReturn('220 server.com foo'."\r\n");
1344         $buf->shouldReceive('write')
1345             ->zeroOrMoreTimes()
1346             ->with('~^(EH|HE)LO .*?\r\n$~D')
1347             ->andReturn($x = uniqid('', true));
1348         $buf->shouldReceive('readLine')
1349             ->zeroOrMoreTimes()
1350             ->with($x)
1351             ->andReturn('250 ServerName'."\r\n");
1352         $buf->shouldReceive('write')
1353             ->zeroOrMoreTimes()
1354             ->with('~^MAIL FROM:<.*?>\r\n$~D')
1355             ->andReturn($x = uniqid('', true));
1356         $buf->shouldReceive('readLine')
1357             ->zeroOrMoreTimes()
1358             ->with($x)
1359             ->andReturn("250 OK\r\n");
1360         $buf->shouldReceive('write')
1361             ->zeroOrMoreTimes()
1362             ->with('~^RCPT TO:<.*?>\r\n$~D')
1363             ->andReturn($x = uniqid('', true));
1364         $buf->shouldReceive('readLine')
1365             ->zeroOrMoreTimes()
1366             ->with($x)
1367             ->andReturn("250 OK\r\n");
1368         $buf->shouldReceive('write')
1369             ->zeroOrMoreTimes()
1370             ->with("DATA\r\n")
1371             ->andReturn($x = uniqid('', true));
1372         $buf->shouldReceive('readLine')
1373             ->zeroOrMoreTimes()
1374             ->with($x)
1375             ->andReturn("354 OK\r\n");
1376         $buf->shouldReceive('write')
1377             ->zeroOrMoreTimes()
1378             ->with("\r\n.\r\n")
1379             ->andReturn($x = uniqid('', true));
1380         $buf->shouldReceive('readLine')
1381             ->zeroOrMoreTimes()
1382             ->with($x)
1383             ->andReturn("250 OK\r\n");
1384         $buf->shouldReceive('write')
1385             ->zeroOrMoreTimes()
1386             ->with("RSET\r\n")
1387             ->andReturn($x = uniqid('', true));
1388         $buf->shouldReceive('readLine')
1389             ->zeroOrMoreTimes()
1390             ->with($x)
1391             ->andReturn("250 OK\r\n");
1392
1393         $buf->shouldReceive('write')
1394             ->zeroOrMoreTimes()
1395             ->andReturn(false);
1396         $buf->shouldReceive('readLine')
1397             ->zeroOrMoreTimes()
1398             ->andReturn(false);
1399     }
1400 }