ClientTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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;
  36. use PHPUnit\Framework\TestCase;
  37. use Fabiang\Xmpp\Connection\ConnectionInterface;
  38. use Fabiang\Xmpp\Event\EventManagerInterface;
  39. use Fabiang\Xmpp\Protocol\ImplementationInterface;
  40. use Fabiang\Xmpp\EventListener\Logger;
  41. /**
  42. * Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-01-17 at 10:05:30.
  43. *
  44. * @coversDefaultClass Fabiang\Xmpp\Client
  45. */
  46. class ClientTest extends TestCase
  47. {
  48. /**
  49. * @var Client
  50. */
  51. protected $object;
  52. /**
  53. * @var \PHPUnit_Framework_MockObject_MockObject
  54. */
  55. protected $options;
  56. /**
  57. * @var \PHPUnit_Framework_MockObject_MockObject
  58. */
  59. protected $connection;
  60. /**
  61. * @var \PHPUnit_Framework_MockObject_MockObject
  62. */
  63. protected $eventManager;
  64. /**
  65. * @var \PHPUnit_Framework_MockObject_MockObject
  66. */
  67. protected $implementation;
  68. /**
  69. * Sets up the fixture, for example, opens a network connection.
  70. * This method is called before a test is executed.
  71. *
  72. * @return void
  73. */
  74. protected function setUp()
  75. {
  76. $this->options = $this->createMock(Options::class);
  77. $this->connection = $this->createMock(ConnectionInterface::class);
  78. $this->options->expects($this->any())
  79. ->method('getConnection')
  80. ->willReturn($this->connection);
  81. $this->eventManager = $this->createMock(EventManagerInterface::class);
  82. $this->implementation = $this->createMock(ImplementationInterface::class);
  83. $this->options->expects($this->any())
  84. ->method('getImplementation')
  85. ->willReturn($this->implementation);
  86. $this->object = new Client($this->options, $this->eventManager);
  87. }
  88. /**
  89. * Test constructor.
  90. *
  91. * @covers ::__construct
  92. * @covers ::setupImplementation
  93. * @uses Fabiang\Xmpp\Client::getEventManager
  94. * @uses Fabiang\Xmpp\Client::setEventManager
  95. * @uses Fabiang\Xmpp\Client::getOptions
  96. */
  97. public function testConstructor()
  98. {
  99. $this->connection->expects($this->once())
  100. ->method('setEventManager')
  101. ->with($this->equalTo($this->eventManager));
  102. $this->connection->expects($this->once())
  103. ->method('setOptions')
  104. ->with($this->equalTo($this->options));
  105. $this->implementation->expects($this->once())
  106. ->method('registerListener')
  107. ->with($this->isInstanceOf(Logger::class));
  108. $this->implementation->expects($this->once())
  109. ->method('setEventManager')
  110. ->with($this->equalTo($this->eventManager));
  111. $this->implementation->expects($this->once())
  112. ->method('setOptions')
  113. ->with($this->equalTo($this->options));
  114. $this->implementation->expects($this->once())
  115. ->method('register');
  116. $object = new Client($this->options, $this->eventManager);
  117. $this->assertSame($this->eventManager, $object->getEventManager());
  118. $this->assertSame($this->options, $object->getOptions());
  119. }
  120. /**
  121. * @covers ::__construct
  122. * @covers ::getConnection
  123. * @uses Fabiang\Xmpp\Client::setupImplementation
  124. * @uses Fabiang\Xmpp\Client::getEventManager
  125. * @uses Fabiang\Xmpp\Stream\SocketClient::__construct
  126. * @uses Fabiang\Xmpp\Connection\AbstractConnection::setEventManager
  127. * @uses Fabiang\Xmpp\Connection\AbstractConnection::setOptions
  128. * @uses Fabiang\Xmpp\Connection\Socket::__construct
  129. * @uses Fabiang\Xmpp\Connection\Socket::factory
  130. * @uses Fabiang\Xmpp\Connection\Socket::setSocket
  131. * @uses Fabiang\Xmpp\Event\EventManager::__construct
  132. */
  133. public function testConstructorCreatingDefaults()
  134. {
  135. $options = $this->createMock(Options::class);
  136. $options->expects($this->any())
  137. ->method('getImplementation')
  138. ->willReturn($this->implementation);
  139. $object = new Client($options);
  140. $this->assertInstanceOf(EventManagerInterface::class, $object->getEventManager());
  141. $this->assertInstanceOf(ConnectionInterface::class, $object->getConnection());
  142. }
  143. /**
  144. * Test connection.
  145. *
  146. * @covers ::connect
  147. * @uses Fabiang\Xmpp\Client::__construct
  148. * @uses Fabiang\Xmpp\Client::setupImplementation
  149. */
  150. public function testConnect()
  151. {
  152. $this->connection->expects($this->once())
  153. ->method('connect');
  154. $this->object->connect();
  155. }
  156. /**
  157. * Test disconnecting.
  158. *
  159. * @covers ::disconnect
  160. * @uses Fabiang\Xmpp\Client::__construct
  161. * @uses Fabiang\Xmpp\Client::setUpImplementation
  162. */
  163. public function testDisconnect()
  164. {
  165. $this->connection->expects($this->once())
  166. ->method('disconnect');
  167. $this->object->disconnect();
  168. }
  169. /**
  170. * Test sending data.
  171. *
  172. * @covers ::send
  173. * @uses Fabiang\Xmpp\Client::__construct
  174. * @uses Fabiang\Xmpp\Client::setUpImplementation
  175. */
  176. public function testSend()
  177. {
  178. $this->connection->expects($this->once())
  179. ->method('send')
  180. ->with($this->equalTo('test'));
  181. $message = $this->createMock(Protocol\ProtocolImplementationInterface::class);
  182. $message->expects($this->once())
  183. ->method('toString')
  184. ->willReturn('test');
  185. $this->object->send($message);
  186. }
  187. /**
  188. * Test setting and getting event manager.
  189. *
  190. * @covers ::getEventManager
  191. * @covers ::setEventManager
  192. * @uses Fabiang\Xmpp\Client::__construct
  193. * @uses Fabiang\Xmpp\Client::setUpImplementation
  194. */
  195. public function testSetAndGetEventManager()
  196. {
  197. $eventManager = $this->createMock(EventManagerInterface::class);
  198. $this->assertSame($eventManager, $this->object->setEventManager($eventManager)->getEventManager());
  199. }
  200. /**
  201. * Test getting options object.
  202. *
  203. * @covers ::getOptions
  204. * @uses Fabiang\Xmpp\Client::__construct
  205. * @uses Fabiang\Xmpp\Client::setUpImplementation
  206. */
  207. public function testGetOptions()
  208. {
  209. $this->assertSame($this->options, $this->object->getOptions());
  210. }
  211. /**
  212. * Test setter Option Connection in Client constructor
  213. *
  214. * @covers ::__construct
  215. * @uses Fabiang\Xmpp\Client::__construct
  216. * @uses Fabiang\Xmpp\Client::getOptions
  217. * @uses Fabiang\Xmpp\Client::setUpImplementation
  218. * @uses Fabiang\Xmpp\Options::getConnection
  219. */
  220. public function testOptionsConnection()
  221. {
  222. $options = new Options();
  223. $client = $this->getMockBuilder(Client::class)
  224. ->setMethods(array('setupImplement'))
  225. ->setConstructorArgs([$options])
  226. ->getMock();
  227. $optionsAssert = $client->getOptions();
  228. $this->assertInstanceOf(ConnectionInterface::class, $optionsAssert->getConnection());
  229. }
  230. }