/
SmtpTransportTest.php
715 lines (640 loc) · 28.3 KB
/
SmtpTransportTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 2.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Mailer\Transport;
use Cake\Mailer\Email;
use Cake\Mailer\Transport\SmtpTransport;
use Cake\Network\Exception\SocketException;
use Cake\Network\Socket;
use Cake\TestSuite\TestCase;
/**
* Help to test SmtpTransport
*/
class SmtpTestTransport extends SmtpTransport
{
/**
* Helper to change the socket
*
* @param Socket $socket
* @return void
*/
public function setSocket(Socket $socket)
{
$this->_socket = $socket;
}
/**
* Disabled the socket change
*
* @return void
*/
protected function _generateSocket()
{
}
/**
* Magic function to call protected methods
*
* @param string $method
* @param string $args
* @return mixed
*/
public function __call($method, $args)
{
$method = '_' . $method;
return call_user_func_array([$this, $method], $args);
}
}
/**
* Test case
*/
class SmtpTransportTest extends TestCase
{
/**
* Setup
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->socket = $this->getMockBuilder('Cake\Network\Socket')
->setMethods(['read', 'write', 'connect', 'disconnect', 'enableCrypto'])
->getMock();
$this->SmtpTransport = new SmtpTestTransport();
$this->SmtpTransport->setSocket($this->socket);
$this->SmtpTransport->config(['client' => 'localhost']);
}
/**
* testConnectEhlo method
*
* @return void
*/
public function testConnectEhlo()
{
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->any())
->method('read')
->will($this->onConsecutiveCalls(
"220 Welcome message\r\n",
"250 Accepted\r\n"
));
$this->socket->expects($this->once())->method('write')->with("EHLO localhost\r\n");
$this->SmtpTransport->connect();
}
/**
* testConnectEhloTls method
*
* @return void
*/
public function testConnectEhloTls()
{
$this->SmtpTransport->config(['tls' => true]);
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->socket->expects($this->at(4))->method('write')->with("STARTTLS\r\n");
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("220 Server ready\r\n"));
$this->socket->expects($this->at(6))->method('enableCrypto')->with('tls')->will($this->returnValue(true));
$this->socket->expects($this->at(7))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(8))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->SmtpTransport->connect();
}
/**
* testConnectEhloTlsOnNonTlsServer method
*
* @return void
*/
public function testConnectEhloTlsOnNonTlsServer()
{
$this->SmtpTransport->config(['tls' => true]);
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->socket->expects($this->at(4))->method('write')->with("STARTTLS\r\n");
$this->socket->expects($this->at(5))->method('read')
->will($this->returnValue("500 5.3.3 Unrecognized command\r\n"));
$e = null;
try {
$this->SmtpTransport->connect();
} catch (SocketException $e) {
}
$this->assertNotNull($e);
$this->assertEquals('SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.', $e->getMessage());
$this->assertInstanceOf(SocketException::class, $e->getPrevious());
$this->assertContains('500 5.3.3 Unrecognized command', $e->getPrevious()->getMessage());
}
/**
* testConnectEhloNoTlsOnRequiredTlsServer method
*
* @expectedException \Cake\Network\Exception\SocketException
* @expectedExceptionMessage SMTP authentication method not allowed, check if SMTP server requires TLS.
* @return void
*/
public function testConnectEhloNoTlsOnRequiredTlsServer()
{
$this->SmtpTransport->config(['tls' => false, 'username' => 'user', 'password' => 'pass']);
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->socket->expects($this->at(4))->method('write')->with("AUTH LOGIN\r\n");
$this->socket->expects($this->at(5))->method('read')
->will($this->returnValue("504 5.7.4 Unrecognized authentication type\r\n"));
$this->SmtpTransport->connect();
}
/**
* testConnectHelo method
*
* @return void
*/
public function testConnectHelo()
{
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
$this->socket->expects($this->at(4))->method('write')->with("HELO localhost\r\n");
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 Accepted\r\n"));
$this->SmtpTransport->connect();
}
/**
* testConnectFail method
*
* @return void
*/
public function testConnectFail()
{
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
$this->socket->expects($this->at(4))->method('write')->with("HELO localhost\r\n");
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("200 Not Accepted\r\n"));
$e = null;
try {
$this->SmtpTransport->connect();
} catch (SocketException $e) {
}
$this->assertNotNull($e);
$this->assertEquals('SMTP server did not accept the connection.', $e->getMessage());
$this->assertInstanceOf(SocketException::class, $e->getPrevious());
$this->assertContains('200 Not Accepted', $e->getPrevious()->getMessage());
}
/**
* testAuth method
*
* @return void
*/
public function testAuth()
{
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("334 Login\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("bWFyaw==\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("334 Pass\r\n"));
$this->socket->expects($this->at(4))->method('write')->with("c3Rvcnk=\r\n");
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("235 OK\r\n"));
$this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
$this->SmtpTransport->auth();
}
/**
* testAuthNotRecognized method
*
* @expectedException \Cake\Network\Exception\SocketException
* @expectedExceptionMessage AUTH command not recognized or not implemented, SMTP server may not require authentication.
* @return void
*/
public function testAuthNotRecognized()
{
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
$this->socket->expects($this->at(1))->method('read')
->will($this->returnValue("500 5.3.3 Unrecognized command\r\n"));
$this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
$this->SmtpTransport->auth();
}
/**
* testAuthNotImplemented method
*
* @expectedException \Cake\Network\Exception\SocketException
* @expectedExceptionMessage AUTH command not recognized or not implemented, SMTP server may not require authentication.
* @return void
*/
public function testAuthNotImplemented()
{
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
$this->socket->expects($this->at(1))->method('read')
->will($this->returnValue("502 5.3.3 Command not implemented\r\n"));
$this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
$this->SmtpTransport->auth();
}
/**
* testAuthBadSequence method
*
* @expectedException \Cake\Network\Exception\SocketException
* @expectedExceptionMessage SMTP Error: 503 5.5.1 Already authenticated
* @return void
*/
public function testAuthBadSequence()
{
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
$this->socket->expects($this->at(1))
->method('read')->will($this->returnValue("503 5.5.1 Already authenticated\r\n"));
$this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
$this->SmtpTransport->auth();
}
/**
* testAuthBadUsername method
*
* @return void
*/
public function testAuthBadUsername()
{
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("334 Login\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("bWFyaw==\r\n");
$this->socket->expects($this->at(3))->method('read')
->will($this->returnValue("535 5.7.8 Authentication failed\r\n"));
$this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
$e = null;
try {
$this->SmtpTransport->auth();
} catch (SocketException $e) {
}
$this->assertNotNull($e);
$this->assertEquals('SMTP server did not accept the username.', $e->getMessage());
$this->assertInstanceOf(SocketException::class, $e->getPrevious());
$this->assertContains('535 5.7.8 Authentication failed', $e->getPrevious()->getMessage());
}
/**
* testAuthBadPassword method
*
* @return void
*/
public function testAuthBadPassword()
{
$this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("334 Login\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("bWFyaw==\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("334 Pass\r\n"));
$this->socket->expects($this->at(4))->method('write')->with("c3Rvcnk=\r\n");
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("535 5.7.8 Authentication failed\r\n"));
$this->SmtpTransport->config(['username' => 'mark', 'password' => 'story']);
$e = null;
try {
$this->SmtpTransport->auth();
} catch (SocketException $e) {
}
$this->assertNotNull($e);
$this->assertEquals('SMTP server did not accept the password.', $e->getMessage());
$this->assertInstanceOf(SocketException::class, $e->getPrevious());
$this->assertContains('535 5.7.8 Authentication failed', $e->getPrevious()->getMessage());
}
/**
* testRcpt method
*
* @return void
*/
public function testRcpt()
{
$email = new Email();
$email->setFrom('noreply@cakephp.org', 'CakePHP Test');
$email->setTo('cake@cakephp.org', 'CakePHP');
$email->setBcc('phpnut@cakephp.org');
$email->setCc(['mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso']);
$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(4))->method('write')->with("RCPT TO:<mark@cakephp.org>\r\n");
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(6))->method('write')->with("RCPT TO:<juan@cakephp.org>\r\n");
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(8))->method('write')->with("RCPT TO:<phpnut@cakephp.org>\r\n");
$this->socket->expects($this->at(9))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->SmtpTransport->sendRcpt($email);
}
/**
* testRcptWithReturnPath method
*
* @return void
*/
public function testRcptWithReturnPath()
{
$email = new Email();
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->setTo('cake@cakephp.org', 'CakePHP');
$email->returnPath('pleasereply@cakephp.org', 'CakePHP Return');
$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<pleasereply@cakephp.org>\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->SmtpTransport->sendRcpt($email);
}
/**
* testSendData method
*
* @return void
*/
public function testSendData()
{
$email = $this->getMockBuilder('Cake\Mailer\Email')
->setMethods(['message'])
->getMock();
$email->setFrom('noreply@cakephp.org', 'CakePHP Test');
$email->setReturnPath('pleasereply@cakephp.org', 'CakePHP Return');
$email->setTo('cake@cakephp.org', 'CakePHP');
$email->setCc(['mark@cakephp.org' => 'Mark Story', 'juan@cakephp.org' => 'Juan Basso']);
$email->setBcc('phpnut@cakephp.org');
$email->setMessageId('<4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>');
$email->setSubject('Testing SMTP');
$date = date(DATE_RFC2822);
$email->setHeaders(['Date' => $date]);
$email->expects($this->once())
->method('message')
->will($this->returnValue(['First Line', 'Second Line', '.Third Line', '']));
$data = "From: CakePHP Test <noreply@cakephp.org>\r\n";
$data .= "Return-Path: CakePHP Return <pleasereply@cakephp.org>\r\n";
$data .= "To: CakePHP <cake@cakephp.org>\r\n";
$data .= "Cc: Mark Story <mark@cakephp.org>, Juan Basso <juan@cakephp.org>\r\n";
$data .= 'Date: ' . $date . "\r\n";
$data .= "Message-ID: <4d9946cf-0a44-4907-88fe-1d0ccbdd56cb@localhost>\r\n";
$data .= "Subject: Testing SMTP\r\n";
$data .= "MIME-Version: 1.0\r\n";
$data .= "Content-Type: text/plain; charset=UTF-8\r\n";
$data .= "Content-Transfer-Encoding: 8bit\r\n";
$data .= "\r\n";
$data .= "First Line\r\n";
$data .= "Second Line\r\n";
$data .= "..Third Line\r\n"; // RFC5321 4.5.2.Transparency
$data .= "\r\n";
$data .= "\r\n\r\n.\r\n";
$this->socket->expects($this->at(0))->method('write')->with("DATA\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("354 OK\r\n"));
$this->socket->expects($this->at(2))->method('write')->with($data);
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->SmtpTransport->sendData($email);
}
/**
* testQuit method
*
* @return void
*/
public function testQuit()
{
$this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
$this->socket->connected = true;
$this->SmtpTransport->disconnect();
}
/**
* testEmptyConfigArray method
*
* @return void
*/
public function testEmptyConfigArray()
{
$this->SmtpTransport->config([
'client' => 'myhost.com',
'port' => 666
]);
$expected = $this->SmtpTransport->config();
$this->assertEquals(666, $expected['port']);
$this->SmtpTransport->config([]);
$result = $this->SmtpTransport->config();
$this->assertEquals($expected, $result);
}
/**
* testGetLastResponse method
*
* @return void
*/
public function testGetLastResponse()
{
$this->assertEmpty($this->SmtpTransport->getLastResponse());
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->any())
->method('read')
->will($this->onConsecutiveCalls(
"220 Welcome message\r\n",
"250-PIPELINING\r\n",
"250-SIZE 102400000\r\n",
"250-VRFY\r\n",
"250-ETRN\r\n",
"250-STARTTLS\r\n",
"250-AUTH PLAIN LOGIN\r\n",
"250-AUTH=PLAIN LOGIN\r\n",
"250-ENHANCEDSTATUSCODES\r\n",
"250-8BITMIME\r\n",
"250 DSN\r\n"
));
$this->socket->expects($this->once())->method('write')->with("EHLO localhost\r\n");
$this->SmtpTransport->connect();
$expected = [
['code' => '250', 'message' => 'PIPELINING'],
['code' => '250', 'message' => 'SIZE 102400000'],
['code' => '250', 'message' => 'VRFY'],
['code' => '250', 'message' => 'ETRN'],
['code' => '250', 'message' => 'STARTTLS'],
['code' => '250', 'message' => 'AUTH PLAIN LOGIN'],
['code' => '250', 'message' => 'AUTH=PLAIN LOGIN'],
['code' => '250', 'message' => 'ENHANCEDSTATUSCODES'],
['code' => '250', 'message' => '8BITMIME'],
['code' => '250', 'message' => 'DSN']
];
$result = $this->SmtpTransport->getLastResponse();
$this->assertEquals($expected, $result);
}
/**
* Test getLastResponse() with multiple operations
*
* @return void
*/
public function testGetLastResponseMultipleOperations()
{
$email = new Email();
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->setTo('cake@cakephp.org', 'CakePHP');
$this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->SmtpTransport->sendRcpt($email);
$expected = [
['code' => '250', 'message' => 'OK'],
];
$result = $this->SmtpTransport->getLastResponse();
$this->assertEquals($expected, $result);
}
/**
* testBufferResponseLines method
*
* @return void
*/
public function testBufferResponseLines()
{
$responseLines = [
'123',
"456\tFOO",
'FOOBAR',
'250-PIPELINING',
'250-ENHANCEDSTATUSCODES',
'250-8BITMIME',
'250 DSN',
];
$this->SmtpTransport->bufferResponseLines($responseLines);
$expected = [
['code' => '123', 'message' => null],
['code' => '250', 'message' => 'PIPELINING'],
['code' => '250', 'message' => 'ENHANCEDSTATUSCODES'],
['code' => '250', 'message' => '8BITMIME'],
['code' => '250', 'message' => 'DSN']
];
$result = $this->SmtpTransport->getLastResponse();
$this->assertEquals($expected, $result);
}
/**
* testExplicitConnectAlreadyConnected method
*
* @return void
*/
public function testExplicitConnectAlreadyConnected()
{
$this->socket->expects($this->never())->method('connect');
$this->socket->connected = true;
$this->SmtpTransport->connect();
}
/**
* testConnected method
*
* @return void
*/
public function testConnected()
{
$this->socket->connected = true;
$this->assertTrue($this->SmtpTransport->connected());
$this->socket->connected = false;
$this->assertFalse($this->SmtpTransport->connected());
}
/**
* testAutoDisconnect method
*
* @return void
*/
public function testAutoDisconnect()
{
$this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
$this->socket->expects($this->at(1))->method('disconnect');
$this->socket->connected = true;
unset($this->SmtpTransport);
}
/**
* testExplicitDisconnect method
*
* @return void
*/
public function testExplicitDisconnect()
{
$this->socket->expects($this->at(0))->method('write')->with("QUIT\r\n");
$this->socket->expects($this->at(1))->method('disconnect');
$this->socket->connected = true;
$this->SmtpTransport->disconnect();
}
/**
* testExplicitDisconnectNotConnected method
*
* @return void
*/
public function testExplicitDisconnectNotConnected()
{
$callback = function ($arg) {
$this->assertNotEquals("QUIT\r\n", $arg);
};
$this->socket->expects($this->any())->method('write')->will($this->returnCallback($callback));
$this->socket->expects($this->never())->method('disconnect');
$this->SmtpTransport->disconnect();
}
/**
* testKeepAlive method
*
* @return void
*/
public function testKeepAlive()
{
$this->SmtpTransport->config(['keepAlive' => true]);
$email = $this->getMockBuilder('Cake\Mailer\Email')
->setMethods(['message'])
->getMock();
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->setTo('cake@cakephp.org', 'CakePHP');
$email->expects($this->exactly(2))->method('message')->will($this->returnValue(['First Line']));
$callback = function ($arg) {
$this->assertNotEquals("QUIT\r\n", $arg);
};
$this->socket->expects($this->any())->method('write')->will($this->returnCallback($callback));
$this->socket->expects($this->never())->method('disconnect');
$this->socket->expects($this->at(0))->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(4))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(6))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(8))->method('write')->with("DATA\r\n");
$this->socket->expects($this->at(9))->method('read')->will($this->returnValue("354 OK\r\n"));
$this->socket->expects($this->at(10))->method('write')->with($this->stringContains('First Line'));
$this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(12))->method('write')->with("RSET\r\n");
$this->socket->expects($this->at(13))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(14))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
$this->socket->expects($this->at(15))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(16))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
$this->socket->expects($this->at(17))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(18))->method('write')->with("DATA\r\n");
$this->socket->expects($this->at(19))->method('read')->will($this->returnValue("354 OK\r\n"));
$this->socket->expects($this->at(20))->method('write')->with($this->stringContains('First Line'));
$this->socket->expects($this->at(21))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->SmtpTransport->send($email);
$this->socket->connected = true;
$this->SmtpTransport->send($email);
}
/**
* testSendDefaults method
*
* @return void
*/
public function testSendDefaults()
{
$email = $this->getMockBuilder('Cake\Mailer\Email')
->setMethods(['message'])
->getMock();
$email->from('noreply@cakephp.org', 'CakePHP Test');
$email->setTo('cake@cakephp.org', 'CakePHP');
$email->expects($this->once())->method('message')->will($this->returnValue(['First Line']));
$this->socket->expects($this->at(0))->method('connect')->will($this->returnValue(true));
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(4))->method('write')->with("MAIL FROM:<noreply@cakephp.org>\r\n");
$this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(6))->method('write')->with("RCPT TO:<cake@cakephp.org>\r\n");
$this->socket->expects($this->at(7))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(8))->method('write')->with("DATA\r\n");
$this->socket->expects($this->at(9))->method('read')->will($this->returnValue("354 OK\r\n"));
$this->socket->expects($this->at(10))->method('write')->with($this->stringContains('First Line'));
$this->socket->expects($this->at(11))->method('read')->will($this->returnValue("250 OK\r\n"));
$this->socket->expects($this->at(12))->method('write')->with("QUIT\r\n");
$this->socket->expects($this->at(13))->method('disconnect');
$this->SmtpTransport->send($email);
}
}