StompIOTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. /** Zend/Queue/Adapter/Stomp/IO.php */
  30. require_once 'Zend/Queue/Adapter/Stomp/IO.php';
  31. /**
  32. * @category Zend
  33. * @package Zend_Queue
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Queue
  38. */
  39. class Zend_Queue_Adapter_StompIOTest extends PHPUnit_Framework_TestCase
  40. {
  41. protected $config = array(
  42. 'scheme' => 'tcp',
  43. 'host' => '127.0.0.1',
  44. 'port' => 61613,
  45. );
  46. protected $io = false;
  47. protected $body = 'hello world'; // 11 characters
  48. public function setUp()
  49. {
  50. if ( $this->io === false ) {
  51. $this->io = new Zend_Queue_Adapter_Stomp_IO($this->config);
  52. }
  53. }
  54. public function test_constructFrame()
  55. {
  56. $frame = $this->io->constructFrame('SEND', array(), $this->body);
  57. $correct = 'SEND' . Zend_Queue_Adapter_Stomp_IO::EOL;
  58. $correct .= 'content-length: 11' . Zend_Queue_Adapter_Stomp_IO::EOL;
  59. $correct .= Zend_Queue_Adapter_Stomp_IO::EOL;
  60. $correct .= $this->body;
  61. $correct .= Zend_Queue_Adapter_Stomp_IO::END_OF_FRAME;
  62. $this->assertEquals($frame, $correct);
  63. // validate parameters
  64. try {
  65. $frame = $this->io->constructFrame(array());
  66. $this->fail('constructFrame() should have failed $action as an array');
  67. } catch (Exception $e) {
  68. $this->assertTrue(true);
  69. }
  70. try { // this won't test, I think because phpunit suppresses the error
  71. $frame = $this->io->constructFrame('SEND', 'string');
  72. $this->fail('constructFrame() should have failed $headers as a string');
  73. } catch (Exception $e) {
  74. $this->assertTrue(true);
  75. }
  76. try { // this won't test, I think because phpunit suppresses the error
  77. $frame = $this->io->constructFrame('SEND', array(), array());
  78. $this->fail('constructFrame() should have failed $body as a array');
  79. } catch (Exception $e) {
  80. $this->assertTrue(true);
  81. }
  82. }
  83. public function test_deconstructFrame()
  84. {
  85. $correct = array(
  86. 'headers' => array(),
  87. 'body' => $this->body,
  88. 'command' => 'SEND'
  89. );
  90. $frame = $this->io->constructFrame($correct['command'], array(), $correct['body']);
  91. $frame = $this->io->deconstructFrame($frame);
  92. $this->assertEquals($correct['command'], $frame['command']);
  93. $this->assertEquals($correct['body'], $frame['body']);
  94. // validate parameters
  95. try {
  96. $frame = $this->io->deconstructFrame(array());
  97. $this->fail('deconstructFrame() should have failed with an array');
  98. } catch (Exception $e) {
  99. $this->assertTrue(true);
  100. }
  101. }
  102. public function test_write_read()
  103. {
  104. $frame = $this->io->constructFrame('CONNECT');
  105. $frame = $this->io->writeAndRead($frame);
  106. $headers = array(
  107. 'destination' => '/queue/testing',
  108. 'ack' => 'auto'
  109. );
  110. $frame = $this->io->constructFrame('SEND', $headers, $this->body);
  111. $this->io->write($frame);
  112. $frame = $this->io->constructFrame('SUBSCRIBE', $headers);
  113. $this->io->write($frame);
  114. $frame = $this->io->read();
  115. $frame = $this->io->deconstructFrame($frame);
  116. $this->assertEquals($this->body, $frame['body']);
  117. // validate parameters
  118. try {
  119. $frame = $this->io->write(array());
  120. $this->fail('write() should have failed with an array');
  121. } catch (Exception $e) {
  122. $this->assertTrue(true);
  123. }
  124. }
  125. public function test_open_close()
  126. {
  127. try {
  128. $obj = new Zend_Queue_Adapter_Stomp_IO($this->config);
  129. } catch (Exception $e) {
  130. $this->fail('failed to create Zend_Queue_Adapter_Stomp_IO object:' . $e->getMessage());
  131. }
  132. try {
  133. $obj->close();
  134. } catch (Exception $e) {
  135. $this->fail('failed to close Zend_Queue_Adapter_Stomp_IO object:' . $e->getMessage());
  136. }
  137. // validate parameters
  138. $config = array(
  139. 'scheme' => 'tcp',
  140. 'host' => 'blahblahb asfd',
  141. 'port' => '0'
  142. );
  143. try {
  144. $frame = $this->io->open($config);
  145. $this->fail('open() should have failed with an invalid configuration');
  146. } catch (Exception $e) {
  147. $this->assertTrue(true);
  148. }
  149. }
  150. public function test_constant()
  151. {
  152. $this->assertTrue(is_string(Zend_Queue_Adapter_Stomp_IO::END_OF_FRAME));
  153. $this->assertTrue(is_string(Zend_Queue_Adapter_Stomp_IO::CONTENT_LENGTH));
  154. $this->assertTrue(is_string(Zend_Queue_Adapter_Stomp_IO::EOL));
  155. }
  156. public function test_checkSocket()
  157. {
  158. $this->assertTrue($this->io->checkSocket());
  159. $this->io->close();
  160. try {
  161. $this->io->checkSocket();
  162. $this->fail('checkSocket() should have failed on a fclose($socket)');
  163. } catch (Exception $e) {
  164. $this->assertTrue(true);
  165. }
  166. }
  167. }