StreamTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\net\socket;
  9. use lithium\net\http\Request;
  10. use lithium\net\http\Response;
  11. use lithium\net\socket\Stream;
  12. class StreamTest extends \lithium\test\Unit {
  13. protected $_testConfig = array(
  14. 'persistent' => false,
  15. 'scheme' => 'http',
  16. 'host' => 'google.com',
  17. 'port' => 80,
  18. 'timeout' => 2,
  19. 'classes' => array('request' => 'lithium\net\http\Request')
  20. );
  21. public function skip() {
  22. $message = "No internet connection established.";
  23. $this->skipIf(!$this->_hasNetwork($this->_testConfig), $message);
  24. }
  25. public function testAllMethodsNoConnection() {
  26. $stream = new Stream(array('scheme' => null));
  27. $this->assertFalse($stream->open());
  28. $this->assertTrue($stream->close());
  29. $this->assertFalse($stream->timeout(2));
  30. $this->assertFalse($stream->encoding('UTF-8'));
  31. $this->assertFalse($stream->write(null));
  32. $this->assertFalse($stream->read());
  33. $this->assertTrue($stream->eof());
  34. $this->assertNull($stream->send(new Request()));
  35. }
  36. public function testOpen() {
  37. $stream = new Stream($this->_testConfig);
  38. $result = $stream->open();
  39. $this->assertTrue($result);
  40. $result = $stream->resource();
  41. $this->assertTrue(is_resource($result));
  42. }
  43. public function testClose() {
  44. $stream = new Stream($this->_testConfig);
  45. $result = $stream->open();
  46. $this->assertTrue($result);
  47. $result = $stream->close();
  48. $this->assertTrue($result);
  49. $result = $stream->resource();
  50. $this->assertFalse(is_resource($result));
  51. }
  52. public function testTimeout() {
  53. $stream = new Stream($this->_testConfig);
  54. $result = $stream->open();
  55. $stream->timeout(10);
  56. $result = $stream->resource();
  57. $this->assertTrue(is_resource($result));
  58. }
  59. public function testEncoding() {
  60. $stream = new Stream($this->_testConfig);
  61. $result = $stream->open();
  62. $stream->encoding('UTF-8');
  63. $result = $stream->resource();
  64. $this->assertTrue(is_resource($result));
  65. $stream = new Stream($this->_testConfig + array('encoding' => 'UTF-8'));
  66. $result = $stream->open();
  67. $result = $stream->resource();
  68. $this->assertTrue(is_resource($result));
  69. }
  70. public function testWriteAndRead() {
  71. $stream = new Stream($this->_testConfig);
  72. $this->assertTrue(is_resource($stream->open()));
  73. $this->assertTrue(is_resource($stream->resource()));
  74. $result = $stream->write();
  75. $this->assertEqual(83, $result);
  76. $this->assertPattern("/^HTTP/", (string) $stream->read());
  77. }
  78. public function testSendWithNull() {
  79. $stream = new Stream($this->_testConfig);
  80. $this->assertTrue(is_resource($stream->open()));
  81. $result = $stream->send(
  82. new Request($this->_testConfig),
  83. array('response' => 'lithium\net\http\Response')
  84. );
  85. $this->assertTrue($result instanceof Response);
  86. $this->assertPattern("/^HTTP/", (string) $result);
  87. $this->assertTrue($stream->eof());
  88. }
  89. public function testSendWithArray() {
  90. $stream = new Stream($this->_testConfig);
  91. $this->assertTrue(is_resource($stream->open()));
  92. $result = $stream->send($this->_testConfig,
  93. array('response' => 'lithium\net\http\Response')
  94. );
  95. $this->assertTrue($result instanceof Response);
  96. $this->assertPattern("/^HTTP/", (string) $result);
  97. $this->assertTrue($stream->eof());
  98. }
  99. public function testSendWithObject() {
  100. $stream = new Stream($this->_testConfig);
  101. $this->assertTrue(is_resource($stream->open()));
  102. $result = $stream->send(
  103. new Request($this->_testConfig),
  104. array('response' => 'lithium\net\http\Response')
  105. );
  106. $this->assertTrue($result instanceof Response);
  107. $this->assertPattern("/^HTTP/", (string) $result);
  108. $this->assertTrue($stream->eof());
  109. }
  110. }
  111. ?>