QueueTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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$
  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-2010 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. * @see Zend_Log
  55. */
  56. require_once 'Zend/Log.php';
  57. require_once 'Zend/Log/Writer/Stream.php';
  58. require_once 'Zend/Log/Writer/Null.php';
  59. if (! isset($this->logger)) {
  60. if (1) { // vebose?
  61. $this->_logger = new Zend_Log(new Zend_Log_Writer_Stream('php://output'));
  62. } else {
  63. $this->_logger = new Zend_Log(new Zend_Log_Writer_Null());
  64. }
  65. }
  66. $this->queue->setLogger($this->_logger);
  67. }
  68. protected function tearDown()
  69. {
  70. }
  71. public function testConst()
  72. {
  73. $this->assertTrue(is_string(Zend_Queue::TIMEOUT));
  74. $this->assertTrue(is_integer(Zend_Queue::VISABILITY_TIMEOUT));
  75. $this->assertTrue(is_string(Zend_Queue::NAME));
  76. }
  77. /**
  78. * Constructor
  79. *
  80. * @param string|Zend_Queue_Adapter_Abstract $adapter
  81. * @param array $config
  82. */
  83. public function testConstruct()
  84. {
  85. // Test Zend_Config
  86. $config = array(
  87. 'name' => 'queue1',
  88. 'params' => array(),
  89. 'adapter' => 'array'
  90. );
  91. $zend_config = new Zend_Config($config);
  92. $obj = new Zend_Queue($config);
  93. $this->assertTrue($obj instanceof Zend_Queue);
  94. // test logger
  95. $this->assertTrue($obj->getLogger() instanceof Zend_Log);
  96. $obj = new Zend_Queue($zend_config);
  97. $this->assertTrue($obj instanceof Zend_Queue);
  98. try {
  99. $obj = new Zend_Queue('ops');
  100. $this->fail('Zend_Queue cannot accept a string');
  101. } catch (Exception $e) {
  102. $this->assertTrue(true);
  103. }
  104. }
  105. public function test_getConfig()
  106. {
  107. $config = $this->queue->getConfig();
  108. $this->assertTrue(is_array($config));
  109. $this->assertEquals($this->config['name'], $config['name']);
  110. }
  111. public function test_set_getAdapter()
  112. {
  113. $adapter = new Zend_Queue_Adapter_Array($this->config);
  114. $this->assertTrue($this->queue->setAdapter($adapter) instanceof Zend_Queue);
  115. $this->assertTrue($this->queue->getAdapter($adapter) instanceof Zend_Queue_Adapter_Array);
  116. }
  117. public function test_set_getMessageClass()
  118. {
  119. $class = 'test';
  120. $this->assertTrue($this->queue->setMessageClass($class) instanceof Zend_Queue);
  121. $this->assertEquals($class, $this->queue->getMessageClass());
  122. }
  123. public function test_set_getMessageSetClass()
  124. {
  125. $class = 'test';
  126. $this->assertTrue($this->queue->setMessageSetClass($class) instanceof Zend_Queue);
  127. $this->assertEquals($class, $this->queue->getMessageSetClass());
  128. }
  129. public function test_set_getName()
  130. {
  131. // $this->assertTrue($this->queue->setName($new) instanceof Zend_Queue);
  132. $this->assertEquals($this->config['name'], $this->queue->getName());
  133. }
  134. public function test_create_deleteQueue()
  135. {
  136. // parameter testing
  137. try {
  138. $this->queue->createQueue(array());
  139. $this->fail('createQueue() $name must be a string');
  140. } catch (Exception $e) {
  141. $this->assertTrue(true);
  142. }
  143. try {
  144. $this->queue->createQueue('test', 'test');
  145. $this->fail('createQueue() $timeout must be an integer');
  146. } catch (Exception $e) {
  147. $this->assertTrue(true);
  148. }
  149. // isExists
  150. $queue = 'test';
  151. $new = $this->queue->createQueue($queue);
  152. $this->assertTrue($new instanceof Zend_Queue);
  153. $this->assertFalse($this->queue->createQueue($queue));
  154. $this->assertTrue($new->deleteQueue());
  155. }
  156. public function test_send_count_receive_deleteMessage()
  157. {
  158. // ------------------------------------ send()
  159. // parameter verification
  160. try {
  161. $this->queue->send(array());
  162. $this->fail('send() $mesage must be a string');
  163. } catch (Exception $e) {
  164. $this->assertTrue(true);
  165. }
  166. $message = 'Hello world'; // never gets boring!
  167. $this->assertTrue($this->queue->send($message) instanceof Zend_Queue_Message);
  168. // ------------------------------------ count()
  169. $this->assertEquals($this->queue->count(), 1);
  170. // ------------------------------------ receive()
  171. // parameter verification
  172. try {
  173. $this->queue->receive(array());
  174. $this->fail('receive() $maxMessages must be a integer or null');
  175. } catch (Exception $e) {
  176. $this->assertTrue(true);
  177. }
  178. try {
  179. $this->queue->receive(1, array());
  180. $this->fail('receive() $timeout must be a integer or null');
  181. } catch (Exception $e) {
  182. $this->assertTrue(true);
  183. }
  184. $messages = $this->queue->receive();
  185. $this->assertTrue($messages instanceof Zend_Queue_Message_Iterator);
  186. // ------------------------------------ deleteMessage()
  187. foreach ($messages as $i => $message) {
  188. $this->assertTrue($this->queue->deleteMessage($message));
  189. }
  190. }
  191. public function test_set_getLogger()
  192. {
  193. /**
  194. * @see Zend_Log
  195. */
  196. require_once 'Zend/Log.php';
  197. require_once 'Zend/Log/Writer/Null.php';
  198. $logger = new Zend_Log(new Zend_Log_Writer_Null);
  199. $this->assertTrue($this->queue->setLogger($logger) instanceof Zend_Queue);
  200. $this->assertTrue($this->queue->getLogger() instanceof Zend_Log);
  201. // parameter verification
  202. try {
  203. $this->queue->setLogger(array());
  204. $this->fail('setlogger() passed an array and succeeded (bad)');
  205. } catch (Exception $e) {
  206. $this->assertTrue(true);
  207. }
  208. }
  209. public function test_capabilities()
  210. {
  211. $list = $this->queue->getCapabilities();
  212. $this->assertTrue(is_array($list));
  213. // these functions must have an boolean answer
  214. $func = array(
  215. 'create', 'delete', 'send', 'receive',
  216. 'deleteMessage', 'getQueues', 'count',
  217. 'isExists'
  218. );
  219. foreach ( array_values($func) as $f ) {
  220. $this->assertTrue(isset($list[$f]));
  221. $this->assertTrue(is_bool($list[$f]));
  222. }
  223. }
  224. public function test_isSupported()
  225. {
  226. $list = $this->queue->getCapabilities();
  227. foreach ( $list as $function => $result ) {
  228. $this->assertTrue(is_bool($result));
  229. if ( $result ) {
  230. $this->assertTrue($this->queue->isSupported($function));
  231. } else {
  232. $this->assertFalse($this->queue->isSupported($function));
  233. }
  234. }
  235. }
  236. public function test_getQueues()
  237. {
  238. $queues = $this->queue->getQueues();
  239. $this->assertTrue(is_array($queues));
  240. $this->assertTrue(in_array($this->config['name'], $queues));
  241. }
  242. }