ClientTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: AllTests.php 13626 2009-01-14 18:24:57Z matthew $
  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. class Zend_Queue_Stomp_Connection_Mock
  38. extends Zend_Queue_Stomp_Client_Connection
  39. {
  40. /**
  41. * open() opens a socket to the Stomp server
  42. *
  43. * @param array $config ('scheme', 'host', 'port')
  44. * @return true;
  45. */
  46. public function open($scheme, $host, $port)
  47. {
  48. if ( $port == 0 ) return false;
  49. return true;
  50. }
  51. public function close($destructor = false)
  52. {
  53. }
  54. /**
  55. * Check whether we are connected to the server
  56. *
  57. * @return true
  58. * @throws Zend_Queue_Exception
  59. */
  60. public function ping()
  61. {
  62. return true;
  63. }
  64. /**
  65. * write a frame to the stomp server
  66. *
  67. * @example $response = $client->write($frame)->read();
  68. *
  69. * @param Zend_Queue_Stom_Frame $frame
  70. * @return $this
  71. */
  72. public function write(Zend_Queue_Stomp_FrameInterface $frame)
  73. {
  74. $this->_buffer[] = $frame;
  75. }
  76. /**
  77. * tests the socket to see if there is data for us
  78. */
  79. public function canRead()
  80. {
  81. return count($this->_buffer) > 0;
  82. }
  83. /**
  84. * reads in a frame from the socket or returns false.
  85. *
  86. * @return Zend_Queue_Stomp_Frame|false
  87. * @throws Zend_Queue_Exception
  88. */
  89. public function read()
  90. {
  91. if (! $this->canRead()) return false;
  92. return array_shift($this->_buffer);
  93. }
  94. }
  95. class Zend_Queue_Stomp_ClientTest extends PHPUnit_Framework_TestCase
  96. {
  97. public function testConstruct()
  98. {
  99. $client = new Zend_Queue_Stomp_Client('tcp', 'localhost', '11232', 'Zend_Queue_Stomp_Connection_Mock');
  100. $this->assertTrue($client->getConnection() instanceof Zend_Queue_Stomp_Client_ConnectionInterface);
  101. }
  102. public function testAddConnection()
  103. {
  104. $client = new Zend_Queue_Stomp_Client();
  105. $client->addConnection('tcp', 'localhost', '11232', 'Zend_Queue_Stomp_Connection_Mock');
  106. $this->assertTrue($client->getConnection() instanceof Zend_Queue_Stomp_Client_ConnectionInterface);
  107. $client = new Zend_Queue_Stomp_Client();
  108. $this->assertFalse($client->addConnection('tcp', 'localhost', 0, 'Zend_Queue_Stomp_Connection_Mock'));
  109. }
  110. public function testGetAndSetConnection()
  111. {
  112. $connection = new Zend_Queue_Stomp_Connection_Mock('tcp', 'localhost', '11232');
  113. $client = new Zend_Queue_Stomp_Client();
  114. $this->assertTrue($client->setConnection($connection) instanceof Zend_Queue_Stomp_Client);
  115. $try = $client->getConnection();
  116. $this->assertEquals($connection, $try);
  117. }
  118. public function testSendAndReceive()
  119. {
  120. $frame = new Zend_Queue_Stomp_Frame();
  121. $frame->setCommand('testing');
  122. $frame->setHeader('testing',1);
  123. $frame->setBody('hello world');
  124. $client = new Zend_Queue_Stomp_Client();
  125. $client->addConnection('tcp', 'localhost', '11232', 'Zend_Queue_Stomp_Connection_Mock');
  126. $client->send($frame);
  127. $this->assertTrue($client->canRead());
  128. $test = $client->receive();
  129. $this->assertEquals('testing', $test->getCommand());
  130. $this->assertEquals(1, $test->getHeader('testing'));
  131. $this->assertEquals('hello world', $test->getBody());
  132. }
  133. }