PageRenderTime 33ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/ZendFramework/tests/Zend/Queue/Adapter/PlatformJobQueueTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 329 lines | 194 code | 61 blank | 74 comment | 16 complexity | 7750726e50753f563ae99fe81b44a37d 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: PlatformJobQueueTest.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /** Zend_Queue */
  23. require_once 'Zend/Queue.php';
  24. /** Zend_Queue */
  25. require_once 'Zend/Queue/Message.php';
  26. /** Zend_Queue_Message_Test */
  27. require_once 'MessageTestClass.php';
  28. /** Base Adapter test class */
  29. require_once dirname(__FILE__) . '/AdapterTest.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Queue
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Queue
  37. */
  38. class Zend_Queue_Adapter_PlatformJobQueueTest extends Zend_Queue_Adapter_AdapterTest
  39. {
  40. public function setUp()
  41. {
  42. if (!defined('TESTS_ZEND_QUEUE_PLATFORMJQ_HOST')
  43. || !constant('TESTS_ZEND_QUEUE_PLATFORMJQ_HOST')
  44. ) {
  45. $this->markTestSkipped();
  46. }
  47. }
  48. /**
  49. * getAdapterName() is a method to help make AdapterTest work with any
  50. * new adapters
  51. *
  52. * You must overload this method
  53. *
  54. * @return string
  55. */
  56. public function getAdapterName()
  57. {
  58. return 'PlatformJobQueue';
  59. }
  60. public function getTestConfig()
  61. {
  62. return array('daemonOptions' => array(
  63. 'host' => constant('TESTS_ZEND_QUEUE_PLATFORMJQ_HOST'),
  64. 'password' => constant('TESTS_ZEND_QUEUE_PLATFORMJQ_PASS'),
  65. ));
  66. }
  67. /**
  68. * getAdapterFullName() is a method to help make AdapterTest work with any
  69. * new adapters
  70. *
  71. * You may overload this method. The default return is
  72. * 'Zend_Queue_Adapter_' . $this->getAdapterName()
  73. *
  74. * @return string
  75. */
  76. public function getAdapterFullName()
  77. {
  78. return 'Zend_Queue_Adapter_' . $this->getAdapterName();
  79. }
  80. public function testFailedConstructor()
  81. {
  82. try {
  83. $queue = $this->createQueue(__FUNCTION__, array());
  84. $this->fail('The test should fail if no host and password are passed');
  85. } catch (Exception $e) {
  86. $this->assertTrue( true, 'Job Queue host and password should be provided');
  87. }
  88. try {
  89. $queue = $this->createQueue(__FUNCTION__, array('daemonOptions' => array()));
  90. $this->fail('The test should fail if no host is passed');
  91. } catch (Exception $e) {
  92. $this->assertTrue(true, 'Platform Job Queue host should be provided');
  93. }
  94. try {
  95. $queue = $this->createQueue(__FUNCTION__, array('daemonOptions' => array('host' => 'localhost')));
  96. $this->fail('The test should fail if no password is passed');
  97. } catch (Exception $e) {
  98. $this->assertTrue(true, 'Platform Job Queue password should be provided');
  99. }
  100. }
  101. // this tests the configuration option $config['messageClass']
  102. public function testZendQueueMessageTest()
  103. {
  104. $config = $this->getTestConfig();
  105. if (!$queue = $this->createQueue(__FUNCTION__, $config)) {
  106. return;
  107. }
  108. $message = $queue->send(array('script' => 'info.php'));
  109. $this->assertTrue($message instanceof Zend_Queue_Message);
  110. $list = $queue->receive();
  111. $this->assertTrue($list instanceof Zend_Queue_Message_Iterator);
  112. foreach ( $list as $message ) {
  113. $this->assertTrue($message instanceof Zend_Queue_Message_PlatformJob);
  114. $queue->deleteMessage($message);
  115. }
  116. // delete the queue we created
  117. $queue->deleteQueue();
  118. }
  119. public function testSend()
  120. {
  121. if (!$queue = $this->createQueue(__FUNCTION__)) {
  122. return;
  123. }
  124. $adapter = $queue->getAdapter();
  125. $message = $adapter->send(array('script' => 'info.php'));
  126. $this->assertTrue($message instanceof Zend_Queue_Message);
  127. $list = $queue->receive();
  128. $this->assertTrue($list instanceof Zend_Queue_Message_Iterator);
  129. foreach ($list as $message) {
  130. $this->assertTrue($message instanceof Zend_Queue_Message_PlatformJob);
  131. $queue->deleteMessage($message);
  132. }
  133. // delete the queue we created
  134. $queue->deleteQueue();
  135. }
  136. public function testReceive()
  137. {
  138. if (!$queue = $this->createQueue(__FUNCTION__)) {
  139. return;
  140. }
  141. $adapter = $queue->getAdapter();
  142. // check to see if this function is supported
  143. $func = 'receive';
  144. if (!$adapter->isSupported($func)) {
  145. $this->markTestSkipped($func . '() is not supported');
  146. return;
  147. }
  148. $scriptName = 'info.php';
  149. // send the message
  150. $message = $adapter->send((array('script' => $scriptName)));
  151. $this->assertTrue($message instanceof Zend_Queue_Message);
  152. // get it back
  153. $list = $adapter->receive(1);
  154. $this->assertEquals(1, count($list));
  155. $this->assertTrue($list instanceof Zend_Queue_Message_Iterator);
  156. $this->assertTrue($list->valid());
  157. $message = $list->current();
  158. if ($adapter->isSupported('deleteMessage')) {
  159. $adapter->deleteMessage($list->current());
  160. }
  161. $this->assertTrue($message instanceof Zend_Queue_Message);
  162. $this->assertEquals($message->getJob()->getScript(), $scriptName);
  163. // delete the queue we created
  164. $queue->deleteQueue();
  165. }
  166. public function testDeleteMessage()
  167. {
  168. if (!$queue = $this->createQueue(__FUNCTION__)) {
  169. return;
  170. }
  171. $adapter = $queue->getAdapter();
  172. // check to see if this function is supported
  173. $func = 'receive';
  174. if (!$adapter->isSupported($func)) {
  175. $this->markTestSkipped($func . '() is not supported');
  176. return;
  177. }
  178. $scriptName = 'info.php';
  179. // send the message
  180. $message = $adapter->send((array('script' => $scriptName)));
  181. $this->assertTrue($message instanceof Zend_Queue_Message);
  182. // get it back
  183. $list = $adapter->receive(1);
  184. $this->assertEquals(1, count($list));
  185. $this->assertTrue($list instanceof Zend_Queue_Message_Iterator);
  186. $this->assertTrue($list->valid());
  187. $message = $list->current();
  188. if ($adapter->isSupported('deleteMessage')) {
  189. $adapter->deleteMessage($list->current());
  190. }
  191. $this->assertTrue($message instanceof Zend_Queue_Message);
  192. $this->assertEquals($message->getJob()->getScript(), $scriptName);
  193. $id = $message->getJob()->getID();
  194. $this->assertFalse($adapter->isJobIdExist($id));
  195. // delete the queue we created
  196. $queue->deleteQueue();
  197. }
  198. public function testCount()
  199. {
  200. if (!$queue = $this->createQueue(__FUNCTION__)) {
  201. return;
  202. }
  203. $adapter = $queue->getAdapter();
  204. // check to see if this function is supported
  205. $func = 'count';
  206. if (!$adapter->isSupported($func)) {
  207. $this->markTestSkipped($func . '() is not supported');
  208. return;
  209. }
  210. $initCount = $adapter->count();
  211. // send a message
  212. $message = $adapter->send(array('script' => 'info.php'));
  213. // test queue count for being 1
  214. $this->assertEquals($adapter->count(), ($initCount + 1));
  215. // receive the message
  216. $message = $adapter->receive();
  217. /* we need to delete the messages we put in the queue before
  218. * counting.
  219. *
  220. * not all adapters support deleteMessage, but we should remove
  221. * the messages that we created if we can.
  222. */
  223. if ($adapter->isSupported('deleteMessage')) {
  224. foreach ($message as $msg) {
  225. $adapter->deleteMessage($msg);
  226. }
  227. }
  228. // test the count for being 0
  229. $this->assertEquals($adapter->count(), $initCount);
  230. // delete the queue we created
  231. $queue->deleteQueue();
  232. }
  233. public function testSampleBehavior()
  234. {
  235. if (!$queue = $this->createQueue(__FUNCTION__)) {
  236. return;
  237. }
  238. $this->assertTrue($queue instanceof Zend_Queue);
  239. $initCount = $queue->count();
  240. $scriptName = 'info.php';
  241. for ($i = 0; $i < 10; $i++) {
  242. $queue->send(array('script' => $scriptName));
  243. }
  244. $messages = $queue->receive(5);
  245. foreach($messages as $i => $message) {
  246. $this->assertEquals($message->getJob()->getScript(), $scriptName);
  247. $queue->deleteMessage($message);
  248. }
  249. for ($i = 0; $i < 5; $i++) {
  250. $messages = $queue->receive();
  251. $message = $messages->current();
  252. $queue->deleteMessage($message);
  253. }
  254. $this->assertEquals($initCount, count($queue));
  255. $this->assertTrue($queue->deleteQueue());
  256. // delete the queue we created
  257. $queue->deleteQueue();
  258. }
  259. public function testVisibility()
  260. {
  261. $this->markTestSkipped('testVisibility unsupported');
  262. }
  263. // test the constants
  264. public function testConst()
  265. {
  266. $this->markTestSkipped('no constants to test');
  267. }
  268. }