PersistentCollectionTest.php 1.95 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\ORM;

5
use Doctrine\Common\Collections\ArrayCollection;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
use Doctrine\ORM\PersistentCollection;
use Doctrine\Tests\Mocks\ConnectionMock;
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;

require_once __DIR__ . '/../TestInit.php';

/**
 * Tests the lazy-loading capabilities of the PersistentCollection.
 * @author Giorgio Sironi <piccoloprincipeazzurro@gmail.com>
 */
class PersistentCollectionTest extends \Doctrine\Tests\OrmTestCase
{
    private $_connectionMock;
    private $_emMock;
    
    protected function setUp()
    {
        parent::setUp();
        // SUT
        $this->_connectionMock = new ConnectionMock(array(), new \Doctrine\Tests\Mocks\DriverMock());
        $this->_emMock = EntityManagerMock::create($this->_connectionMock);
    }

    public function testCanBePutInLazyLoadingMode()
    {
        $class = $this->_emMock->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
33 34
        $collection = new PersistentCollection($this->_emMock, $class, new ArrayCollection);
        $collection->setInitialized(false);
35 36 37 38 39
    }

    public function testQueriesAssociationToLoadItself()
    {
        $class = $this->_emMock->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
40 41
        $collection = new PersistentCollection($this->_emMock, $class, new ArrayCollection);
        $collection->setInitialized(false);
42 43 44 45 46 47 48 49 50 51 52 53

        $association = $this->getMock('Doctrine\ORM\Mapping\OneToManyMapping', array('load'), array(), '', false, false, false);
        $association->targetEntityName = 'Doctrine\Tests\Models\ECommerce\ECommerceFeature';
        $product = new ECommerceProduct();
        $association->expects($this->once())
                    ->method('load')
                    ->with($product, $this->isInstanceOf($collection), $this->isInstanceOf($this->_emMock));
        $collection->setOwner($product, $association);

        count($collection);
    }
}