OneToOneSelfReferentialAssociationTest.php 3.39 KB
Newer Older
1 2 3 4 5
<?php

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\Tests\Models\ECommerce\ECommerceCustomer;
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 self referential one-to-one association mapping (without inheritance).
 * Relation is defined as the mentor that a customer choose. The mentor could 
 * help only one other customer, while a customer can choose only one mentor
 * for receiving support.
 * Inverse side is not present.
 */
class OneToOneSelfReferentialAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCase
{
    private $customer;
    private $mentor;

    protected function setUp()
    {
        $this->useModelSet('ecommerce');
        parent::setUp();
        $this->customer = new ECommerceCustomer();
        $this->customer->setName('Anakin Skywalker');
        $this->mentor = new ECommerceCustomer();
        $this->mentor->setName('Obi-wan Kenobi');
    }

    public function testSavesAOneToOneAssociationWithCascadeSaveSet() {
        $this->customer->setMentor($this->mentor);
romanb's avatar
romanb committed
34
        $this->_em->persist($this->customer);
35
        $this->_em->flush();
36 37 38 39 40 41 42
        
        $this->assertForeignKeyIs($this->mentor->getId());
    }

    public function testRemovesOneToOneAssociation()
    {
        $this->customer->setMentor($this->mentor);
romanb's avatar
romanb committed
43
        $this->_em->persist($this->customer);
44 45 46 47 48 49 50
        $this->customer->removeMentor();

        $this->_em->flush();

        $this->assertForeignKeyIs(null);
    }

51
    public function testEagerLoadsAssociation()
52
    {
53
        $this->_createFixture();
54

55
        $query = $this->_em->createQuery('select c, m from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c left join c.mentor m order by c.id asc');
56
        $result = $query->getResult();
57
        $customer = $result[0];
58 59 60
        $this->assertLoadingOfAssociation($customer);
    }
    
61 62 63 64
    /**
     * @group mine
     * @return unknown_type
     */
65 66 67 68 69 70 71
    public function testLazyLoadsAssociation()
    {    
        $this->_createFixture();

        $this->_em->getConfiguration()->setAllowPartialObjects(false);
        $metadata = $this->_em->getClassMetadata('Doctrine\Tests\Models\ECommerce\ECommerceCustomer');
        $metadata->getAssociationMapping('mentor')->fetchMode = AssociationMapping::FETCH_LAZY;
72
        
73
        $query = $this->_em->createQuery("select c from Doctrine\Tests\Models\ECommerce\ECommerceCustomer c where c.name='Luke Skywalker'");
74
        $result = $query->getResult();
75 76 77 78 79 80
        $customer = $result[0];
        $this->assertLoadingOfAssociation($customer);
    }

    public function assertLoadingOfAssociation($customer)
    {
81 82 83 84 85 86 87 88
        $this->assertTrue($customer->getMentor() instanceof ECommerceCustomer);
        $this->assertEquals('Obi-wan Kenobi', $customer->getMentor()->getName());
    }

    public function assertForeignKeyIs($value) {
        $foreignKey = $this->_em->getConnection()->execute('SELECT mentor_id FROM ecommerce_customers WHERE id=?', array($this->customer->getId()))->fetchColumn();
        $this->assertEquals($value, $foreignKey);
    }
89 90 91 92 93 94 95 96 97 98 99 100 101 102

    private function _createFixture()
    {
        $customer = new ECommerceCustomer;
        $customer->setName('Luke Skywalker');
        $mentor = new ECommerceCustomer;
        $mentor->setName('Obi-wan Kenobi');
        $customer->setMentor($mentor);
        
        $this->_em->persist($customer);
        
        $this->_em->flush();
        $this->_em->clear();
    }
103
}