Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / ManyToManySelfReferentialAssociationTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
6 use Doctrine\ORM\Mapping\AssociationMapping;
7 use Doctrine\ORM\Mapping\ClassMetadata;
8
9 require_once __DIR__ . '/../../TestInit.php';
10
11 /**
12  * Tests a self referential many-to-many association mapping (from a model to the same model, without inheritance).
13  * For simplicity the relation duplicates entries in the association table
14  * to remain simmetrical.
15  */
16 class ManyToManySelfReferentialAssociationTest extends AbstractManyToManyAssociationTestCase
17 {
18     protected $_firstField = 'product_id';
19     protected $_secondField = 'related_id';
20     protected $_table = 'ecommerce_products_related';
21     private $firstProduct;
22     private $secondProduct;
23     private $firstRelated;
24     private $secondRelated;
25
26     protected function setUp()
27     {
28         $this->useModelSet('ecommerce');
29         parent::setUp();
30         $this->firstProduct = new ECommerceProduct();
31         $this->secondProduct = new ECommerceProduct();
32         $this->firstRelated = new ECommerceProduct();
33         $this->firstRelated->setName("Business");
34         $this->secondRelated = new ECommerceProduct();
35         $this->secondRelated->setName("Home");
36     }
37
38     public function testSavesAManyToManyAssociationWithCascadeSaveSet()
39     {
40         $this->firstProduct->addRelated($this->firstRelated);
41         $this->firstProduct->addRelated($this->secondRelated);
42         $this->_em->persist($this->firstProduct);
43         $this->_em->flush();
44
45         $this->assertForeignKeysContain($this->firstProduct->getId(),
46                                    $this->firstRelated->getId());
47         $this->assertForeignKeysContain($this->firstProduct->getId(),
48                                    $this->secondRelated->getId());
49     }
50
51     public function testRemovesAManyToManyAssociation()
52     {
53         $this->firstProduct->addRelated($this->firstRelated);
54         $this->firstProduct->addRelated($this->secondRelated);
55         $this->_em->persist($this->firstProduct);
56         $this->firstProduct->removeRelated($this->firstRelated);
57
58         $this->_em->flush();
59
60         $this->assertForeignKeysNotContain($this->firstProduct->getId(),
61                                    $this->firstRelated->getId());
62         $this->assertForeignKeysContain($this->firstProduct->getId(),
63                                    $this->secondRelated->getId());
64     }
65
66     public function testEagerLoadsOwningSide()
67     {
68         $this->_createLoadingFixture();
69         $products = $this->_findProducts();
70         $this->assertLoadingOfOwningSide($products);
71     }
72
73     public function testLazyLoadsOwningSide()
74     {
75         $this->_createLoadingFixture();
76
77         $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceProduct');
78         $metadata->associationMappings['related']['fetch'] = ClassMetadata::FETCH_LAZY;
79
80         $query = $this->_em->createQuery('SELECT p FROM Doctrine\Tests\Models\ECommerce\ECommerceProduct p');
81         $products = $query->getResult();
82         $this->assertLoadingOfOwningSide($products);
83     }
84
85     public function assertLoadingOfOwningSide($products)
86     {
87         list ($firstProduct, $secondProduct) = $products;
88         $this->assertEquals(2, count($firstProduct->getRelated()));
89         $this->assertEquals(2, count($secondProduct->getRelated()));
90
91         $categories = $firstProduct->getRelated();
92         $firstRelatedBy = $categories[0]->getRelated();
93         $secondRelatedBy = $categories[1]->getRelated();
94
95         $this->assertEquals(2, count($firstRelatedBy));
96         $this->assertEquals(2, count($secondRelatedBy));
97
98         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $firstRelatedBy[0]);
99         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $firstRelatedBy[1]);
100         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $secondRelatedBy[0]);
101         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $secondRelatedBy[1]);
102
103         $this->assertCollectionEquals($firstRelatedBy, $secondRelatedBy);
104     }
105
106     protected function _createLoadingFixture()
107     {
108         $this->firstProduct->addRelated($this->firstRelated);
109         $this->firstProduct->addRelated($this->secondRelated);
110         $this->secondProduct->addRelated($this->firstRelated);
111         $this->secondProduct->addRelated($this->secondRelated);
112         $this->_em->persist($this->firstProduct);
113         $this->_em->persist($this->secondProduct);
114
115         $this->_em->flush();
116         $this->_em->clear();
117     }
118
119     protected function _findProducts()
120     {
121         $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');
122         return $query->getResult();
123     }
124 }