2
0

CustomTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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-2009 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. * This test code tests the customization functions provided in the example
  24. * documentation code.
  25. */
  26. /** PHPUnit Test Case */
  27. require_once 'PHPUnit/Framework/TestCase.php';
  28. /** TestHelp.php */
  29. require_once dirname(__FILE__) . '/../../TestHelper.php';
  30. /** Custom_Queue */
  31. require_once 'Custom/Queue.php';
  32. /** Custom_Message */
  33. require_once 'Custom/Message.php';
  34. /** Custom_Messages */
  35. require_once 'Custom/Messages.php';
  36. /**
  37. * @category Zend
  38. * @package Zend_Queue
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Queue
  43. */
  44. class Custom_Object {
  45. public $a;
  46. public function __construct()
  47. {
  48. $a = rand(1,200);
  49. }
  50. public function getA()
  51. {
  52. return $this->a;
  53. }
  54. public function setA($a)
  55. {
  56. $this->a = $a;
  57. }
  58. public function __sleep()
  59. {
  60. return array('a'); // serialize only this variable
  61. }
  62. }
  63. class Zend_Queue_CustomTest extends PHPUnit_Framework_TestCase
  64. {
  65. public function test_behavior()
  66. {
  67. $object_count = 10;
  68. $objects = array();
  69. $queue = new Custom_Queue('Array', array('name'=>'ObjectA'));
  70. $this->assertTrue($queue instanceof Custom_Queue);
  71. // ------------------------------------------------ send
  72. // add items $objects[0-4]
  73. $objects = array();
  74. for ($i = 0; $i < $object_count-5; $i++) {
  75. $object = new Custom_Object();
  76. $queue->send(new Custom_Message($object));
  77. $objects[] = $object;
  78. }
  79. // add items $objects[5-9]
  80. $messages = new Custom_Messages();
  81. for ($i = 0; $i < 5; $i++) {
  82. $object = new Custom_Object();
  83. $messages->append( new Custom_Message($object));
  84. $objects[] = $object;
  85. }
  86. $queue->send($messages);
  87. $this->assertEquals($object_count, count($queue));
  88. unset($messages);
  89. // ------------------------------------------------ receive
  90. // get the first 5 doing 0-4
  91. $receive = $queue->receive(5);
  92. $this->assertTrue($receive instanceof Custom_Messages);
  93. $this->assertEquals(5, count($receive));
  94. // test them
  95. for ($index = 0; $index < 5; $index++) {
  96. $this->assertEquals($objects[$index]->getA(), $receive[$index]->getBody()->getA());
  97. try {
  98. unset($receive[$index]);
  99. $this->assertTrue(true, '$receive[$index] successfully deleted');
  100. } catch(Zend_Queue_Exception $e) {
  101. $this->fail('$receive[$index] should have been deleted' . $e->getMessage());
  102. }
  103. }
  104. // there should only be 5 objects left
  105. $this->assertEquals($object_count - $index, count($queue));
  106. // get 1 doing $objects[5]
  107. $receive = $queue->receive();
  108. $index++;
  109. $this->assertTrue($receive instanceof Custom_Messages);
  110. $this->assertEquals(1, count($receive));
  111. // testing Custom_Messages::__deconstruct()
  112. unset($receive);
  113. $this->assertEquals($object_count - $index, count($queue));
  114. // get all the rest doing 6-20
  115. $receive = $queue->receive($object_count - $index);
  116. $this->assertTrue($receive instanceof Custom_Messages);
  117. $this->assertEquals($object_count - $index, count($receive));
  118. // test them
  119. $r_index = -1;
  120. for (; $index < $object_count; $index++) {
  121. $r_index++;
  122. $this->assertEquals($objects[$index]->getA(), $receive[$r_index]->getBody()->getA());
  123. try {
  124. unset($receive[$r_index]);
  125. $this->assertTrue(true, '$receive[$index] successfully deleted');
  126. } catch(Zend_Queue_Exception $e) {
  127. $this->fail('$receive[$index] should have been deleted' . $e->getMessage());
  128. }
  129. }
  130. // auto-delete should have been called on $receive
  131. $this->assertEquals(0, count($queue));
  132. }
  133. }