2
0

StompIOTest.php 6.2 KB

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