ContextTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\Context;
  12. class ContextTest extends \lithium\test\Unit {
  13. protected $_testConfig = array(
  14. 'persistent' => false,
  15. 'scheme' => 'http',
  16. 'host' => 'google.com',
  17. 'port' => 80,
  18. 'timeout' => 4,
  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 testConstruct() {
  26. $subject = new Context(array('timeout' => 300) + $this->_testConfig);
  27. $this->assertTrue(300, $subject->timeout());
  28. unset($subject);
  29. }
  30. public function testGetSetTimeout() {
  31. $subject = new Context($this->_testConfig);
  32. $this->assertEqual(4, $subject->timeout());
  33. $this->assertEqual(25, $subject->timeout(25));
  34. $this->assertEqual(25, $subject->timeout());
  35. $subject->open();
  36. $this->assertEqual(25, $subject->timeout());
  37. $result = stream_context_get_options($subject->resource());
  38. $this->assertEqual(25, $result['http']['timeout']);
  39. }
  40. public function testOpen() {
  41. $stream = new Context($this->_testConfig);
  42. $this->assertTrue(is_resource($stream->open()));
  43. }
  44. public function testClose() {
  45. $stream = new Context($this->_testConfig);
  46. $this->assertEqual(true, $stream->close());
  47. }
  48. public function testEncoding() {
  49. $stream = new Context($this->_testConfig);
  50. $this->assertEqual(false, $stream->encoding());
  51. }
  52. public function testEof() {
  53. $stream = new Context($this->_testConfig);
  54. $this->assertTrue(true, $stream->eof());
  55. }
  56. public function testMessageInConfig() {
  57. $socket = new Context(array('message' => new Request($this->_testConfig)));
  58. $this->assertTrue(is_resource($socket->open()));
  59. }
  60. public function testWriteAndRead() {
  61. $stream = new Context($this->_testConfig);
  62. $this->assertTrue(is_resource($stream->open()));
  63. $this->assertTrue(is_resource($stream->resource()));
  64. $this->assertEqual(1, $stream->write());
  65. $this->assertPattern("/^HTTP/", (string) $stream->read());
  66. }
  67. public function testSendWithNull() {
  68. $stream = new Context($this->_testConfig);
  69. $this->assertTrue(is_resource($stream->open()));
  70. $result = $stream->send(
  71. new Request($this->_testConfig),
  72. array('response' => 'lithium\net\http\Response')
  73. );
  74. $this->assertTrue($result instanceof Response);
  75. $this->assertPattern("/^HTTP/", (string) $result);
  76. $this->assertTrue($stream->eof());
  77. }
  78. public function testSendWithArray() {
  79. $stream = new Context($this->_testConfig);
  80. $this->assertTrue(is_resource($stream->open()));
  81. $result = $stream->send($this->_testConfig,
  82. array('response' => 'lithium\net\http\Response')
  83. );
  84. $this->assertTrue($result instanceof Response);
  85. $this->assertPattern("/^HTTP/", (string) $result);
  86. $this->assertTrue($stream->eof());
  87. }
  88. public function testSendWithObject() {
  89. $stream = new Context($this->_testConfig);
  90. $this->assertTrue(is_resource($stream->open()));
  91. $result = $stream->send(
  92. new Request($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. }
  100. ?>