StompIOTest.php 6.0 KB

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