QueueTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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-2015 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 */
  30. require_once 'Zend/Queue.php';
  31. /** Zend_Queue */
  32. require_once 'Zend/Queue/Message.php';
  33. /** Zend_Queue_Adapter_Array */
  34. require_once 'Zend/Queue/Adapter/Array.php';
  35. /**
  36. * @category Zend
  37. * @package Zend_Queue
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2015 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_QueueTest extends PHPUnit_Framework_TestCase
  44. {
  45. protected function setUp()
  46. {
  47. // Test Zend_Config
  48. $this->config = array(
  49. 'name' => 'queue1',
  50. 'params' => array(),
  51. );
  52. $this->queue = new Zend_Queue('array', $this->config);
  53. }
  54. protected function tearDown()
  55. {
  56. }
  57. public function testConst()
  58. {
  59. $this->assertTrue(is_string(Zend_Queue::TIMEOUT));
  60. $this->assertTrue(is_integer(Zend_Queue::VISIBILITY_TIMEOUT));
  61. $this->assertTrue(is_string(Zend_Queue::NAME));
  62. }
  63. /**
  64. * Constructor
  65. *
  66. * @param string|Zend_Queue_Adapter_Abstract $adapter
  67. * @param array $config
  68. */
  69. public function testConstruct()
  70. {
  71. // Test Zend_Config
  72. $config = array(
  73. 'name' => 'queue1',
  74. 'params' => array(),
  75. 'adapter' => 'array'
  76. );
  77. require_once "Zend/Config.php";
  78. $zend_config = new Zend_Config($config);
  79. $obj = new Zend_Queue($config);
  80. $this->assertTrue($obj instanceof Zend_Queue);
  81. try {
  82. $obj = new Zend_Queue('ops');
  83. $this->fail('Zend_Queue cannot accept a string');
  84. } catch (Exception $e) {
  85. $this->assertTrue(true);
  86. }
  87. }
  88. public function test_getOptions()
  89. {
  90. $config = $this->queue->getOptions();
  91. $this->assertTrue(is_array($config));
  92. $this->assertEquals($this->config['name'], $config['name']);
  93. }
  94. public function test_set_getAdapter()
  95. {
  96. $adapter = new Zend_Queue_Adapter_Array($this->config);
  97. $this->assertTrue($this->queue->setAdapter($adapter) instanceof Zend_Queue);
  98. $this->assertTrue($this->queue->getAdapter($adapter) instanceof Zend_Queue_Adapter_Array);
  99. }
  100. public function test_set_getMessageClass()
  101. {
  102. $class = 'test';
  103. $this->assertTrue($this->queue->setMessageClass($class) instanceof Zend_Queue);
  104. $this->assertEquals($class, $this->queue->getMessageClass());
  105. }
  106. public function test_set_getMessageSetClass()
  107. {
  108. $class = 'test';
  109. $this->assertTrue($this->queue->setMessageSetClass($class) instanceof Zend_Queue);
  110. $this->assertEquals($class, $this->queue->getMessageSetClass());
  111. }
  112. public function test_set_getName()
  113. {
  114. // $this->assertTrue($this->queue->setName($new) instanceof Zend_Queue);
  115. $this->assertEquals($this->config['name'], $this->queue->getName());
  116. }
  117. public function test_create_deleteQueue()
  118. {
  119. // parameter testing
  120. try {
  121. $this->queue->createQueue(array());
  122. $this->fail('createQueue() $name must be a string');
  123. } catch (Exception $e) {
  124. $this->assertTrue(true);
  125. }
  126. try {
  127. $this->queue->createQueue('test', 'test');
  128. $this->fail('createQueue() $timeout must be an integer');
  129. } catch (Exception $e) {
  130. $this->assertTrue(true);
  131. }
  132. // isExists
  133. $queue = 'test';
  134. $new = $this->queue->createQueue($queue);
  135. $this->assertTrue($new instanceof Zend_Queue);
  136. $this->assertFalse($this->queue->createQueue($queue));
  137. $this->assertTrue($new->deleteQueue());
  138. }
  139. public function test_send_count_receive_deleteMessage()
  140. {
  141. // ------------------------------------ send()
  142. // parameter verification
  143. try {
  144. $this->queue->send(array());
  145. $this->fail('send() $mesage must be a string');
  146. } catch (Exception $e) {
  147. $this->assertTrue(true);
  148. }
  149. $message = 'Hello world'; // never gets boring!
  150. $this->assertTrue($this->queue->send($message) instanceof Zend_Queue_Message);
  151. // ------------------------------------ count()
  152. $this->assertEquals($this->queue->count(), 1);
  153. // ------------------------------------ receive()
  154. // parameter verification
  155. try {
  156. $this->queue->receive(array());
  157. $this->fail('receive() $maxMessages must be a integer or null');
  158. } catch (Exception $e) {
  159. $this->assertTrue(true);
  160. }
  161. try {
  162. $this->queue->receive(1, array());
  163. $this->fail('receive() $timeout must be a integer or null');
  164. } catch (Exception $e) {
  165. $this->assertTrue(true);
  166. }
  167. $messages = $this->queue->receive();
  168. $this->assertTrue($messages instanceof Zend_Queue_Message_Iterator);
  169. // ------------------------------------ deleteMessage()
  170. foreach ($messages as $i => $message) {
  171. $this->assertTrue($this->queue->deleteMessage($message));
  172. }
  173. }
  174. public function test_capabilities()
  175. {
  176. $list = $this->queue->getCapabilities();
  177. $this->assertTrue(is_array($list));
  178. // these functions must have an boolean answer
  179. $func = array(
  180. 'create', 'delete', 'send', 'receive',
  181. 'deleteMessage', 'getQueues', 'count',
  182. 'isExists'
  183. );
  184. foreach ( array_values($func) as $f ) {
  185. $this->assertTrue(isset($list[$f]));
  186. $this->assertTrue(is_bool($list[$f]));
  187. }
  188. }
  189. public function test_isSupported()
  190. {
  191. $list = $this->queue->getCapabilities();
  192. foreach ( $list as $function => $result ) {
  193. $this->assertTrue(is_bool($result));
  194. if ( $result ) {
  195. $this->assertTrue($this->queue->isSupported($function));
  196. } else {
  197. $this->assertFalse($this->queue->isSupported($function));
  198. }
  199. }
  200. }
  201. public function test_getQueues()
  202. {
  203. $queues = $this->queue->getQueues();
  204. $this->assertTrue(is_array($queues));
  205. $this->assertTrue(in_array($this->config['name'], $queues));
  206. }
  207. }