ActivemqOfflineTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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: ActivemqTest.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /** PHPUnit Test Case */
  23. require_once 'PHPUnit/Framework/TestCase.php';
  24. /** TestHelp.php */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. require_once 'Zend/Queue/Adapter/Activemq.php';
  27. require_once 'Zend/Queue/Stomp/Client.php';
  28. require_once 'Zend/Queue/Stomp/Frame.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Queue
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Queue
  36. */
  37. class Zend_Queue_Adapter_ActivemqOfflineTest extends PHPUnit_Framework_TestCase
  38. {
  39. /**
  40. * @group ZF-7948
  41. */
  42. public function testSubscribesOncePerQueue()
  43. {
  44. $stompClient = new StompClientMock;
  45. $options['driverOptions']['stompClient'] = $stompClient;
  46. $adapter = new Zend_Queue_Adapter_Activemq($options);
  47. $queue = new Zend_Queue('array', array('name' => 'foo'));
  48. $adapter->receive(null, null, $queue);
  49. $adapter->receive(null, null, $queue);
  50. // iterate through mock StompClient and ensure SUBSCRIBE is only sent once per queue
  51. $subscribes = 0;
  52. foreach ($stompClient->frameStack as $frame) {
  53. if ($frame->getCommand() === 'SUBSCRIBE') {
  54. $subscribes++;
  55. }
  56. }
  57. $this->assertEquals(1, $subscribes);
  58. }
  59. }
  60. class StompClientMock extends Zend_Queue_Stomp_Client
  61. {
  62. public $frameStack = array();
  63. public $responseStack = array();
  64. public function __construct() {
  65. // spoof a successful connection in the response stack
  66. $frame = new Zend_Queue_Stomp_Frame;
  67. $frame->setCommand('CONNECTED');
  68. $this->responseStack[] = $frame;
  69. }
  70. public function __destruct() {}
  71. public function send(Zend_Queue_Stomp_FrameInterface $frame)
  72. {
  73. $this->frameStack[] = $frame;
  74. return $this;
  75. }
  76. public function receive()
  77. {
  78. return array_shift($this->responseStack);
  79. }
  80. public function canRead()
  81. {
  82. return count($this->responseStack) > 0;
  83. }
  84. public function createFrame()
  85. {
  86. return new Zend_Queue_Stomp_Frame;
  87. }
  88. }