/** * @depends testReferenceMapping */ public function testNewInstance(ClassMetadata $cm) { $instance1 = $cm->newInstance(); $instance2 = $cm->newInstance(); $this->assertInstanceOf('Doctrine\\Tests\\ORM\\ODMAdapter\\Mapping\\Driver\\Model\\ReferenceMappingObject', $instance1); $this->assertNotSame($instance1, $instance2); }
/** * This method will have a look into the mappings and figure out if the current * document has got some mapped common fields and sync them by the mapped sync-type. * * @param $object * @param $referencedObject * @param ClassMetadata $classMetadata * @throws Exception\MappingException */ private function syncCommonFields($object, $referencedObject, ClassMetadata $classMetadata) { $referencedObjets = array_filter($classMetadata->getReferencedObjects(), function ($refObject) use($referencedObject) { $refl = new \ReflectionClass($refObject['target-object']); return $refl->isInstance($referencedObject); }); if (!$referencedObjets || !is_array($referencedObjets)) { return; } // neither a sleeping proxy of the object or referenced objects is good for reflections or the sync process if ($this->objectAdapterManager->isSleepingProxy($object) || $this->objectAdapterManager->isSleepingProxy($referencedObject)) { return; } $objectReflection = new \ReflectionClass($object); foreach ($referencedObjets as $fieldName => $reference) { $commonFieldMappings = $classMetadata->getCommonFields(); if (!$commonFieldMappings) { continue; } $commonFieldMappings = array_filter($commonFieldMappings, function ($field) use($fieldName) { return $field['target-field'] === $fieldName; }); $referencedObjectReflection = new \ReflectionClass($referencedObject); foreach ($commonFieldMappings as $commonField) { if (!$referencedObjectReflection->hasProperty($commonField['referenced-by'])) { continue; } $referencedObjectProperty = $referencedObjectReflection->getProperty($commonField['referenced-by']); $referencedObjectProperty->setAccessible(true); if (!$objectReflection->hasProperty($commonField['inversed-by'])) { continue; } $objectProperty = $objectReflection->getProperty($commonField['inversed-by']); $objectProperty->setAccessible(true); if ($commonField['sync-type'] === 'to-reference') { $value = $objectProperty->getValue($object); $referencedObjectProperty->setValue($referencedObject, $value); } elseif ($commonField['sync-type'] === 'from-reference') { $value = $referencedObjectProperty->getValue($referencedObject); $objectProperty->setValue($object, $value); } } } }
/** * @depends testClassName */ public function testSerialize(ClassMetadata $cm) { $expected = 'O:40:"Doctrine\\ODM\\PHPCR\\Mapping\\ClassMetadata":18:{s:8:"nodeType";s:15:"nt:unstructured";s:10:"identifier";s:2:"id";s:4:"name";s:39:"Doctrine\\Tests\\ODM\\PHPCR\\Mapping\\Person";s:11:"idGenerator";i:2;s:8:"mappings";a:5:{s:2:"id";a:7:{s:9:"fieldName";s:2:"id";s:2:"id";b:1;s:8:"strategy";s:8:"assigned";s:4:"type";s:6:"string";s:10:"multivalue";b:0;s:8:"nullable";b:0;s:8:"property";s:2:"id";}s:8:"username";a:5:{s:9:"fieldName";s:8:"username";s:8:"property";s:8:"username";s:4:"type";s:6:"string";s:10:"multivalue";b:0;s:8:"nullable";b:0;}s:7:"created";a:5:{s:9:"fieldName";s:7:"created";s:8:"property";s:7:"created";s:4:"type";s:8:"datetime";s:10:"multivalue";b:0;s:8:"nullable";b:0;}s:6:"locale";a:3:{s:9:"fieldName";s:6:"locale";s:4:"type";s:6:"locale";s:8:"property";s:6:"locale";}s:15:"translatedField";a:7:{s:9:"fieldName";s:15:"translatedField";s:4:"type";s:6:"string";s:10:"translated";b:1;s:8:"property";s:15:"translatedField";s:10:"multivalue";b:0;s:5:"assoc";N;s:8:"nullable";b:0;}}s:13:"fieldMappings";a:4:{i:0;s:2:"id";i:1;s:8:"username";i:2;s:7:"created";i:3;s:15:"translatedField";}s:17:"referenceMappings";a:0:{}s:17:"referrersMappings";a:0:{}s:22:"mixedReferrersMappings";a:0:{}s:16:"childrenMappings";a:0:{}s:13:"childMappings";a:0:{}s:25:"customRepositoryClassName";s:51:"Doctrine\\Tests\\ODM\\PHPCR\\Mapping\\DocumentRepository";s:18:"isMappedSuperclass";b:1;s:11:"versionable";b:1;s:18:"lifecycleCallbacks";a:1:{s:8:"postLoad";a:1:{i:0;s:8:"callback";}}s:13:"localeMapping";s:6:"locale";s:10:"translator";s:9:"attribute";s:18:"translatableFields";a:1:{i:0;s:15:"translatedField";}}'; $cm->addLifecycleCallback('callback', 'postLoadDocument'); $this->assertEquals($expected, serialize($cm)); }
/** * Validate runtime metadata is correctly defined. * * @param ClassMetadata $class * @throws MappingException */ protected function validateRuntimeMetadata($class) { if (!$class->getReflectionClass()) { // only validate if there is a reflection class instance return; } $class->validateClassMetadata(); $class->validateLifecycleCallbacks($this->getReflectionService()); }