2
0

ResponseTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Tool
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see TestHelper.php
  23. */
  24. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  25. /**
  26. * @see Zend_Tool_Framework_Client_Request
  27. */
  28. require_once 'Zend/Tool/Framework/Client/Response.php';
  29. require_once 'Zend/Tool/Framework/Client/Response/ContentDecorator/Separator.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Tool
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. *
  37. * @group Zend_Tool_Framework
  38. * @group Zend_Tool_Framework_Action
  39. */
  40. class Zend_Tool_Framework_Client_ResponseTest extends PHPUnit_Framework_TestCase
  41. {
  42. /**
  43. * @var Zend_Tool_Framework_Client_Response
  44. */
  45. protected $_response = null;
  46. protected $_responseBuffer = array();
  47. public function setup()
  48. {
  49. $this->_response = new Zend_Tool_Framework_Client_Response();
  50. }
  51. public function testContentGetterAndSetter()
  52. {
  53. $this->_response->setContent('foo');
  54. $this->assertEquals('foo', $this->_response->getContent());
  55. $this->_response->setContent('bar');
  56. $this->assertEquals('bar', $this->_response->getContent());
  57. }
  58. public function testContentCanBeAppended()
  59. {
  60. $this->_response->setContent('foo');
  61. $this->assertEquals('foo', $this->_response->getContent());
  62. $this->_response->setContent('bar');
  63. $this->assertEquals('bar', $this->_response->getContent());
  64. $this->_response->appendContent('foo');
  65. $this->assertEquals('barfoo', $this->_response->getContent());
  66. }
  67. public function testContentCallback()
  68. {
  69. $this->_response->setContentCallback(array($this, '_responseCallback'));
  70. $this->_response->appendContent('foo');
  71. $this->assertEquals('foo', implode('', $this->_responseBuffer));
  72. $this->_response->appendContent('bar');
  73. $this->_response->appendContent('baz');
  74. $this->assertEquals('foo-bar-baz', implode('-', $this->_responseBuffer));
  75. }
  76. public function testExceptionHandling()
  77. {
  78. $this->assertFalse($this->_response->isException());
  79. $this->_response->setException(new Exception('my response exception'));
  80. $this->assertTrue($this->_response->isException());
  81. $this->assertEquals('my response exception', $this->_response->getException()->getMessage());
  82. }
  83. /**
  84. * @expectedException Zend_Tool_Framework_Client_Exception
  85. */
  86. public function testSetCallbackThrowsExceptionOnInvalidCallback()
  87. {
  88. $this->_response->setContentCallback(5);
  89. }
  90. public function testCastingToString()
  91. {
  92. $this->_response->appendContent('foo');
  93. $this->_response->appendContent('boo');
  94. $this->assertEquals('fooboo', $this->_response->__toString());
  95. }
  96. public function testAddContentDecoratorPersistsDecorators()
  97. {
  98. $separator = new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator();
  99. $this->_response->addContentDecorator($separator);
  100. $decorators = $this->_response->getContentDecorators();
  101. $this->assertArrayHasKey('separator', $decorators);
  102. $this->assertContains($separator, $decorators);
  103. }
  104. public function testResponseWillApplyDecorator()
  105. {
  106. $separator = new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator();
  107. $this->_response->addContentDecorator($separator);
  108. $this->_response->appendContent('foo', array('separator' => true));
  109. $this->_response->appendContent('boo', array('separator' => true));
  110. $this->assertEquals('foo' . PHP_EOL . 'boo' . PHP_EOL, $this->_response->__toString());
  111. }
  112. public function testResponseWillIgnoreUnknownDecoratorOptions()
  113. {
  114. $separator = new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator();
  115. $this->_response->addContentDecorator($separator);
  116. $this->_response->appendContent('foo', array('foo' => 'foo'));
  117. $this->_response->appendContent('boo', array('bar' => 'bar'));
  118. $this->assertEquals('fooboo', $this->_response->__toString());
  119. }
  120. public function testResponseWillApplyDecoratorWithDefaultOptions()
  121. {
  122. $separator = new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator();
  123. $this->_response->addContentDecorator($separator);
  124. $this->_response->setDefaultDecoratorOptions(array('separator' => true));
  125. $this->_response->appendContent('foo');
  126. $this->_response->appendContent('boo');
  127. $this->assertEquals('foo' . PHP_EOL . 'boo' . PHP_EOL, $this->_response->__toString());
  128. }
  129. public function _responseCallback($content)
  130. {
  131. $this->_responseBuffer[] = $content;
  132. }
  133. }