PageRenderTime 24ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Queue/QueueBaseTest.php

https://github.com/sidealice/zf2
PHP | 249 lines | 159 code | 33 blank | 57 comment | 8 complexity | c0f1a8f5fb367c654484b9dc3870c911 MD5 | raw file
  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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace ZendTest\Queue;
  25. use Zend\Queue;
  26. use Zend\Queue\Adapter;
  27. /*
  28. * The adapter test class provides a universal test class for all of the
  29. * abstract methods.
  30. *
  31. * All methods marked not supported are explictly checked for for throwing
  32. * an exception.
  33. */
  34. /**
  35. * @category Zend
  36. * @package Zend_Queue
  37. * @subpackage UnitTests
  38. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. * @group Zend_Queue
  41. */
  42. abstract class QueueBaseTest extends \PHPUnit_Framework_TestCase
  43. {
  44. protected function setUp()
  45. {
  46. // Test Zend_Config
  47. $this->config = array(
  48. 'name' => 'queue1',
  49. );
  50. $this->queue = new Queue\Queue('Null', $this->config);
  51. }
  52. protected function tearDown()
  53. {
  54. }
  55. public function testConst()
  56. {
  57. $this->assertTrue(is_string(Queue\Queue::TIMEOUT));
  58. $this->assertTrue(is_integer(Queue\Queue::VISIBILITY_TIMEOUT));
  59. $this->assertTrue(is_string(Queue\Queue::NAME));
  60. }
  61. /**
  62. * Constructor
  63. *
  64. * @param string|Zend_Queue_Adapter_Abstract $adapter
  65. * @param array $config
  66. */
  67. public function testConstruct()
  68. {
  69. // Test Zend_Config
  70. $config = array(
  71. 'name' => 'queue1',
  72. 'params' => array(),
  73. 'adapter' => 'ArrayAdapter'
  74. );
  75. $zend_config = new \Zend\Config\Config($config);
  76. $obj = new Queue\Queue($config);
  77. $this->assertTrue($obj instanceof Queue\Queue);
  78. $obj = new Queue\Queue($zend_config);
  79. $this->assertTrue($obj instanceof Queue\Queue);
  80. }
  81. public function testDebugInfo()
  82. {
  83. $this->assertTrue(is_array($this->queue->debugInfo()));
  84. // var_dump($this->queue->debugInfo());
  85. }
  86. public function testGetOptions()
  87. {
  88. $options = $this->queue->getOptions();
  89. $this->assertTrue(is_array($options));
  90. $this->assertEquals($this->config['name'], $options['name']);
  91. }
  92. public function testSetAndGetAdapter()
  93. {
  94. $adapter = new Adapter\ArrayAdapter($this->config);
  95. $this->assertTrue($this->queue->setAdapter($adapter) instanceof Queue\Queue);
  96. $this->assertTrue($this->queue->getAdapter($adapter) instanceof Adapter\ArrayAdapter);
  97. }
  98. public function testSetAndGetMessageClass()
  99. {
  100. $class = 'test';
  101. $this->assertTrue($this->queue->setMessageClass($class) instanceof Queue\Queue);
  102. $this->assertEquals($class, $this->queue->getMessageClass());
  103. }
  104. public function testSetAndGetMessageSetClass()
  105. {
  106. $class = 'test';
  107. $this->assertTrue($this->queue->setMessageSetClass($class) instanceof Queue\Queue);
  108. $this->assertEquals($class, $this->queue->getMessageSetClass());
  109. }
  110. public function testSetAndGetName()
  111. {
  112. $this->assertEquals($this->config['name'], $this->queue->getName());
  113. }
  114. public function testCreateAndDeleteQueue()
  115. {
  116. // parameter testing
  117. try {
  118. $this->queue->createQueue(array());
  119. $this->fail('createQueue() $name must be a string');
  120. } catch (\Exception $e) {
  121. $this->assertTrue(true);
  122. }
  123. try {
  124. $this->queue->createQueue('test', 'test');
  125. $this->fail('createQueue() $timeout must be an integer');
  126. } catch (\Exception $e) {
  127. $this->assertTrue(true);
  128. }
  129. // isExists
  130. $queue = 'test';
  131. $new = $this->queue->createQueue($queue);
  132. $this->assertTrue($new instanceof Queue\Queue);
  133. // createQueue() will return true if the adapter cannot
  134. // do isExist($queue);
  135. // $this->assertFalse($this->queue->createQueue($queue));
  136. if ($new->isSupported('deleteQueue')) {
  137. $this->assertTrue($new->deleteQueue());
  138. }
  139. }
  140. public function testSendAndCountAndReceiveAndDeleteMessage()
  141. {
  142. if (! $this->queue->isSupported('send')
  143. && ! $this->queue->isSupported('receive')
  144. && ! $this->queue->isSupported('count')) {
  145. $this->markTestSkipped('send/count/receive are not supported');
  146. return;
  147. }
  148. // ------------------------------------ send()
  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, var_export($this->queue->getAdapter()->getData(), 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\MessageIterator);
  169. // ------------------------------------ deleteMessage()
  170. foreach ($messages as $i => $message) {
  171. $this->assertTrue($this->queue->deleteMessage($message));
  172. }
  173. }
  174. public function testCapabilities()
  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 testIsSupported()
  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 testGetQueues()
  202. {
  203. if ($this->queue->isSupported('getQueues')) {
  204. $queues = $this->queue->getQueues();
  205. $this->assertTrue(is_array($queues));
  206. $this->assertTrue(in_array($this->config['name'], $queues));
  207. } else {
  208. try {
  209. $queues = $this->queue->getQueues();
  210. $this->fail('getQueues() should have thrown an error');
  211. } catch (\Exception $e) {
  212. $this->assertTrue(true);
  213. }
  214. }
  215. }
  216. }