QueueBaseTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. /** TestHelper.php */
  30. require_once dirname(__FILE__) . '/../../TestHelper.php';
  31. /** Zend_Config */
  32. require_once 'Zend/Config.php';
  33. /** Zend_Queue */
  34. require_once 'Zend/Queue.php';
  35. /** Zend_Queue */
  36. require_once 'Zend/Queue/Message.php';
  37. /** Zend_Queue_Adapter_Array */
  38. require_once 'Zend/Queue/Adapter/Array.php';
  39. /**
  40. * @category Zend
  41. * @package Zend_Queue
  42. * @subpackage UnitTests
  43. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. * @version $Id: AllTests.php 13626 2009-01-14 18:24:57Z matthew $
  46. */
  47. abstract class Zend_Queue_QueueBaseTest extends PHPUnit_Framework_TestCase
  48. {
  49. protected function setUp()
  50. {
  51. // Test Zend_Config
  52. $this->config = array(
  53. 'name' => 'queue1',
  54. );
  55. $this->queue = new Zend_Queue('Null', $this->config);
  56. }
  57. protected function tearDown()
  58. {
  59. }
  60. public function testConst()
  61. {
  62. $this->assertTrue(is_string(Zend_Queue::TIMEOUT));
  63. $this->assertTrue(is_integer(Zend_Queue::VISIBILITY_TIMEOUT));
  64. $this->assertTrue(is_string(Zend_Queue::NAME));
  65. }
  66. /**
  67. * Constructor
  68. *
  69. * @param string|Zend_Queue_Adapter_Abstract $adapter
  70. * @param array $config
  71. */
  72. public function testConstruct()
  73. {
  74. // Test Zend_Config
  75. $config = array(
  76. 'name' => 'queue1',
  77. 'params' => array(),
  78. 'adapter' => 'array'
  79. );
  80. $zend_config = new Zend_Config($config);
  81. $obj = new Zend_Queue($config);
  82. $this->assertTrue($obj instanceof Zend_Queue);
  83. $obj = new Zend_Queue($zend_config);
  84. $this->assertTrue($obj instanceof Zend_Queue);
  85. try {
  86. $obj = new Zend_Queue('ops');
  87. $this->fail('Zend_Queue cannot accept a string');
  88. } catch (Exception $e) {
  89. $this->assertTrue(true);
  90. }
  91. }
  92. public function testDebugInfo()
  93. {
  94. $this->assertTrue(is_array($this->queue->debugInfo()));
  95. // var_dump($this->queue->debugInfo());
  96. }
  97. public function testGetOptions()
  98. {
  99. $options = $this->queue->getOptions();
  100. $this->assertTrue(is_array($options));
  101. $this->assertEquals($this->config['name'], $options['name']);
  102. }
  103. public function testSetAndGetAdapter()
  104. {
  105. $adapter = new Zend_Queue_Adapter_Array($this->config);
  106. $this->assertTrue($this->queue->setAdapter($adapter) instanceof Zend_Queue);
  107. $this->assertTrue($this->queue->getAdapter($adapter) instanceof Zend_Queue_Adapter_Array);
  108. }
  109. public function testSetAndGetMessageClass()
  110. {
  111. $class = 'test';
  112. $this->assertTrue($this->queue->setMessageClass($class) instanceof Zend_Queue);
  113. $this->assertEquals($class, $this->queue->getMessageClass());
  114. }
  115. public function testSetAndGetMessageSetClass()
  116. {
  117. $class = 'test';
  118. $this->assertTrue($this->queue->setMessageSetClass($class) instanceof Zend_Queue);
  119. $this->assertEquals($class, $this->queue->getMessageSetClass());
  120. }
  121. public function testSetAndGetName()
  122. {
  123. $this->assertEquals($this->config['name'], $this->queue->getName());
  124. }
  125. public function testCreateAndDeleteQueue()
  126. {
  127. // parameter testing
  128. try {
  129. $this->queue->createQueue(array());
  130. $this->fail('createQueue() $name must be a string');
  131. } catch (Exception $e) {
  132. $this->assertTrue(true);
  133. }
  134. try {
  135. $this->queue->createQueue('test', 'test');
  136. $this->fail('createQueue() $timeout must be an integer');
  137. } catch (Exception $e) {
  138. $this->assertTrue(true);
  139. }
  140. // isExists
  141. $queue = 'test';
  142. $new = $this->queue->createQueue($queue);
  143. $this->assertTrue($new instanceof Zend_Queue);
  144. // createQueue() will return true if the adapter cannot
  145. // do isExist($queue);
  146. // $this->assertFalse($this->queue->createQueue($queue));
  147. if ($new->isSupported('deleteQueue')) {
  148. $this->assertTrue($new->deleteQueue());
  149. }
  150. }
  151. public function testSendAndCountAndReceiveAndDeleteMessage()
  152. {
  153. if (! $this->queue->isSupported('send')
  154. && ! $this->queue->isSupported('receive')
  155. && ! $this->queue->isSupported('count')) {
  156. $this->markTestSkipped('send/count/receive are not supported');
  157. return;
  158. }
  159. // ------------------------------------ send()
  160. // parameter verification
  161. try {
  162. $this->queue->send(array());
  163. $this->fail('send() $mesage must be a string');
  164. } catch (Exception $e) {
  165. $this->assertTrue(true);
  166. }
  167. $message = 'Hello world'; // never gets boring!
  168. $this->assertTrue($this->queue->send($message) instanceof Zend_Queue_Message);
  169. // ------------------------------------ count()
  170. $this->assertEquals($this->queue->count(), 1);
  171. // ------------------------------------ receive()
  172. // parameter verification
  173. try {
  174. $this->queue->receive(array());
  175. $this->fail('receive() $maxMessages must be a integer or null');
  176. } catch (Exception $e) {
  177. $this->assertTrue(true);
  178. }
  179. try {
  180. $this->queue->receive(1, array());
  181. $this->fail('receive() $timeout must be a integer or null');
  182. } catch (Exception $e) {
  183. $this->assertTrue(true);
  184. }
  185. $messages = $this->queue->receive();
  186. $this->assertTrue($messages instanceof Zend_Queue_Message_Iterator);
  187. // ------------------------------------ deleteMessage()
  188. foreach ($messages as $i => $message) {
  189. $this->assertTrue($this->queue->deleteMessage($message));
  190. }
  191. }
  192. public function testCapabilities()
  193. {
  194. $list = $this->queue->getCapabilities();
  195. $this->assertTrue(is_array($list));
  196. // these functions must have an boolean answer
  197. $func = array(
  198. 'create', 'delete', 'send', 'receive',
  199. 'deleteMessage', 'getQueues', 'count',
  200. 'isExists'
  201. );
  202. foreach ( array_values($func) as $f ) {
  203. $this->assertTrue(isset($list[$f]));
  204. $this->assertTrue(is_bool($list[$f]));
  205. }
  206. }
  207. public function testIsSupported()
  208. {
  209. $list = $this->queue->getCapabilities();
  210. foreach ( $list as $function => $result ) {
  211. $this->assertTrue(is_bool($result));
  212. if ( $result ) {
  213. $this->assertTrue($this->queue->isSupported($function));
  214. } else {
  215. $this->assertFalse($this->queue->isSupported($function));
  216. }
  217. }
  218. }
  219. public function testGetQueues()
  220. {
  221. if ($this->queue->isSupported('getQueues')) {
  222. $queues = $this->queue->getQueues();
  223. $this->assertTrue(is_array($queues));
  224. $this->assertTrue(in_array($this->config['name'], $queues));
  225. } else {
  226. try {
  227. $queues = $this->queue->getQueues();
  228. $this->fail('getQueues() should have thrown an error');
  229. } catch (Exception $e) {
  230. $this->assertTrue(true);
  231. }
  232. }
  233. }
  234. }