PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/simpus/vendor/swiftmailer/swiftmailer/tests/unit/Swift/Transport/EsmtpTransportTest.php

https://gitlab.com/isdzulqor/Slis-Dev
PHP | 298 lines | 208 code | 27 blank | 63 comment | 1 complexity | cff841dbd415a8e526af47fd62348507 MD5 | raw file
  1. <?php
  2. class Swift_Transport_EsmtpTransportTest
  3. extends Swift_Transport_AbstractSmtpEventSupportTest
  4. {
  5. protected function _getTransport($buf, $dispatcher = null)
  6. {
  7. if (!$dispatcher) {
  8. $dispatcher = $this->_createEventDispatcher();
  9. }
  10. return new Swift_Transport_EsmtpTransport($buf, array(), $dispatcher);
  11. }
  12. public function testHostCanBeSetAndFetched()
  13. {
  14. $buf = $this->_getBuffer();
  15. $smtp = $this->_getTransport($buf);
  16. $smtp->setHost('foo');
  17. $this->assertEquals('foo', $smtp->getHost(), '%s: Host should be returned');
  18. }
  19. public function testPortCanBeSetAndFetched()
  20. {
  21. $buf = $this->_getBuffer();
  22. $smtp = $this->_getTransport($buf);
  23. $smtp->setPort(25);
  24. $this->assertEquals(25, $smtp->getPort(), '%s: Port should be returned');
  25. }
  26. public function testTimeoutCanBeSetAndFetched()
  27. {
  28. $buf = $this->_getBuffer();
  29. $buf->shouldReceive('setParam')
  30. ->once()
  31. ->with('timeout', 10);
  32. $smtp = $this->_getTransport($buf);
  33. $smtp->setTimeout(10);
  34. $this->assertEquals(10, $smtp->getTimeout(), '%s: Timeout should be returned');
  35. }
  36. public function testEncryptionCanBeSetAndFetched()
  37. {
  38. $buf = $this->_getBuffer();
  39. $smtp = $this->_getTransport($buf);
  40. $smtp->setEncryption('tls');
  41. $this->assertEquals('tls', $smtp->getEncryption(), '%s: Crypto should be returned');
  42. }
  43. public function testStartSendsHeloToInitiate()
  44. {
  45. //Overridden for EHLO instead
  46. }
  47. public function testStartSendsEhloToInitiate()
  48. {
  49. /* -- RFC 2821, 3.2.
  50. 3.2 Client Initiation
  51. Once the server has sent the welcoming message and the client has
  52. received it, the client normally sends the EHLO command to the
  53. server, indicating the client's identity. In addition to opening the
  54. session, use of EHLO indicates that the client is able to process
  55. service extensions and requests that the server provide a list of the
  56. extensions it supports. Older SMTP systems which are unable to
  57. support service extensions and contemporary clients which do not
  58. require service extensions in the mail session being initiated, MAY
  59. use HELO instead of EHLO. Servers MUST NOT return the extended
  60. EHLO-style response to a HELO command. For a particular connection
  61. attempt, if the server returns a "command not recognized" response to
  62. EHLO, the client SHOULD be able to fall back and send HELO.
  63. In the EHLO command the host sending the command identifies itself;
  64. the command may be interpreted as saying "Hello, I am <domain>" (and,
  65. in the case of EHLO, "and I support service extension requests").
  66. -- RFC 2281, 4.1.1.1.
  67. ehlo = "EHLO" SP Domain CRLF
  68. helo = "HELO" SP Domain CRLF
  69. -- RFC 2821, 4.3.2.
  70. EHLO or HELO
  71. S: 250
  72. E: 504, 550
  73. */
  74. $buf = $this->_getBuffer();
  75. $smtp = $this->_getTransport($buf);
  76. $buf->shouldReceive('initialize')
  77. ->once();
  78. $buf->shouldReceive('readLine')
  79. ->once()
  80. ->with(0)
  81. ->andReturn("220 some.server.tld bleh\r\n");
  82. $buf->shouldReceive('write')
  83. ->once()
  84. ->with('~^EHLO .+?\r\n$~D')
  85. ->andReturn(1);
  86. $buf->shouldReceive('readLine')
  87. ->once()
  88. ->with(1)
  89. ->andReturn('250 ServerName'."\r\n");
  90. $this->_finishBuffer($buf);
  91. try {
  92. $smtp->start();
  93. } catch (Exception $e) {
  94. $this->fail('Starting Esmtp should send EHLO and accept 250 response');
  95. }
  96. }
  97. public function testHeloIsUsedAsFallback()
  98. {
  99. /* -- RFC 2821, 4.1.4.
  100. If the EHLO command is not acceptable to the SMTP server, 501, 500,
  101. or 502 failure replies MUST be returned as appropriate. The SMTP
  102. server MUST stay in the same state after transmitting these replies
  103. that it was in before the EHLO was received.
  104. */
  105. $buf = $this->_getBuffer();
  106. $smtp = $this->_getTransport($buf);
  107. $buf->shouldReceive('initialize')
  108. ->once();
  109. $buf->shouldReceive('readLine')
  110. ->once()
  111. ->with(0)
  112. ->andReturn("220 some.server.tld bleh\r\n");
  113. $buf->shouldReceive('write')
  114. ->once()
  115. ->with('~^EHLO .+?\r\n$~D')
  116. ->andReturn(1);
  117. $buf->shouldReceive('readLine')
  118. ->once()
  119. ->with(1)
  120. ->andReturn('501 WTF'."\r\n");
  121. $buf->shouldReceive('write')
  122. ->once()
  123. ->with('~^HELO .+?\r\n$~D')
  124. ->andReturn(2);
  125. $buf->shouldReceive('readLine')
  126. ->once()
  127. ->with(2)
  128. ->andReturn('250 HELO'."\r\n");
  129. $this->_finishBuffer($buf);
  130. try {
  131. $smtp->start();
  132. } catch (Exception $e) {
  133. $this->fail(
  134. 'Starting Esmtp should fallback to HELO if needed and accept 250 response'
  135. );
  136. }
  137. }
  138. public function testInvalidHeloResponseCausesException()
  139. {
  140. //Overridden to first try EHLO
  141. $buf = $this->_getBuffer();
  142. $smtp = $this->_getTransport($buf);
  143. $buf->shouldReceive('initialize')
  144. ->once();
  145. $buf->shouldReceive('readLine')
  146. ->once()
  147. ->with(0)
  148. ->andReturn("220 some.server.tld bleh\r\n");
  149. $buf->shouldReceive('write')
  150. ->once()
  151. ->with('~^EHLO .+?\r\n$~D')
  152. ->andReturn(1);
  153. $buf->shouldReceive('readLine')
  154. ->once()
  155. ->with(1)
  156. ->andReturn('501 WTF'."\r\n");
  157. $buf->shouldReceive('write')
  158. ->once()
  159. ->with('~^HELO .+?\r\n$~D')
  160. ->andReturn(2);
  161. $buf->shouldReceive('readLine')
  162. ->once()
  163. ->with(2)
  164. ->andReturn('504 WTF'."\r\n");
  165. $this->_finishBuffer($buf);
  166. try {
  167. $this->assertFalse($smtp->isStarted(), '%s: SMTP should begin non-started');
  168. $smtp->start();
  169. $this->fail('Non 250 HELO response should raise Exception');
  170. } catch (Exception $e) {
  171. $this->assertFalse($smtp->isStarted(), '%s: SMTP start() should have failed');
  172. }
  173. }
  174. public function testDomainNameIsPlacedInEhlo()
  175. {
  176. /* -- RFC 2821, 4.1.4.
  177. The SMTP client MUST, if possible, ensure that the domain parameter
  178. to the EHLO command is a valid principal host name (not a CNAME or MX
  179. name) for its host. If this is not possible (e.g., when the client's
  180. address is dynamically assigned and the client does not have an
  181. obvious name), an address literal SHOULD be substituted for the
  182. domain name and supplemental information provided that will assist in
  183. identifying the client.
  184. */
  185. $buf = $this->_getBuffer();
  186. $smtp = $this->_getTransport($buf);
  187. $buf->shouldReceive('initialize')
  188. ->once();
  189. $buf->shouldReceive('readLine')
  190. ->once()
  191. ->with(0)
  192. ->andReturn("220 some.server.tld bleh\r\n");
  193. $buf->shouldReceive('write')
  194. ->once()
  195. ->with("EHLO mydomain.com\r\n")
  196. ->andReturn(1);
  197. $buf->shouldReceive('readLine')
  198. ->once()
  199. ->with(1)
  200. ->andReturn('250 ServerName'."\r\n");
  201. $this->_finishBuffer($buf);
  202. $smtp->setLocalDomain('mydomain.com');
  203. $smtp->start();
  204. }
  205. public function testDomainNameIsPlacedInHelo()
  206. {
  207. //Overridden to include ESMTP
  208. /* -- RFC 2821, 4.1.4.
  209. The SMTP client MUST, if possible, ensure that the domain parameter
  210. to the EHLO command is a valid principal host name (not a CNAME or MX
  211. name) for its host. If this is not possible (e.g., when the client's
  212. address is dynamically assigned and the client does not have an
  213. obvious name), an address literal SHOULD be substituted for the
  214. domain name and supplemental information provided that will assist in
  215. identifying the client.
  216. */
  217. $buf = $this->_getBuffer();
  218. $smtp = $this->_getTransport($buf);
  219. $buf->shouldReceive('initialize')
  220. ->once();
  221. $buf->shouldReceive('readLine')
  222. ->once()
  223. ->with(0)
  224. ->andReturn("220 some.server.tld bleh\r\n");
  225. $buf->shouldReceive('write')
  226. ->once()
  227. ->with('~^EHLO .+?\r\n$~D')
  228. ->andReturn(1);
  229. $buf->shouldReceive('readLine')
  230. ->once()
  231. ->with(1)
  232. ->andReturn('501 WTF'."\r\n");
  233. $buf->shouldReceive('write')
  234. ->once()
  235. ->with("HELO mydomain.com\r\n")
  236. ->andReturn(2);
  237. $buf->shouldReceive('readLine')
  238. ->once()
  239. ->with(2)
  240. ->andReturn('250 ServerName'."\r\n");
  241. $this->_finishBuffer($buf);
  242. $smtp->setLocalDomain('mydomain.com');
  243. $smtp->start();
  244. }
  245. public function testFluidInterface()
  246. {
  247. $buf = $this->_getBuffer();
  248. $smtp = $this->_getTransport($buf);
  249. $buf->shouldReceive('setParam')
  250. ->once()
  251. ->with('timeout', 30);
  252. $ref = $smtp
  253. ->setHost('foo')
  254. ->setPort(25)
  255. ->setEncryption('tls')
  256. ->setTimeout(30)
  257. ;
  258. $this->assertEquals($ref, $smtp);
  259. }
  260. }