SimpleHeaderFactoryTest.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. require_once 'Swift/Tests/SwiftUnitTestCase.php';
  3. require_once 'Swift/Mime/SimpleHeaderFactory.php';
  4. require_once 'Swift/Mime/HeaderEncoder.php';
  5. require_once 'Swift/Encoder.php';
  6. require_once 'Swift/Mime/Grammar.php';
  7. class Swift_Mime_SimpleHeaderFactoryTest extends Swift_Tests_SwiftUnitTestCase
  8. {
  9. private $_factory;
  10. public function setUp()
  11. {
  12. $this->_factory = $this->_createFactory();
  13. }
  14. public function testMailboxHeaderIsCorrectType()
  15. {
  16. $header = $this->_factory->createMailboxHeader('X-Foo');
  17. $this->assertIsA($header, 'Swift_Mime_Headers_MailboxHeader');
  18. }
  19. public function testMailboxHeaderHasCorrectName()
  20. {
  21. $header = $this->_factory->createMailboxHeader('X-Foo');
  22. $this->assertEqual('X-Foo', $header->getFieldName());
  23. }
  24. public function testMailboxHeaderHasCorrectModel()
  25. {
  26. $header = $this->_factory->createMailboxHeader('X-Foo',
  27. array('foo@bar'=>'FooBar')
  28. );
  29. $this->assertEqual(array('foo@bar'=>'FooBar'), $header->getFieldBodyModel());
  30. }
  31. public function testDateHeaderHasCorrectType()
  32. {
  33. $header = $this->_factory->createDateHeader('X-Date');
  34. $this->assertIsA($header, 'Swift_Mime_Headers_DateHeader');
  35. }
  36. public function testDateHeaderHasCorrectName()
  37. {
  38. $header = $this->_factory->createDateHeader('X-Date');
  39. $this->assertEqual('X-Date', $header->getFieldName());
  40. }
  41. public function testDateHeaderHasCorrectModel()
  42. {
  43. $header = $this->_factory->createDateHeader('X-Date', 123);
  44. $this->assertEqual(123, $header->getFieldBodyModel());
  45. }
  46. public function testTextHeaderHasCorrectType()
  47. {
  48. $header = $this->_factory->createTextHeader('X-Foo');
  49. $this->assertIsA($header, 'Swift_Mime_Headers_UnstructuredHeader');
  50. }
  51. public function testTextHeaderHasCorrectName()
  52. {
  53. $header = $this->_factory->createTextHeader('X-Foo');
  54. $this->assertEqual('X-Foo', $header->getFieldName());
  55. }
  56. public function testTextHeaderHasCorrectModel()
  57. {
  58. $header = $this->_factory->createTextHeader('X-Foo', 'bar');
  59. $this->assertEqual('bar', $header->getFieldBodyModel());
  60. }
  61. public function testParameterizedHeaderHasCorrectType()
  62. {
  63. $header = $this->_factory->createParameterizedHeader('X-Foo');
  64. $this->assertIsA($header, 'Swift_Mime_Headers_ParameterizedHeader');
  65. }
  66. public function testParameterizedHeaderHasCorrectName()
  67. {
  68. $header = $this->_factory->createParameterizedHeader('X-Foo');
  69. $this->assertEqual('X-Foo', $header->getFieldName());
  70. }
  71. public function testParameterizedHeaderHasCorrectModel()
  72. {
  73. $header = $this->_factory->createParameterizedHeader('X-Foo', 'bar');
  74. $this->assertEqual('bar', $header->getFieldBodyModel());
  75. }
  76. public function testParameterizedHeaderHasCorrectParams()
  77. {
  78. $header = $this->_factory->createParameterizedHeader('X-Foo', 'bar',
  79. array('zip' => 'button')
  80. );
  81. $this->assertEqual(array('zip'=>'button'), $header->getParameters());
  82. }
  83. public function testIdHeaderHasCorrectType()
  84. {
  85. $header = $this->_factory->createIdHeader('X-ID');
  86. $this->assertIsA($header, 'Swift_Mime_Headers_IdentificationHeader');
  87. }
  88. public function testIdHeaderHasCorrectName()
  89. {
  90. $header = $this->_factory->createIdHeader('X-ID');
  91. $this->assertEqual('X-ID', $header->getFieldName());
  92. }
  93. public function testIdHeaderHasCorrectModel()
  94. {
  95. $header = $this->_factory->createIdHeader('X-ID', 'xyz@abc');
  96. $this->assertEqual(array('xyz@abc'), $header->getFieldBodyModel());
  97. }
  98. public function testPathHeaderHasCorrectType()
  99. {
  100. $header = $this->_factory->createPathHeader('X-Path');
  101. $this->assertIsA($header, 'Swift_Mime_Headers_PathHeader');
  102. }
  103. public function testPathHeaderHasCorrectName()
  104. {
  105. $header = $this->_factory->createPathHeader('X-Path');
  106. $this->assertEqual('X-Path', $header->getFieldName());
  107. }
  108. public function testPathHeaderHasCorrectModel()
  109. {
  110. $header = $this->_factory->createPathHeader('X-Path', 'foo@bar');
  111. $this->assertEqual('foo@bar', $header->getFieldBodyModel());
  112. }
  113. public function testCharsetChangeNotificationNotifiesEncoders()
  114. {
  115. $encoder = $this->_createHeaderEncoder(false);
  116. $paramEncoder = $this->_createParamEncoder(false);
  117. $factory = $this->_createFactory($encoder, $paramEncoder);
  118. $this->_checking(Expectations::create()
  119. -> one($encoder)->charsetChanged('utf-8')
  120. -> one($paramEncoder)->charsetChanged('utf-8')
  121. -> ignoring($encoder)
  122. -> ignoring($paramEncoder)
  123. );
  124. $factory->charsetChanged('utf-8');
  125. }
  126. // -- Creation methods
  127. private function _createFactory($encoder = null, $paramEncoder = null)
  128. {
  129. return new Swift_Mime_SimpleHeaderFactory(
  130. $encoder
  131. ? $encoder : $this->_createHeaderEncoder(),
  132. $paramEncoder
  133. ? $paramEncoder : $this->_createParamEncoder(),
  134. new Swift_Mime_Grammar()
  135. );
  136. }
  137. private function _createHeaderEncoder($stub = true)
  138. {
  139. return $stub
  140. ? $this->_stub('Swift_Mime_HeaderEncoder')
  141. : $this->_mock('Swift_Mime_HeaderEncoder');
  142. }
  143. private function _createParamEncoder($stub = true)
  144. {
  145. return $stub
  146. ? $this->_stub('Swift_Encoder')
  147. : $this->_mock('Swift_Encoder');
  148. }
  149. }