CustomTest.php 4.6 KB

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