ClassMetadataTest.php 3.31 KB
Newer Older
1
<?php
2

3
namespace Doctrine\Tests\ORM\Mapping;
4

5
use Doctrine\ORM\Mapping\ClassMetadata;
6
use Doctrine\ORM\Events;
7

8
require_once __DIR__ . '/../../TestInit.php';
9
 
10
class ClassMetadataTest extends \Doctrine\Tests\OrmTestCase
11
{
12 13
    public function testClassMetadataInstanceSerialization()
    {
14
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');
15 16

        // Test initial state
17
        $this->assertTrue(count($cm->getReflectionProperties()) == 0);
18 19 20 21 22
        $this->assertTrue($cm->reflClass instanceof \ReflectionClass);
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->name);
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->rootEntityName);
        $this->assertEquals(array(), $cm->subClasses);
        $this->assertEquals(array(), $cm->parentClasses);
23 24 25 26 27 28

        // Customize state
        $cm->setSubclasses(array("One", "Two", "Three"));
        $cm->setParentClasses(array("UserParent"));
        $cm->setCustomRepositoryClass("UserRepository");
        $cm->setDiscriminatorColumn(array('name' => 'disc', 'type' => 'integer'));
29
        $cm->mapOneToOne(array('fieldName' => 'phonenumbers', 'targetEntity' => 'Bar', 'mappedBy' => 'foo'));
30
        $this->assertTrue($cm->getAssociationMapping('phonenumbers') instanceof \Doctrine\ORM\Mapping\OneToOneMapping);
31
        $this->assertEquals(1, count($cm->associationMappings));
32 33 34 35 36 37

        $serialized = serialize($cm);
        $cm = unserialize($serialized);

        // Check state
        $this->assertTrue(count($cm->getReflectionProperties()) > 0);
38 39 40
        $this->assertTrue($cm->reflClass instanceof \ReflectionClass);
        $this->assertEquals('Doctrine\Tests\Models\CMS\CmsUser', $cm->name);
        $this->assertEquals('UserParent', $cm->rootEntityName);
41
        $this->assertEquals(array('Doctrine\Tests\Models\CMS\One', 'Doctrine\Tests\Models\CMS\Two', 'Doctrine\Tests\Models\CMS\Three'), $cm->subClasses);
42
        $this->assertEquals(array('UserParent'), $cm->parentClasses);
43
        $this->assertEquals('UserRepository', $cm->getCustomRepositoryClass());
44
        $this->assertEquals(array('name' => 'disc', 'type' => 'integer', 'fieldName' => 'disc'), $cm->discriminatorColumn);
45
        $this->assertTrue($cm->getAssociationMapping('phonenumbers') instanceof \Doctrine\ORM\Mapping\OneToOneMapping);
46
        $this->assertEquals(1, count($cm->associationMappings));
47 48
        $oneOneMapping = $cm->getAssociationMapping('phonenumbers');
        $this->assertEquals('phonenumbers', $oneOneMapping->getSourceFieldName());
49
        $this->assertEquals('Doctrine\Tests\Models\CMS\Bar', $oneOneMapping->getTargetEntityName());
50
    }
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    public function testFieldIsNullable()
    {
        $cm = new ClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');

        // Explicit Nullable
        $cm->mapField(array('fieldName' => 'status', 'nullable' => true, 'type' => 'string', 'length' => 50));
        $this->assertTrue($cm->isNullable('status'));

        // Explicit Not Nullable
        $cm->mapField(array('fieldName' => 'username', 'nullable' => false, 'type' => 'string', 'length' => 50));
        $this->assertFalse($cm->isNullable('username'));

        // Implicit Not Nullable
        $cm->mapField(array('fieldName' => 'name', 'type' => 'string', 'length' => 50));
        $this->assertFalse($cm->isNullable('name'), "By default a field should not be nullable.");
    }
68
}