Node.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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\Socket as HoaSocket;
  37. use Hoa\Test;
  38. use Mock\Hoa\Websocket\Node as SUT;
  39. /**
  40. * Class \Hoa\Websocket\Test\Unit\Node.
  41. *
  42. * Test suite for the node class.
  43. *
  44. * @copyright Copyright © 2007-2017 Hoa community
  45. * @license New BSD License
  46. */
  47. class Node extends Test\Unit\Suite
  48. {
  49. public function case_is_a_node()
  50. {
  51. $this
  52. ->given($this->mockGenerator->orphanize('__construct'))
  53. ->when($result = new SUT())
  54. ->then
  55. ->object($result)
  56. ->isInstanceOf(HoaSocket\Node::class);
  57. }
  58. public function case_set_protocol_implementation()
  59. {
  60. $this
  61. ->given(
  62. $this->mockGenerator->orphanize('__construct'),
  63. $protocolA = new \Mock\Hoa\Websocket\Protocol\Generic(),
  64. $this->mockGenerator->orphanize('__construct'),
  65. $node = new SUT()
  66. )
  67. ->when($result = $node->setProtocolImplementation($protocolA))
  68. ->then
  69. ->variable($result)
  70. ->isNull()
  71. ->given(
  72. $this->mockGenerator->orphanize('__construct'),
  73. $protocolB = new \Mock\Hoa\Websocket\Protocol\Generic()
  74. )
  75. ->when($result = $node->setProtocolImplementation($protocolB))
  76. ->then
  77. ->object($result)
  78. ->isIdenticalTo($protocolA);
  79. }
  80. public function case_get_protocol_implementation()
  81. {
  82. $this
  83. ->given(
  84. $this->mockGenerator->orphanize('__construct'),
  85. $protocol = new \Mock\Hoa\Websocket\Protocol\Generic(),
  86. $this->mockGenerator->orphanize('__construct'),
  87. $node = new SUT(),
  88. $node->setProtocolImplementation($protocol)
  89. )
  90. ->when($result = $node->getProtocolImplementation())
  91. ->then
  92. ->object($result)
  93. ->isIdenticalTo($protocol);
  94. }
  95. public function case_set_handshake()
  96. {
  97. $this
  98. ->given(
  99. $this->mockGenerator->orphanize('__construct'),
  100. $node = new SUT()
  101. )
  102. ->when($result = $node->setHandshake(true))
  103. ->then
  104. ->boolean($result)
  105. ->isFalse()
  106. ->when($result = $node->setHandshake(false))
  107. ->then
  108. ->boolean($result)
  109. ->isTrue();
  110. }
  111. public function case_get_handshake()
  112. {
  113. $this
  114. ->given(
  115. $this->mockGenerator->orphanize('__construct'),
  116. $node = new SUT(),
  117. $node->setHandshake(true)
  118. )
  119. ->when($result = $node->getHandshake())
  120. ->then
  121. ->boolean($result)
  122. ->isTrue();
  123. }
  124. public function case_append_message_fragment()
  125. {
  126. $this
  127. ->given(
  128. $this->mockGenerator->orphanize('__construct'),
  129. $node = new SUT()
  130. )
  131. ->when($result = $node->appendMessageFragment('foo'))
  132. ->then
  133. ->string($result)
  134. ->isEqualTo('foo');
  135. }
  136. public function case_append_many_message_fragments()
  137. {
  138. $this
  139. ->given(
  140. $this->mockGenerator->orphanize('__construct'),
  141. $node = new SUT(),
  142. $fragmentA = 'foo',
  143. $fragmentB = 'bar',
  144. $fragmentC = 'baz'
  145. )
  146. ->when($result = $node->appendMessageFragment($fragmentA))
  147. ->then
  148. ->string($result)
  149. ->isEqualTo($fragmentA)
  150. ->when($result = $node->appendMessageFragment($fragmentB))
  151. ->then
  152. ->string($result)
  153. ->isEqualTo($fragmentA . $fragmentB)
  154. ->when($result = $node->appendMessageFragment($fragmentC))
  155. ->then
  156. ->string($result)
  157. ->isEqualTo($fragmentA . $fragmentB . $fragmentC);
  158. }
  159. public function case_get_fragmented_message()
  160. {
  161. $this
  162. ->given(
  163. $this->mockGenerator->orphanize('__construct'),
  164. $node = new SUT(),
  165. $fragmentA = 'foo',
  166. $fragmentB = 'bar',
  167. $fragmentC = 'baz',
  168. $node->appendMessageFragment($fragmentA),
  169. $node->appendMessageFragment($fragmentB),
  170. $node->appendMessageFragment($fragmentC)
  171. )
  172. ->when($result = $node->getFragmentedMessage())
  173. ->then
  174. ->string($result)
  175. ->isEqualTo($fragmentA . $fragmentB . $fragmentC);
  176. }
  177. public function case_get_number_of_fragments_for_an_message()
  178. {
  179. $this
  180. ->given(
  181. $this->mockGenerator->orphanize('__construct'),
  182. $node = new SUT()
  183. )
  184. ->when($result = $node->getNumberOfFragments())
  185. ->then
  186. ->integer($result)
  187. ->isEqualTo(0);
  188. }
  189. public function case_get_number_of_fragments()
  190. {
  191. $this
  192. ->given(
  193. $this->mockGenerator->orphanize('__construct'),
  194. $node = new SUT(),
  195. $fragmentA = 'foo',
  196. $fragmentB = 'bar',
  197. $fragmentC = 'baz',
  198. $node->appendMessageFragment($fragmentA),
  199. $node->appendMessageFragment($fragmentB),
  200. $node->appendMessageFragment($fragmentC)
  201. )
  202. ->when($result = $node->getNumberOfFragments())
  203. ->then
  204. ->integer($result)
  205. ->isEqualTo(3);
  206. }
  207. public function case_set_complete()
  208. {
  209. $this
  210. ->given(
  211. $this->mockGenerator->orphanize('__construct'),
  212. $node = new SUT()
  213. )
  214. ->when($result = $node->setComplete(false))
  215. ->then
  216. ->boolean($result)
  217. ->isTrue()
  218. ->when($result = $node->setComplete(true))
  219. ->then
  220. ->boolean($result)
  221. ->isFalse();
  222. }
  223. public function case_is_message_complete()
  224. {
  225. $this
  226. ->given(
  227. $this->mockGenerator->orphanize('__construct'),
  228. $node = new SUT(),
  229. $node->setComplete(true)
  230. )
  231. ->when($result = $node->isMessageComplete())
  232. ->then
  233. ->boolean($result)
  234. ->isTrue();
  235. }
  236. public function case_is_message_not_complete()
  237. {
  238. $this
  239. ->given(
  240. $this->mockGenerator->orphanize('__construct'),
  241. $node = new SUT(),
  242. $node->setComplete(false)
  243. )
  244. ->when($result = $node->isMessageComplete())
  245. ->then
  246. ->boolean($result)
  247. ->isFalse();
  248. }
  249. public function case_message_is_complete_by_default()
  250. {
  251. $this
  252. ->given(
  253. $this->mockGenerator->orphanize('__construct'),
  254. $node = new SUT()
  255. )
  256. ->when($result = $node->isMessageComplete())
  257. ->then
  258. ->boolean($result)
  259. ->isTrue();
  260. }
  261. public function case_set_binary()
  262. {
  263. $this
  264. ->given(
  265. $this->mockGenerator->orphanize('__construct'),
  266. $node = new SUT()
  267. )
  268. ->when($result = $node->setBinary(true))
  269. ->then
  270. ->boolean($result)
  271. ->isFalse()
  272. ->when($result = $node->setBinary(false))
  273. ->then
  274. ->boolean($result)
  275. ->isTrue();
  276. }
  277. public function case_is_binary()
  278. {
  279. $this
  280. ->given(
  281. $this->mockGenerator->orphanize('__construct'),
  282. $node = new SUT(),
  283. $node->setBinary(true)
  284. )
  285. ->when($result = $node->isBinary())
  286. ->then
  287. ->boolean($result)
  288. ->isTrue();
  289. }
  290. public function case_is_not_binary()
  291. {
  292. $this
  293. ->given(
  294. $this->mockGenerator->orphanize('__construct'),
  295. $node = new SUT(),
  296. $node->setBinary(false)
  297. )
  298. ->when($result = $node->isBinary())
  299. ->then
  300. ->boolean($result)
  301. ->isFalse();
  302. }
  303. public function case_not_binary_by_default()
  304. {
  305. $this
  306. ->given(
  307. $this->mockGenerator->orphanize('__construct'),
  308. $node = new SUT()
  309. )
  310. ->when($result = $node->isBinary())
  311. ->then
  312. ->boolean($result)
  313. ->isFalse();
  314. }
  315. public function case_clear_fragmentation()
  316. {
  317. $this
  318. ->given(
  319. $this->mockGenerator->orphanize('__construct'),
  320. $protocol = new \Mock\Hoa\Websocket\Protocol\Generic(),
  321. $this->mockGenerator->orphanize('__construct'),
  322. $node = new SUT(),
  323. $node->setProtocolImplementation($protocol),
  324. $node->appendMessageFragment('foo'),
  325. $node->appendMessageFragment('bar'),
  326. $node->setBinary(true),
  327. $node->setComplete(false)
  328. )
  329. ->when($result = $node->clearFragmentation())
  330. ->then
  331. ->variable($result)
  332. ->isNull()
  333. ->object($node->getProtocolImplementation())
  334. ->isIdenticalTo($protocol)
  335. ->variable($node->getFragmentedMessage())
  336. ->isNull()
  337. ->boolean($node->isBinary())
  338. ->isFalse()
  339. ->boolean($node->isMessageComplete())
  340. ->isTrue();
  341. }
  342. }