2
0

QueueTest.php 8.6 KB

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