FeatureContext.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * Copyright 2014 Fabian Grutschus. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. * The views and conclusions contained in the software and documentation are those
  27. * of the authors and should not be interpreted as representing official policies,
  28. * either expressed or implied, of the copyright holders.
  29. *
  30. * @author Fabian Grutschus <f.grutschus@lubyte.de>
  31. * @copyright 2014 Fabian Grutschus. All rights reserved.
  32. * @license BSD
  33. * @link http://github.com/fabiang/xmpp
  34. */
  35. namespace Fabiang\Xmpp\Integration;
  36. use Behat\Behat\Context\BehatContext;
  37. use Fabiang\Xmpp\Options;
  38. use Fabiang\Xmpp\Client;
  39. use Fabiang\Xmpp\Connection\Test;
  40. use Fabiang\Xmpp\Connection\Socket;
  41. use Fabiang\Xmpp\Stream\SocketClient;
  42. /**
  43. * Description of FeatureContext
  44. *
  45. * @author f.grutschus
  46. */
  47. class FeatureContext extends BehatContext
  48. {
  49. /**
  50. *
  51. * @var Client
  52. */
  53. protected $client;
  54. /**
  55. *
  56. * @var Options
  57. */
  58. protected $options;
  59. /**
  60. *
  61. * @var Test
  62. */
  63. protected $connection;
  64. /**
  65. * Catch connection exceptions.
  66. *
  67. * @var boolean
  68. */
  69. protected $catch = false;
  70. /**
  71. * Catched exception.
  72. *
  73. * @var \Exception
  74. */
  75. public $exception;
  76. /**
  77. * Constructor.
  78. *
  79. * @param array $parameters
  80. */
  81. public function __construct(array $parameters)
  82. {
  83. /* @var $autoloader \Composer\Autoload\ClassLoader */
  84. $autoloader = require realpath(__DIR__ . '/../../../vendor/autoload.php');
  85. $autoloader->add(__NAMESPACE__, __DIR__);
  86. $this->useContext('authentication', new AuthenticationContext);
  87. $this->useContext('bind', new BindContext);
  88. $this->useContext('session', new SessionContext);
  89. $this->useContext('roster', new RosterContext);
  90. }
  91. /**
  92. * @Given /^Test connection adapter$/
  93. */
  94. public function testConnectionAdapter()
  95. {
  96. $this->connection = new Test;
  97. $this->options = new Options;
  98. $this->options->setTo('localhost');
  99. $this->options->setConnection($this->connection)
  100. ->setUsername('aaa')
  101. ->setPassword('bbb');
  102. $this->client = new Client($this->options);
  103. }
  104. /**
  105. * @Given /^Socket connection adapter with address (.+)$/
  106. */
  107. public function socketConnectionAdapterWithAddressTcpLocalhost($address)
  108. {
  109. $mock = new SocketClient($address);
  110. $this->connection = new Socket($mock);
  111. $this->options = new Options;
  112. $this->options->setConnection($this->connection);
  113. $this->client = new Client($this->options);
  114. }
  115. /**
  116. * @Given /^URL is (.+)$/
  117. */
  118. public function urlIsTcpUnknowenTld($address)
  119. {
  120. $this->connection->getOptions()->setAddress($address);
  121. }
  122. /**
  123. * @Given /^Test response data for non-TLS$/
  124. */
  125. public function testResponseDataForNonTls()
  126. {
  127. $this->connection->setData(array(
  128. "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' "
  129. . "id='1234567890' from='localhost' version='1.0' xml:lang='en'><stream:features></stream:features>"
  130. ));
  131. }
  132. /**
  133. * @Given /^Test response data for TLS$/
  134. */
  135. public function testResponseDataForTls()
  136. {
  137. $this->connection->setData(array(
  138. "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' "
  139. . "id='1234567890' from='localhost' version='1.0' xml:lang='en'><stream:features>"
  140. . '<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>'
  141. . "</stream:features>",
  142. "<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>",
  143. "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' "
  144. . "id='1234567890' from='localhost' version='1.0' xml:lang='en'><stream:features></stream:features>"
  145. ));
  146. }
  147. /**
  148. * @Given /^Test response data for disconnect$/
  149. */
  150. public function testResponseDataForDisconnect()
  151. {
  152. $this->connection->setData(array(
  153. "<?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' "
  154. . "id='1234567890' from='localhost' version='1.0' xml:lang='en'>",
  155. '</stream:stream>'
  156. ));
  157. }
  158. /**
  159. * @Given /^exceptions are catched when connecting$/
  160. */
  161. public function exceptionsAreCatchedWhenConnecting()
  162. {
  163. $this->catch = true;
  164. }
  165. /**
  166. * @Given /^timeout is set to (\d+) seconds$/
  167. */
  168. public function timeoutIsSetToSeconds($timeout)
  169. {
  170. $this->getConnection()->getOptions()->setTimeout($timeout);
  171. }
  172. /**
  173. * @When /^connecting/
  174. */
  175. public function connecting()
  176. {
  177. try {
  178. $this->connection->connect();
  179. } catch (\Exception $exception) {
  180. $this->exception = $exception;
  181. if (false === $this->catch) {
  182. throw $exception;
  183. }
  184. }
  185. }
  186. /**
  187. * @Then /^should be connected$/
  188. */
  189. public function shouldBeConnected()
  190. {
  191. assertTrue($this->connection->isConnected());
  192. }
  193. /**
  194. * @Given /^Stream start should be send$/
  195. * @Then /^Stream start should be send (\d+) times$/
  196. */
  197. public function streamStartShouldBeSend($num = 1)
  198. {
  199. $expected = sprintf(Socket::STREAM_START, 'localhost');
  200. $counts = array_count_values($this->connection->getBuffer());
  201. assertEquals($num, $counts[$expected]);
  202. }
  203. /**
  204. * @Then /^timeout exception should have been thrown$/
  205. */
  206. public function timeoutExceptionShouldHaveThrown()
  207. {
  208. assertInstanceOf('\\Fabiang\\Xmpp\\Exception\\TimeoutException', $this->exception);
  209. }
  210. /**
  211. * @Then /^socket exception should have been thrown$/
  212. */
  213. public function socketExceptionShouldHaveBeenThrown()
  214. {
  215. assertInstanceOf('\\Fabiang\\Xmpp\\Exception\\ErrorException', $this->exception);
  216. }
  217. /**
  218. * @When /^disconnecting$/
  219. */
  220. public function disconnecting()
  221. {
  222. $this->connection->disconnect();
  223. }
  224. /**
  225. * @Then /^Stream end should be send$/
  226. */
  227. public function streamEndShouldBeSend()
  228. {
  229. assertContains(Socket::STREAM_END, $this->connection->getBuffer());
  230. }
  231. /**
  232. * @Given /^should be disconnected$/
  233. */
  234. public function shouldBeDisconnected()
  235. {
  236. assertFalse($this->connection->isConnected());
  237. }
  238. /**
  239. * @Then /^Starttls should be send$/
  240. */
  241. public function starttlsShouldBeSend()
  242. {
  243. assertContains('<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"/>', $this->connection->getBuffer());
  244. }
  245. /**
  246. * @Then /^Stream end received$/
  247. */
  248. public function streamEndReceived()
  249. {
  250. assertContains('</stream:stream>', $this->connection->getData());
  251. }
  252. /**
  253. *
  254. * @return Client
  255. */
  256. public function getClient()
  257. {
  258. return $this->client;
  259. }
  260. /**
  261. *
  262. * @return Options
  263. */
  264. public function getOptions()
  265. {
  266. return $this->options;
  267. }
  268. /**
  269. *
  270. * @return Connection
  271. */
  272. public function getConnection()
  273. {
  274. return $this->connection;
  275. }
  276. }