Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / ManyToManyUnidirectionalAssociationTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\ECommerce\ECommerceCart;
6 use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
7 use Doctrine\ORM\Mapping\AssociationMapping;
8 use Doctrine\ORM\Mapping\ClassMetadata;
9
10 require_once __DIR__ . '/../../TestInit.php';
11
12 /**
13  * Tests a unidirectional many-to-many association mapping (without inheritance).
14  * Inverse side is not present.
15  */
16 class ManyToManyUnidirectionalAssociationTest extends AbstractManyToManyAssociationTestCase
17 {
18     protected $_firstField = 'cart_id';
19     protected $_secondField = 'product_id';
20     protected $_table = 'ecommerce_carts_products';
21     private $firstProduct;
22     private $secondProduct;
23     private $firstCart;
24     private $secondCart;
25
26     protected function setUp()
27     {
28         $this->useModelSet('ecommerce');
29         parent::setUp();
30         $this->firstProduct = new ECommerceProduct();
31         $this->firstProduct->setName('Doctrine 1.x Manual');
32         $this->secondProduct = new ECommerceProduct();
33         $this->secondProduct->setName('Doctrine 2.x Manual');
34         $this->firstCart = new ECommerceCart();
35         $this->secondCart = new ECommerceCart();
36     }
37
38     public function testSavesAManyToManyAssociationWithCascadeSaveSet()
39     {
40         $this->firstCart->addProduct($this->firstProduct);
41         $this->firstCart->addProduct($this->secondProduct);
42         $this->_em->persist($this->firstCart);
43         $this->_em->flush();
44
45         $this->assertForeignKeysContain($this->firstCart->getId(), $this->firstProduct->getId());
46         $this->assertForeignKeysContain($this->firstCart->getId(), $this->secondProduct->getId());
47     }
48
49     public function testRemovesAManyToManyAssociation()
50     {
51         $this->firstCart->addProduct($this->firstProduct);
52         $this->firstCart->addProduct($this->secondProduct);
53         $this->_em->persist($this->firstCart);
54         $this->firstCart->removeProduct($this->firstProduct);
55
56         $this->_em->flush();
57
58         $this->assertForeignKeysNotContain($this->firstCart->getId(), $this->firstProduct->getId());
59         $this->assertForeignKeysContain($this->firstCart->getId(), $this->secondProduct->getId());
60     }
61
62     public function testEagerLoad()
63     {
64         $this->_createFixture();
65
66         $query = $this->_em->createQuery('SELECT c, p FROM Doctrine\Tests\Models\ECommerce\ECommerceCart c LEFT JOIN c.products p ORDER BY c.id, p.id');
67         $result = $query->getResult();
68         $firstCart = $result[0];
69         $products = $firstCart->getProducts();
70         $secondCart = $result[1];
71
72         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $products[0]);
73         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $products[1]);
74         $this->assertCollectionEquals($products, $secondCart->getProducts());
75         //$this->assertEquals("Doctrine 1.x Manual", $products[0]->getName());
76         //$this->assertEquals("Doctrine 2.x Manual", $products[1]->getName());
77     }
78
79     public function testLazyLoadsCollection()
80     {
81         $this->_createFixture();
82         $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCart');
83         $metadata->associationMappings['products']['fetch'] = ClassMetadata::FETCH_LAZY;
84
85         $query = $this->_em->createQuery('SELECT c FROM Doctrine\Tests\Models\ECommerce\ECommerceCart c');
86         $result = $query->getResult();
87         $firstCart = $result[0];
88         $products = $firstCart->getProducts();
89         $secondCart = $result[1];
90
91         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $products[0]);
92         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceProduct', $products[1]);
93         $this->assertCollectionEquals($products, $secondCart->getProducts());
94     }
95
96     private function _createFixture()
97     {
98         $this->firstCart->addProduct($this->firstProduct);
99         $this->firstCart->addProduct($this->secondProduct);
100         $this->secondCart->addProduct($this->firstProduct);
101         $this->secondCart->addProduct($this->secondProduct);
102         $this->_em->persist($this->firstCart);
103         $this->_em->persist($this->secondCart);
104
105         $this->_em->flush();
106         $this->_em->clear();
107     }
108 }