123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Socket\Test\Unit;
  36. use Hoa\Socket\Client as SUT;
  37. use Hoa\Stream;
  38. use Hoa\Test;
  39. /**
  40. * Class \Hoa\Socket\Test\Unit\Client.
  41. *
  42. * Test suite for the client object.
  43. *
  44. * @copyright Copyright © 2007-2017 Hoa community
  45. * @license New BSD License
  46. */
  47. class Client extends Test\Unit\Suite
  48. {
  49. public function case_is_a_connection()
  50. {
  51. $this
  52. ->given($this->mockGenerator->orphanize('__construct'))
  53. ->when($result = new \Mock\Hoa\Socket\Client())
  54. ->then
  55. ->object($result)
  56. ->isInstanceOf('Hoa\Socket\Connection');
  57. }
  58. public function case_constructor()
  59. {
  60. $this
  61. ->given(
  62. $socket = 'tcp://hoa-project.net:80',
  63. $timeout = 42,
  64. $flag = SUT::ASYNCHRONOUS,
  65. $context = 'foo'
  66. )
  67. ->when($result = new SUT($socket, $timeout, $flag, $context))
  68. ->then
  69. ->let($_socket = $result->getSocket())
  70. ->object($_socket)
  71. ->isInstanceOf('Hoa\Socket\Socket')
  72. ->integer($_socket->getAddressType())
  73. ->isEqualTo($_socket::ADDRESS_DOMAIN)
  74. ->string($_socket->getTransport())
  75. ->isEqualTo('tcp')
  76. ->string($_socket->getAddress())
  77. ->isEqualTo('hoa-project.net')
  78. ->integer($_socket->getPort())
  79. ->isEqualTo(80)
  80. ->boolean($_socket->isSecured())
  81. ->isFalse()
  82. ->integer($result->getTimeout())
  83. ->isEqualTo($timeout)
  84. ->integer($result->getFlag())
  85. ->isEqualTo($flag | SUT::CONNECT)
  86. ->string($result->getContext())
  87. ->isEqualTo($context);
  88. }
  89. public function case_open_cannot_join()
  90. {
  91. $self = $this;
  92. $this
  93. ->given(
  94. $this->mockGenerator->orphanize('__construct'),
  95. $client = new \Mock\Hoa\Socket\Client(),
  96. $streamName = 'foobar',
  97. $timeout = 42,
  98. $flag = SUT::CONNECT,
  99. $this->calling($client)->getTimeout = $timeout,
  100. $this->calling($client)->getFlag = $flag,
  101. $this->function->stream_socket_client = function ($_streamName, &$_errno, &$_errstr, $_timeout, $_flag) use ($self, &$called, $streamName, $timeout, $flag) {
  102. $called = true;
  103. $_errno = 0;
  104. $self
  105. ->string($_streamName)
  106. ->isEqualTo($streamName)
  107. ->integer($_timeout)
  108. ->isEqualTo($timeout)
  109. ->integer($_flag)
  110. ->isEqualTo($flag);
  111. return false;
  112. }
  113. )
  114. ->exception(function () use ($self, $client, $streamName) {
  115. $self->invoke($client)->_open($streamName);
  116. })
  117. ->isInstanceOf('Hoa\Socket\Exception')
  118. ->hasCode(0)
  119. ->boolean($called)
  120. ->isTrue();
  121. }
  122. public function case_open_returns_an_error()
  123. {
  124. $self = $this;
  125. $this
  126. ->given(
  127. $this->mockGenerator->orphanize('__construct'),
  128. $client = new \Mock\Hoa\Socket\Client(),
  129. $streamName = 'foobar',
  130. $timeout = 42,
  131. $flag = SUT::CONNECT,
  132. $this->calling($client)->getTimeout = $timeout,
  133. $this->calling($client)->getFlag = $flag,
  134. $this->function->stream_socket_client = function ($_streamName, &$_errno, &$_errstr, $_timeout, $_flag) use ($self, &$called, $streamName, $timeout, $flag) {
  135. $called = true;
  136. $_errno = 1;
  137. $self
  138. ->string($_streamName)
  139. ->isEqualTo($streamName)
  140. ->integer($_timeout)
  141. ->isEqualTo($timeout)
  142. ->integer($_flag)
  143. ->isEqualTo($flag);
  144. return false;
  145. }
  146. )
  147. ->exception(function () use ($self, $client, $streamName) {
  148. $self->invoke($client)->_open($streamName);
  149. })
  150. ->isInstanceOf('Hoa\Socket\Exception')
  151. ->hasCode(1)
  152. ->boolean($called)
  153. ->isTrue();
  154. }
  155. public function case_open()
  156. {
  157. $self = $this;
  158. $this
  159. ->given(
  160. $this->mockGenerator->orphanize('__construct'),
  161. $client = new \Mock\Hoa\Socket\Client(),
  162. $streamName = 'foobar',
  163. $timeout = 42,
  164. $flag = SUT::CONNECT,
  165. $oldStack = $this->invoke($client)->getStack(),
  166. $oldNodes = $client->getNodes(),
  167. $this->calling($client)->getTimeout = $timeout,
  168. $this->calling($client)->getFlag = $flag,
  169. $this->function->stream_socket_client = function ($_streamName, &$_errno, &$_errstr, $_timeout, $_flag) use ($self, &$called, $streamName, $timeout, $flag) {
  170. $called = true;
  171. $self
  172. ->string($_streamName)
  173. ->isEqualTo($streamName)
  174. ->integer($_timeout)
  175. ->isEqualTo($timeout)
  176. ->integer($_flag)
  177. ->isEqualTo($flag);
  178. return fopen(__FILE__, 'r');
  179. }
  180. )
  181. ->when($result = $this->invoke($client)->_open($streamName))
  182. ->then
  183. ->resource($result)
  184. ->let($stack = $this->invoke($client)->getStack())
  185. ->integer(count($stack))
  186. ->isEqualTo(count($oldStack) + 1)
  187. ->isEqualTo(1)
  188. ->array($stack)
  189. ->resource($stack[0])
  190. ->isIdenticalTo($result)
  191. ->let($node = $client->getCurrentNode())
  192. ->object($node)
  193. ->isInstanceOf('Hoa\Socket\Node')
  194. ->string($node->getId())
  195. ->isEqualTo($this->invoke($client)->getNodeId($result))
  196. ->resource($node->getSocket())
  197. ->isIdenticalTo($result)
  198. ->object($node->getConnection())
  199. ->isIdenticalTo($client)
  200. ->let($nodes = $client->getNodes())
  201. ->integer(count($nodes))
  202. ->isEqualTo(count($oldNodes) + 1)
  203. ->array($nodes)
  204. ->object($nodes[$node->getId()])
  205. ->isIdenticalTo($node);
  206. }
  207. public function case_open_with_context()
  208. {
  209. $self = $this;
  210. $this
  211. ->given(
  212. $this->mockGenerator->orphanize('__construct'),
  213. $client = new \Mock\Hoa\Socket\Client(),
  214. $streamName = 'foobar',
  215. $context = Stream\Context::getInstance('foo'),
  216. $timeout = 42,
  217. $flag = SUT::CONNECT,
  218. $oldStack = $this->invoke($client)->getStack(),
  219. $oldNodes = $client->getNodes(),
  220. $this->calling($client)->getTimeout = $timeout,
  221. $this->calling($client)->getFlag = $flag,
  222. $this->function->stream_socket_client = function ($_streamName, &$_errno, &$_errstr, $_timeout, $_flag, $_context) use ($self, &$called, $streamName, $timeout, $flag, $context) {
  223. $called = true;
  224. $self
  225. ->string($_streamName)
  226. ->isEqualTo($streamName)
  227. ->integer($_timeout)
  228. ->isEqualTo($timeout)
  229. ->integer($_flag)
  230. ->isEqualTo($flag)
  231. ->resource($_context)
  232. ->isStreamContext()
  233. ->isIdenticalTo($context->getContext());
  234. return fopen(__FILE__, 'r');
  235. }
  236. )
  237. ->when($result = $this->invoke($client)->_open($streamName, $context))
  238. ->then
  239. ->resource($result)
  240. ->let($stack = $this->invoke($client)->getStack())
  241. ->integer(count($stack))
  242. ->isEqualTo(count($oldStack) + 1)
  243. ->isEqualTo(1)
  244. ->array($stack)
  245. ->resource($stack[0])
  246. ->isIdenticalTo($result)
  247. ->let($node = $client->getCurrentNode())
  248. ->object($node)
  249. ->isInstanceOf('Hoa\Socket\Node')
  250. ->string($node->getId())
  251. ->isEqualTo($this->invoke($client)->getNodeId($result))
  252. ->resource($node->getSocket())
  253. ->isIdenticalTo($result)
  254. ->object($node->getConnection())
  255. ->isIdenticalTo($client)
  256. ->let($nodes = $client->getNodes())
  257. ->integer(count($nodes))
  258. ->isEqualTo(count($oldNodes) + 1)
  259. ->array($nodes)
  260. ->object($nodes[$node->getId()])
  261. ->isIdenticalTo($node);
  262. }
  263. public function case_close_persistent_connection()
  264. {
  265. $this
  266. ->given(
  267. $this->mockGenerator->orphanize('__construct'),
  268. $client = new \Mock\Hoa\Socket\Client(),
  269. $this->calling($client)->isPersistent = true
  270. )
  271. ->when($result = $this->invoke($client)->_close())
  272. ->then
  273. ->boolean($result)
  274. ->isFalse();
  275. }
  276. public function case_close()
  277. {
  278. $self = $this;
  279. $this
  280. ->given(
  281. $this->mockGenerator->orphanize('__construct'),
  282. $client = new \Mock\Hoa\Socket\Client(),
  283. $stream = 'foo',
  284. $this->calling($client)->isPersistent = false,
  285. $this->calling($client)->getStream = $stream,
  286. $this->function->fclose = function ($_stream) use ($self, &$called, $stream) {
  287. $called = true;
  288. $self
  289. ->string($_stream)
  290. ->isEqualTo($stream);
  291. return true;
  292. }
  293. )
  294. ->when($result = $this->invoke($client)->_close())
  295. ->then
  296. ->boolean($result)
  297. ->isTrue()
  298. ->boolean($called)
  299. ->isTrue();
  300. }
  301. public function case_select()
  302. {
  303. $self = $this;
  304. $this
  305. ->given(
  306. $this->mockGenerator->orphanize('__construct'),
  307. $client = new \Mock\Hoa\Socket\Client(),
  308. $timeout = 42,
  309. $this->calling($client)->getTimeout = $timeout,
  310. $this->function->stream_select = function (&$_read, &$_write, &$_except, $_timeout, $_ttimeout) use ($self, &$called, $timeout) {
  311. $called = true;
  312. $self
  313. ->array($_read)
  314. ->isEmpty()
  315. ->variable($_write)
  316. ->isNull()
  317. ->variable($_except)
  318. ->isNull()
  319. ->integer($_timeout)
  320. ->isEqualTo($timeout)
  321. ->integer($_ttimeout)
  322. ->isEqualTo(0);
  323. $_read = ['a', 'b', 'c'];
  324. }
  325. )
  326. ->when($result = $client->select())
  327. ->then
  328. ->object($result)
  329. ->isIdenticalTo($client)
  330. ->array($this->invoke($client)->getIteratorValues())
  331. ->isEqualTo(['a', 'b', 'c'])
  332. ->boolean($called)
  333. ->isTrue();
  334. }
  335. public function case_consider_not_a_client()
  336. {
  337. $this
  338. ->given(
  339. $this->mockGenerator->orphanize('__construct'),
  340. $client = new \Mock\Hoa\Socket\Client(),
  341. $this->mockGenerator->orphanize('__construct'),
  342. $other = new \Mock\Hoa\Socket\Server()
  343. )
  344. ->exception(function () use ($client, $other) {
  345. $client->consider($other);
  346. })
  347. ->isInstanceOf('Hoa\Socket\Exception');
  348. }
  349. public function case_consider_disconnected_client()
  350. {
  351. $this->_case_consider(true);
  352. }
  353. public function case_consider()
  354. {
  355. $this->_case_consider(false);
  356. }
  357. protected function _case_consider($disconnected)
  358. {
  359. return
  360. $this
  361. ->given(
  362. $this->mockGenerator->orphanize('__construct'),
  363. $client = new \Mock\Hoa\Socket\Client(),
  364. $other = new \Mock\Hoa\Socket\Client(),
  365. $this->mockGenerator->orphanize('__construct'),
  366. $node = new \Mock\Hoa\Socket\Node(),
  367. $this->calling($node)->getSocket = 42,
  368. $this->calling($node)->getId = 'foo',
  369. $this->calling($other)->isDisconnected = $disconnected,
  370. $this->calling($other)->getCurrentNode = $node,
  371. $this->calling($other)->connect = function () use (&$called) {
  372. $called = true;
  373. },
  374. $oldStack = $this->invoke($client)->getStack(),
  375. $oldNodes = $client->getNodes()
  376. )
  377. ->when($result = $client->consider($other))
  378. ->then
  379. ->object($result)
  380. ->isIdenticalTo($client)
  381. ->variable($called)
  382. ->isEqualTo($disconnected ?: null)
  383. ->let($stack = $this->invoke($client)->getStack())
  384. ->array($stack)
  385. ->hasSize(count($oldStack) + 1)
  386. ->variable($stack[0])
  387. ->isEqualTo($node->getSocket())
  388. ->let($nodes = $client->getNodes())
  389. ->array($nodes)
  390. ->hasSize(count($oldNodes) + 1)
  391. ->object($nodes[$node->getId()])
  392. ->isEqualTo($node);
  393. }
  394. public function case_is()
  395. {
  396. $this
  397. ->_case_is('foo', 'foo')
  398. ->isTrue();
  399. }
  400. public function case_is_not()
  401. {
  402. $this
  403. ->_case_is('foo', 'bar')
  404. ->isFalse();
  405. }
  406. protected function _case_is($streamA, $streamB)
  407. {
  408. return
  409. $this
  410. ->given(
  411. $this->mockGenerator->orphanize('__construct'),
  412. $client = new \Mock\Hoa\Socket\Client(),
  413. $this->mockGenerator->orphanize('__construct'),
  414. $server = new \Mock\Hoa\Socket\Server(),
  415. $this->calling($client)->getStream = $streamA,
  416. $this->calling($server)->getStream = $streamB
  417. )
  418. ->when($result = $client->is($server))
  419. ->then
  420. ->boolean($result);
  421. }
  422. public function case_is_connected()
  423. {
  424. $this
  425. ->_case_flag_is(SUT::CONNECT, 'isConnected');
  426. }
  427. public function case_is_asynchronous()
  428. {
  429. $this
  430. ->_case_flag_is(SUT::ASYNCHRONOUS, 'isAsynchronous');
  431. }
  432. public function case_is_persistent()
  433. {
  434. $this
  435. ->_case_flag_is(SUT::PERSISTENT, 'isPersistent');
  436. }
  437. protected function _case_flag_is($flag, $method)
  438. {
  439. return
  440. $this
  441. ->given(
  442. $socket = 'tcp://hoa-project.net:80',
  443. $timeout = 42
  444. )
  445. ->when($result = new SUT($socket, $timeout, $flag))
  446. ->then
  447. ->boolean($result->$method())
  448. ->isTrue();
  449. }
  450. }