ResponseTest.php 5.3 KB

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