AssignedIdTest.php 1.79 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6 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 34 35 36 37 38 39 40 41 42 43 44
<?php

namespace Doctrine\Tests\ORM\Id;

use Doctrine\ORM\Id\Assigned;

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

/**
 * AssignedIdTest
 *
 * @author robo
 */
class AssignedIdTest extends \Doctrine\Tests\OrmTestCase
{
    private $_em;
    private $_assignedGen;

    protected function setUp()
    {
        $this->_em = $this->_getTestEntityManager();
        $this->_assignedGen = new Assigned;
    }

    public function testThrowsExceptionIfIdNotAssigned()
    {
        try {
            $entity = new AssignedSingleIdEntity;
            $this->_assignedGen->generate($this->_em, $entity);
            $this->fail('Assigned generator did not throw exception even though ID was missing.');
        } catch (\Doctrine\ORM\ORMException $expected) {}

        try {
            $entity = new AssignedCompositeIdEntity;
            $this->_assignedGen->generate($this->_em, $entity);
            $this->fail('Assigned generator did not throw exception even though ID was missing.');
        } catch (\Doctrine\ORM\ORMException $expected) {}
    }
    
    public function testCorrectIdGeneration()
    {
        $entity = new AssignedSingleIdEntity;
        $entity->myId = 1;
        $id = $this->_assignedGen->generate($this->_em, $entity);
45
        $this->assertEquals(array('myId' => 1), $id);
romanb's avatar
romanb committed
46 47 48 49 50
        
        $entity = new AssignedCompositeIdEntity;
        $entity->myId2 = 2;
        $entity->myId1 = 4;
        $id = $this->_assignedGen->generate($this->_em, $entity);
51
        $this->assertEquals(array('myId1' => 4, 'myId2' => 2), $id);
romanb's avatar
romanb committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    }
}

/** @Entity */
class AssignedSingleIdEntity {
    /** @Id @Column(type="integer") */
    public $myId;
}

/** @Entity */
class AssignedCompositeIdEntity {
    /** @Id @Column(type="integer") */
    public $myId1;
    /** @Id @Column(type="integer") */
    public $myId2;
}