1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/tests/Doctrine/Tests/ORM/Functional/ManyToManySelfReferentialAssociationTest.php

112 lines
4.1 KiB
PHP

<?php
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Common\Collections\Collection;
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
require_once __DIR__ . '/../../TestInit.php';
/**
* Tests a self referential many-to-many association mapping (from a model to the same model, without inheritance).
* For simplicity the relation duplicates entries in the association table
* to remain simmetrical.
*/
class ManyToManySelfReferentialAssociationTest extends AbstractManyToManyAssociationTestCase
{
protected $_firstField = 'product_id';
protected $_secondField = 'related_id';
protected $_table = 'ecommerce_products_related';
private $firstProduct;
private $secondProduct;
private $firstRelated;
private $secondRelated;
protected function setUp()
{
$this->useModelSet('ecommerce');
parent::setUp();
$this->firstProduct = new ECommerceProduct();
$this->secondProduct = new ECommerceProduct();
$this->firstRelated = new ECommerceProduct();
$this->firstRelated->setName("Business");
$this->secondRelated = new ECommerceProduct();
$this->secondRelated->setName("Home");
}
public function testSavesAManyToManyAssociationWithCascadeSaveSet()
{
$this->firstProduct->addRelated($this->firstRelated);
$this->firstProduct->addRelated($this->secondRelated);
$this->_em->save($this->firstProduct);
$this->_em->flush();
$this->assertForeignKeysContain($this->firstProduct->getId(),
$this->firstRelated->getId());
$this->assertForeignKeysContain($this->firstProduct->getId(),
$this->secondRelated->getId());
}
public function testRemovesAManyToManyAssociation()
{
$this->firstProduct->addRelated($this->firstRelated);
$this->firstProduct->addRelated($this->secondRelated);
$this->_em->save($this->firstProduct);
$this->firstProduct->removeRelated($this->firstRelated);
$this->_em->flush();
$this->assertForeignKeysNotContain($this->firstProduct->getId(),
$this->firstRelated->getId());
$this->assertForeignKeysContain($this->firstProduct->getId(),
$this->secondRelated->getId());
}
public function testEagerLoadsOwningSide()
{
$this->_createLoadingFixture();
list ($firstProduct, $secondProduct) = $this->_findProducts();
$this->assertEquals(2, count($firstProduct->getRelated()));
$this->assertEquals(2, count($secondProduct->getRelated()));
$categories = $firstProduct->getRelated();
$firstRelatedBy = $categories[0]->getRelated();
$secondRelatedBy = $categories[1]->getRelated();
$this->assertEquals(2, count($firstRelatedBy));
$this->assertEquals(2, count($secondRelatedBy));
$this->assertTrue($firstRelatedBy[0] instanceof ECommerceProduct);
$this->assertTrue($firstRelatedBy[1] instanceof ECommerceProduct);
$this->assertTrue($secondRelatedBy[0] instanceof ECommerceProduct);
$this->assertTrue($secondRelatedBy[1] instanceof ECommerceProduct);
$this->assertCollectionEquals($firstRelatedBy, $secondRelatedBy);
}
protected function _createLoadingFixture()
{
$this->firstProduct->addRelated($this->firstRelated);
$this->firstProduct->addRelated($this->secondRelated);
$this->secondProduct->addRelated($this->firstRelated);
$this->secondProduct->addRelated($this->secondRelated);
$this->_em->save($this->firstProduct);
$this->_em->save($this->secondProduct);
$this->_em->flush();
$this->_em->clear();
}
protected function _findProducts()
{
$query = $this->_em->createQuery('SELECT p, r FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p LEFT JOIN p.related r ORDER BY p.id, r.id');
return $query->getResultList();
}
/* TODO: not yet implemented
public function testLazyLoad() {
}*/
}