PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/cases/libs/socket.test.php

https://github.com/hardsshah/bookmarks
PHP | 178 lines | 87 code | 6 blank | 85 comment | 0 complexity | 979bf804234d9a9a40327b0e1eed6955 MD5 | raw file
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * SocketTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @filesource
  17. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
  18. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  19. * @package cake
  20. * @subpackage cake.tests.cases.libs
  21. * @since CakePHP(tm) v 1.2.0.4206
  22. * @version $Revision$
  23. * @modifiedby $LastChangedBy$
  24. * @lastmodified $Date$
  25. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  26. */
  27. App::import('Core', 'Socket');
  28. /**
  29. * SocketTest class
  30. *
  31. * @package cake
  32. * @subpackage cake.tests.cases.libs
  33. */
  34. class SocketTest extends CakeTestCase {
  35. /**
  36. * setUp method
  37. *
  38. * @access public
  39. * @return void
  40. */
  41. function setUp() {
  42. $this->Socket = new CakeSocket();
  43. }
  44. /**
  45. * tearDown method
  46. *
  47. * @access public
  48. * @return void
  49. */
  50. function tearDown() {
  51. unset($this->Socket);
  52. }
  53. /**
  54. * testConstruct method
  55. *
  56. * @access public
  57. * @return void
  58. */
  59. function testConstruct() {
  60. $this->Socket->__construct();
  61. $baseConfig = $this->Socket->_baseConfig;
  62. $this->assertIdentical($baseConfig, array(
  63. 'persistent' => false,
  64. 'host' => 'localhost',
  65. 'protocol' => 'tcp',
  66. 'port' => 80,
  67. 'timeout' => 30
  68. ));
  69. $this->Socket->reset();
  70. $this->Socket->__construct(array('host' => 'foo-bar'));
  71. $baseConfig['host'] = 'foo-bar';
  72. $baseConfig['protocol'] = getprotobyname($baseConfig['protocol']);
  73. $this->assertIdentical($this->Socket->config, $baseConfig);
  74. $this->Socket = new CakeSocket(array('host' => 'www.cakephp.org', 'port' => 23, 'protocol' => 'udp'));
  75. $baseConfig = $this->Socket->_baseConfig;
  76. $baseConfig['host'] = 'www.cakephp.org';
  77. $baseConfig['port'] = 23;
  78. $baseConfig['protocol'] = 17;
  79. $this->assertIdentical($this->Socket->config, $baseConfig);
  80. }
  81. /**
  82. * testSocketConnection method
  83. *
  84. * @access public
  85. * @return void
  86. */
  87. function testSocketConnection() {
  88. $this->assertFalse($this->Socket->connected);
  89. $this->Socket->disconnect();
  90. $this->assertFalse($this->Socket->connected);
  91. $this->Socket->connect();
  92. $this->assertTrue($this->Socket->connected);
  93. $this->Socket->connect();
  94. $this->assertTrue($this->Socket->connected);
  95. $this->Socket->disconnect();
  96. $config = array('persistent' => true);
  97. $this->Socket = new CakeSocket($config);
  98. $this->Socket->connect();
  99. $this->assertTrue($this->Socket->connected);
  100. }
  101. /**
  102. * testSocketHost method
  103. *
  104. * @access public
  105. * @return void
  106. */
  107. function testSocketHost() {
  108. $this->Socket = new CakeSocket();
  109. $this->Socket->connect();
  110. $this->assertEqual($this->Socket->address(), '127.0.0.1');
  111. $this->assertEqual(gethostbyaddr('127.0.0.1'), $this->Socket->host());
  112. $this->assertEqual($this->Socket->lastError(), null);
  113. $this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
  114. $this->Socket = new CakeSocket(array('host' => '127.0.0.1'));
  115. $this->Socket->connect();
  116. $this->assertEqual($this->Socket->address(), '127.0.0.1');
  117. $this->assertEqual(gethostbyaddr('127.0.0.1'), $this->Socket->host());
  118. $this->assertEqual($this->Socket->lastError(), null);
  119. $this->assertTrue(in_array('127.0.0.1', $this->Socket->addresses()));
  120. }
  121. /**
  122. * testSocketWriting method
  123. *
  124. * @access public
  125. * @return void
  126. */
  127. function testSocketWriting() {
  128. $request = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n";
  129. $this->assertTrue($this->Socket->write($request));
  130. }
  131. /**
  132. * testSocketReading method
  133. *
  134. * @access public
  135. * @return void
  136. */
  137. function testSocketReading() {
  138. $this->Socket = new CakeSocket(array('timeout' => 5));
  139. $this->Socket->connect();
  140. $this->assertEqual($this->Socket->read(26), null);
  141. }
  142. /**
  143. * testLastError method
  144. *
  145. * @access public
  146. * @return void
  147. */
  148. function testLastError() {
  149. $this->Socket = new CakeSocket();
  150. $this->Socket->setLastError(4, 'some error here');
  151. $this->assertEqual($this->Socket->lastError(), '4: some error here');
  152. }
  153. /**
  154. * testReset method
  155. *
  156. * @access public
  157. * @return void
  158. */
  159. function testReset() {
  160. $config = array(
  161. 'persistent' => true,
  162. 'host' => '127.0.0.1',
  163. 'protocol' => 'udp',
  164. 'port' => 80,
  165. 'timeout' => 20
  166. );
  167. $anotherSocket = new CakeSocket($config);
  168. $anotherSocket->reset();
  169. $this->assertEqual(array(), $anotherSocket->config);
  170. }
  171. }
  172. ?>