ClientTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Queue
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /*
  23. * The adapter test class provides a universal test class for all of the
  24. * abstract methods.
  25. *
  26. * All methods marked not supported are explictly checked for for throwing
  27. * an exception.
  28. */
  29. /** Zend_Queue_Stomp_Frame */
  30. require_once 'Zend/Queue/Stomp/Frame.php';
  31. /** Zend_Queue_Stomp_Client */
  32. require_once 'Zend/Queue/Stomp/Client.php';
  33. /** Zend_Queue_Stomp_Client_Interface */
  34. require_once 'Zend/Queue/Stomp/Client/Connection.php';
  35. /**
  36. * @category Zend
  37. * @package Zend_Queue
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. * @group Zend_Queue
  42. */
  43. class Zend_Queue_Stomp_Connection_Mock
  44. extends Zend_Queue_Stomp_Client_Connection
  45. {
  46. /**
  47. * open() opens a socket to the Stomp server
  48. *
  49. * @param array $config ('scheme', 'host', 'port')
  50. * @return true;
  51. */
  52. public function open($scheme, $host, $port)
  53. {
  54. if ( $port == 0 ) return false;
  55. return true;
  56. }
  57. public function close($destructor = false)
  58. {
  59. }
  60. /**
  61. * Check whether we are connected to the server
  62. *
  63. * @return true
  64. * @throws Zend_Queue_Exception
  65. */
  66. public function ping()
  67. {
  68. return true;
  69. }
  70. /**
  71. * write a frame to the stomp server
  72. *
  73. * @example $response = $client->write($frame)->read();
  74. *
  75. * @param Zend_Queue_Stom_Frame $frame
  76. * @return $this
  77. */
  78. public function write(Zend_Queue_Stomp_FrameInterface $frame)
  79. {
  80. $this->_buffer[] = $frame;
  81. }
  82. /**
  83. * tests the socket to see if there is data for us
  84. */
  85. public function canRead()
  86. {
  87. return count($this->_buffer) > 0;
  88. }
  89. /**
  90. * reads in a frame from the socket or returns false.
  91. *
  92. * @return Zend_Queue_Stomp_Frame|false
  93. * @throws Zend_Queue_Exception
  94. */
  95. public function read()
  96. {
  97. if (! $this->canRead()) return false;
  98. return array_shift($this->_buffer);
  99. }
  100. }
  101. /**
  102. * @category Zend
  103. * @package Zend_Queue
  104. * @subpackage UnitTests
  105. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  106. * @license http://framework.zend.com/license/new-bsd New BSD License
  107. * @group Zend_Queue
  108. */
  109. class Zend_Queue_Stomp_ClientTest extends PHPUnit_Framework_TestCase
  110. {
  111. public function testConstruct()
  112. {
  113. $client = new Zend_Queue_Stomp_Client('tcp', 'localhost', '11232', 'Zend_Queue_Stomp_Connection_Mock');
  114. $this->assertTrue($client->getConnection() instanceof Zend_Queue_Stomp_Client_ConnectionInterface);
  115. }
  116. public function testAddConnection()
  117. {
  118. $client = new Zend_Queue_Stomp_Client();
  119. $client->addConnection('tcp', 'localhost', '11232', 'Zend_Queue_Stomp_Connection_Mock');
  120. $this->assertTrue($client->getConnection() instanceof Zend_Queue_Stomp_Client_ConnectionInterface);
  121. $client = new Zend_Queue_Stomp_Client();
  122. $this->assertFalse($client->addConnection('tcp', 'localhost', 0, 'Zend_Queue_Stomp_Connection_Mock'));
  123. }
  124. public function testGetAndSetConnection()
  125. {
  126. $connection = new Zend_Queue_Stomp_Connection_Mock('tcp', 'localhost', '11232');
  127. $client = new Zend_Queue_Stomp_Client();
  128. $this->assertTrue($client->setConnection($connection) instanceof Zend_Queue_Stomp_Client);
  129. $try = $client->getConnection();
  130. $this->assertEquals($connection, $try);
  131. }
  132. public function testSendAndReceive()
  133. {
  134. $frame = new Zend_Queue_Stomp_Frame();
  135. $frame->setCommand('testing');
  136. $frame->setHeader('testing',1);
  137. $frame->setBody('hello world');
  138. $client = new Zend_Queue_Stomp_Client();
  139. $client->addConnection('tcp', 'localhost', '11232', 'Zend_Queue_Stomp_Connection_Mock');
  140. $client->send($frame);
  141. $this->assertTrue($client->canRead());
  142. $test = $client->receive();
  143. $this->assertEquals('testing', $test->getCommand());
  144. $this->assertEquals(1, $test->getHeader('testing'));
  145. $this->assertEquals('hello world', $test->getBody());
  146. }
  147. }