TestAdapterTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. require_once realpath(dirname(__FILE__) . '/../../../') . '/TestHelper.php';
  3. require_once 'Zend/Http/Client.php';
  4. require_once 'Zend/Http/Client/Adapter/Test.php';
  5. require_once 'PHPUnit/Framework/TestCase.php';
  6. /**
  7. * Exercises Zend_Http_Client_Adapter_Test
  8. *
  9. * @category Zend
  10. * @package Zend_Http_Client
  11. * @subpackage UnitTests
  12. * @version $Id$
  13. */
  14. class Zend_Http_Client_TestAdapterTest extends PHPUnit_Framework_TestCase
  15. {
  16. public function setUp()
  17. {
  18. $this->adapter = new Zend_Http_Client_Adapter_Test();
  19. }
  20. public function testSetConfigThrowsOnInvalidConfig()
  21. {
  22. try {
  23. $this->adapter->setConfig('foo');
  24. } catch (Exception $e) {
  25. $class = 'Zend_Http_Client_Adapter_Exception';
  26. $this->assertType($class, $e);
  27. $this->assertRegexp('/expects an array/i', $e->getMessage());
  28. }
  29. }
  30. public function testSetConfigReturnsQuietly()
  31. {
  32. $this->adapter->setConfig(array('foo' => 'bar'));
  33. }
  34. public function testConnectReturnsQuietly()
  35. {
  36. $this->adapter->connect('http://foo');
  37. }
  38. public function testCloseReturnsQuietly()
  39. {
  40. $this->adapter->close();
  41. }
  42. public function testReadDefaultResponse()
  43. {
  44. $expected = "HTTP/1.1 400 Bad Request\r\n\r\n";
  45. $this->assertEquals($expected, $this->adapter->read());
  46. }
  47. public function testReadingSingleResponse()
  48. {
  49. $expected = "HTTP/1.1 200 OK\r\n\r\n";
  50. $this->adapter->setResponse($expected);
  51. $this->assertEquals($expected, $this->adapter->read());
  52. $this->assertEquals($expected, $this->adapter->read());
  53. }
  54. public function testReadingResponseCycles()
  55. {
  56. $expected = array("HTTP/1.1 200 OK\r\n\r\n",
  57. "HTTP/1.1 302 Moved Temporarily\r\n\r\n");
  58. $this->adapter->setResponse($expected[0]);
  59. $this->adapter->addResponse($expected[1]);
  60. $this->assertEquals($expected[0], $this->adapter->read());
  61. $this->assertEquals($expected[1], $this->adapter->read());
  62. $this->assertEquals($expected[0], $this->adapter->read());
  63. }
  64. public function testReadingResponseCyclesWhenSetByArray()
  65. {
  66. $expected = array("HTTP/1.1 200 OK\r\n\r\n",
  67. "HTTP/1.1 302 Moved Temporarily\r\n\r\n");
  68. $this->adapter->setResponse($expected);
  69. $this->assertEquals($expected[0], $this->adapter->read());
  70. $this->assertEquals($expected[1], $this->adapter->read());
  71. $this->assertEquals($expected[0], $this->adapter->read());
  72. }
  73. public function testSettingNextResponseByIndex()
  74. {
  75. $expected = array("HTTP/1.1 200 OK\r\n\r\n",
  76. "HTTP/1.1 302 Moved Temporarily\r\n\r\n",
  77. "HTTP/1.1 404 Not Found\r\n\r\n");
  78. $this->adapter->setResponse($expected);
  79. $this->assertEquals($expected[0], $this->adapter->read());
  80. foreach ($expected as $i => $expected) {
  81. $this->adapter->setResponseIndex($i);
  82. $this->assertEquals($expected, $this->adapter->read());
  83. }
  84. }
  85. public function testSettingNextResponseToAnInvalidIndex()
  86. {
  87. $indexes = array(-1, 1);
  88. foreach ($indexes as $i) {
  89. try {
  90. $this->adapter->setResponseIndex($i);
  91. $this->fail();
  92. } catch (Exception $e) {
  93. $class = 'Zend_Http_Client_Adapter_Exception';
  94. $this->assertType($class, $e);
  95. $this->assertRegexp('/out of range/i', $e->getMessage());
  96. }
  97. }
  98. }
  99. }