Skip to content

Commit

Permalink
Adding some additional inheritance tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Jul 28, 2010
1 parent 7a44608 commit 4a7e8c1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 17 deletions.
15 changes: 15 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php
Expand Up @@ -175,6 +175,13 @@ class ClassMetadata
*/
public $fieldMappings = array();

/**
* READ-ONLY: The registered lifecycle callbacks for documents of this class.
*
* @var array
*/
public $lifecycleCallbacks = array();

/**
* READ-ONLY: The discriminator value of this class.
*
Expand Down Expand Up @@ -567,6 +574,14 @@ public function mapField(array $mapping)
$mapping['targetDocument'] = $this->namespace . '\\' . $mapping['targetDocument'];
}

if (isset($mapping['discriminatorMap'])) {
foreach ($mapping['discriminatorMap'] as $key => $class) {
if (strpos($class, '\\') === false && strlen($this->namespace)) {
$mapping['discriminatorMap'][$key] = $this->namespace . '\\' . $class;
}
}
}

if ($this->reflClass->hasProperty($mapping['fieldName'])) {
$reflProp = $this->reflClass->getProperty($mapping['fieldName']);
$reflProp->setAccessible(true);
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php
Expand Up @@ -192,6 +192,7 @@ private function _loadMetadata($className)
$this->_addInheritedFields($class, $parent);
$class->setIdentifier($parent->identifier);
$class->setDiscriminatorMap($parent->discriminatorMap);
$class->setLifecycleCallbacks($parent->lifecycleCallbacks);
}

$this->_driver->loadMetadataForClass($className, $class);
Expand Down
45 changes: 28 additions & 17 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/FunctionalTest.php
Expand Up @@ -10,9 +10,11 @@
Documents\Manager,
Documents\Address,
Documents\Group,
Documents\Project;

use Documents\Functional\AlsoLoad,
Documents\Project,
Documents\Agent,
Documents\Server,
Documents\GuestServer,
Documents\Functional\AlsoLoad,
Documents\Functional\EmbeddedTestLevel0,
Documents\Functional\EmbeddedTestLevel1,
Documents\Functional\EmbeddedTestLevel2,
Expand All @@ -29,21 +31,30 @@

class FunctionalTest extends \Doctrine\ODM\MongoDB\Tests\BaseTest
{
public function tearDown()
public function testPersistingNewDocumentWithOnlyOneReference()
{
parent::tearDown();
$documents = array(
'Documents\Functional\AlsoLoad',
'Documents\Functional\NotAnnotatedDocument',
'Documents\Functional\NotSaved',
'Documents\Functional\NullFieldValues',
'Documents\Functional\SimpleEmbedAndReference',
'Documents\Functional\FavoritesUser',
'Documents\Functional\EmbeddedTestLevel0'
);
foreach ($documents as $document) {
$this->dm->getDocumentCollection($document)->drop();
}
$server = new \Documents\GuestServer();
$server->name = 'test';
$this->dm->persist($server);
$this->dm->flush();
$id = $server->id;

$this->dm->clear();

$server = $this->dm->getReference('Documents\GuestServer', $id);

$agent = new \Documents\Agent();
$agent->server = $server;
$this->dm->persist($agent);
$this->dm->flush();
$this->dm->clear();

$test = $this->dm->getDocumentCollection('Documents\Agent')->findOne();

$this->assertEquals('servers', $test['server']['$ref']);
$this->assertTrue(isset($test['server']['$id']));
$this->assertEquals('doctrine_odm_tests', $test['server']['$db']);
$this->assertEquals('server_guest', $test['server']['_doctrine_class_name']);
}

public function testCollection()
Expand Down
18 changes: 18 additions & 0 deletions tests/Documents/Agent.php
@@ -0,0 +1,18 @@
<?php

namespace Documents;

/** @Document(collection="agents") */
class Agent
{
/** @Id */
public $id;

/**
* @ReferenceOne(discriminatorMap={
* "server"="Server",
* "server_guest"="GuestServer"
* })
*/
public $server;
}
8 changes: 8 additions & 0 deletions tests/Documents/GuestServer.php
@@ -0,0 +1,8 @@
<?php

namespace Documents;

/** @Document */
class GuestServer extends Server
{
}
21 changes: 21 additions & 0 deletions tests/Documents/Server.php
@@ -0,0 +1,21 @@
<?php

namespace Documents;

/**
* @Document(collection="servers")
* @InheritanceType("SINGLE_COLLECTION")
* @DiscriminatorField(fieldName="stype")
* @DiscriminatorMap({
* "server"="Server",
* "server_guest"="GuestServer"
* })
*/
class Server
{
/** @Id */
public $id;

/** @String */
public $name;
}

0 comments on commit 4a7e8c1

Please sign in to comment.