ResponseTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see TestHelper.php
  24. */
  25. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  26. /**
  27. * @see Zend_Tool_Framework_Client_Request
  28. */
  29. require_once 'Zend/Tool/Framework/Client/Response.php';
  30. require_once 'Zend/Tool/Framework/Client/Response/ContentDecorator/Separator.php';
  31. /**
  32. * @category Zend
  33. * @package Zend_Tool
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. *
  38. * @group Zend_Tool
  39. * @group Zend_Tool_Framework
  40. * @group Zend_Tool_Framework_Client
  41. */
  42. class Zend_Tool_Framework_Client_ResponseTest extends PHPUnit_Framework_TestCase
  43. {
  44. /**
  45. * @var Zend_Tool_Framework_Client_Response
  46. */
  47. protected $_response = null;
  48. protected $_responseBuffer = array();
  49. public function setup()
  50. {
  51. $this->_response = new Zend_Tool_Framework_Client_Response();
  52. }
  53. public function testContentGetterAndSetter()
  54. {
  55. $this->_response->setContent('foo');
  56. $this->assertEquals('foo', $this->_response->getContent());
  57. $this->_response->setContent('bar');
  58. $this->assertEquals('bar', $this->_response->getContent());
  59. }
  60. public function testContentCanBeAppended()
  61. {
  62. $this->_response->setContent('foo');
  63. $this->assertEquals('foo', $this->_response->getContent());
  64. $this->_response->setContent('bar');
  65. $this->assertEquals('bar', $this->_response->getContent());
  66. $this->_response->appendContent('foo');
  67. $this->assertEquals('barfoo', $this->_response->getContent());
  68. }
  69. public function testContentCallback()
  70. {
  71. $this->_response->setContentCallback(array($this, '_responseCallback'));
  72. $this->_response->appendContent('foo');
  73. $this->assertEquals('foo', implode('', $this->_responseBuffer));
  74. $this->_response->appendContent('bar');
  75. $this->_response->appendContent('baz');
  76. $this->assertEquals('foo-bar-baz', implode('-', $this->_responseBuffer));
  77. }
  78. public function testExceptionHandling()
  79. {
  80. $this->assertFalse($this->_response->isException());
  81. $this->_response->setException(new Exception('my response exception'));
  82. $this->assertTrue($this->_response->isException());
  83. $this->assertEquals('my response exception', $this->_response->getException()->getMessage());
  84. }
  85. /**
  86. * @expectedException Zend_Tool_Framework_Client_Exception
  87. */
  88. public function testSetCallbackThrowsExceptionOnInvalidCallback()
  89. {
  90. $this->_response->setContentCallback(5);
  91. }
  92. public function testCastingToString()
  93. {
  94. $this->_response->appendContent('foo');
  95. $this->_response->appendContent('boo');
  96. $this->assertEquals('fooboo', $this->_response->__toString());
  97. }
  98. public function testAddContentDecoratorPersistsDecorators()
  99. {
  100. $separator = new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator();
  101. $this->_response->addContentDecorator($separator);
  102. $decorators = $this->_response->getContentDecorators();
  103. $this->assertArrayHasKey('separator', $decorators);
  104. $this->assertContains($separator, $decorators);
  105. }
  106. public function testResponseWillApplyDecorator()
  107. {
  108. $separator = new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator();
  109. $this->_response->addContentDecorator($separator);
  110. $this->_response->appendContent('foo', array('separator' => true));
  111. $this->_response->appendContent('boo', array('separator' => true));
  112. $this->assertEquals('foo' . PHP_EOL . 'boo' . PHP_EOL, $this->_response->__toString());
  113. }
  114. public function testResponseWillIgnoreUnknownDecoratorOptions()
  115. {
  116. $separator = new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator();
  117. $this->_response->addContentDecorator($separator);
  118. $this->_response->appendContent('foo', array('foo' => 'foo'));
  119. $this->_response->appendContent('boo', array('bar' => 'bar'));
  120. $this->assertEquals('fooboo', $this->_response->__toString());
  121. }
  122. public function testResponseWillApplyDecoratorWithDefaultOptions()
  123. {
  124. $separator = new Zend_Tool_Framework_Client_Response_ContentDecorator_Separator();
  125. $this->_response->addContentDecorator($separator);
  126. $this->_response->setDefaultDecoratorOptions(array('separator' => true));
  127. $this->_response->appendContent('foo');
  128. $this->_response->appendContent('boo');
  129. $this->assertEquals('foo' . PHP_EOL . 'boo' . PHP_EOL, $this->_response->__toString());
  130. }
  131. public function _responseCallback($content)
  132. {
  133. $this->_responseBuffer[] = $content;
  134. }
  135. }