12
12
namespace Symfony \Component \Mailer \Tests \Transport \Smtp ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \Mailer \Envelope ;
15
16
use Symfony \Component \Mailer \Transport \Smtp \SmtpTransport ;
17
+ use Symfony \Component \Mailer \Transport \Smtp \Stream \AbstractStream ;
16
18
use Symfony \Component \Mailer \Transport \Smtp \Stream \SocketStream ;
19
+ use Symfony \Component \Mime \Address ;
20
+ use Symfony \Component \Mime \RawMessage ;
17
21
18
22
class SmtpTransportTest extends TestCase
19
23
{
@@ -25,4 +29,95 @@ public function testToString()
25
29
$ t = new SmtpTransport ((new SocketStream ())->setHost ('127.0.0.1 ' )->setPort (2525 )->disableTls ());
26
30
$ this ->assertEquals ('smtp://127.0.0.1:2525 ' , (string ) $ t );
27
31
}
32
+
33
+ public function testSendDoesNotPingBelowThreshold (): void
34
+ {
35
+ $ stream = new DummyStream ();
36
+ $ envelope = new Envelope (new Address ('sender@example.org ' ), [new Address ('recipient@example.org ' )]);
37
+
38
+ $ transport = new SmtpTransport ($ stream );
39
+ $ transport ->send (new RawMessage ('Message 1 ' ), $ envelope );
40
+ $ transport ->send (new RawMessage ('Message 2 ' ), $ envelope );
41
+ $ transport ->send (new RawMessage ('Message 3 ' ), $ envelope );
42
+
43
+ $ this ->assertNotContains ("NOOP \r\n" , $ stream ->getCommands ());
44
+ }
45
+
46
+ public function testSendDoesPingAboveThreshold (): void
47
+ {
48
+ $ stream = new DummyStream ();
49
+ $ envelope = new Envelope (new Address ('sender@example.org ' ), [new Address ('recipient@example.org ' )]);
50
+
51
+ $ transport = new SmtpTransport ($ stream );
52
+ $ transport ->setPingThreshold (1 );
53
+
54
+ $ transport ->send (new RawMessage ('Message 1 ' ), $ envelope );
55
+ $ transport ->send (new RawMessage ('Message 2 ' ), $ envelope );
56
+
57
+ $ this ->assertNotContains ("NOOP \r\n" , $ stream ->getCommands ());
58
+
59
+ $ stream ->clearCommands ();
60
+ sleep (1 );
61
+
62
+ $ transport ->send (new RawMessage ('Message 3 ' ), $ envelope );
63
+ $ this ->assertContains ("NOOP \r\n" , $ stream ->getCommands ());
64
+ }
65
+ }
66
+
67
+ class DummyStream extends AbstractStream
68
+ {
69
+ /**
70
+ * @var string
71
+ */
72
+ private $ nextResponse ;
73
+
74
+ /**
75
+ * @var string[]
76
+ */
77
+ private $ commands ;
78
+
79
+ public function initialize (): void
80
+ {
81
+ $ this ->nextResponse = '220 localhost ' ;
82
+ }
83
+
84
+ public function write (string $ bytes , $ debug = true ): void
85
+ {
86
+ $ this ->commands [] = $ bytes ;
87
+
88
+ if (0 === strpos ($ bytes , 'DATA ' )) {
89
+ $ this ->nextResponse = '354 Enter message, ending with "." on a line by itself ' ;
90
+ } elseif (0 === strpos ($ bytes , 'QUIT ' )) {
91
+ $ this ->nextResponse = '221 Goodbye ' ;
92
+ } else {
93
+ $ this ->nextResponse = '250 OK ' ;
94
+ }
95
+ }
96
+
97
+ public function readLine (): string
98
+ {
99
+ return $ this ->nextResponse ;
100
+ }
101
+
102
+ public function flush (): void
103
+ {
104
+ }
105
+
106
+ /**
107
+ * @return string[]
108
+ */
109
+ public function getCommands (): array
110
+ {
111
+ return $ this ->commands ;
112
+ }
113
+
114
+ public function clearCommands (): void
115
+ {
116
+ $ this ->commands = [];
117
+ }
118
+
119
+ protected function getReadConnectionDescription (): string
120
+ {
121
+ return 'null ' ;
122
+ }
28
123
}
0 commit comments