OneToManySelfReferentialAssociationTest.php 4.57 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Tests\Models\ECommerce\ECommerceCategory;
6
use Doctrine\ORM\Mapping\AssociationMapping;
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 33

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

/**
 * Tests a bidirectional one-to-one association mapping (without inheritance).
 */
class OneToManySelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
    private $parent;
    private $firstChild;
    private $secondChild;

    protected function setUp()
    {
        $this->useModelSet('ecommerce');
        parent::setUp();
        $this->parent = new ECommerceCategory();
        $this->parent->setName('Programming languages books');
        $this->firstChild = new ECommerceCategory();
        $this->firstChild->setName('Java books');
        $this->secondChild = new ECommerceCategory();
        $this->secondChild->setName('Php books');
    }

    public function testSavesAOneToManyAssociationWithCascadeSaveSet() {
        $this->parent->addChild($this->firstChild);
        $this->parent->addChild($this->secondChild);
romanb's avatar
romanb committed
34
        $this->_em->persist($this->parent);
35
        
36 37
        $this->_em->flush();
        
38 39 40 41 42 43
        $this->assertForeignKeyIs($this->parent->getId(), $this->firstChild);
        $this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
    }

    public function testSavesAnEmptyCollection()
    {
romanb's avatar
romanb committed
44
        $this->_em->persist($this->parent);
45
        $this->_em->flush();
46 47 48 49 50 51

        $this->assertEquals(0, count($this->parent->getChildren()));
    }

    public function testDoesNotSaveAnInverseSideSet() {
        $this->parent->brokenAddChild($this->firstChild);
romanb's avatar
romanb committed
52
        $this->_em->persist($this->parent);
53
        $this->_em->flush();
54 55 56 57 58 59 60 61
        
        $this->assertForeignKeyIs(null, $this->firstChild);
    }

    public function testRemovesOneToManyAssociation()
    {
        $this->parent->addChild($this->firstChild);
        $this->parent->addChild($this->secondChild);
romanb's avatar
romanb committed
62
        $this->_em->persist($this->parent);
63 64 65 66 67 68 69 70 71 72

        $this->parent->removeChild($this->firstChild);
        $this->_em->flush();

        $this->assertForeignKeyIs(null, $this->firstChild);
        $this->assertForeignKeyIs($this->parent->getId(), $this->secondChild);
    }

    public function testEagerLoadsOneToManyAssociation()
    {
73
        $this->_createFixture();
74 75

        $query = $this->_em->createQuery('select c1, c2 from Doctrine\Tests\Models\ECommerce\ECommerceCategory c1 join c1.children c2');
76
        $result = $query->getResult();
77 78 79 80 81 82
        $this->assertEquals(1, count($result));
        $parent = $result[0];
        $children = $parent->getChildren();
        
        $this->assertTrue($children[0] instanceof ECommerceCategory);
        $this->assertSame($parent, $children[0]->getParent());
83
        $this->assertEquals(' books', strstr($children[0]->getName(), ' books'));
84 85
        $this->assertTrue($children[1] instanceof ECommerceCategory);
        $this->assertSame($parent, $children[1]->getParent());
86
        $this->assertEquals(' books', strstr($children[1]->getName(), ' books'));
87
    }
88 89 90 91 92 93 94 95 96

    public function testLazyLoadsOneToManyAssociation()
    {
        $this->_createFixture();
        $this->_em->getConfiguration()->setAllowPartialObjects(false);
        $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCategory');
        $metadata->getAssociationMapping('children')->fetchMode = AssociationMapping::FETCH_LAZY;

        $query = $this->_em->createQuery('select c from Doctrine\Tests\Models\ECommerce\ECommerceCategory c order by c.id asc');
97
        $result = $query->getResult();
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        $parent = $result[0];
        $children = $parent->getChildren();
        
        $this->assertTrue($children[0] instanceof ECommerceCategory);
        $this->assertSame($parent, $children[0]->getParent());
        $this->assertEquals(' books', strstr($children[0]->getName(), ' books'));
        $this->assertTrue($children[1] instanceof ECommerceCategory);
        $this->assertSame($parent, $children[1]->getParent());
        $this->assertEquals(' books', strstr($children[1]->getName(), ' books'));
    }

    private function _createFixture()
    {
        $this->parent->addChild($this->firstChild);
        $this->parent->addChild($this->secondChild);
        $this->_em->persist($this->parent);
114
        
115 116 117
        $this->_em->flush();
        $this->_em->clear();
    }
118 119 120 121 122 123

    public function assertForeignKeyIs($value, ECommerceCategory $child) {
        $foreignKey = $this->_em->getConnection()->execute('SELECT parent_id FROM ecommerce_categories WHERE id=?', array($child->getId()))->fetchColumn();
        $this->assertEquals($value, $foreignKey);
    }
}