Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / OneToManySelfReferentialAssociationTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional;
4
5 use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
6 use Doctrine\ORM\Mapping\AssociationMapping;
7 use Doctrine\ORM\Mapping\ClassMetadata;
8
9 require_once __DIR__ . '/../../TestInit.php';
10
11 /**
12  * Tests a bidirectional one-to-one association mapping (without inheritance).
13  */
14 class OneToManySelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
15 {
16     private $parent;
17     private $firstChild;
18     private $secondChild;
19
20     protected function setUp()
21     {
22         $this->useModelSet('ecommerce');
23         parent::setUp();
24         $this->parent = new ECommerceCategory();
25         $this->parent->setName('Programming languages books');
26         $this->firstChild = new ECommerceCategory();
27         $this->firstChild->setName('Java books');
28         $this->secondChild = new ECommerceCategory();
29         $this->secondChild->setName('Php books');
30     }
31
32     public function testSavesAOneToManyAssociationWithCascadeSaveSet() {
33         $this->parent->addChild($this->firstChild);
34         $this->parent->addChild($this->secondChild);
35         $this->_em->persist($this->parent);
36
37         $this->_em->flush();
38
39         $this->assertForeignKeyIs($this->parent->getId(), $this->firstChild);
40         $this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
41     }
42
43     public function testSavesAnEmptyCollection()
44     {
45         $this->_em->persist($this->parent);
46         $this->_em->flush();
47
48         $this->assertEquals(0, count($this->parent->getChildren()));
49     }
50
51     public function testDoesNotSaveAnInverseSideSet() {
52         $this->parent->brokenAddChild($this->firstChild);
53         $this->_em->persist($this->parent);
54         $this->_em->flush();
55
56         $this->assertForeignKeyIs(null, $this->firstChild);
57     }
58
59     public function testRemovesOneToManyAssociation()
60     {
61         $this->parent->addChild($this->firstChild);
62         $this->parent->addChild($this->secondChild);
63         $this->_em->persist($this->parent);
64
65         $this->parent->removeChild($this->firstChild);
66         $this->_em->flush();
67
68         $this->assertForeignKeyIs(null, $this->firstChild);
69         $this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
70     }
71
72     public function testEagerLoadsOneToManyAssociation()
73     {
74         $this->_createFixture();
75
76         $query = $this->_em->createQuery('select c1, c2 from Doctrine\Tests\Models\ECommerce\ECommerceCategory c1 join c1.children c2');
77         $result = $query->getResult();
78         $this->assertEquals(1, count($result));
79         $parent = $result[0];
80         $children = $parent->getChildren();
81
82         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $children[0]);
83         $this->assertSame($parent, $children[0]->getParent());
84         $this->assertEquals(' books', strstr($children[0]->getName(), ' books'));
85         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $children[1]);
86         $this->assertSame($parent, $children[1]->getParent());
87         $this->assertEquals(' books', strstr($children[1]->getName(), ' books'));
88     }
89
90     public function testLazyLoadsOneToManyAssociation()
91     {
92         $this->_createFixture();
93         $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCategory');
94         $metadata->associationMappings['children']['fetch'] = ClassMetadata::FETCH_LAZY;
95
96         $query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc');
97         $result = $query->getResult();
98         $parent = $result[0];
99         $children = $parent->getChildren();
100
101         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $children[0]);
102         $this->assertSame($parent, $children[0]->getParent());
103         $this->assertEquals(' books', strstr($children[0]->getName(), ' books'));
104         $this->assertInstanceOf('Doctrine\Tests\Models\ECommerce\ECommerceCategory', $children[1]);
105         $this->assertSame($parent, $children[1]->getParent());
106         $this->assertEquals(' books', strstr($children[1]->getName(), ' books'));
107     }
108
109     private function _createFixture()
110     {
111         $this->parent->addChild($this->firstChild);
112         $this->parent->addChild($this->secondChild);
113         $this->_em->persist($this->parent);
114
115         $this->_em->flush();
116         $this->_em->clear();
117     }
118
119     public function assertForeignKeyIs($value, ECommerceCategory $child) {
120         $foreignKey = $this->_em->getConnection()->executeQuery('SELECT parent_id FROM ecommerce_categories WHERE id=?', array($child->getId()))->fetchColumn();
121         $this->assertEquals($value, $foreignKey);
122     }
123 }