public function testClassMetadataInstanceSerialization()
 {
     $cm = new ClassMetadata('Documents\\CmsUser');
     // Test initial state
     $this->assertTrue(count($cm->getReflectionProperties()) == 0);
     $this->assertTrue($cm->reflClass instanceof \ReflectionClass);
     $this->assertEquals('Documents\\CmsUser', $cm->name);
     $this->assertEquals('Documents\\CmsUser', $cm->rootDocumentName);
     $this->assertEquals(array(), $cm->subClasses);
     $this->assertEquals(array(), $cm->parentClasses);
     $this->assertEquals(ClassMetadata::INHERITANCE_TYPE_NONE, $cm->inheritanceType);
     // Customize state
     $cm->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_COLLECTION);
     $cm->setSubclasses(array("One", "Two", "Three"));
     $cm->setParentClasses(array("UserParent"));
     $cm->setCustomRepositoryClass("UserRepository");
     $cm->setDiscriminatorField('disc');
     $cm->mapOneEmbedded(array('fieldName' => 'phonenumbers', 'targetDocument' => 'Bar'));
     $cm->setFile('customFileProperty');
     $cm->setDistance('customDistanceProperty');
     $cm->setSlaveOkay(true);
     $cm->setShardKey(array('_id' => '1'));
     $cm->setCollectionCapped(true);
     $cm->setCollectionMax(1000);
     $cm->setCollectionSize(500);
     $this->assertTrue(is_array($cm->getFieldMapping('phonenumbers')));
     $this->assertEquals(1, count($cm->fieldMappings));
     $this->assertEquals(1, count($cm->associationMappings));
     $serialized = serialize($cm);
     $cm = unserialize($serialized);
     // Check state
     $this->assertTrue(count($cm->getReflectionProperties()) > 0);
     $this->assertEquals('Documents', $cm->namespace);
     $this->assertTrue($cm->reflClass instanceof \ReflectionClass);
     $this->assertEquals('Documents\\CmsUser', $cm->name);
     $this->assertEquals('UserParent', $cm->rootDocumentName);
     $this->assertEquals(array('Documents\\One', 'Documents\\Two', 'Documents\\Three'), $cm->subClasses);
     $this->assertEquals(array('UserParent'), $cm->parentClasses);
     $this->assertEquals('Documents\\UserRepository', $cm->customRepositoryClassName);
     $this->assertEquals('disc', $cm->discriminatorField);
     $this->assertTrue(is_array($cm->getFieldMapping('phonenumbers')));
     $this->assertEquals(1, count($cm->fieldMappings));
     $this->assertEquals(1, count($cm->associationMappings));
     $this->assertEquals('customFileProperty', $cm->file);
     $this->assertEquals('customDistanceProperty', $cm->distance);
     $this->assertTrue($cm->slaveOkay);
     $this->assertEquals(array('keys' => array('_id' => 1), 'options' => array()), $cm->getShardKey());
     $mapping = $cm->getFieldMapping('phonenumbers');
     $this->assertEquals('Documents\\Bar', $mapping['targetDocument']);
     $this->assertTrue($cm->getCollectionCapped());
     $this->assertEquals(1000, $cm->getCollectionMax());
     $this->assertEquals(500, $cm->getCollectionSize());
 }
 public static function loadMetadata(ClassMetadata $metadata)
 {
     $metadata->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_NONE);
     $metadata->setCollection('cms_users');
     $metadata->addLifecycleCallback('doStuffOnPrePersist', 'prePersist');
     $metadata->addLifecycleCallback('doOtherStuffOnPrePersistToo', 'prePersist');
     $metadata->addLifecycleCallback('doStuffOnPostPersist', 'postPersist');
     $metadata->setDiscriminatorField(array('fieldName' => 'discr'));
     $metadata->setDiscriminatorMap(array('default' => __CLASS__));
     $metadata->setDefaultDiscriminatorValue('default');
     $metadata->mapField(array('id' => true, 'fieldName' => 'id'));
     $metadata->mapField(array('fieldName' => 'version', 'type' => 'int', 'version' => true));
     $metadata->mapField(array('fieldName' => 'lock', 'type' => 'int', 'lock' => true));
     $metadata->mapField(array('fieldName' => 'name', 'name' => 'username', 'type' => 'string'));
     $metadata->mapField(array('fieldName' => 'email', 'type' => 'string'));
     $metadata->mapField(array('fieldName' => 'mysqlProfileId', 'type' => 'integer'));
     $metadata->mapOneReference(array('fieldName' => 'address', 'targetDocument' => 'Doctrine\\ODM\\MongoDB\\Tests\\Mapping\\Address', 'cascade' => array(0 => 'remove')));
     $metadata->mapManyReference(array('fieldName' => 'phonenumbers', 'targetDocument' => 'Doctrine\\ODM\\MongoDB\\Tests\\Mapping\\Phonenumber', 'cascade' => array(1 => 'persist'), 'discriminatorField' => 'discr', 'discriminatorMap' => array('home' => 'HomePhonenumber', 'work' => 'WorkPhonenumber'), 'defaultDiscriminatorValue' => 'home'));
     $metadata->mapManyReference(array('fieldName' => 'morePhoneNumbers', 'name' => 'more_phone_numbers', 'targetDocument' => 'Doctrine\\ODM\\MongoDB\\Tests\\Mapping\\Phonenumber'));
     $metadata->mapManyReference(array('fieldName' => 'groups', 'targetDocument' => 'Doctrine\\ODM\\MongoDB\\Tests\\Mapping\\Group', 'cascade' => array(0 => 'remove', 1 => 'persist', 2 => 'refresh', 3 => 'merge', 4 => 'detach')));
     $metadata->mapOneEmbedded(array('fieldName' => 'embeddedPhonenumber', 'name' => 'embedded_phone_number'));
     $metadata->mapManyEmbedded(array('fieldName' => 'otherPhonenumbers', 'targetDocument' => 'Doctrine\\ODM\\MongoDB\\Tests\\Mapping\\Phonenumber', 'discriminatorField' => 'discr', 'discriminatorMap' => array('home' => 'HomePhonenumber', 'work' => 'WorkPhonenumber'), 'defaultDiscriminatorValue' => 'home'));
     $metadata->addIndex(array('username' => 'desc'), array('unique' => true, 'dropDups' => false));
     $metadata->addIndex(array('email' => 'desc'), array('unique' => true, 'dropDups' => true));
     $metadata->addIndex(array('mysqlProfileId' => 'desc'), array('unique' => true, 'dropDups' => true));
     $metadata->addIndex(array('createdAt' => 'asc'), array('expireAfterSeconds' => 3600));
     $metadata->setShardKey(array('name' => 'asc'), array('unique' => true, 'numInitialChunks' => 4096));
 }
 /**
  * Adds inherited shard key to the subclass mapping.
  *
  * @param ClassMetadata $subClass
  * @param ClassMetadata $parentClass
  */
 private function setInheritedShardKey(ClassMetadata $subClass, ClassMetadata $parentClass)
 {
     if ($parentClass->isSharded()) {
         $subClass->setShardKey($parentClass->shardKey['keys'], $parentClass->shardKey['options']);
     }
 }