/tests/Xmpp/ConnectionTest.php

https://github.com/donglinkang/Xmpp · PHP · 185 lines · 86 code · 21 blank · 78 comment · 0 complexity · ad9712b9570b958d95cd872c98bbed85 MD5 · raw file

  1. <?php
  2. /**
  3. * PHP XMPP Library
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE
  8. *
  9. * This source file is subject to the new BSD license that is bundled
  10. * with this package in the file LICENSE.
  11. * It is also available through the world-wide-web at this URL:
  12. * http://m.me.uk/xmpp/license/
  13. *
  14. * @category Xmpp
  15. * @package Xmpp
  16. * @author Alex Mace <a@m.me.uk>
  17. * @copyright 2010-2011 Alex Mace (http://m.me.uk)
  18. * @license http://m.me.uk/xmpp/license/ New BSD License
  19. * @link http://pear.m.me.uk/package/Xmpp
  20. */
  21. require_once 'Stream.php';
  22. require_once 'Xmpp/Connection.php';
  23. /**
  24. * Tests for the XMPP class. Each tests need to mock the Stream object and stub the
  25. * _getStream method so that the XMPP class doesn't actually have to connect to
  26. * anything.
  27. *
  28. * @category XMPP
  29. * @package XMPP
  30. * @author Alex Mace <a@m.me.uk>
  31. * @copyright 2010-2011 Alex Mace (http://m.me.uk)
  32. * @license http://m.me.uk/license New BSD License
  33. * @link http://m.me.uk/xmpp
  34. */
  35. class Xmpp_ConnectionTest extends PHPUnit_Framework_TestCase
  36. {
  37. /**
  38. * Sets up the fixture, for example, opens a network connection.
  39. * This method is called before a test is executed.
  40. *
  41. * @return void
  42. */
  43. public function setUp()
  44. {
  45. }
  46. /**
  47. * Tears down the fixture, for example, closes a network connection.
  48. * This method is called after a test is executed.
  49. *
  50. * @return void
  51. */
  52. public function tearDown()
  53. {
  54. }
  55. /**
  56. * Gets a mock instance of Stream for use in tests.
  57. *
  58. * @return type
  59. */
  60. public function getMockStream()
  61. {
  62. return $this->getMock('Stream', array(), array(), '', false);
  63. }
  64. /**
  65. * Gets a mock instance of Xmpp_Connection for use in tests.
  66. *
  67. * @param Stream $stream A stream to connect with.
  68. *
  69. * @return type
  70. */
  71. public function getMockXmppConnection(Stream $stream)
  72. {
  73. $xmpp = $this->getMock(
  74. 'Xmpp_Connection', array('_getStream'),
  75. array('test@test.server.com', 'testPass', 'test.xmpp.com')
  76. );
  77. $xmpp->expects($this->once())
  78. ->method('_getStream')
  79. ->with($this->equalTo('tcp://test.xmpp.com:5222'))
  80. ->will($this->returnValue($stream));
  81. return $xmpp;
  82. }
  83. /**
  84. * Test for the connect function
  85. *
  86. * @return void
  87. */
  88. public function testConnect()
  89. {
  90. $stream = $this->getMockStream();
  91. // Set up what we expect the XMPP class to send to the server
  92. $message = '<stream:stream to="test.xmpp.com" '
  93. . 'xmlns:stream="http://etherx.jabber.org/streams" '
  94. . 'xmlns="jabber:client" version="1.0">';
  95. $stream->expects($this->at(1))
  96. ->method('send')
  97. ->with($this->equalTo($message));
  98. // Next we expect to tell the stream to wait for a response from the
  99. // server
  100. $stream->expects($this->at(2))
  101. ->method('select')
  102. ->will($this->returnValue(1));
  103. // Set up what expect the reponse of the server to be to this
  104. $message = "<?xml version='1.0' encoding='UTF-8'?>"
  105. . '<stream:stream '
  106. . 'xmlns:stream="http://etherx.jabber.org/streams" '
  107. . 'xmlns="jabber:client" from="test.xmpp.com" '
  108. . 'id="e674e243" xml:lang="en" version="1.0">';
  109. $stream->expects($this->at(3))
  110. ->method('read')
  111. ->with($this->equalTo(4096))
  112. ->will($this->returnValue($message));
  113. // Next we expect to tell the stream to wait for a response from the
  114. // server
  115. $stream->expects($this->at(4))
  116. ->method('select')
  117. ->will($this->returnValue(1));
  118. // Next the server should report what features it supports
  119. $message = '<stream:features>'
  120. . '<starttls xmlns="urn:ietf:params:xml:ns:xmpp-tls"></starttls>'
  121. . '<mechanisms xmlns="urn:ietf:params:xml:ns:xmpp-sasl">'
  122. . '<mechanism>DIGEST-MD5</mechanism>'
  123. . '<mechanism>PLAIN</mechanism>'
  124. . '<mechanism>ANONYMOUS</mechanism>'
  125. . '<mechanism>CRAM-MD5</mechanism>'
  126. . '</mechanisms>'
  127. . '<compression xmlns="http://jabber.org/features/compress">'
  128. . '<method>zlib</method>'
  129. . '</compression>'
  130. . '<auth xmlns="http://jabber.org/features/iq-auth"/>'
  131. . '<register xmlns="http://jabber.org/features/iq-register"/>'
  132. . '</stream:features>';
  133. $stream->expects($this->at(5))
  134. ->method('read')
  135. ->with($this->equalTo(4096))
  136. ->will($this->returnValue($message));
  137. // Now get a mock of XMPP and replace the _getStream function with a
  138. // stub that will return our stream mock.
  139. $xmpp = $this->getMockXmppConnection($stream);
  140. $xmpp->connect();
  141. }
  142. /**
  143. * Tests disconnecting from an XMPP server.
  144. *
  145. * @return void
  146. */
  147. public function testDisconnect()
  148. {
  149. $stream = $this->getMockStream();
  150. // Setup what we expect the Xmpp_Connection class to send to the server.
  151. // Disconnecting from the server should basically just be sending a
  152. // closing stream tag followed by disconnection.
  153. $message = '</stream:stream>';
  154. $stream->expects($this->at(0))
  155. ->method('send')
  156. ->with($this->equalTo($message));
  157. // Next we expect the Stream to be told to close to the connect
  158. $stream->expects($this->at(1))
  159. ->method('disconnect')
  160. ->will($this->returnValue(true));
  161. $xmpp = $this->getMockXmppConnection($stream);
  162. $xmpp->disconnect();
  163. }
  164. }