Вход Регистрация
Файл: concrete5.7.5.6/concrete/vendor/zendframework/zend-queue/tests/ZendQueue/CustomTest.php
Строк: 119
<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 * @package   Zend_Queue
 */

namespace ZendQueueTest;

use 
ZendQueue;

/*
 * This test code tests the customization functions provided in the example
 * documentation code.
 */

/**
 * @category   Zend
 * @package    Zend_Queue
 * @subpackage UnitTests
 * @group      Zend_Queue
 */
class CustomTest extends PHPUnit_Framework_TestCase
{
    public function 
test_behavior()
    {
        
$object_count 10;
        
$objects = array();

        
$queue = new CustomQueue('ArrayAdapter', array('name'=>'ObjectA'));
        
$this->assertTrue($queue instanceof CustomQueue);

        
// ------------------------------------------------ send

        // add items $objects[0-4]
        
$objects = array();
        for (
$i 0$i $object_count-5$i++) {
            
$object = new CustomObject();
            
$queue->send(new CustomMessage($object));
            
$objects[] = $object;
        }

        
// add items $objects[5-9]
        
$messages = new CustomMessages();
        for (
$i 0$i 5$i++) {
            
$object = new CustomObject();
            
$messages->append( new CustomMessage($object));
            
$objects[] = $object;
        }
        
$queue->send($messages);

        
$this->assertEquals($object_countcount($queue));
        unset(
$messages);

        
// ------------------------------------------------ receive

        // get the first 5 doing 0-4
        
$receive $queue->receive(5);
        
$this->assertTrue($receive instanceof CustomMessages);
        
$this->assertEquals(5count($receive));

        
// test them
        
for ($index 0$index 5$index++) {
            
$this->assertEquals($objects[$index]->getA(), $receive[$index]->getBody()->getA());
            try {
                unset(
$receive[$index]);
                
$this->assertTrue(true'$receive[$index] successfully deleted');
            } catch(
QueueException $e) {
                
$this->fail('$receive[$index] should have been deleted' $e->getMessage());
            }
        }
        
// there should only be 5 objects left
        
$this->assertEquals($object_count $indexcount($queue));

        
// get 1 doing $objects[5]
        
$receive $queue->receive();
        
$index++;
        
$this->assertTrue($receive instanceof CustomMessages);
        
$this->assertEquals(1count($receive));

        
// testing CustomMessages::__deconstruct()
        
unset($receive);
        
$this->assertEquals($object_count $indexcount($queue));


        
// get all the rest doing 6-20
        
$receive $queue->receive($object_count $index);
        
$this->assertTrue($receive instanceof CustomMessages);
        
$this->assertEquals($object_count $indexcount($receive));

        
// test them
        
$r_index = -1;
        for (; 
$index $object_count$index++) {
            
$r_index++;
            
$this->assertEquals($objects[$index]->getA(), $receive[$r_index]->getBody()->getA());

            try {
                unset(
$receive[$r_index]);
                
$this->assertTrue(true'$receive[$index] successfully deleted');
            } catch(
QueueException $e) {
                
$this->fail('$receive[$index] should have been deleted' $e->getMessage());
            }
        }

        
// auto-delete should have been called on $receive
        
$this->assertEquals(0count($queue));
    }
}

class 
CustomObject
{
    public 
$a;

    public function 
__construct()
    {
        
$a rand(1,200);
    }

    public function 
getA()
    {
        return 
$this->a;
    }

    public function 
setA($a)
    {
        
$this->$a;
    }

    public function 
__sleep()
    {
        return array(
'a'); // serialize only this variable
    
}
}
Онлайн: 0
Реклама