1
0
mirror of synced 2024-12-14 23:26:04 +03:00
doctrine2/tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php

84 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace Doctrine\Tests\ORM\Mapping;
use Doctrine\Tests\Mocks\MetadataDriverMock;
use Doctrine\Tests\Mocks\DatabasePlatformMock;
use Doctrine\ORM\Mapping\ClassMetadata;
require_once __DIR__ . '/../../TestInit.php';
class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
{
2009-01-06 21:30:51 +03:00
public function testGetMetadataForSingleClass()
{
$mockPlatform = new DatabasePlatformMock();
$mockDriver = new MetadataDriverMock();
$mockPlatform->setPrefersSequences(true);
$mockPlatform->setPrefersIdentityColumns(false);
2009-01-06 21:30:51 +03:00
// Self-made metadata
$cm1 = new ClassMetadata('Doctrine\Tests\ORM\Mapping\TestEntity1');
2009-01-06 21:30:51 +03:00
// Add a mapped field
2009-01-08 14:23:24 +03:00
$cm1->mapField(array('fieldName' => 'name', 'type' => 'varchar'));
// Add a mapped field
$cm1->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
2009-01-06 21:30:51 +03:00
// and a mapped association
$cm1->mapOneToOne(array('fieldName' => 'other', 'targetEntity' => 'Other', 'mappedBy' => 'this'));
// and an id generator type
$cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
2009-01-06 21:30:51 +03:00
// SUT
$cmf = new ClassMetadataFactoryTestSubject($mockDriver, $mockPlatform);
$cmf->setMetadataForClass('Doctrine\Tests\ORM\Mapping\TestEntity1', $cm1);
2009-01-06 21:30:51 +03:00
// Prechecks
$this->assertEquals(array(), $cm1->parentClasses);
$this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $cm1->inheritanceType);
2009-01-06 21:30:51 +03:00
$this->assertTrue($cm1->hasField('name'));
$this->assertEquals(1, count($cm1->associationMappings));
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_AUTO, $cm1->generatorType);
2009-01-06 21:30:51 +03:00
// Go
$cm1 = $cmf->getMetadataFor('Doctrine\Tests\ORM\Mapping\TestEntity1');
2009-01-06 21:30:51 +03:00
$this->assertEquals(array(), $cm1->parentClasses);
2009-01-06 21:30:51 +03:00
$this->assertTrue($cm1->hasField('name'));
$this->assertEquals(ClassMetadata::GENERATOR_TYPE_SEQUENCE, $cm1->generatorType);
}
}
/* Test subject class with overriden factory method for mocking purposes */
class ClassMetadataFactoryTestSubject extends \Doctrine\ORM\Mapping\ClassMetadataFactory
{
private $_mockMetadata = array();
private $_requestedClasses = array();
/** @override */
protected function _newClassMetadataInstance($className)
{
$this->_requestedClasses[] = $className;
if ( ! isset($this->_mockMetadata[$className])) {
throw new InvalidArgumentException("No mock metadata found for class $className.");
}
return $this->_mockMetadata[$className];
}
public function setMetadataForClass($className, $metadata)
{
$this->_mockMetadata[$className] = $metadata;
}
public function getRequestedClasses()
{
return $this->_requestedClasses;
}
}
class TestEntity1
{
private $id;
private $name;
private $other;
}