ClassMetadataFactoryTest.php 4.94 KB
Newer Older
1 2
<?php

3 4 5 6
namespace Doctrine\Tests\ORM\Mapping;

use Doctrine\Tests\Mocks\MetadataDriverMock;
use Doctrine\Tests\Mocks\DatabasePlatformMock;
7 8 9
use Doctrine\Tests\Mocks\EntityManagerMock;
use Doctrine\Tests\Mocks\ConnectionMock;
use Doctrine\Tests\Mocks\DriverMock;
10
use Doctrine\ORM\Mapping\ClassMetadata;
11
use Doctrine\Common\EventManager;
12

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

15 16 17 18
class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
{
    public function testGetMetadataForSingleClass()
    {
19
        $mockDriver = new MetadataDriverMock();
20
        $entityManager = $this->_createEntityManager($mockDriver);
21

22
        $conn = $entityManager->getConnection();
23
        $mockPlatform = $conn->getDatabasePlatform();
24 25
        $mockPlatform->setPrefersSequences(true);
        $mockPlatform->setPrefersIdentityColumns(false);
romanb's avatar
romanb committed
26 27

        // Self-made metadata
28
        $cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1');
romanb's avatar
romanb committed
29
        // Add a mapped field
romanb's avatar
romanb committed
30
        $cm1->mapField(array('fieldName' => 'name', 'type' => 'varchar'));
31 32
        // Add a mapped field
        $cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
romanb's avatar
romanb committed
33 34
        // and a mapped association
        $cm1->mapOneToOne(array('fieldName' => 'other', 'targetEntity' => 'Other', 'mappedBy' => 'this'));
35 36 37 38 39
        // and an association on the owning side
        $joinColumns = array(
            array('name' => 'other_id', 'referencedColumnName' => 'id')
        );
        $cm1->mapOneToOne(array('fieldName' => 'association', 'targetEntity' => 'Other', 'joinColumns' => $joinColumns));
romanb's avatar
romanb committed
40
        // and an id generator type
41
        $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
romanb's avatar
romanb committed
42 43

        // SUT
44
        $cmf = new ClassMetadataFactoryTestSubject($entityManager);
45
        $cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1);
romanb's avatar
romanb committed
46 47

        // Prechecks
48
        $this->assertEquals(array(), $cm1->parentClasses);
49
        $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $cm1->inheritanceType);
romanb's avatar
romanb committed
50
        $this->assertTrue($cm1->hasField('name'));
51
        $this->assertEquals(2, count($cm1->associationMappings));
52
        $this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $cm1->generatorType);
romanb's avatar
romanb committed
53 54

        // Go
55
        $cm1 = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity1');
romanb's avatar
romanb committed
56

57
        $this->assertEquals(array(), $cm1->parentClasses);
romanb's avatar
romanb committed
58
        $this->assertTrue($cm1->hasField('name'));
59
        $this->assertEquals(ClassMetadata::GENERATOR_TYPE_SEQUENCE, $cm1->generatorType);
60
    }
61

62
    public function testHasGetMetadata_NamespaceSeperatorIsNotNormalized()
63 64 65 66 67 68 69 70 71 72 73 74
    {
        require_once __DIR__."/../../Models/Global/GlobalNamespaceModel.php";

        $reader = new \Doctrine\Common\Annotations\AnnotationReader(new \Doctrine\Common\Cache\ArrayCache);
        $reader->setDefaultAnnotationNamespace('Doctrine\ORM\Mapping\\');
        $metadataDriver = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader);
        $metadataDriver->setClassDirectory(__DIR__."/../../Models/Global/");

        $entityManager = $this->_createEntityManager($metadataDriver);

        $mf = $entityManager->getMetadataFactory();
        $m1 = $mf->getMetadataFor("DoctrineGlobal_Article");
75
        $h1 = $mf->hasMetadataFor("DoctrineGlobal_Article");
76 77 78
        $h2 = $mf->hasMetadataFor("\DoctrineGlobal_Article");
        $m2 = $mf->getMetadataFor("\DoctrineGlobal_Article");

79 80 81
        $this->assertNotSame($m1, $m2);
        $this->assertFalse($h2);
        $this->assertTrue($h1);
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    }

    protected function _createEntityManager($metadataDriver)
    {
        $driverMock = new DriverMock();
        $config = new \Doctrine\ORM\Configuration();
        $config->setProxyDir(__DIR__ . '/../../Proxies');
        $config->setProxyNamespace('Doctrine\Tests\Proxies');
        $eventManager = new EventManager();
        $conn = new ConnectionMock(array(), $driverMock, $config, $eventManager);
        $mockDriver = new MetadataDriverMock();
        $config->setMetadataDriverImpl($metadataDriver);

        return EntityManagerMock::create($conn, $config, $eventManager);
    }
97 98 99
}

/* Test subject class with overriden factory method for mocking purposes */
100 101
class ClassMetadataFactoryTestSubject extends \Doctrine\ORM\Mapping\ClassMetadataFactory
{
102 103
    private $_mockMetadata = array();
    private $_requestedClasses = array();
104

105
    /** @override */
106 107
    protected function _newClassMetadataInstance($className)
    {
108 109 110 111 112 113
        $this->_requestedClasses[] = $className;
        if ( ! isset($this->_mockMetadata[$className])) {
            throw new InvalidArgumentException("No mock metadata found for class $className.");
        }
        return $this->_mockMetadata[$className];
    }
114 115 116

    public function setMetadataForClass($className, $metadata)
    {
117 118
        $this->_mockMetadata[$className] = $metadata;
    }
119 120 121 122 123

    public function getRequestedClasses()
    {
        return $this->_requestedClasses;
    }
124
}
125 126 127 128 129 130

class TestEntity1
{
    private $id;
    private $name;
    private $other;
131 132
    private $association;
}