PageRenderTime 65ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/ZendFramework/tests/Zend/Queue/QueueBaseTest.php

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