Connection.php 49KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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\Websocket\Test\Unit;
  36. use Hoa\Event;
  37. use Hoa\Socket;
  38. use Hoa\Test;
  39. use Hoa\Websocket;
  40. use Mock\Hoa\Websocket\Connection as SUT;
  41. /**
  42. * Class \Hoa\Websocket\Test\Unit\Connection.
  43. *
  44. * Test suite for the WebSocket connection class.
  45. *
  46. * @copyright Copyright © 2007-2017 Hoa community
  47. * @license New BSD License
  48. */
  49. class Connection extends Test\Unit\Suite
  50. {
  51. public function case_opcodes()
  52. {
  53. $this
  54. ->then
  55. ->integer(SUT::OPCODE_CONTINUATION_FRAME)
  56. ->isEqualTo(0x0)
  57. ->integer(SUT::OPCODE_TEXT_FRAME)
  58. ->isEqualTo(0x1)
  59. ->integer(SUT::OPCODE_BINARY_FRAME)
  60. ->isEqualTo(0x2)
  61. ->integer(SUT::OPCODE_CONNECTION_CLOSE)
  62. ->isEqualTo(0x8)
  63. ->integer(SUT::OPCODE_PING)
  64. ->isEqualTo(0x9)
  65. ->integer(SUT::OPCODE_PONG)
  66. ->isEqualTo(0xa);
  67. }
  68. public function case_close_codes()
  69. {
  70. $this
  71. ->then
  72. ->integer(SUT::CLOSE_NORMAL)
  73. ->isEqualTo(1000)
  74. ->integer(SUT::CLOSE_GOING_AWAY)
  75. ->isEqualTo(1001)
  76. ->integer(SUT::CLOSE_PROTOCOL_ERROR)
  77. ->isEqualTo(1002)
  78. ->integer(SUT::CLOSE_DATA_ERROR)
  79. ->isEqualTo(1003)
  80. ->integer(SUT::CLOSE_STATUS_ERROR)
  81. ->isEqualTo(1005)
  82. ->integer(SUT::CLOSE_ABNORMAL)
  83. ->isEqualTo(1006)
  84. ->integer(SUT::CLOSE_MESSAGE_ERROR)
  85. ->isEqualTo(1007)
  86. ->integer(SUT::CLOSE_POLICY_ERROR)
  87. ->isEqualTo(1008)
  88. ->integer(SUT::CLOSE_MESSAGE_TOO_BIG)
  89. ->isEqualTo(1009)
  90. ->integer(SUT::CLOSE_EXTENSION_MISSING)
  91. ->isEqualTo(1010)
  92. ->integer(SUT::CLOSE_SERVER_ERROR)
  93. ->isEqualTo(1011)
  94. ->integer(SUT::CLOSE_TLS)
  95. ->isEqualTo(1015);
  96. }
  97. public function case_constructor()
  98. {
  99. $this
  100. ->given($socket = new Socket\Client('tcp://*:1234'))
  101. ->when($result = new SUT($socket))
  102. ->then
  103. ->object($result->getConnection())
  104. ->isIdenticalTo($socket)
  105. ->string($socket->getNodeName())
  106. ->isEqualTo(Websocket\Node::class)
  107. ->let($listener = $this->invoke($result)->getListener())
  108. ->object($listener)
  109. ->isInstanceOf(Event\Listener::class)
  110. ->boolean($listener->listenerExists('open'))
  111. ->isTrue()
  112. ->boolean($listener->listenerExists('message'))
  113. ->isTrue()
  114. ->boolean($listener->listenerExists('binary-message'))
  115. ->isTrue()
  116. ->boolean($listener->listenerExists('ping'))
  117. ->isTrue()
  118. ->boolean($listener->listenerExists('close'))
  119. ->isTrue()
  120. ->boolean($listener->listenerExists('error'))
  121. ->isTrue();
  122. }
  123. public function case_run_do_handshake()
  124. {
  125. $self = $this;
  126. $this
  127. ->given(
  128. $socket = new Socket\Client('tcp://*:1234'),
  129. $connection = new SUT($socket),
  130. $this->mockGenerator->orphanize('__construct'),
  131. $node = new \Mock\Hoa\Websocket\Node(),
  132. $connection->on(
  133. 'open',
  134. function (Event\Bucket $bucket) use (&$calledA, $self) {
  135. $calledA = true;
  136. $self
  137. ->variable($bucket->getData())
  138. ->isNull();
  139. return;
  140. }
  141. ),
  142. $this->calling($node)->getHandshake = FAILED,
  143. $this->calling($connection)->doHandshake = function () use (&$calledB) {
  144. $calledB = true;
  145. return;
  146. }
  147. )
  148. ->when($result = $this->invoke($connection)->_run($node))
  149. ->then
  150. ->variable($result)
  151. ->isNull()
  152. ->boolean($calledA)
  153. ->isTrue()
  154. ->boolean($calledB)
  155. ->isTrue();
  156. }
  157. public function case_run_cannot_read_the_frame()
  158. {
  159. $self = $this;
  160. $this
  161. ->given(
  162. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  163. $connection = new SUT($socket),
  164. $this->mockGenerator->orphanize('__construct'),
  165. $node = new \Mock\Hoa\Websocket\Node(),
  166. $node->setProtocolImplementation(new Websocket\Protocol\Rfc6455($socket)),
  167. $this->calling($socket)->read[1] =
  168. chr(
  169. (0x1 << 7)
  170. | (0x1 << 6)
  171. | (0x0 << 5)
  172. | (0x0 << 4)
  173. | Websocket\Connection::OPCODE_TEXT_FRAME
  174. ),
  175. $this->calling($socket)->read[2] =
  176. chr(
  177. (0x1 << 7)
  178. | 42
  179. ),
  180. $this->calling($socket)->getCurrentNode = $node,
  181. $this->calling($node)->getHandshake = SUCCEED,
  182. $this->calling($connection)->close = function ($_code, $_reason) use (&$called, $self) {
  183. $called = true;
  184. $self
  185. ->integer($_code)
  186. ->isEqualTo(SUT::CLOSE_PROTOCOL_ERROR)
  187. ->string($_reason)
  188. ->isNotEmpty();
  189. return;
  190. }
  191. )
  192. ->when($result = $this->invoke($connection)->_run($node))
  193. ->then
  194. ->variable($result)
  195. ->isNull()
  196. ->boolean($called)
  197. ->isTrue();
  198. }
  199. public function case_run_client_messages_must_be_masked()
  200. {
  201. $self = $this;
  202. $this
  203. ->given(
  204. $socket = new \Mock\Hoa\Socket\Server('tcp://*:1234'),
  205. $connection = new \Mock\Hoa\Websocket\Server($socket),
  206. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  207. $this->mockGenerator->orphanize('__construct'),
  208. $node = new \Mock\Hoa\Websocket\Node(),
  209. $node->setProtocolImplementation($protocol),
  210. $this->calling($node)->getHandshake = SUCCEED,
  211. $this->calling($protocol)->readFrame = [
  212. 'fin' => 0x1,
  213. 'rsv1' => 0x0,
  214. 'rsv2' => 0x0,
  215. 'rsv3' => 0x0,
  216. 'opcode' => SUT::OPCODE_TEXT_FRAME,
  217. 'mask' => 0x0,
  218. 'length' => 6,
  219. 'message' => 'foobar'
  220. ],
  221. $this->calling($connection)->close = function ($_code, $_reason) use (&$called, $self) {
  222. $called = true;
  223. $self
  224. ->integer($_code)
  225. ->isEqualTo(SUT::CLOSE_MESSAGE_ERROR)
  226. ->string($_reason)
  227. ->isEqualTo('All messages from the client must be masked.');
  228. return;
  229. }
  230. )
  231. ->when($result = $this->invoke($connection)->_run($node))
  232. ->then
  233. ->variable($result)
  234. ->isNull()
  235. ->boolean($called)
  236. ->isTrue();
  237. }
  238. public function case_run_ping_opcode()
  239. {
  240. $self = $this;
  241. $this
  242. ->given(
  243. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  244. $connection = new SUT($socket),
  245. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  246. $this->mockGenerator->orphanize('__construct'),
  247. $node = new \Mock\Hoa\Websocket\Node(),
  248. $node->setProtocolImplementation($protocol),
  249. $this->calling($node)->getHandshake = SUCCEED,
  250. $this->calling($protocol)->readFrame = [
  251. 'fin' => 0x1,
  252. 'rsv1' => 0x0,
  253. 'rsv2' => 0x0,
  254. 'rsv3' => 0x0,
  255. 'opcode' => SUT::OPCODE_PING,
  256. 'mask' => 0x1,
  257. 'length' => 6,
  258. 'message' => 'foobar'
  259. ],
  260. $this->calling($protocol)->writeFrame = function ($_message, $_opcode, $_mask) use (&$calledA, $self) {
  261. $calledA = true;
  262. $self
  263. ->string($_message)
  264. ->isEqualTo('foobar')
  265. ->integer($_opcode)
  266. ->isEqualTo(SUT::OPCODE_PONG)
  267. ->boolean($_mask)
  268. ->isTrue();
  269. return;
  270. },
  271. $connection->on(
  272. 'ping',
  273. function (Event\Bucket $bucket) use (&$calledB, $self) {
  274. $calledB = true;
  275. $self
  276. ->array($bucket->getData())
  277. ->isEqualTo([
  278. 'message' => 'foobar'
  279. ]);
  280. return;
  281. }
  282. )
  283. )
  284. ->when($result = $this->invoke($connection)->_run($node))
  285. ->then
  286. ->variable($result)
  287. ->isNull()
  288. ->boolean($calledA)
  289. ->isTrue()
  290. ->boolean($calledB)
  291. ->isTrue();
  292. }
  293. public function case_run_ping_opcode_not_fin()
  294. {
  295. return $this->_case_run_ping_opcode_with_invalid_frame([
  296. 'fin' => 0x0,
  297. 'rsv1' => 0x0,
  298. 'rsv2' => 0x0,
  299. 'rsv3' => 0x0,
  300. 'opcode' => SUT::OPCODE_PING,
  301. 'mask' => 0x1,
  302. 'length' => 6,
  303. 'message' => 'foobar'
  304. ]);
  305. }
  306. public function case_run_ping_opcode_with_length_too_big()
  307. {
  308. return $this->_case_run_ping_opcode_with_invalid_frame([
  309. 'fin' => 0x1,
  310. 'rsv1' => 0x0,
  311. 'rsv2' => 0x0,
  312. 'rsv3' => 0x0,
  313. 'opcode' => SUT::OPCODE_PING,
  314. 'mask' => 0x1,
  315. 'length' => 0x7e,
  316. 'message' => 'foobar…'
  317. ]);
  318. }
  319. protected function _case_run_ping_opcode_with_invalid_frame(array $frame)
  320. {
  321. $self = $this;
  322. $this
  323. ->given(
  324. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  325. $connection = new SUT($socket),
  326. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  327. $this->mockGenerator->orphanize('__construct'),
  328. $node = new \Mock\Hoa\Websocket\Node(),
  329. $node->setProtocolImplementation($protocol),
  330. $this->calling($node)->getHandshake = SUCCEED,
  331. $this->calling($protocol)->readFrame = $frame,
  332. $this->calling($protocol)->writeFrame = function ($_message, $_opcode, $_mask) use (&$calledA) {
  333. $calledA = true;
  334. return;
  335. },
  336. $this->calling($connection)->close = function ($_code, $_reason) use (&$calledB, $self) {
  337. $calledB = true;
  338. $self
  339. ->integer($_code)
  340. ->isEqualTo(SUT::CLOSE_PROTOCOL_ERROR)
  341. ->variable($_reason)
  342. ->isNull();
  343. return;
  344. },
  345. $connection->on(
  346. 'ping',
  347. function (Event\Bucket $bucket) use (&$calledC) {
  348. $calledC = true;
  349. return;
  350. }
  351. )
  352. )
  353. ->when($result = $this->invoke($connection)->_run($node))
  354. ->then
  355. ->variable($result)
  356. ->isNull()
  357. ->variable($calledA)
  358. ->isNull()
  359. ->boolean($calledB)
  360. ->isTrue()
  361. ->variable($calledC)
  362. ->isNull();
  363. }
  364. public function case_run_pong_opcode()
  365. {
  366. $this
  367. ->given(
  368. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  369. $connection = new SUT($socket),
  370. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  371. $this->mockGenerator->orphanize('__construct'),
  372. $node = new \Mock\Hoa\Websocket\Node(),
  373. $node->setProtocolImplementation($protocol),
  374. $this->calling($node)->getHandshake = SUCCEED,
  375. $this->calling($protocol)->readFrame = [
  376. 'fin' => 0x1,
  377. 'rsv1' => 0x0,
  378. 'rsv2' => 0x0,
  379. 'rsv3' => 0x0,
  380. 'opcode' => SUT::OPCODE_PONG,
  381. 'mask' => 0x1,
  382. 'length' => 6,
  383. 'message' => 'foobar'
  384. ],
  385. $this->calling($protocol)->writeFrame = function ($_message, $_opcode, $_mask) use (&$called) {
  386. $called = true;
  387. return;
  388. }
  389. )
  390. ->when($result = $this->invoke($connection)->_run($node))
  391. ->then
  392. ->variable($result)
  393. ->isNull()
  394. ->variable($called)
  395. ->isNull();
  396. }
  397. public function case_run_pong_opcode_not_fin()
  398. {
  399. $self = $this;
  400. $this
  401. ->given(
  402. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  403. $connection = new SUT($socket),
  404. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  405. $this->mockGenerator->orphanize('__construct'),
  406. $node = new \Mock\Hoa\Websocket\Node(),
  407. $node->setProtocolImplementation($protocol),
  408. $this->calling($node)->getHandshake = SUCCEED,
  409. $this->calling($protocol)->readFrame = [
  410. 'fin' => 0x0,
  411. 'rsv1' => 0x0,
  412. 'rsv2' => 0x0,
  413. 'rsv3' => 0x0,
  414. 'opcode' => SUT::OPCODE_PONG,
  415. 'mask' => 0x1,
  416. 'length' => 6,
  417. 'message' => 'foobar'
  418. ],
  419. $this->calling($protocol)->writeFrame = function ($_message, $_opcode, $_mask) use (&$calledA) {
  420. $calledA = true;
  421. return;
  422. },
  423. $this->calling($connection)->close = function ($_code, $_reason) use (&$calledB, $self) {
  424. $calledB = true;
  425. $self
  426. ->integer($_code)
  427. ->isEqualTo(SUT::CLOSE_PROTOCOL_ERROR)
  428. ->variable($_reason)
  429. ->isNull();
  430. return;
  431. }
  432. )
  433. ->when($result = $this->invoke($connection)->_run($node))
  434. ->then
  435. ->variable($result)
  436. ->isNull()
  437. ->variable($calledA)
  438. ->isNull()
  439. ->boolean($calledB)
  440. ->isTrue();
  441. }
  442. public function case_run_text_frame_opcode_with_fragments()
  443. {
  444. return $this->_case_run_x_frame_opcode_with_fragments(SUT::OPCODE_TEXT_FRAME);
  445. }
  446. public function case_run_binary_frame_opcode_with_fragments()
  447. {
  448. return $this->_case_run_x_frame_opcode_with_fragments(SUT::OPCODE_BINARY_FRAME);
  449. }
  450. protected function _case_run_x_frame_opcode_with_fragments($opcode)
  451. {
  452. $self = $this;
  453. $this
  454. ->given(
  455. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  456. $connection = new SUT($socket),
  457. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  458. $this->mockGenerator->orphanize('__construct'),
  459. $node = new \Mock\Hoa\Websocket\Node(),
  460. $node->setProtocolImplementation($protocol),
  461. $this->calling($node)->getHandshake = SUCCEED,
  462. $this->calling($node)->getNumberOfFragments = 42,
  463. $this->calling($protocol)->readFrame = [
  464. 'fin' => 0x1,
  465. 'rsv1' => 0x0,
  466. 'rsv2' => 0x0,
  467. 'rsv3' => 0x0,
  468. 'opcode' => $opcode,
  469. 'mask' => 0x1,
  470. 'length' => 6,
  471. 'message' => 'foobar'
  472. ],
  473. $this->calling($protocol)->writeFrame = function ($_message, $_opcode, $_mask) use (&$calledA) {
  474. $calledA = true;
  475. return;
  476. },
  477. $this->calling($connection)->close = function ($_code, $_reason) use (&$calledB, $self) {
  478. $calledB = true;
  479. $self
  480. ->integer($_code)
  481. ->isEqualTo(SUT::CLOSE_PROTOCOL_ERROR)
  482. ->variable($_reason)
  483. ->isNull();
  484. return;
  485. }
  486. )
  487. ->when($result = $this->invoke($connection)->_run($node))
  488. ->then
  489. ->variable($result)
  490. ->isNull()
  491. ->variable($calledA)
  492. ->isNull()
  493. ->boolean($calledB)
  494. ->isTrue();
  495. }
  496. public function case_run_text_frame_opcode_with_invalid_message_encoding()
  497. {
  498. $self = $this;
  499. $this
  500. ->given(
  501. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  502. $connection = new SUT($socket),
  503. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  504. $this->mockGenerator->orphanize('__construct'),
  505. $node = new \Mock\Hoa\Websocket\Node(),
  506. $node->setProtocolImplementation($protocol),
  507. $this->calling($node)->getHandshake = SUCCEED,
  508. $this->calling($protocol)->readFrame = [
  509. 'fin' => 0x1,
  510. 'rsv1' => 0x0,
  511. 'rsv2' => 0x0,
  512. 'rsv3' => 0x0,
  513. 'opcode' => SUT::OPCODE_TEXT_FRAME,
  514. 'mask' => 0x1,
  515. 'length' => 6,
  516. 'message' => iconv('UTF-8', 'UTF-16', '😄')
  517. ],
  518. $this->calling($protocol)->writeFrame = function ($_message, $_opcode, $_mask) use (&$calledA) {
  519. $calledA = true;
  520. return;
  521. },
  522. $this->calling($connection)->close = function ($_code, $_reason) use (&$calledB, $self) {
  523. $calledB = true;
  524. $self
  525. ->integer($_code)
  526. ->isEqualTo(SUT::CLOSE_MESSAGE_ERROR)
  527. ->variable($_reason)
  528. ->isNull();
  529. return;
  530. }
  531. )
  532. ->when($result = $this->invoke($connection)->_run($node))
  533. ->then
  534. ->variable($result)
  535. ->isNull()
  536. ->variable($calledA)
  537. ->isNull()
  538. ->boolean($calledB)
  539. ->isTrue();
  540. }
  541. public function case_run_text_frame_opcode()
  542. {
  543. return $this->_case_run_x_frame_opcode(SUT::OPCODE_TEXT_FRAME);
  544. }
  545. public function case_run_binary_frame_opcode()
  546. {
  547. return $this->_case_run_x_frame_opcode(SUT::OPCODE_BINARY_FRAME);
  548. }
  549. protected function _case_run_x_frame_opcode($opcode)
  550. {
  551. $self = $this;
  552. $this
  553. ->given(
  554. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  555. $connection = new SUT($socket),
  556. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  557. $this->mockGenerator->orphanize('__construct'),
  558. $node = new \Mock\Hoa\Websocket\Node(),
  559. $node->setProtocolImplementation($protocol),
  560. $this->calling($node)->getHandshake = SUCCEED,
  561. $this->calling($protocol)->readFrame = [
  562. 'fin' => 0x1,
  563. 'rsv1' => 0x0,
  564. 'rsv2' => 0x0,
  565. 'rsv3' => 0x0,
  566. 'opcode' => $opcode,
  567. 'mask' => 0x1,
  568. 'length' => 6,
  569. 'message' => 'foobar'
  570. ],
  571. $this->calling($protocol)->writeFrame = function ($_message, $_opcode, $_mask) use (&$calledA) {
  572. $calledA = true;
  573. return;
  574. },
  575. $this->calling($connection)->close = function ($_code, $_reason) use (&$calledB) {
  576. $calledB = true;
  577. return;
  578. },
  579. $connection->on(
  580. SUT::OPCODE_TEXT_FRAME === $opcode ? 'message' : 'binary-message',
  581. function (Event\Bucket $bucket) use (&$calledC, $self) {
  582. $calledC = true;
  583. $self
  584. ->array($bucket->getData())
  585. ->isEqualTo([
  586. 'message' => 'foobar'
  587. ]);
  588. return;
  589. }
  590. )
  591. )
  592. ->when($result = $this->invoke($connection)->_run($node))
  593. ->then
  594. ->variable($result)
  595. ->isNull()
  596. ->variable($calledA)
  597. ->isNull()
  598. ->variable($calledB)
  599. ->isNull()
  600. ->boolean($calledC)
  601. ->isTrue();
  602. }
  603. public function case_run_text_frame_opcode_with_an_exception_from_the_listener()
  604. {
  605. return $this->_case_run_x_frame_opcode_with_an_exception_from_the_listener(SUT::OPCODE_TEXT_FRAME);
  606. }
  607. public function case_run_binary_frame_opcode_with_an_exception_from_the_listener()
  608. {
  609. return $this->_case_run_x_frame_opcode_with_an_exception_from_the_listener(SUT::OPCODE_BINARY_FRAME);
  610. }
  611. protected function _case_run_x_frame_opcode_with_an_exception_from_the_listener($opcode)
  612. {
  613. $self = $this;
  614. $this
  615. ->given(
  616. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  617. $connection = new SUT($socket),
  618. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  619. $this->mockGenerator->orphanize('__construct'),
  620. $node = new \Mock\Hoa\Websocket\Node(),
  621. $node->setProtocolImplementation($protocol),
  622. $this->calling($node)->getHandshake = SUCCEED,
  623. $this->calling($protocol)->readFrame = [
  624. 'fin' => 0x1,
  625. 'rsv1' => 0x0,
  626. 'rsv2' => 0x0,
  627. 'rsv3' => 0x0,
  628. 'opcode' => $opcode,
  629. 'mask' => 0x1,
  630. 'length' => 6,
  631. 'message' => 'foobar'
  632. ],
  633. $this->calling($protocol)->writeFrame = function ($_message, $_opcode, $_mask) use (&$calledA) {
  634. $calledA = true;
  635. return;
  636. },
  637. $this->calling($connection)->close = function ($_code, $_reason) use (&$calledB) {
  638. $calledB = true;
  639. return;
  640. },
  641. $connection->on(
  642. SUT::OPCODE_TEXT_FRAME === $opcode ? 'message' : 'binary-message',
  643. function (Event\Bucket $bucket) use (&$calledC, $self) {
  644. $calledC = true;
  645. $self
  646. ->array($bucket->getData())
  647. ->isEqualTo([
  648. 'message' => 'foobar'
  649. ]);
  650. throw new \RuntimeException('bang');
  651. }
  652. ),
  653. $connection->on(
  654. 'error',
  655. function (Event\Bucket $bucket) use (&$calledD, $self) {
  656. $calledD = true;
  657. $self
  658. ->let($data = $bucket->getData())
  659. ->array($data)
  660. ->hasSize(1)
  661. ->hasKey('exception')
  662. ->exception($data['exception'])
  663. ->isInstanceOf(\RuntimeException::class)
  664. ->hasMessage('bang');
  665. return;
  666. }
  667. )
  668. )
  669. ->when($result = $this->invoke($connection)->_run($node))
  670. ->then
  671. ->variable($result)
  672. ->isNull()
  673. ->variable($calledA)
  674. ->isNull()
  675. ->variable($calledB)
  676. ->isNull()
  677. ->boolean($calledC)
  678. ->isTrue()
  679. ->boolean($calledD)
  680. ->isTrue();
  681. }
  682. public function case_run_incomplete_text_frame_opcode()
  683. {
  684. return $this->_case_run_incomplete_x_frame_opcode(SUT::OPCODE_TEXT_FRAME);
  685. }
  686. public function case_run_incomplete_binary_frame_opcode()
  687. {
  688. return $this->_case_run_incomplete_x_frame_opcode(SUT::OPCODE_BINARY_FRAME);
  689. }
  690. protected function _case_run_incomplete_x_frame_opcode($opcode)
  691. {
  692. $this
  693. ->given(
  694. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  695. $connection = new SUT($socket),
  696. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  697. $this->mockGenerator->orphanize('__construct'),
  698. $node = new \Mock\Hoa\Websocket\Node(),
  699. $node->setProtocolImplementation($protocol),
  700. $this->calling($node)->getHandshake = SUCCEED,
  701. $this->calling($protocol)->readFrame = [
  702. 'fin' => 0x0,
  703. 'rsv1' => 0x0,
  704. 'rsv2' => 0x0,
  705. 'rsv3' => 0x0,
  706. 'opcode' => $opcode,
  707. 'mask' => 0x1,
  708. 'length' => 6,
  709. 'message' => 'foobar'
  710. ],
  711. $this->calling($protocol)->writeFrame = function ($_message, $_opcode, $_mask) use (&$calledA) {
  712. $calledA = true;
  713. return;
  714. },
  715. $this->calling($connection)->close = function ($_code, $_reason) use (&$calledB) {
  716. $calledB = true;
  717. return;
  718. },
  719. $connection->on(
  720. 'message',
  721. function (Event\Bucket $bucket) use (&$calledC) {
  722. $calledC = true;
  723. return;
  724. }
  725. )
  726. )
  727. ->when($result = $this->invoke($connection)->_run($node))
  728. ->then
  729. ->variable($result)
  730. ->isNull()
  731. ->variable($calledA)
  732. ->isNull()
  733. ->variable($calledB)
  734. ->isNull()
  735. ->variable($calledC)
  736. ->isNull()
  737. ->boolean($node->isMessageComplete())
  738. ->isFalse()
  739. ->integer($node->getNumberOfFragments())
  740. ->isEqualTo(1)
  741. ->string($node->getFragmentedMessage())
  742. ->isEqualTo('foobar')
  743. ->boolean($node->isBinary())
  744. ->isEqualTo(SUT::OPCODE_BINARY_FRAME === $opcode);
  745. }
  746. public function case_run_continuation_frame_opcode_with_fragments()
  747. {
  748. $self = $this;
  749. $this
  750. ->given(
  751. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  752. $connection = new SUT($socket),
  753. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  754. $this->mockGenerator->orphanize('__construct'),
  755. $node = new \Mock\Hoa\Websocket\Node(),
  756. $node->setProtocolImplementation($protocol),
  757. $this->calling($node)->getHandshake = SUCCEED,
  758. $this->calling($node)->getNumberOfFragments = 42,
  759. $this->calling($protocol)->readFrame = [
  760. 'fin' => 0x0,
  761. 'rsv1' => 0x0,
  762. 'rsv2' => 0x0,
  763. 'rsv3' => 0x0,
  764. 'opcode' => SUT::OPCODE_CONTINUATION_FRAME,
  765. 'mask' => 0x1,
  766. 'length' => 6,
  767. 'message' => 'foobar'
  768. ],
  769. $this->calling($connection)->close = function ($_code, $_reason) use (&$called) {
  770. $called = true;
  771. $self
  772. ->integer($_code)
  773. ->isEqualTo(SUT::CLOSE_PROTOCOL_ERROR)
  774. ->variable($_reason)
  775. ->isNull();
  776. return;
  777. }
  778. )
  779. ->when($result = $this->invoke($connection)->_run($node))
  780. ->then
  781. ->variable($result)
  782. ->isNull()
  783. ->variable($called)
  784. ->isNull();
  785. }
  786. public function case_run_text_frame_and_continuation_frame_opcodes()
  787. {
  788. return $this->_case_run_x_frame_and_continuation_frame_opcodes(SUT::OPCODE_TEXT_FRAME);
  789. }
  790. public function case_run_binary_frame_and_continuation_frame_opcodes()
  791. {
  792. return $this->_case_run_x_frame_and_continuation_frame_opcodes(SUT::OPCODE_BINARY_FRAME);
  793. }
  794. protected function _case_run_x_frame_and_continuation_frame_opcodes($opcode)
  795. {
  796. $self = $this;
  797. $this
  798. ->given(
  799. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  800. $connection = new SUT($socket),
  801. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  802. $this->mockGenerator->orphanize('__construct'),
  803. $node = new \Mock\Hoa\Websocket\Node(),
  804. $node->setProtocolImplementation($protocol),
  805. $this->calling($node)->getHandshake = SUCCEED,
  806. $this->calling($protocol)->readFrame[1] = [
  807. 'fin' => 0x0,
  808. 'rsv1' => 0x0,
  809. 'rsv2' => 0x0,
  810. 'rsv3' => 0x0,
  811. 'opcode' => $opcode,
  812. 'mask' => 0x1,
  813. 'length' => 3,
  814. 'message' => 'foo'
  815. ],
  816. $this->calling($protocol)->readFrame[2] = [
  817. 'fin' => 0x0,
  818. 'rsv1' => 0x0,
  819. 'rsv2' => 0x0,
  820. 'rsv3' => 0x0,
  821. 'opcode' => SUT::OPCODE_CONTINUATION_FRAME,
  822. 'mask' => 0x1,
  823. 'length' => 3,
  824. 'message' => 'bar'
  825. ],
  826. $this->calling($protocol)->readFrame[3] = [
  827. 'fin' => 0x1,
  828. 'rsv1' => 0x0,
  829. 'rsv2' => 0x0,
  830. 'rsv3' => 0x0,
  831. 'opcode' => SUT::OPCODE_CONTINUATION_FRAME,
  832. 'mask' => 0x1,
  833. 'length' => 3,
  834. 'message' => 'baz'
  835. ],
  836. $this->calling($socket)->getCurrentNode = $node,
  837. $connection->on(
  838. SUT::OPCODE_TEXT_FRAME === $opcode ? 'message' : 'binary-message',
  839. function (Event\Bucket $bucket) use (&$called, $self) {
  840. $called = true;
  841. $self
  842. ->array($bucket->getData())
  843. ->isEqualTo([
  844. 'message' => 'foobarbaz'
  845. ]);
  846. return;
  847. }
  848. )
  849. )
  850. ->when($result = $this->invoke($connection)->_run($node))
  851. ->then
  852. ->integer($node->getNumberOfFragments())
  853. ->isEqualTo(1)
  854. ->string($node->getFragmentedMessage())
  855. ->isEqualTo('foo')
  856. ->boolean($node->isMessageComplete())
  857. ->isFalse()
  858. ->variable($called)
  859. ->isNull()
  860. ->when($result = $this->invoke($connection)->_run($node))
  861. ->then
  862. ->integer($node->getNumberOfFragments())
  863. ->isEqualTo(2)
  864. ->string($node->getFragmentedMessage())
  865. ->isEqualTo('foobar')
  866. ->boolean($node->isMessageComplete())
  867. ->isFalse()
  868. ->variable($called)
  869. ->isNull()
  870. ->when($result = $this->invoke($connection)->_run($node))
  871. ->then
  872. ->integer($node->getNumberOfFragments())
  873. ->isEqualTo(0)
  874. ->variable($node->getFragmentedMessage())
  875. ->isNull()
  876. ->boolean($node->isMessageComplete())
  877. ->isTrue()
  878. ->boolean($called)
  879. ->isTrue();
  880. }
  881. public function case_run_text_frame_and_continuation_frame_opcodes_with_an_exception_from_the_listener()
  882. {
  883. return $this->_case_run_x_frame_and_continuation_frame_opcodes_with_an_exception_from_the_listener(SUT::OPCODE_TEXT_FRAME);
  884. }
  885. public function case_run_binary_frame_and_continuation_frame_opcodes_with_an_exception_from_the_listener()
  886. {
  887. return $this->_case_run_x_frame_and_continuation_frame_opcodes_with_an_exception_from_the_listener(SUT::OPCODE_BINARY_FRAME);
  888. }
  889. protected function _case_run_x_frame_and_continuation_frame_opcodes_with_an_exception_from_the_listener($opcode)
  890. {
  891. $self = $this;
  892. $this
  893. ->given(
  894. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  895. $connection = new SUT($socket),
  896. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  897. $this->mockGenerator->orphanize('__construct'),
  898. $node = new \Mock\Hoa\Websocket\Node(),
  899. $node->setProtocolImplementation($protocol),
  900. $this->calling($node)->getHandshake = SUCCEED,
  901. $this->calling($protocol)->readFrame[1] = [
  902. 'fin' => 0x0,
  903. 'rsv1' => 0x0,
  904. 'rsv2' => 0x0,
  905. 'rsv3' => 0x0,
  906. 'opcode' => $opcode,
  907. 'mask' => 0x1,
  908. 'length' => 3,
  909. 'message' => 'foo'
  910. ],
  911. $this->calling($protocol)->readFrame[2] = [
  912. 'fin' => 0x0,
  913. 'rsv1' => 0x0,
  914. 'rsv2' => 0x0,
  915. 'rsv3' => 0x0,
  916. 'opcode' => SUT::OPCODE_CONTINUATION_FRAME,
  917. 'mask' => 0x1,
  918. 'length' => 3,
  919. 'message' => 'bar'
  920. ],
  921. $this->calling($protocol)->readFrame[3] = [
  922. 'fin' => 0x1,
  923. 'rsv1' => 0x0,
  924. 'rsv2' => 0x0,
  925. 'rsv3' => 0x0,
  926. 'opcode' => SUT::OPCODE_CONTINUATION_FRAME,
  927. 'mask' => 0x1,
  928. 'length' => 3,
  929. 'message' => 'baz'
  930. ],
  931. $this->calling($socket)->getCurrentNode = $node,
  932. $connection->on(
  933. SUT::OPCODE_TEXT_FRAME === $opcode ? 'message' : 'binary-message',
  934. function (Event\Bucket $bucket) use (&$calledA, $self) {
  935. $calledA = true;
  936. $self
  937. ->array($bucket->getData())
  938. ->isEqualTo([
  939. 'message' => 'foobarbaz'
  940. ]);
  941. throw new \RuntimeException('bang');
  942. }
  943. ),
  944. $connection->on(
  945. 'error',
  946. function (Event\Bucket $bucket) use (&$calledB, $self) {
  947. $calledB = true;
  948. $self
  949. ->let($data = $bucket->getData())
  950. ->array($data)
  951. ->hasSize(1)
  952. ->hasKey('exception')
  953. ->exception($data['exception'])
  954. ->isInstanceOf(\RuntimeException::class)
  955. ->hasMessage('bang');
  956. return;
  957. }
  958. )
  959. )
  960. ->when($result = $this->invoke($connection)->_run($node))
  961. ->then
  962. ->integer($node->getNumberOfFragments())
  963. ->isEqualTo(1)
  964. ->string($node->getFragmentedMessage())
  965. ->isEqualTo('foo')
  966. ->boolean($node->isMessageComplete())
  967. ->isFalse()
  968. ->variable($calledA)
  969. ->isNull()
  970. ->variable($calledB)
  971. ->isNull()
  972. ->when($result = $this->invoke($connection)->_run($node))
  973. ->then
  974. ->integer($node->getNumberOfFragments())
  975. ->isEqualTo(2)
  976. ->string($node->getFragmentedMessage())
  977. ->isEqualTo('foobar')
  978. ->boolean($node->isMessageComplete())
  979. ->isFalse()
  980. ->variable($calledA)
  981. ->isNull()
  982. ->variable($calledB)
  983. ->isNull()
  984. ->when($result = $this->invoke($connection)->_run($node))
  985. ->then
  986. ->integer($node->getNumberOfFragments())
  987. ->isEqualTo(0)
  988. ->variable($node->getFragmentedMessage())
  989. ->isNull()
  990. ->boolean($node->isMessageComplete())
  991. ->isTrue()
  992. ->boolean($calledA)
  993. ->isTrue()
  994. ->boolean($calledB)
  995. ->isTrue();
  996. }
  997. public function case_run_text_frame_and_continuation_frame_opcodes_with_invalid_message_encoding()
  998. {
  999. $self = $this;
  1000. $this
  1001. ->given(
  1002. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  1003. $connection = new SUT($socket),
  1004. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  1005. $this->mockGenerator->orphanize('__construct'),
  1006. $node = new \Mock\Hoa\Websocket\Node(),
  1007. $node->setProtocolImplementation($protocol),
  1008. $this->calling($node)->getHandshake = SUCCEED,
  1009. $this->calling($protocol)->readFrame[1] = [
  1010. 'fin' => 0x0,
  1011. 'rsv1' => 0x0,
  1012. 'rsv2' => 0x0,
  1013. 'rsv3' => 0x0,
  1014. 'opcode' => SUT::OPCODE_TEXT_FRAME,
  1015. 'mask' => 0x1,
  1016. 'length' => 3,
  1017. 'message' => 'foo'
  1018. ],
  1019. $this->calling($protocol)->readFrame[2] = [
  1020. 'fin' => 0x1,
  1021. 'rsv1' => 0x0,
  1022. 'rsv2' => 0x0,
  1023. 'rsv3' => 0x0,
  1024. 'opcode' => SUT::OPCODE_CONTINUATION_FRAME,
  1025. 'mask' => 0x1,
  1026. 'length' => 6,
  1027. 'message' => iconv('UTF-8', 'UTF-16', '😄')
  1028. ],
  1029. $this->calling($socket)->getCurrentNode = $node,
  1030. $this->calling($connection)->close = function ($_code, $_reason) use (&$called, $self) {
  1031. $called = true;
  1032. $self
  1033. ->integer($_code)
  1034. ->isEqualTo(SUT::CLOSE_MESSAGE_ERROR)
  1035. ->variable($_reason)
  1036. ->isNull();
  1037. return;
  1038. }
  1039. )
  1040. ->when($result = $this->invoke($connection)->_run($node))
  1041. ->then
  1042. ->integer($node->getNumberOfFragments())
  1043. ->isEqualTo(1)
  1044. ->string($node->getFragmentedMessage())
  1045. ->isEqualTo('foo')
  1046. ->boolean($node->isMessageComplete())
  1047. ->isFalse()
  1048. ->variable($called)
  1049. ->isNull()
  1050. ->when($result = $this->invoke($connection)->_run($node))
  1051. ->then
  1052. ->integer($node->getNumberOfFragments())
  1053. ->isEqualTo(0)
  1054. ->variable($node->getFragmentedMessage())
  1055. ->isNull()
  1056. ->boolean($node->isMessageComplete())
  1057. ->isTrue()
  1058. ->boolean($called)
  1059. ->isTrue();
  1060. }
  1061. public function case__send()
  1062. {
  1063. $self = $this;
  1064. $this
  1065. ->given(
  1066. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  1067. $connection = new SUT($socket),
  1068. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  1069. $this->mockGenerator->orphanize('__construct'),
  1070. $node = new \Mock\Hoa\Websocket\Node(),
  1071. $node->setProtocolImplementation($protocol),
  1072. $message = 'foobar',
  1073. $opcode = SUT::OPCODE_TEXT_FRAME,
  1074. $end = true,
  1075. $this->calling($node)->getHandshake = true,
  1076. $this->calling($protocol)->send = function ($_message, $_opcode, $_end, $_mask) use (&$called, $self, $message, $opcode, $end) {
  1077. $called = true;
  1078. $self
  1079. ->string($_message)
  1080. ->isEqualTo($message)
  1081. ->integer($_opcode)
  1082. ->isEqualTo($opcode)
  1083. ->boolean($_end)
  1084. ->isEqualTo($end)
  1085. ->boolean($_mask)
  1086. ->isFalse();
  1087. return 42;
  1088. },
  1089. $closure = $this->invoke($connection)->_send($message, $node)
  1090. )
  1091. ->when($result = new \ReflectionFunction($closure))
  1092. ->then
  1093. ->integer($result->getNumberOfRequiredParameters())
  1094. ->isEqualTo(2)
  1095. ->when($result = $closure($opcode, $end))
  1096. ->then
  1097. ->integer($result)
  1098. ->isEqualTo(42)
  1099. ->boolean($called)
  1100. ->isTrue();
  1101. }
  1102. public function case__send_with_no_handshake()
  1103. {
  1104. $this
  1105. ->given(
  1106. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  1107. $connection = new SUT($socket),
  1108. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  1109. $this->mockGenerator->orphanize('__construct'),
  1110. $node = new \Mock\Hoa\Websocket\Node(),
  1111. $node->setProtocolImplementation($protocol),
  1112. $message = 'foobar',
  1113. $opcode = SUT::OPCODE_TEXT_FRAME,
  1114. $end = true,
  1115. $this->calling($protocol)->send = function ($_message, $_opcode, $_end, $_mask) use (&$called) {
  1116. $called = true;
  1117. return 42;
  1118. },
  1119. $closure = $this->invoke($connection)->_send($message, $node)
  1120. )
  1121. ->when($result = new \ReflectionFunction($closure))
  1122. ->then
  1123. ->integer($result->getNumberOfRequiredParameters())
  1124. ->isEqualTo(2)
  1125. ->when($result = $closure($opcode, $end))
  1126. ->then
  1127. ->variable($result)
  1128. ->isNull()
  1129. ->variable($called)
  1130. ->isNull();
  1131. }
  1132. public function case_close_with_no_protocol()
  1133. {
  1134. $this
  1135. ->given(
  1136. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  1137. $connection = new SUT($socket),
  1138. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  1139. $this->mockGenerator->orphanize('__construct'),
  1140. $node = new \Mock\Hoa\Websocket\Node(),
  1141. $this->calling($socket)->getCurrentNode = $node
  1142. )
  1143. ->when($result = $connection->close(SUT::CLOSE_DATA_ERROR, 'foo'))
  1144. ->then
  1145. ->variable($result)
  1146. ->isNull()
  1147. ->boolean($socket->isDisconnected())
  1148. ->isTrue();
  1149. }
  1150. public function case_close()
  1151. {
  1152. $self = $this;
  1153. $this
  1154. ->given(
  1155. $socket = new \Mock\Hoa\Socket\Client('tcp://*:1234'),
  1156. $connection = new SUT($socket),
  1157. $protocol = new \Mock\Hoa\Websocket\Protocol\Rfc6455($socket),
  1158. $this->mockGenerator->orphanize('__construct'),
  1159. $node = new \Mock\Hoa\Websocket\Node(),
  1160. $node->setProtocolImplementation($protocol),
  1161. $code = SUT::CLOSE_DATA_ERROR,
  1162. $reason = 'foo',
  1163. $this->calling($protocol)->close = function ($_code, $_reason) use (&$called, $self, $code, $reason) {
  1164. $called = true;
  1165. $self
  1166. ->integer($_code)
  1167. ->isEqualTo($code)
  1168. ->string($_reason)
  1169. ->isEqualTo($reason);
  1170. return;
  1171. },
  1172. $this->calling($socket)->getCurrentNode = $node
  1173. )
  1174. ->when($result = $connection->close($code, $reason))
  1175. ->then
  1176. ->variable($result)
  1177. ->isNull()
  1178. ->boolean($socket->isDisconnected())
  1179. ->isTrue()
  1180. ->boolean($called)
  1181. ->isTrue();
  1182. }
  1183. }