ClientTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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-2010 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. /** TestHelp.php */
  30. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  31. /** Zend_Queue_Stomp_Frame */
  32. require_once 'Zend/Queue/Stomp/Frame.php';
  33. /** Zend_Queue_Stomp_Client */
  34. require_once 'Zend/Queue/Stomp/Client.php';
  35. /** Zend_Queue_Stomp_Client_Interface */
  36. require_once 'Zend/Queue/Stomp/Client/Connection.php';
  37. /**
  38. * @category Zend
  39. * @package Zend_Queue
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @group Zend_Queue
  44. */
  45. class Zend_Queue_Stomp_Connection_Mock
  46. extends Zend_Queue_Stomp_Client_Connection
  47. {
  48. /**
  49. * open() opens a socket to the Stomp server
  50. *
  51. * @param array $config ('scheme', 'host', 'port')
  52. * @return true;
  53. */
  54. public function open($scheme, $host, $port)
  55. {
  56. if ( $port == 0 ) return false;
  57. return true;
  58. }
  59. public function close($destructor = false)
  60. {
  61. }
  62. /**
  63. * Check whether we are connected to the server
  64. *
  65. * @return true
  66. * @throws Zend_Queue_Exception
  67. */
  68. public function ping()
  69. {
  70. return true;
  71. }
  72. /**
  73. * write a frame to the stomp server
  74. *
  75. * @example $response = $client->write($frame)->read();
  76. *
  77. * @param Zend_Queue_Stom_Frame $frame
  78. * @return $this
  79. */
  80. public function write(Zend_Queue_Stomp_FrameInterface $frame)
  81. {
  82. $this->_buffer[] = $frame;
  83. }
  84. /**
  85. * tests the socket to see if there is data for us
  86. */
  87. public function canRead()
  88. {
  89. return count($this->_buffer) > 0;
  90. }
  91. /**
  92. * reads in a frame from the socket or returns false.
  93. *
  94. * @return Zend_Queue_Stomp_Frame|false
  95. * @throws Zend_Queue_Exception
  96. */
  97. public function read()
  98. {
  99. if (! $this->canRead()) return false;
  100. return array_shift($this->_buffer);
  101. }
  102. }
  103. /**
  104. * @category Zend
  105. * @package Zend_Queue
  106. * @subpackage UnitTests
  107. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  108. * @license http://framework.zend.com/license/new-bsd New BSD License
  109. * @group Zend_Queue
  110. */
  111. class Zend_Queue_Stomp_ClientTest extends PHPUnit_Framework_TestCase
  112. {
  113. public function testConstruct()
  114. {
  115. $client = new Zend_Queue_Stomp_Client('tcp', 'localhost', '11232', 'Zend_Queue_Stomp_Connection_Mock');
  116. $this->assertTrue($client->getConnection() instanceof Zend_Queue_Stomp_Client_ConnectionInterface);
  117. }
  118. public function testAddConnection()
  119. {
  120. $client = new Zend_Queue_Stomp_Client();
  121. $client->addConnection('tcp', 'localhost', '11232', 'Zend_Queue_Stomp_Connection_Mock');
  122. $this->assertTrue($client->getConnection() instanceof Zend_Queue_Stomp_Client_ConnectionInterface);
  123. $client = new Zend_Queue_Stomp_Client();
  124. $this->assertFalse($client->addConnection('tcp', 'localhost', 0, 'Zend_Queue_Stomp_Connection_Mock'));
  125. }
  126. public function testGetAndSetConnection()
  127. {
  128. $connection = new Zend_Queue_Stomp_Connection_Mock('tcp', 'localhost', '11232');
  129. $client = new Zend_Queue_Stomp_Client();
  130. $this->assertTrue($client->setConnection($connection) instanceof Zend_Queue_Stomp_Client);
  131. $try = $client->getConnection();
  132. $this->assertEquals($connection, $try);
  133. }
  134. public function testSendAndReceive()
  135. {
  136. $frame = new Zend_Queue_Stomp_Frame();
  137. $frame->setCommand('testing');
  138. $frame->setHeader('testing',1);
  139. $frame->setBody('hello world');
  140. $client = new Zend_Queue_Stomp_Client();
  141. $client->addConnection('tcp', 'localhost', '11232', 'Zend_Queue_Stomp_Connection_Mock');
  142. $client->send($frame);
  143. $this->assertTrue($client->canRead());
  144. $test = $client->receive();
  145. $this->assertEquals('testing', $test->getCommand());
  146. $this->assertEquals(1, $test->getHeader('testing'));
  147. $this->assertEquals('hello world', $test->getBody());
  148. }
  149. }