SimpleHeaderSetTest.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Mime/SimpleHeaderSet.php';
  4. require_once 'Swift/Mime/HeaderFactory.php';
  5. require_once 'Swift/Mime/Header.php';
  6. class Swift_Mime_SimpleHeaderSetTest extends Swift_Tests_SwiftUnitTestCase
  7. {
  8. public function testAddMailboxHeaderDelegatesToFactory()
  9. {
  10. $factory = $this->_createFactory();
  11. $this->_checking(Expectations::create()
  12. -> one($factory)->createMailboxHeader('From', array('person@domain'=>'Person'))
  13. -> returns($this->_createHeader('From'))
  14. );
  15. $set = $this->_createSet($factory);
  16. $set->addMailboxHeader('From', array('person@domain'=>'Person'));
  17. }
  18. public function testAddDateHeaderDelegatesToFactory()
  19. {
  20. $factory = $this->_createFactory();
  21. $this->_checking(Expectations::create()
  22. -> one($factory)->createDateHeader('Date', 1234)
  23. -> returns($this->_createHeader('Date'))
  24. );
  25. $set = $this->_createSet($factory);
  26. $set->addDateHeader('Date', 1234);
  27. }
  28. public function testAddTextHeaderDelegatesToFactory()
  29. {
  30. $factory = $this->_createFactory();
  31. $this->_checking(Expectations::create()
  32. -> one($factory)->createTextHeader('Subject', 'some text')
  33. -> returns($this->_createHeader('Subject'))
  34. );
  35. $set = $this->_createSet($factory);
  36. $set->addTextHeader('Subject', 'some text');
  37. }
  38. public function testAddParameterizedHeaderDelegatesToFactory()
  39. {
  40. $factory = $this->_createFactory();
  41. $this->_checking(Expectations::create()
  42. -> one($factory)->createParameterizedHeader(
  43. 'Content-Type', 'text/plain', array('charset'=>'utf-8')
  44. ) -> returns($this->_createHeader('Content-Type'))
  45. );
  46. $set = $this->_createSet($factory);
  47. $set->addParameterizedHeader('Content-Type', 'text/plain',
  48. array('charset'=>'utf-8')
  49. );
  50. }
  51. public function testAddIdHeaderDelegatesToFactory()
  52. {
  53. $factory = $this->_createFactory();
  54. $this->_checking(Expectations::create()
  55. -> one($factory)->createIdHeader('Message-ID', 'some@id')
  56. -> returns($this->_createHeader('Message-ID'))
  57. );
  58. $set = $this->_createSet($factory);
  59. $set->addIdHeader('Message-ID', 'some@id');
  60. }
  61. public function testAddPathHeaderDelegatesToFactory()
  62. {
  63. $factory = $this->_createFactory();
  64. $this->_checking(Expectations::create()
  65. -> one($factory)->createPathHeader('Return-Path', 'some@path')
  66. -> returns($this->_createHeader('Return-Path'))
  67. );
  68. $set = $this->_createSet($factory);
  69. $set->addPathHeader('Return-Path', 'some@path');
  70. }
  71. public function testHasReturnsFalseWhenNoHeaders()
  72. {
  73. $set = $this->_createSet($this->_createFactory());
  74. $this->assertFalse($set->has('Some-Header'));
  75. }
  76. public function testAddedMailboxHeaderIsSeenByHas()
  77. {
  78. $factory = $this->_createFactory();
  79. $this->_checking(Expectations::create()
  80. -> ignoring($factory)->createMailboxHeader('From', array('person@domain'=>'Person'))
  81. -> returns($this->_createHeader('From'))
  82. );
  83. $set = $this->_createSet($factory);
  84. $set->addMailboxHeader('From', array('person@domain'=>'Person'));
  85. $this->assertTrue($set->has('From'));
  86. }
  87. public function testAddedDateHeaderIsSeenByHas()
  88. {
  89. $factory = $this->_createFactory();
  90. $this->_checking(Expectations::create()
  91. -> ignoring($factory)->createDateHeader('Date', 1234)
  92. -> returns($this->_createHeader('Date'))
  93. );
  94. $set = $this->_createSet($factory);
  95. $set->addDateHeader('Date', 1234);
  96. $this->assertTrue($set->has('Date'));
  97. }
  98. public function testAddedTextHeaderIsSeenByHas()
  99. {
  100. $factory = $this->_createFactory();
  101. $this->_checking(Expectations::create()
  102. -> ignoring($factory)->createTextHeader('Subject', 'some text')
  103. -> returns($this->_createHeader('Subject'))
  104. );
  105. $set = $this->_createSet($factory);
  106. $set->addTextHeader('Subject', 'some text');
  107. $this->assertTrue($set->has('Subject'));
  108. }
  109. public function testAddedParameterizedHeaderIsSeenByHas()
  110. {
  111. $factory = $this->_createFactory();
  112. $this->_checking(Expectations::create()
  113. -> ignoring($factory)->createParameterizedHeader(
  114. 'Content-Type', 'text/plain', array('charset'=>'utf-8')
  115. ) -> returns($this->_createHeader('Content-Type'))
  116. );
  117. $set = $this->_createSet($factory);
  118. $set->addParameterizedHeader('Content-Type', 'text/plain',
  119. array('charset'=>'utf-8')
  120. );
  121. $this->assertTrue($set->has('Content-Type'));
  122. }
  123. public function testAddedIdHeaderIsSeenByHas()
  124. {
  125. $factory = $this->_createFactory();
  126. $this->_checking(Expectations::create()
  127. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  128. -> returns($this->_createHeader('Message-ID'))
  129. );
  130. $set = $this->_createSet($factory);
  131. $set->addIdHeader('Message-ID', 'some@id');
  132. $this->assertTrue($set->has('Message-ID'));
  133. }
  134. public function testAddedPathHeaderIsSeenByHas()
  135. {
  136. $factory = $this->_createFactory();
  137. $this->_checking(Expectations::create()
  138. -> ignoring($factory)->createPathHeader('Return-Path', 'some@path')
  139. -> returns($this->_createHeader('Return-Path'))
  140. );
  141. $set = $this->_createSet($factory);
  142. $set->addPathHeader('Return-Path', 'some@path');
  143. $this->assertTrue($set->has('Return-Path'));
  144. }
  145. public function testNewlySetHeaderIsSeenByHas()
  146. {
  147. $factory = $this->_createFactory();
  148. $header = $this->_createHeader('X-Foo', 'bar');
  149. $set = $this->_createSet($factory);
  150. $set->set($header);
  151. $this->assertTrue($set->has('X-Foo'));
  152. }
  153. public function testHasCanAcceptOffset()
  154. {
  155. $factory = $this->_createFactory();
  156. $this->_checking(Expectations::create()
  157. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  158. -> returns($this->_createHeader('Message-ID'))
  159. );
  160. $set = $this->_createSet($factory);
  161. $set->addIdHeader('Message-ID', 'some@id');
  162. $this->assertTrue($set->has('Message-ID', 0));
  163. }
  164. public function testHasWithIllegalOffsetReturnsFalse()
  165. {
  166. $factory = $this->_createFactory();
  167. $this->_checking(Expectations::create()
  168. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  169. -> returns($this->_createHeader('Message-ID'))
  170. );
  171. $set = $this->_createSet($factory);
  172. $set->addIdHeader('Message-ID', 'some@id');
  173. $this->assertFalse($set->has('Message-ID', 1));
  174. }
  175. public function testHasCanDistinguishMultipleHeaders()
  176. {
  177. $factory = $this->_createFactory();
  178. $this->_checking(Expectations::create()
  179. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  180. -> returns($this->_createHeader('Message-ID'))
  181. -> ignoring($factory)->createIdHeader('Message-ID', 'other@id')
  182. -> returns($this->_createHeader('Message-ID'))
  183. );
  184. $set = $this->_createSet($factory);
  185. $set->addIdHeader('Message-ID', 'some@id');
  186. $set->addIdHeader('Message-ID', 'other@id');
  187. $this->assertTrue($set->has('Message-ID', 1));
  188. }
  189. public function testGetWithUnspecifiedOffset()
  190. {
  191. $header = $this->_createHeader('Message-ID');
  192. $factory = $this->_createFactory();
  193. $this->_checking(Expectations::create()
  194. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  195. -> returns($header)
  196. );
  197. $set = $this->_createSet($factory);
  198. $set->addIdHeader('Message-ID', 'some@id');
  199. $this->assertSame($header, $set->get('Message-ID'));
  200. }
  201. public function testGetWithSpeiciedOffset()
  202. {
  203. $header0 = $this->_createHeader('Message-ID');
  204. $header1 = $this->_createHeader('Message-ID');
  205. $header2 = $this->_createHeader('Message-ID');
  206. $factory = $this->_createFactory();
  207. $this->_checking(Expectations::create()
  208. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  209. -> returns($header0)
  210. -> ignoring($factory)->createIdHeader('Message-ID', 'other@id')
  211. -> returns($header1)
  212. -> ignoring($factory)->createIdHeader('Message-ID', 'more@id')
  213. -> returns($header2)
  214. );
  215. $set = $this->_createSet($factory);
  216. $set->addIdHeader('Message-ID', 'some@id');
  217. $set->addIdHeader('Message-ID', 'other@id');
  218. $set->addIdHeader('Message-ID', 'more@id');
  219. $this->assertSame($header1, $set->get('Message-ID', 1));
  220. }
  221. public function testGetReturnsNullIfHeaderNotSet()
  222. {
  223. $set = $this->_createSet($this->_createFactory());
  224. $this->assertNull($set->get('Message-ID', 99));
  225. }
  226. public function testGetAllReturnsAllHeadersMatchingName()
  227. {
  228. $header0 = $this->_createHeader('Message-ID');
  229. $header1 = $this->_createHeader('Message-ID');
  230. $header2 = $this->_createHeader('Message-ID');
  231. $factory = $this->_createFactory();
  232. $this->_checking(Expectations::create()
  233. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  234. -> returns($header0)
  235. -> ignoring($factory)->createIdHeader('Message-ID', 'other@id')
  236. -> returns($header1)
  237. -> ignoring($factory)->createIdHeader('Message-ID', 'more@id')
  238. -> returns($header2)
  239. );
  240. $set = $this->_createSet($factory);
  241. $set->addIdHeader('Message-ID', 'some@id');
  242. $set->addIdHeader('Message-ID', 'other@id');
  243. $set->addIdHeader('Message-ID', 'more@id');
  244. $this->assertEqual(array($header0, $header1, $header2),
  245. $set->getAll('Message-ID')
  246. );
  247. }
  248. public function testGetAllReturnsAllHeadersIfNoArguments()
  249. {
  250. $header0 = $this->_createHeader('Message-ID');
  251. $header1 = $this->_createHeader('Subject');
  252. $header2 = $this->_createHeader('To');
  253. $factory = $this->_createFactory();
  254. $this->_checking(Expectations::create()
  255. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  256. -> returns($header0)
  257. -> ignoring($factory)->createIdHeader('Subject', 'thing')
  258. -> returns($header1)
  259. -> ignoring($factory)->createIdHeader('To', 'person@example.org')
  260. -> returns($header2)
  261. );
  262. $set = $this->_createSet($factory);
  263. $set->addIdHeader('Message-ID', 'some@id');
  264. $set->addIdHeader('Subject', 'thing');
  265. $set->addIdHeader('To', 'person@example.org');
  266. $this->assertEqual(array($header0, $header1, $header2),
  267. $set->getAll()
  268. );
  269. }
  270. public function testGetAllReturnsEmptyArrayIfNoneSet()
  271. {
  272. $set = $this->_createSet($this->_createFactory());
  273. $this->assertEqual(array(), $set->getAll('Received'));
  274. }
  275. public function testRemoveWithUnspecifiedOffset()
  276. {
  277. $header = $this->_createHeader('Message-ID');
  278. $factory = $this->_createFactory();
  279. $this->_checking(Expectations::create()
  280. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  281. -> returns($header)
  282. );
  283. $set = $this->_createSet($factory);
  284. $set->addIdHeader('Message-ID', 'some@id');
  285. $set->remove('Message-ID');
  286. $this->assertFalse($set->has('Message-ID'));
  287. }
  288. public function testRemoveWithSpecifiedIndexRemovesHeader()
  289. {
  290. $header0 = $this->_createHeader('Message-ID');
  291. $header1 = $this->_createHeader('Message-ID');
  292. $factory = $this->_createFactory();
  293. $this->_checking(Expectations::create()
  294. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  295. -> returns($header0)
  296. -> ignoring($factory)->createIdHeader('Message-ID', 'other@id')
  297. -> returns($header1)
  298. );
  299. $set = $this->_createSet($factory);
  300. $set->addIdHeader('Message-ID', 'some@id');
  301. $set->addIdHeader('Message-ID', 'other@id');
  302. $set->remove('Message-ID', 1);
  303. $this->assertFalse($set->has('Message-ID', 1));
  304. }
  305. public function testRemoveWithSpecifiedIndexLeavesOtherHeaders()
  306. {
  307. $header0 = $this->_createHeader('Message-ID');
  308. $header1 = $this->_createHeader('Message-ID');
  309. $factory = $this->_createFactory();
  310. $this->_checking(Expectations::create()
  311. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  312. -> returns($header0)
  313. -> ignoring($factory)->createIdHeader('Message-ID', 'other@id')
  314. -> returns($header1)
  315. );
  316. $set = $this->_createSet($factory);
  317. $set->addIdHeader('Message-ID', 'some@id');
  318. $set->addIdHeader('Message-ID', 'other@id');
  319. $set->remove('Message-ID', 1);
  320. $this->assertTrue($set->has('Message-ID', 0));
  321. }
  322. public function testRemoveWithInvalidOffsetDoesNothing()
  323. {
  324. $header = $this->_createHeader('Message-ID');
  325. $factory = $this->_createFactory();
  326. $this->_checking(Expectations::create()
  327. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  328. -> returns($header)
  329. );
  330. $set = $this->_createSet($factory);
  331. $set->addIdHeader('Message-ID', 'some@id');
  332. $set->remove('Message-ID', 50);
  333. $this->assertTrue($set->has('Message-ID'));
  334. }
  335. public function testRemoveAllRemovesAllHeadersWithName()
  336. {
  337. $header0 = $this->_createHeader('Message-ID');
  338. $header1 = $this->_createHeader('Message-ID');
  339. $factory = $this->_createFactory();
  340. $this->_checking(Expectations::create()
  341. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  342. -> returns($header0)
  343. -> ignoring($factory)->createIdHeader('Message-ID', 'other@id')
  344. -> returns($header1)
  345. );
  346. $set = $this->_createSet($factory);
  347. $set->addIdHeader('Message-ID', 'some@id');
  348. $set->addIdHeader('Message-ID', 'other@id');
  349. $set->removeAll('Message-ID');
  350. $this->assertFalse($set->has('Message-ID', 0));
  351. $this->assertFalse($set->has('Message-ID', 1));
  352. }
  353. public function testHasIsNotCaseSensitive()
  354. {
  355. $header = $this->_createHeader('Message-ID');
  356. $factory = $this->_createFactory();
  357. $this->_checking(Expectations::create()
  358. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  359. -> returns($header)
  360. );
  361. $set = $this->_createSet($factory);
  362. $set->addIdHeader('Message-ID', 'some@id');
  363. $this->assertTrue($set->has('message-id'));
  364. }
  365. public function testGetIsNotCaseSensitive()
  366. {
  367. $header = $this->_createHeader('Message-ID');
  368. $factory = $this->_createFactory();
  369. $this->_checking(Expectations::create()
  370. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  371. -> returns($header)
  372. );
  373. $set = $this->_createSet($factory);
  374. $set->addIdHeader('Message-ID', 'some@id');
  375. $this->assertSame($header, $set->get('message-id'));
  376. }
  377. public function testGetAllIsNotCaseSensitive()
  378. {
  379. $header = $this->_createHeader('Message-ID');
  380. $factory = $this->_createFactory();
  381. $this->_checking(Expectations::create()
  382. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  383. -> returns($header)
  384. );
  385. $set = $this->_createSet($factory);
  386. $set->addIdHeader('Message-ID', 'some@id');
  387. $this->assertEqual(array($header), $set->getAll('message-id'));
  388. }
  389. public function testRemoveIsNotCaseSensitive()
  390. {
  391. $header = $this->_createHeader('Message-ID');
  392. $factory = $this->_createFactory();
  393. $this->_checking(Expectations::create()
  394. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  395. -> returns($header)
  396. );
  397. $set = $this->_createSet($factory);
  398. $set->addIdHeader('Message-ID', 'some@id');
  399. $set->remove('message-id');
  400. $this->assertFalse($set->has('Message-ID'));
  401. }
  402. public function testRemoveAllIsNotCaseSensitive()
  403. {
  404. $header = $this->_createHeader('Message-ID');
  405. $factory = $this->_createFactory();
  406. $this->_checking(Expectations::create()
  407. -> ignoring($factory)->createIdHeader('Message-ID', 'some@id')
  408. -> returns($header)
  409. );
  410. $set = $this->_createSet($factory);
  411. $set->addIdHeader('Message-ID', 'some@id');
  412. $set->removeAll('message-id');
  413. $this->assertFalse($set->has('Message-ID'));
  414. }
  415. public function testNewInstance()
  416. {
  417. $set = $this->_createSet($this->_createFactory());
  418. $instance = $set->newInstance();
  419. $this->assertIsA($instance, 'Swift_Mime_HeaderSet');
  420. }
  421. public function testToStringJoinsHeadersTogether()
  422. {
  423. $factory = $this->_createFactory();
  424. $this->_checking(Expectations::create()
  425. -> one($factory)->createTextHeader('Foo', 'bar')
  426. -> returns($this->_createHeader('Foo', 'bar'))
  427. -> one($factory)->createTextHeader('Zip', 'buttons')
  428. -> returns($this->_createHeader('Zip', 'buttons'))
  429. );
  430. $set = $this->_createSet($factory);
  431. $set->addTextHeader('Foo', 'bar');
  432. $set->addTextHeader('Zip', 'buttons');
  433. $this->assertEqual(
  434. "Foo: bar\r\n" .
  435. "Zip: buttons\r\n",
  436. $set->toString()
  437. );
  438. }
  439. public function testHeadersWithoutBodiesAreNotDisplayed()
  440. {
  441. $factory = $this->_createFactory();
  442. $this->_checking(Expectations::create()
  443. -> one($factory)->createTextHeader('Foo', 'bar')
  444. -> returns($this->_createHeader('Foo', 'bar'))
  445. -> one($factory)->createTextHeader('Zip', '')
  446. -> returns($this->_createHeader('Zip', ''))
  447. );
  448. $set = $this->_createSet($factory);
  449. $set->addTextHeader('Foo', 'bar');
  450. $set->addTextHeader('Zip', '');
  451. $this->assertEqual(
  452. "Foo: bar\r\n",
  453. $set->toString()
  454. );
  455. }
  456. public function testHeadersWithoutBodiesCanBeForcedToDisplay()
  457. {
  458. $factory = $this->_createFactory();
  459. $this->_checking(Expectations::create()
  460. -> one($factory)->createTextHeader('Foo', '')
  461. -> returns($this->_createHeader('Foo', ''))
  462. -> one($factory)->createTextHeader('Zip', '')
  463. -> returns($this->_createHeader('Zip', ''))
  464. );
  465. $set = $this->_createSet($factory);
  466. $set->addTextHeader('Foo', '');
  467. $set->addTextHeader('Zip', '');
  468. $set->setAlwaysDisplayed(array('Foo', 'Zip'));
  469. $this->assertEqual(
  470. "Foo: \r\n" .
  471. "Zip: \r\n",
  472. $set->toString()
  473. );
  474. }
  475. public function testHeaderSequencesCanBeSpecified()
  476. {
  477. $factory = $this->_createFactory();
  478. $this->_checking(Expectations::create()
  479. -> one($factory)->createTextHeader('First', 'one')
  480. -> returns($this->_createHeader('First', 'one'))
  481. -> one($factory)->createTextHeader('Second', 'two')
  482. -> returns($this->_createHeader('Second', 'two'))
  483. -> one($factory)->createTextHeader('Third', 'three')
  484. -> returns($this->_createHeader('Third', 'three'))
  485. );
  486. $set = $this->_createSet($factory);
  487. $set->addTextHeader('Third', 'three');
  488. $set->addTextHeader('First', 'one');
  489. $set->addTextHeader('Second', 'two');
  490. $set->defineOrdering(array('First', 'Second', 'Third'));
  491. $this->assertEqual(
  492. "First: one\r\n" .
  493. "Second: two\r\n" .
  494. "Third: three\r\n",
  495. $set->toString()
  496. );
  497. }
  498. public function testUnsortedHeadersAppearAtEnd()
  499. {
  500. $factory = $this->_createFactory();
  501. $this->_checking(Expectations::create()
  502. -> one($factory)->createTextHeader('First', 'one')
  503. -> returns($this->_createHeader('First', 'one'))
  504. -> one($factory)->createTextHeader('Second', 'two')
  505. -> returns($this->_createHeader('Second', 'two'))
  506. -> one($factory)->createTextHeader('Third', 'three')
  507. -> returns($this->_createHeader('Third', 'three'))
  508. -> one($factory)->createTextHeader('Fourth', 'four')
  509. -> returns($this->_createHeader('Fourth', 'four'))
  510. -> one($factory)->createTextHeader('Fifth', 'five')
  511. -> returns($this->_createHeader('Fifth', 'five'))
  512. );
  513. $set = $this->_createSet($factory);
  514. $set->addTextHeader('Fourth', 'four');
  515. $set->addTextHeader('Fifth', 'five');
  516. $set->addTextHeader('Third', 'three');
  517. $set->addTextHeader('First', 'one');
  518. $set->addTextHeader('Second', 'two');
  519. $set->defineOrdering(array('First', 'Second', 'Third'));
  520. $this->assertEqual(
  521. "First: one\r\n" .
  522. "Second: two\r\n" .
  523. "Third: three\r\n" .
  524. "Fourth: four\r\n" .
  525. "Fifth: five\r\n",
  526. $set->toString()
  527. );
  528. }
  529. public function testSettingCharsetNotifiesAlreadyExistingHeaders()
  530. {
  531. $subject = $this->_createHeader('Subject', 'some text');
  532. $xHeader = $this->_createHeader('X-Header', 'some text');
  533. $factory = $this->_createFactory();
  534. $this->_checking(Expectations::create()
  535. -> ignoring($factory)->createTextHeader('Subject', 'some text')
  536. -> returns($subject)
  537. -> ignoring($factory)->createTextHeader('X-Header', 'some text')
  538. -> returns($xHeader)
  539. -> ignoring($factory)
  540. -> one($subject)->setCharset('utf-8')
  541. -> one($xHeader)->setCharset('utf-8')
  542. );
  543. $set = $this->_createSet($factory);
  544. $set->addTextHeader('Subject', 'some text');
  545. $set->addTextHeader('X-Header', 'some text');
  546. $set->setCharset('utf-8');
  547. }
  548. public function testCharsetChangeNotifiesAlreadyExistingHeaders()
  549. {
  550. $subject = $this->_createHeader('Subject', 'some text');
  551. $xHeader = $this->_createHeader('X-Header', 'some text');
  552. $factory = $this->_createFactory();
  553. $this->_checking(Expectations::create()
  554. -> ignoring($factory)->createTextHeader('Subject', 'some text')
  555. -> returns($subject)
  556. -> ignoring($factory)->createTextHeader('X-Header', 'some text')
  557. -> returns($xHeader)
  558. -> ignoring($factory)
  559. -> one($subject)->setCharset('utf-8')
  560. -> one($xHeader)->setCharset('utf-8')
  561. );
  562. $set = $this->_createSet($factory);
  563. $set->addTextHeader('Subject', 'some text');
  564. $set->addTextHeader('X-Header', 'some text');
  565. $set->charsetChanged('utf-8');
  566. }
  567. public function testCharsetChangeNotifiesFactory()
  568. {
  569. $factory = $this->_createFactory();
  570. $this->_checking(Expectations::create()
  571. -> one($factory)->charsetChanged('utf-8')
  572. -> ignoring($factory)
  573. );
  574. $set = $this->_createSet($factory);
  575. $set->setCharset('utf-8');
  576. }
  577. // -- Creation methods
  578. private function _createSet($factory)
  579. {
  580. return new Swift_Mime_SimpleHeaderSet($factory);
  581. }
  582. private function _createFactory()
  583. {
  584. return $this->_mock('Swift_Mime_HeaderFactory');
  585. }
  586. private function _createHeader($name, $body = '')
  587. {
  588. $header = $this->_mock('Swift_Mime_Header');
  589. $this->_checking(Expectations::create()
  590. -> ignoring($header)->getFieldName() -> returns($name)
  591. -> ignoring($header)->toString() -> returns(sprintf("%s: %s\r\n", $name, $body))
  592. -> ignoring($header)->getFieldBody() -> returns($body)
  593. );
  594. return $header;
  595. }
  596. }