From 9fea083f2412fa0ba70a136c959a8e9ba50dff43 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Tue, 9 Oct 2018 21:59:27 +0200 Subject: [PATCH] Drop doctrine/common proxies in favor of ProxyManager --- UPGRADE-2.0.md | 22 ++ composer.json | 1 + .../cookbook/blending-orm-and-mongodb-odm.rst | 2 - lib/Doctrine/ODM/MongoDB/Configuration.php | 77 ++++-- lib/Doctrine/ODM/MongoDB/DocumentManager.php | 35 +-- .../ODM/MongoDB/Hydrator/HydratorFactory.php | 20 +- .../ODM/MongoDB/Mapping/ClassMetadata.php | 10 +- .../MongoDB/Mapping/ClassMetadataFactory.php | 5 + .../MongoDB/Persisters/DocumentPersister.php | 8 +- .../ODM/MongoDB/Proxy/ClassNameResolver.php | 41 ++++ .../MongoDB/Proxy/Factory/ProxyFactory.php | 24 ++ .../Proxy/Factory/StaticProxyFactory.php | 150 ++++++++++++ .../ODM/MongoDB/Proxy/ProxyFactory.php | 221 ------------------ .../ODM/MongoDB/Query/ReferencePrimer.php | 6 +- .../Command/GenerateProxiesCommand.php | 17 +- .../Console/Command/Schema/CreateCommand.php | 11 +- lib/Doctrine/ODM/MongoDB/UnitOfWork.php | 36 +-- .../ODM/MongoDB/Tests/DocumentManagerTest.php | 2 +- .../Tests/Functional/DetachedDocumentTest.php | 35 --- .../Tests/Functional/IdentifiersTest.php | 6 +- .../Tests/Functional/ReferencePrimerTest.php | 68 +++--- .../Tests/Functional/ReferencesTest.php | 23 +- .../Tests/Functional/SimpleReferencesTest.php | 8 +- .../Tests/Functional/Ticket/GH520Test.php | 6 +- .../Tests/Functional/Ticket/GH593Test.php | 12 +- .../Tests/Functional/Ticket/GH602Test.php | 12 +- .../Tests/Functional/Ticket/GH852Test.php | 16 +- .../Tests/Functional/Ticket/GH936Test.php | 4 +- .../ODM/MongoDB/Tests/HydratorTest.php | 6 +- .../Mapping/ClassMetadataFactoryTest.php | 24 +- .../Tests/Mapping/ClassMetadataTest.php | 14 +- .../ODM/MongoDB/Tests/SchemaManagerTest.php | 10 +- .../ODM/MongoDB/Tests/UnitOfWorkTest.php | 4 +- tests/Stubs/DocumentManager.php | 2 +- 34 files changed, 465 insertions(+), 473 deletions(-) create mode 100644 lib/Doctrine/ODM/MongoDB/Proxy/ClassNameResolver.php create mode 100644 lib/Doctrine/ODM/MongoDB/Proxy/Factory/ProxyFactory.php create mode 100644 lib/Doctrine/ODM/MongoDB/Proxy/Factory/StaticProxyFactory.php delete mode 100644 lib/Doctrine/ODM/MongoDB/Proxy/ProxyFactory.php diff --git a/UPGRADE-2.0.md b/UPGRADE-2.0.md index c13d5f2a08..83273a6c12 100644 --- a/UPGRADE-2.0.md +++ b/UPGRADE-2.0.md @@ -37,6 +37,10 @@ * The `setRetryConnect` and `setRetryQuery` methods have been dropped without replacement. You should implement proper error handling instead of simply re-running queries or connection attempts. +* The `AUTOGENERATE_ALWAYS` and `AUTOGENERATE_NEVER` generation strategies for + proxy objects have been deprecated. Use `AUTOGENERATE_EVAL` and + `AUTOGENERATE_FILE_NOT_EXISTS` instead. This does not affect hydrator or + collection generation strategies. ## Cursor changes @@ -168,6 +172,24 @@ or annotations. To migrate away from YAML mappings, first update to MongoDB ODM `Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver` has been dropped without replacement. You should register a class loader in the `AnnotationRegistry` instead. + +## Proxy objects + +The proxy implementation no longer relies on Doctrine proxies but rather +the Proxy Manager library by ocramius. If you are checking for proxies, the +following changed: +* Proxies no longer implement `Doctrine\ODM\MongoDB\Proxy\Proxy` or + any other Doctrine proxy interface. To check whether a returned object is a + proxy, check for the `ProxyManager\Proxy\GhostObjectInterface` interface. +* The `__load` method has been replaced by `initializeProxy`. +* The `__isInitialized` method has been replaced by `isProxyInitialized`. +* To resolve the original class name for a proxy object, you can no longer use + the `Doctrine\Common\Util\ClassUtils` class. Instead, fetch the class name + resolver from the document manager: + ```php + $dm->getClassNameResolver()->getRealClass($className); + $dm->getClassNameResolver()->getClass($object); + ``` ## Repository diff --git a/composer.json b/composer.json index 3c731e6f7c..9d37057fda 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,7 @@ "doctrine/common": "^2.9", "doctrine/instantiator": "^1.1", "mongodb/mongodb": "^1.2.0", + "ocramius/proxy-manager": "^2.2", "symfony/console": "^3.4|^4.1" }, "require-dev": { diff --git a/docs/en/cookbook/blending-orm-and-mongodb-odm.rst b/docs/en/cookbook/blending-orm-and-mongodb-odm.rst index 30e040c8ee..72d888d6e3 100644 --- a/docs/en/cookbook/blending-orm-and-mongodb-odm.rst +++ b/docs/en/cookbook/blending-orm-and-mongodb-odm.rst @@ -176,10 +176,8 @@ Later we can retrieve the entity and lazily load the reference to the document i $order = $em->find('Order', $order->getId()); - // Instance of an uninitialized product proxy $product = $order->getProduct(); - // Initializes proxy and queries the database echo "Order Title: " . $product->getTitle(); If you were to print the `$order` you would see that we got back regular PHP objects: diff --git a/lib/Doctrine/ODM/MongoDB/Configuration.php b/lib/Doctrine/ODM/MongoDB/Configuration.php index 7396af0694..bb1553b0c1 100644 --- a/lib/Doctrine/ODM/MongoDB/Configuration.php +++ b/lib/Doctrine/ODM/MongoDB/Configuration.php @@ -19,6 +19,12 @@ use Doctrine\ODM\MongoDB\Repository\DocumentRepository; use Doctrine\ODM\MongoDB\Repository\GridFSRepository; use Doctrine\ODM\MongoDB\Repository\RepositoryFactory; +use InvalidArgumentException; +use ProxyManager\Configuration as ProxyManagerConfiguration; +use ProxyManager\Factory\LazyLoadingGhostFactory; +use ProxyManager\FileLocator\FileLocator; +use ProxyManager\GeneratorStrategy\EvaluatingGeneratorStrategy; +use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy; use ReflectionClass; use function trim; @@ -34,13 +40,6 @@ */ class Configuration { - /** - * Array of attributes for this configuration instance. - * - * @var array - */ - private $attributes = []; - /** * Never autogenerate a proxy/hydrator/persistent collection and rely that * it was generated by some process before deployment. Copied from @@ -72,6 +71,25 @@ class Configuration */ public const AUTOGENERATE_EVAL = 3; + /** + * Array of attributes for this configuration instance. + * + * @var array + */ + private $attributes = []; + + /** @var ProxyManagerConfiguration|null */ + private $proxyManagerConfiguration; + + /** @var int */ + private $autoGenerateProxyClasses = self::AUTOGENERATE_EVAL; + + public function __construct() + { + $this->proxyManagerConfiguration = new ProxyManagerConfiguration(); + $this->setAutoGenerateProxyClasses(self::AUTOGENERATE_FILE_NOT_EXISTS); + } + /** * Adds a namespace under a certain alias. */ @@ -154,7 +172,12 @@ public function setMetadataCacheImpl(Cache $cacheImpl) : void */ public function setProxyDir(string $dir) : void { - $this->attributes['proxyDir'] = $dir; + $this->getProxyManagerConfiguration()->setProxiesTargetDir($dir); + + // Recreate proxy generator to ensure its path was updated + if ($this->autoGenerateProxyClasses === self::AUTOGENERATE_FILE_NOT_EXISTS) { + $this->setAutoGenerateProxyClasses($this->autoGenerateProxyClasses); + } } /** @@ -162,7 +185,7 @@ public function setProxyDir(string $dir) : void */ public function getProxyDir() : ?string { - return $this->attributes['proxyDir'] ?? null; + return $this->getProxyManagerConfiguration()->getProxiesTargetDir(); } /** @@ -171,26 +194,44 @@ public function getProxyDir() : ?string */ public function getAutoGenerateProxyClasses() : int { - return $this->attributes['autoGenerateProxyClasses'] ?? self::AUTOGENERATE_ALWAYS; + return $this->autoGenerateProxyClasses; } /** * Sets an int flag that indicates whether proxy classes should always be regenerated * during each script execution. + * + * @throws InvalidArgumentException If an invalid mode was given. */ public function setAutoGenerateProxyClasses(int $mode) : void { - $this->attributes['autoGenerateProxyClasses'] = $mode; + $this->autoGenerateProxyClasses = $mode; + $proxyManagerConfig = $this->getProxyManagerConfiguration(); + + switch ($mode) { + case self::AUTOGENERATE_FILE_NOT_EXISTS: + $proxyManagerConfig->setGeneratorStrategy(new FileWriterGeneratorStrategy( + new FileLocator($proxyManagerConfig->getProxiesTargetDir()) + )); + + break; + case self::AUTOGENERATE_EVAL: + $proxyManagerConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy()); + + break; + default: + throw new InvalidArgumentException('Invalid proxy generation strategy given - only AUTOGENERATE_FILE_NOT_EXISTS and AUTOGENERATE_EVAL are supported.'); + } } public function getProxyNamespace() : ?string { - return $this->attributes['proxyNamespace'] ?? null; + return $this->getProxyManagerConfiguration()->getProxiesNamespace(); } public function setProxyNamespace(string $ns) : void { - $this->attributes['proxyNamespace'] = $ns; + $this->getProxyManagerConfiguration()->setProxiesNamespace($ns); } public function setHydratorDir(string $dir) : void @@ -410,4 +451,14 @@ public function getPersistentCollectionGenerator() : PersistentCollectionGenerat } return $this->attributes['persistentCollectionGenerator']; } + + public function buildGhostObjectFactory() : LazyLoadingGhostFactory + { + return new LazyLoadingGhostFactory(clone $this->getProxyManagerConfiguration()); + } + + public function getProxyManagerConfiguration() : ProxyManagerConfiguration + { + return $this->proxyManagerConfiguration; + } } diff --git a/lib/Doctrine/ODM/MongoDB/DocumentManager.php b/lib/Doctrine/ODM/MongoDB/DocumentManager.php index e8d3c552f3..c9cb119e13 100644 --- a/lib/Doctrine/ODM/MongoDB/DocumentManager.php +++ b/lib/Doctrine/ODM/MongoDB/DocumentManager.php @@ -11,7 +11,9 @@ use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory; use Doctrine\ODM\MongoDB\Mapping\MappingException; -use Doctrine\ODM\MongoDB\Proxy\ProxyFactory; +use Doctrine\ODM\MongoDB\Proxy\ClassNameResolver; +use Doctrine\ODM\MongoDB\Proxy\Factory\ProxyFactory; +use Doctrine\ODM\MongoDB\Proxy\Factory\StaticProxyFactory; use Doctrine\ODM\MongoDB\Query\FilterCollection; use Doctrine\ODM\MongoDB\Repository\RepositoryFactory; use InvalidArgumentException; @@ -139,6 +141,9 @@ class DocumentManager implements ObjectManager */ private $filterCollection; + /** @var ClassNameResolver */ + private $classNameResolver; + /** * Creates a new Document that operates on the given Mongo connection * and uses the given Configuration. @@ -174,13 +179,9 @@ protected function __construct(?Client $client = null, ?Configuration $config = $this->unitOfWork = new UnitOfWork($this, $this->eventManager, $this->hydratorFactory); $this->hydratorFactory->setUnitOfWork($this->unitOfWork); $this->schemaManager = new SchemaManager($this, $this->metadataFactory); - $this->proxyFactory = new ProxyFactory( - $this, - $this->config->getProxyDir(), - $this->config->getProxyNamespace(), - $this->config->getAutoGenerateProxyClasses() - ); + $this->proxyFactory = new StaticProxyFactory($this); $this->repositoryFactory = $this->config->getRepositoryFactory(); + $this->classNameResolver = new ClassNameResolver($this->config); } /** @@ -263,18 +264,22 @@ public function getSchemaManager() : SchemaManager return $this->schemaManager; } + /** Returns the class name resolver which is used to resolve real class names for proxy objects. */ + public function getClassNameResolver() : ClassNameResolver + { + return $this->classNameResolver; + } + /** * Returns the metadata for a class. * * @internal Performance-sensitive method. * * @param string $className The class name. - * - * @return ClassMetadata */ - public function getClassMetadata($className) + public function getClassMetadata($className) : ClassMetadata { - return $this->metadataFactory->getMetadataFor(ltrim($className, '\\')); + return $this->metadataFactory->getMetadataFor($className); } /** @@ -282,7 +287,7 @@ public function getClassMetadata($className) */ public function getDocumentDatabase(string $className) : Database { - $className = ltrim($className, '\\'); + $className = $this->classNameResolver->getRealClass($className); if (isset($this->documentDatabases[$className])) { return $this->documentDatabases[$className]; @@ -314,7 +319,7 @@ public function getDocumentDatabases() : array */ public function getDocumentCollection(string $className) : Collection { - $className = ltrim($className, '\\'); + $className = $this->classNameResolver->getRealClass($className); /** @var ClassMetadata $metadata */ $metadata = $this->metadataFactory->getMetadataFor($className); @@ -349,7 +354,7 @@ public function getDocumentCollection(string $className) : Collection */ public function getDocumentBucket(string $className) : Bucket { - $className = ltrim($className, '\\'); + $className = $this->classNameResolver->getRealClass($className); /** @var ClassMetadata $metadata */ $metadata = $this->metadataFactory->getMetadataFor($className); @@ -570,7 +575,7 @@ public function getReference(string $documentName, $identifier) : object return $document; } - $document = $this->proxyFactory->getProxy($class->name, [$class->identifier => $identifier]); + $document = $this->proxyFactory->getProxy($class, $identifier); $this->unitOfWork->registerManaged($document, $identifier, []); return $document; diff --git a/lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php b/lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php index 688d810366..7be6876374 100644 --- a/lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php +++ b/lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php @@ -11,9 +11,9 @@ use Doctrine\ODM\MongoDB\Event\PreLoadEventArgs; use Doctrine\ODM\MongoDB\Events; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Types\Type; use Doctrine\ODM\MongoDB\UnitOfWork; +use ProxyManager\Proxy\GhostObjectInterface; use const DIRECTORY_SEPARATOR; use function array_key_exists; use function chmod; @@ -444,26 +444,12 @@ public function hydrate(object $document, array $data, array $hints = []) : arra } } - if ($document instanceof Proxy) { - $document->__isInitialized__ = true; - $document->__setInitializer(null); - $document->__setCloner(null); + if ($document instanceof GhostObjectInterface) { + $document->setProxyInitializer(null); } $data = $this->getHydratorFor($metadata->name)->hydrate($document, $data, $hints); - if ($document instanceof Proxy) { - // lazy properties may be left uninitialized - $properties = $document->__getLazyProperties(); - foreach ($properties as $propertyName => $property) { - if (isset($document->$propertyName)) { - continue; - } - - $document->$propertyName = $properties[$propertyName]; - } - } - // Invoke the postLoad lifecycle callbacks and listeners if (! empty($metadata->lifecycleCallbacks[Events::postLoad])) { $metadata->invokeLifecycleCallbacks(Events::postLoad, $document, [new LifecycleEventArgs($document, $this->dm)]); diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php index e937584ecf..9385e300da 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadata.php @@ -10,11 +10,11 @@ use Doctrine\Instantiator\InstantiatorInterface; use Doctrine\ODM\MongoDB\Id\AbstractIdGenerator; use Doctrine\ODM\MongoDB\LockException; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Types\Type; use Doctrine\ODM\MongoDB\Utility\CollectionHelper; use InvalidArgumentException; use LogicException; +use ProxyManager\Proxy\GhostObjectInterface; use ReflectionClass; use ReflectionProperty; use function array_filter; @@ -1457,10 +1457,10 @@ public function getIdentifierObject(object $document) */ public function setFieldValue(object $document, string $field, $value) : void { - if ($document instanceof Proxy && ! $document->__isInitialized()) { + if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { //property changes to an uninitialized proxy will not be tracked or persisted, //so the proxy needs to be loaded first. - $document->__load(); + $document->initializeProxy(); } $this->reflFields[$field]->setValue($document, $value); @@ -1473,8 +1473,8 @@ public function setFieldValue(object $document, string $field, $value) : void */ public function getFieldValue(object $document, string $field) { - if ($document instanceof Proxy && $field !== $this->identifier && ! $document->__isInitialized()) { - $document->__load(); + if ($document instanceof GhostObjectInterface && $field !== $this->identifier && ! $document->isProxyInitialized()) { + $document->initializeProxy(); } return $this->reflFields[$field]->getValue($document); diff --git a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php index 3282023b59..a2e80df0d4 100644 --- a/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php +++ b/lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php @@ -56,6 +56,11 @@ public function setConfiguration(Configuration $config) : void $this->config = $config; } + public function getMetadataFor($className) + { + return parent::getMetadataFor($this->dm->getClassNameResolver()->getRealClass($className)); + } + /** * Lazy initialization of this stuff, especially the metadata driver, * since these are not needed at all when a metadata cache is active. diff --git a/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php b/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php index 9a54d5acf1..5f3e47c5c4 100644 --- a/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php +++ b/lib/Doctrine/ODM/MongoDB/Persisters/DocumentPersister.php @@ -18,7 +18,6 @@ use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\MongoDBException; use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Query\CriteriaMerger; use Doctrine\ODM\MongoDB\Query\Query; use Doctrine\ODM\MongoDB\Query\ReferencePrimer; @@ -32,6 +31,7 @@ use MongoDB\Driver\Exception\Exception as DriverException; use MongoDB\Driver\Exception\WriteException; use MongoDB\GridFS\Bucket; +use ProxyManager\Proxy\GhostObjectInterface; use stdClass; use function array_combine; use function array_fill; @@ -682,7 +682,7 @@ private function loadReferenceManyCollectionOwningSide(PersistentCollectionInter } // only query for the referenced object if it is not already initialized or the collection is sorted - if (! (($reference instanceof Proxy && ! $reference->__isInitialized__)) && ! $sorted) { + if (! (($reference instanceof GhostObjectInterface && ! $reference->isProxyInitialized())) && ! $sorted) { continue; } @@ -716,11 +716,11 @@ private function loadReferenceManyCollectionOwningSide(PersistentCollectionInter $documents = $cursor->toArray(); foreach ($documents as $documentData) { $document = $this->uow->getById($documentData['_id'], $class); - if ($document instanceof Proxy && ! $document->__isInitialized()) { + if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { $data = $this->hydratorFactory->hydrate($document, $documentData); $this->uow->setOriginalDocumentData($document, $data); - $document->__isInitialized__ = true; } + if (! $sorted) { continue; } diff --git a/lib/Doctrine/ODM/MongoDB/Proxy/ClassNameResolver.php b/lib/Doctrine/ODM/MongoDB/Proxy/ClassNameResolver.php new file mode 100644 index 0000000000..b143328dc1 --- /dev/null +++ b/lib/Doctrine/ODM/MongoDB/Proxy/ClassNameResolver.php @@ -0,0 +1,41 @@ +configuration = $configuration; + } + + /** + * Gets the real class name of a class name that could be a proxy. + */ + public function getRealClass(string $class) : string + { + return $this->getClassNameInflector()->getUserClassName($class); + } + + /** + * Gets the real class name of an object (even if its a proxy). + */ + public function getClass(object $object) : string + { + return $this->getRealClass(get_class($object)); + } + + private function getClassNameInflector() : ClassNameInflectorInterface + { + return $this->configuration->getProxyManagerConfiguration()->getClassNameInflector(); + } +} diff --git a/lib/Doctrine/ODM/MongoDB/Proxy/Factory/ProxyFactory.php b/lib/Doctrine/ODM/MongoDB/Proxy/Factory/ProxyFactory.php new file mode 100644 index 0000000000..f72c864e90 --- /dev/null +++ b/lib/Doctrine/ODM/MongoDB/Proxy/Factory/ProxyFactory.php @@ -0,0 +1,24 @@ +uow = $documentManager->getUnitOfWork(); + $this->lifecycleEventManager = new LifecycleEventManager($documentManager, $this->uow, $documentManager->getEventManager()); + $this->proxyFactory = $documentManager->getConfiguration()->buildGhostObjectFactory(); + } + + public function getProxy(ClassMetadata $metadata, $identifier) : GhostObjectInterface + { + $documentPersister = $this->uow->getDocumentPersister($metadata->getName()); + + $ghostObject = $this + ->proxyFactory + ->createProxy( + $metadata->getName(), + $this->createInitializer($metadata, $documentPersister), + [ + 'skippedProperties' => $this->skippedFieldsFqns($metadata), + ] + ); + + $metadata->setIdentifierValue($ghostObject, $identifier); + + return $ghostObject; + } + + /** + * {@inheritdoc} + * + * @param ClassMetadata[] $classes + */ + public function generateProxyClasses(array $classes) : int + { + $concreteClasses = array_filter($classes, static function (ClassMetadata $metadata) : bool { + return ! ($metadata->isMappedSuperclass || $metadata->isQueryResultDocument || $metadata->getReflectionClass()->isAbstract()); + }); + + foreach ($concreteClasses as $metadata) { + $this + ->proxyFactory + ->createProxy( + $metadata->getName(), + static function () { + // empty closure, serves its purpose, for now + }, + $this->skippedFieldsFqns($metadata) + ); + } + + return count($concreteClasses); + } + + private function createInitializer( + ClassMetadata $metadata, + DocumentPersister $documentPersister + ) : Closure { + return function ( + GhostObjectInterface $ghostObject, + string $method, + // we don't care + array $parameters, + // we don't care + & $initializer, + array $properties // we currently do not use this + ) use ( + $metadata, + $documentPersister + ) : bool { + $originalInitializer = $initializer; + $initializer = null; + $identifier = $metadata->getIdentifierValue($ghostObject); + + if (! $documentPersister->load(['_id' => $identifier], $ghostObject)) { + $initializer = $originalInitializer; + + if (! $this->lifecycleEventManager->documentNotFound($ghostObject, $identifier)) { + throw DocumentNotFoundException::documentNotFound($metadata->getName(), $identifier); + } + } + + if ($ghostObject instanceof NotifyPropertyChanged) { + $ghostObject->addPropertyChangedListener($this->uow); + } + + return true; + }; + } + + /** + * @return string[] + */ + private function skippedFieldsFqns(ClassMetadata $metadata) : array + { + $idFieldFqcns = []; + + foreach ($metadata->getIdentifierFieldNames() as $idField) { + $idFieldFqcns[] = $this->propertyFqcn($metadata->getReflectionProperty($idField)); + } + + return $idFieldFqcns; + } + + private function propertyFqcn(ReflectionProperty $property) : string + { + if ($property->isPrivate()) { + return "\0" . $property->getDeclaringClass()->getName() . "\0" . $property->getName(); + } + + if ($property->isProtected()) { + return "\0*\0" . $property->getName(); + } + + return $property->getName(); + } +} diff --git a/lib/Doctrine/ODM/MongoDB/Proxy/ProxyFactory.php b/lib/Doctrine/ODM/MongoDB/Proxy/ProxyFactory.php deleted file mode 100644 index ed635269a1..0000000000 --- a/lib/Doctrine/ODM/MongoDB/Proxy/ProxyFactory.php +++ /dev/null @@ -1,221 +0,0 @@ -ProxyFactory class that is - * connected to the given DocumentManager. - * - * @param DocumentManager $documentManager The DocumentManager the new factory works for. - * @param string $proxyDir The directory to use for the proxy classes. It - * must exist. - * @param string $proxyNamespace The namespace to use for the proxy classes. - * @param int $autoGenerate Whether to automatically generate proxy classes. - */ - public function __construct(DocumentManager $documentManager, $proxyDir, $proxyNamespace, $autoGenerate = AbstractProxyFactory::AUTOGENERATE_NEVER) - { - $this->metadataFactory = $documentManager->getMetadataFactory(); - $this->uow = $documentManager->getUnitOfWork(); - $this->proxyNamespace = $proxyNamespace; - $this->lifecycleEventManager = new LifecycleEventManager($documentManager, $this->uow, $documentManager->getEventManager()); - $proxyGenerator = new ProxyGenerator($proxyDir, $proxyNamespace); - - $proxyGenerator->setPlaceholder('baseProxyInterface', Proxy::class); - - parent::__construct($proxyGenerator, $this->metadataFactory, $autoGenerate); - } - - /** - * {@inheritDoc} - */ - public function skipClass(BaseClassMetadata $class) - { - /** @var ClassMetadata $class */ - return $class->isMappedSuperclass || $class->isQueryResultDocument || $class->getReflectionClass()->isAbstract(); - } - - /** - * {@inheritDoc} - */ - public function createProxyDefinition($className) - { - /** @var ClassMetadata $classMetadata */ - $classMetadata = $this->metadataFactory->getMetadataFor($className); - $documentPersister = $this->uow->getDocumentPersister($className); - $reflectionId = $classMetadata->reflFields[$classMetadata->identifier]; - - return new ProxyDefinition( - ClassUtils::generateProxyClassName($className, $this->proxyNamespace), - $classMetadata->getIdentifierFieldNames(), - $classMetadata->getReflectionProperties(), - $this->createInitializer($classMetadata, $documentPersister, $reflectionId), - $this->createCloner($classMetadata, $documentPersister, $reflectionId) - ); - } - - /** - * Generates a closure capable of initializing a proxy - * - * @return Closure - * - * @throws DocumentNotFoundException - */ - private function createInitializer( - BaseClassMetadata $classMetadata, - DocumentPersister $documentPersister, - ReflectionProperty $reflectionId - ) { - if ($classMetadata->getReflectionClass()->hasMethod('__wakeup')) { - return function (BaseProxy $proxy) use ($documentPersister, $reflectionId) { - $proxy->__setInitializer(null); - $proxy->__setCloner(null); - - if ($proxy->__isInitialized()) { - return; - } - - $properties = $proxy->__getLazyProperties(); - - foreach ($properties as $propertyName => $property) { - if (isset($proxy->$propertyName)) { - continue; - } - - $proxy->$propertyName = $properties[$propertyName]; - } - - $proxy->__setInitialized(true); - $proxy->__wakeup(); - - $id = $reflectionId->getValue($proxy); - - if ($documentPersister->load(['_id' => $id], $proxy) === null) { - if (! $this->lifecycleEventManager->documentNotFound($proxy, $id)) { - throw DocumentNotFoundException::documentNotFound(get_class($proxy), $id); - } - } - - if (! ($proxy instanceof NotifyPropertyChanged)) { - return; - } - - $proxy->addPropertyChangedListener($this->uow); - }; - } - - return function (BaseProxy $proxy) use ($documentPersister, $reflectionId) { - $proxy->__setInitializer(null); - $proxy->__setCloner(null); - - if ($proxy->__isInitialized()) { - return; - } - - $properties = $proxy->__getLazyProperties(); - - foreach ($properties as $propertyName => $property) { - if (isset($proxy->$propertyName)) { - continue; - } - - $proxy->$propertyName = $properties[$propertyName]; - } - - $proxy->__setInitialized(true); - - $id = $reflectionId->getValue($proxy); - - if ($documentPersister->load(['_id' => $id], $proxy) === null) { - if (! $this->lifecycleEventManager->documentNotFound($proxy, $id)) { - throw DocumentNotFoundException::documentNotFound(get_class($proxy), $id); - } - } - - if (! ($proxy instanceof NotifyPropertyChanged)) { - return; - } - - $proxy->addPropertyChangedListener($this->uow); - }; - } - - /** - * Generates a closure capable of finalizing a cloned proxy - * - * @return Closure - * - * @throws DocumentNotFoundException - */ - private function createCloner( - BaseClassMetadata $classMetadata, - DocumentPersister $documentPersister, - ReflectionProperty $reflectionId - ) { - return function (BaseProxy $proxy) use ($documentPersister, $classMetadata, $reflectionId) { - if ($proxy->__isInitialized()) { - return; - } - - $proxy->__setInitialized(true); - $proxy->__setInitializer(null); - - $id = $reflectionId->getValue($proxy); - $original = $documentPersister->load(['_id' => $id]); - - if ($original === null) { - if (! $this->lifecycleEventManager->documentNotFound($proxy, $id)) { - throw DocumentNotFoundException::documentNotFound(get_class($proxy), $id); - } - } - - foreach ($classMetadata->getReflectionClass()->getProperties() as $reflectionProperty) { - $propertyName = $reflectionProperty->getName(); - - if (! $classMetadata->hasField($propertyName) && ! $classMetadata->hasAssociation($propertyName)) { - continue; - } - - $reflectionProperty->setAccessible(true); - $reflectionProperty->setValue($proxy, $reflectionProperty->getValue($original)); - } - }; - } -} diff --git a/lib/Doctrine/ODM/MongoDB/Query/ReferencePrimer.php b/lib/Doctrine/ODM/MongoDB/Query/ReferencePrimer.php index 433848a61e..2edc0cb116 100644 --- a/lib/Doctrine/ODM/MongoDB/Query/ReferencePrimer.php +++ b/lib/Doctrine/ODM/MongoDB/Query/ReferencePrimer.php @@ -8,10 +8,10 @@ use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\UnitOfWork; use InvalidArgumentException; use LogicException; +use ProxyManager\Proxy\GhostObjectInterface; use Traversable; use function array_push; use function array_shift; @@ -136,7 +136,7 @@ public function primeReferences(ClassMetadata $class, $documents, string $fieldN continue; } - if ($mapping['type'] === 'one' && $fieldValue instanceof Proxy && ! $fieldValue->__isInitialized()) { + if ($mapping['type'] === 'one' && $fieldValue instanceof GhostObjectInterface && ! $fieldValue->isProxyInitialized()) { $refClass = $this->dm->getClassMetadata(get_class($fieldValue)); $id = $this->uow->getDocumentIdentifier($fieldValue); $groupedIds[$refClass->name][serialize($id)] = $id; @@ -253,7 +253,7 @@ private function addManyReferences(PersistentCollectionInterface $persistentColl $document = $this->uow->tryGetById($id, $class); - if ($document && ! (($document instanceof Proxy && ! $document->__isInitialized()))) { + if ($document && ! (($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()))) { continue; } diff --git a/lib/Doctrine/ODM/MongoDB/Tools/Console/Command/GenerateProxiesCommand.php b/lib/Doctrine/ODM/MongoDB/Tools/Console/Command/GenerateProxiesCommand.php index 817f6acfe8..526b676952 100644 --- a/lib/Doctrine/ODM/MongoDB/Tools/Console/Command/GenerateProxiesCommand.php +++ b/lib/Doctrine/ODM/MongoDB/Tools/Console/Command/GenerateProxiesCommand.php @@ -4,11 +4,11 @@ namespace Doctrine\ODM\MongoDB\Tools\Console\Command; +use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Tools\Console\MetadataFilter; use InvalidArgumentException; use Symfony\Component\Console; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use const PHP_EOL; use function array_filter; @@ -40,11 +40,6 @@ protected function configure() InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match documents that should be processed.' ), - new InputArgument( - 'dest-path', - InputArgument::OPTIONAL, - 'The path to generate your proxy classes. If none is provided, it will attempt to grab from configuration.' - ), ]) ->setHelp(<<getHelper('documentManager')->getDocumentManager(); $metadatas = array_filter($dm->getMetadataFactory()->getAllMetadata(), static function (ClassMetadata $classMetadata) { return ! $classMetadata->isEmbeddedDocument && ! $classMetadata->isMappedSuperclass && ! $classMetadata->isQueryResultDocument; }); $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); - $destPath = $input->getArgument('dest-path'); - - // Process destination directory - if ($destPath === null) { - $destPath = $dm->getConfiguration()->getProxyDir(); - } + $destPath = $dm->getConfiguration()->getProxyDir(); if (! is_dir($destPath)) { mkdir($destPath, 0775, true); @@ -94,7 +85,7 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O } // Generating Proxies - $dm->getProxyFactory()->generateProxyClasses($metadatas, $destPath); + $dm->getProxyFactory()->generateProxyClasses($metadatas); // Outputting information message $output->write(PHP_EOL . sprintf('Proxy classes generated to "%s"', $destPath) . PHP_EOL); diff --git a/lib/Doctrine/ODM/MongoDB/Tools/Console/Command/Schema/CreateCommand.php b/lib/Doctrine/ODM/MongoDB/Tools/Console/Command/Schema/CreateCommand.php index c254976c83..6fb568d27f 100644 --- a/lib/Doctrine/ODM/MongoDB/Tools/Console/Command/Schema/CreateCommand.php +++ b/lib/Doctrine/ODM/MongoDB/Tools/Console/Command/Schema/CreateCommand.php @@ -5,7 +5,6 @@ namespace Doctrine\ODM\MongoDB\Tools\Console\Command\Schema; use BadMethodCallException; -use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\SchemaManager; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -107,19 +106,11 @@ protected function processDocumentProxy(SchemaManager $sm, $document) { $classMetadata = $this->getMetadataFactory()->getMetadataFor($document); - if ($classMetadata->isEmbeddedDocument || $classMetadata->isMappedSuperclass || $classMetadata->isQueryResultDocument) { - return; - } - $this->getDocumentManager()->getProxyFactory()->generateProxyClasses([$classMetadata]); } protected function processProxy(SchemaManager $sm) { - $classes = array_filter($this->getMetadataFactory()->getAllMetadata(), static function (ClassMetadata $classMetadata) { - return ! $classMetadata->isEmbeddedDocument && ! $classMetadata->isMappedSuperclass && ! $classMetadata->isQueryResultDocument; - }); - - $this->getDocumentManager()->getProxyFactory()->generateProxyClasses($classes); + $this->getDocumentManager()->getProxyFactory()->generateProxyClasses($this->getMetadataFactory()->getAllMetadata()); } } diff --git a/lib/Doctrine/ODM/MongoDB/UnitOfWork.php b/lib/Doctrine/ODM/MongoDB/UnitOfWork.php index 636acb72d4..b985609966 100644 --- a/lib/Doctrine/ODM/MongoDB/UnitOfWork.php +++ b/lib/Doctrine/ODM/MongoDB/UnitOfWork.php @@ -15,7 +15,6 @@ use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionInterface; use Doctrine\ODM\MongoDB\Persisters\CollectionPersister; use Doctrine\ODM\MongoDB\Persisters\PersistenceBuilder; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Query\Query; use Doctrine\ODM\MongoDB\Types\DateType; use Doctrine\ODM\MongoDB\Types\Type; @@ -23,6 +22,7 @@ use Doctrine\ODM\MongoDB\Utility\LifecycleEventManager; use InvalidArgumentException; use MongoDB\BSON\UTCDateTime; +use ProxyManager\Proxy\GhostObjectInterface; use UnexpectedValueException; use function array_filter; use function count; @@ -825,7 +825,7 @@ public function computeChangeSets() : void foreach ($documentsToProcess as $document) { // Ignore uninitialized proxy objects - if ($document instanceof Proxy && ! $document->__isInitialized__) { + if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { continue; } // Only MANAGED documents that are NOT SCHEDULED FOR INSERTION, UPSERT OR DELETION are processed here. @@ -856,7 +856,7 @@ private function computeAssociationChanges(object $parentDocument, array $assoc, $class = $this->dm->getClassMetadata(get_class($parentDocument)); $topOrExistingDocument = ( ! $isNewParentDocument || ! $class->isEmbeddedDocument); - if ($value instanceof Proxy && ! $value->__isInitialized__) { + if ($value instanceof GhostObjectInterface && ! $value->isProxyInitialized()) { return; } @@ -971,7 +971,7 @@ private function computeAssociationChanges(object $parentDocument, array $assoc, public function recomputeSingleDocumentChangeSet(ClassMetadata $class, object $document) : void { // Ignore uninitialized proxy objects - if ($document instanceof Proxy && ! $document->__isInitialized__) { + if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { return; } @@ -1329,7 +1329,7 @@ public function addToIdentityMap(object $document) : bool $this->identityMap[$class->name][$id] = $document; if ($document instanceof NotifyPropertyChanged && - ( ! $document instanceof Proxy || $document->__isInitialized())) { + ( ! $document instanceof GhostObjectInterface || $document->isProxyInitialized())) { $document->addPropertyChangedListener($this); } @@ -1684,8 +1684,8 @@ private function doMerge(object $document, array &$visited, ?object $prevManaged $managedCopy = $document; if ($this->getDocumentState($document, self::STATE_DETACHED) !== self::STATE_MANAGED) { - if ($document instanceof Proxy && ! $document->__isInitialized()) { - $document->__load(); + if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { + $document->initializeProxy(); } $identifier = $class->getIdentifier(); @@ -1702,8 +1702,8 @@ private function doMerge(object $document, array &$visited, ?object $prevManaged throw new InvalidArgumentException('Removed entity detected during merge. Cannot merge with a removed entity.'); } - if ($managedCopy instanceof Proxy && ! $managedCopy->__isInitialized__) { - $managedCopy->__load(); + if ($managedCopy instanceof GhostObjectInterface && ! $managedCopy->isProxyInitialized()) { + $managedCopy->initializeProxy(); } } @@ -1742,7 +1742,7 @@ private function doMerge(object $document, array &$visited, ?object $prevManaged if ($other === null) { $prop->setValue($managedCopy, null); - } elseif ($other instanceof Proxy && ! $other->__isInitialized__) { + } elseif ($other instanceof GhostObjectInterface && ! $other->isProxyInitialized()) { // Do not merge fields marked lazy that have not been fetched continue; } elseif (! $assoc2['isCascadeMerge']) { @@ -1758,7 +1758,7 @@ private function doMerge(object $document, array &$visited, ?object $prevManaged $other = $this ->dm ->getProxyFactory() - ->getProxy($assoc2['targetDocument'], [$targetClass->identifier => $relatedId]); + ->getProxy($targetClass, $relatedId); $this->registerManaged($other, $relatedId, []); } } @@ -2077,8 +2077,8 @@ private function cascadeRemove(object $document, array &$visited) : void if (! $mapping['isCascadeRemove'] && ( ! isset($mapping['orphanRemoval']) || ! $mapping['orphanRemoval'])) { continue; } - if ($document instanceof Proxy && ! $document->__isInitialized__) { - $document->__load(); + if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { + $document->initializeProxy(); } $relatedDocuments = $class->reflFields[$mapping['fieldName']]->getValue($document); @@ -2503,9 +2503,9 @@ public function getOrCreateDocument(string $className, array $data, array &$hint if ($isManagedObject) { $document = $this->identityMap[$class->name][$serializedId]; $oid = spl_object_hash($document); - if ($document instanceof Proxy && ! $document->__isInitialized__) { - $document->__isInitialized__ = true; - $overrideLocalValues = true; + if ($document instanceof GhostObjectInterface && ! $document->isProxyInitialized()) { + $document->setProxyInitializer(null); + $overrideLocalValues = true; if ($document instanceof NotifyPropertyChanged) { $document->addPropertyChangedListener($this); } @@ -2740,8 +2740,8 @@ public function getScheduledCollectionUpdates() : array */ public function initializeObject(object $obj) : void { - if ($obj instanceof Proxy) { - $obj->__load(); + if ($obj instanceof GhostObjectInterface) { + $obj->initializeProxy(); } elseif ($obj instanceof PersistentCollectionInterface) { $obj->initialize(); } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php b/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php index 9a27c85255..cd36adafea 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/DocumentManagerTest.php @@ -12,7 +12,7 @@ use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory; use Doctrine\ODM\MongoDB\MongoDBException; -use Doctrine\ODM\MongoDB\Proxy\ProxyFactory; +use Doctrine\ODM\MongoDB\Proxy\Factory\ProxyFactory; use Doctrine\ODM\MongoDB\Query\Builder as QueryBuilder; use Doctrine\ODM\MongoDB\Query\FilterCollection; use Doctrine\ODM\MongoDB\SchemaManager; diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/DetachedDocumentTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/DetachedDocumentTest.php index fec0816104..a759c9c89b 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/DetachedDocumentTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/DetachedDocumentTest.php @@ -4,12 +4,9 @@ namespace Doctrine\Tests\ORM\Functional; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Tests\BaseTest; -use Documents\CmsAddress; use Documents\CmsPhonenumber; use Documents\CmsUser; -use function get_class; use function serialize; use function unserialize; @@ -89,36 +86,4 @@ public function testSerializeUnserializeModifyMerge() $this->assertTrue($this->dm->contains($phonenumbers[0])); $this->assertTrue($this->dm->contains($phonenumbers[1])); } - - public function testUninitializedLazyAssociationsAreIgnoredOnMerge() - { - $user = new CmsUser(); - $user->name = 'Guilherme'; - $user->username = 'gblanco'; - $user->status = 'developer'; - - $address = new CmsAddress(); - $address->city = 'Berlin'; - $address->country = 'Germany'; - $address->street = 'Sesamestreet'; - $address->zip = 12345; - $address->setUser($user); - $this->dm->persist($address); - $this->dm->persist($user); - - $this->dm->flush(); - $this->dm->clear(); - - $address2 = $this->dm->find(get_class($address), $address->id); - $this->assertInstanceOf(Proxy::class, $address2->user); - $this->assertFalse($address2->user->__isInitialized()); - $detachedAddress2 = unserialize(serialize($address2)); - $this->assertInstanceOf(Proxy::class, $detachedAddress2->user); - $this->assertFalse($detachedAddress2->user->__isInitialized()); - - $managedAddress2 = $this->dm->merge($detachedAddress2); - $this->assertInstanceOf(Proxy::class, $managedAddress2->user); - $this->assertNotSame($managedAddress2->user, $detachedAddress2->user); - $this->assertFalse($managedAddress2->user->__isInitialized()); - } } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdentifiersTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdentifiersTest.php index 04ab331e2a..bde4582af9 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdentifiersTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/IdentifiersTest.php @@ -26,7 +26,7 @@ public function testGetIdentifierValue() $test = $this->dm->getRepository(get_class($event))->find($event->getId()); $this->assertEquals($user->getId(), $test->getUser()->getId()); - $this->assertFalse($test->getUser()->__isInitialized__); + $this->assertFalse($test->getUser()->isProxyInitialized()); $this->dm->clear(); @@ -35,10 +35,10 @@ public function testGetIdentifierValue() $test = $this->dm->getRepository(get_class($event))->find($event->getId()); $this->assertEquals($user->getId(), $class->getIdentifierValue($test->getUser())); $this->assertEquals($user->getId(), $class->getFieldValue($test->getUser(), 'id')); - $this->assertFalse($test->getUser()->__isInitialized__); + $this->assertFalse($test->getUser()->isProxyInitialized()); $this->assertEquals('jwage', $test->getUser()->getUsername()); - $this->assertTrue($test->getUser()->__isInitialized__); + $this->assertTrue($test->getUser()->isProxyInitialized()); } public function testIdentifiersAreSet() diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php index ff53fce03d..e924c0550f 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencePrimerTest.php @@ -8,7 +8,6 @@ use DateTime; use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Query\Query; use Doctrine\ODM\MongoDB\Tests\BaseTest; use Documents\Account; @@ -32,6 +31,7 @@ use Documents\SimpleReferenceUser; use Documents\User; use MongoDB\Driver\ReadPreference; +use ProxyManager\Proxy\GhostObjectInterface; use function func_get_args; class ReferencePrimerTest extends BaseTest @@ -132,8 +132,8 @@ public function testPrimeReferencesWithDBRefObjects() ->field('groups')->prime(true); foreach ($qb->getQuery() as $user) { - $this->assertInstanceOf(Proxy::class, $user->getAccount()); - $this->assertTrue($user->getAccount()->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $user->getAccount()); + $this->assertTrue($user->getAccount()->isProxyInitialized()); $this->assertCount(2, $user->getGroups()); @@ -141,7 +141,7 @@ public function testPrimeReferencesWithDBRefObjects() * initialized, they will not be hydrated as proxy objects. */ foreach ($user->getGroups() as $group) { - $this->assertNotInstanceOf(Proxy::class, $group); + $this->assertNotInstanceOf(GhostObjectInterface::class, $group); $this->assertInstanceOf(Group::class, $group); } } @@ -170,13 +170,13 @@ public function testPrimeReferencesWithSimpleReferences() ->field('users')->prime(true); foreach ($qb->getQuery() as $simpleUser) { - $this->assertInstanceOf(Proxy::class, $simpleUser->getUser()); - $this->assertTrue($simpleUser->getUser()->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $simpleUser->getUser()); + $this->assertTrue($simpleUser->getUser()->isProxyInitialized()); $this->assertCount(2, $simpleUser->getUsers()); foreach ($simpleUser->getUsers() as $user) { - $this->assertNotInstanceOf(Proxy::class, $user); + $this->assertNotInstanceOf(GhostObjectInterface::class, $user); $this->assertInstanceOf(User::class, $user); } } @@ -225,20 +225,20 @@ public function testPrimeReferencesNestedInNamedEmbeddedReference() ->field('embeddedDocs.referencedDocs')->prime(true); foreach ($qb->getQuery() as $root) { - $this->assertNotInstanceOf(Proxy::class, $root->embeddedDoc); + $this->assertNotInstanceOf(GhostObjectInterface::class, $root->embeddedDoc); $this->assertInstanceOf(EmbeddedWhichReferences::class, $root->embeddedDoc); $this->assertCount(2, $root->embeddedDocs); foreach ($root->embeddedDocs as $embeddedDoc) { - $this->assertNotInstanceOf(Proxy::class, $embeddedDoc); + $this->assertNotInstanceOf(GhostObjectInterface::class, $embeddedDoc); $this->assertInstanceOf(EmbeddedWhichReferences::class, $embeddedDoc); - $this->assertInstanceOf(Proxy::class, $embeddedDoc->referencedDoc); - $this->assertTrue($embeddedDoc->referencedDoc->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $embeddedDoc->referencedDoc); + $this->assertTrue($embeddedDoc->referencedDoc->isProxyInitialized()); $this->assertCount(2, $embeddedDoc->referencedDocs); foreach ($embeddedDoc->referencedDocs as $referencedDoc) { - $this->assertNotInstanceOf(Proxy::class, $referencedDoc); + $this->assertNotInstanceOf(GhostObjectInterface::class, $referencedDoc); $this->assertInstanceOf(Reference::class, $referencedDoc); } } @@ -288,35 +288,35 @@ public function testPrimeReferencesWithDifferentStoreAsReferences() /** @var ReferenceUser $referenceUser */ foreach ($qb->getQuery() as $referenceUser) { // storeAs=id reference - $this->assertInstanceOf(Proxy::class, $referenceUser->getUser()); - $this->assertTrue($referenceUser->getUser()->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $referenceUser->getUser()); + $this->assertTrue($referenceUser->getUser()->isProxyInitialized()); $this->assertCount(1, $referenceUser->getUsers()); foreach ($referenceUser->getUsers() as $user) { - $this->assertNotInstanceOf(Proxy::class, $user); + $this->assertNotInstanceOf(GhostObjectInterface::class, $user); $this->assertInstanceOf(User::class, $user); } // storeAs=dbRef reference - $this->assertInstanceOf(Proxy::class, $referenceUser->getParentUser()); - $this->assertTrue($referenceUser->getParentUser()->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $referenceUser->getParentUser()); + $this->assertTrue($referenceUser->getParentUser()->isProxyInitialized()); $this->assertCount(1, $referenceUser->getParentUsers()); foreach ($referenceUser->getParentUsers() as $user) { - $this->assertNotInstanceOf(Proxy::class, $user); + $this->assertNotInstanceOf(GhostObjectInterface::class, $user); $this->assertInstanceOf(User::class, $user); } // storeAs=dbRefWithDb reference - $this->assertInstanceOf(Proxy::class, $referenceUser->getOtherUser()); - $this->assertTrue($referenceUser->getOtherUser()->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $referenceUser->getOtherUser()); + $this->assertTrue($referenceUser->getOtherUser()->isProxyInitialized()); $this->assertCount(1, $referenceUser->getOtherUsers()); foreach ($referenceUser->getOtherUsers() as $user) { - $this->assertNotInstanceOf(Proxy::class, $user); + $this->assertNotInstanceOf(GhostObjectInterface::class, $user); $this->assertInstanceOf(User::class, $user); } } @@ -343,10 +343,10 @@ public function testPrimeReferencesWithDiscriminatedReferenceMany() foreach ($qb->getQuery() as $user) { $favorites = $user->getFavorites()->toArray(); - $this->assertNotInstanceOf(Proxy::class, $favorites[0]); + $this->assertNotInstanceOf(GhostObjectInterface::class, $favorites[0]); $this->assertInstanceOf(Group::class, $favorites[0]); - $this->assertNotInstanceOf(Proxy::class, $favorites[1]); + $this->assertNotInstanceOf(GhostObjectInterface::class, $favorites[1]); $this->assertInstanceOf(Project::class, $favorites[1]); } } @@ -365,8 +365,8 @@ public function testPrimeReferencesWithDiscriminatedReferenceOne() ->field('server')->prime(true); foreach ($qb->getQuery() as $agent) { - $this->assertInstanceOf(Proxy::class, $agent->server); - $this->assertTrue($agent->server->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $agent->server); + $this->assertTrue($agent->server->isProxyInitialized()); } } @@ -394,7 +394,7 @@ public function testPrimeReferencesIgnoresInitializedProxyObjects() $this->assertCount(2, $user->getGroups()); foreach ($user->getGroups() as $group) { - $this->assertNotInstanceOf(Proxy::class, $group); + $this->assertNotInstanceOf(GhostObjectInterface::class, $group); $this->assertInstanceOf(Group::class, $group); } } @@ -464,7 +464,7 @@ public function testPrimeReferencesInFindAndModifyResult() $this->assertCount(1, $user->getGroups()); foreach ($user->getGroups() as $group) { - $this->assertNotInstanceOf(Proxy::class, $group); + $this->assertNotInstanceOf(GhostObjectInterface::class, $group); $this->assertInstanceOf(Group::class, $group); } } @@ -495,7 +495,7 @@ public function testPrimeEmbeddedReferenceOneLevelDeep() $phonenumber = $phonenumbers->current(); - $this->assertNotInstanceOf(Proxy::class, $phonenumber); + $this->assertNotInstanceOf(GhostObjectInterface::class, $phonenumber); $this->assertInstanceOf(Phonenumber::class, $phonenumber); } @@ -548,8 +548,8 @@ public function testPrimeEmbeddedReferenceTwoLevelsDeep() /** @var Currency $currency */ $currency = $money->getCurrency(); - $this->assertInstanceOf(Proxy::class, $currency); - $this->assertTrue($currency->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $currency); + $this->assertTrue($currency->isProxyInitialized()); } public function testPrimeReferencesInReferenceMany() @@ -576,8 +576,8 @@ public function testPrimeReferencesInReferenceMany() $this->assertInstanceOf(BlogPost::class, $post); $comment = $post->comments->first(); - $this->assertInstanceOf(Proxy::class, $comment->author); - $this->assertTrue($comment->author->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $comment->author); + $this->assertTrue($comment->author->isProxyInitialized()); } public function testPrimeReferencesInReferenceManyWithRepositoryMethodThrowsException() @@ -633,7 +633,7 @@ public function testPrimeReferencesInReferenceManyWithRepositoryMethodEager() $this->assertInstanceOf(BlogPost::class, $post); $comment = $post->repoCommentsEager->first(); - $this->assertInstanceOf(Proxy::class, $comment->author); - $this->assertTrue($comment->author->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $comment->author); + $this->assertTrue($comment->author->isProxyInitialized()); } } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencesTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencesTest.php index d93b90eee5..82d294da5d 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencesTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/ReferencesTest.php @@ -5,7 +5,6 @@ namespace Doctrine\ODM\MongoDB\Tests\Functional; use Closure; -use Doctrine\Common\Persistence\Proxy; use Doctrine\ODM\MongoDB\Event\DocumentNotFoundEventArgs; use Doctrine\ODM\MongoDB\Events; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; @@ -20,6 +19,7 @@ use Documents\User; use MongoDB\BSON\Binary; use MongoDB\BSON\ObjectId; +use ProxyManager\Proxy\GhostObjectInterface; use function get_class; class ReferencesTest extends BaseTest @@ -73,7 +73,8 @@ public function testLazyLoadReference() $profile = $user->getProfile(); - $this->assertInstanceOf(\Proxies\__CG__\Documents\Profile::class, $profile); + $this->assertInstanceOf(Profile::class, $profile); + $this->assertInstanceOf(GhostObjectInterface::class, $profile); $profile->getFirstName(); @@ -94,8 +95,8 @@ public function testLazyLoadedWithNotifyPropertyChanged() $this->dm->clear(); $user = $this->dm->find(get_class($user), $user->getId()); - $this->assertInstanceOf(Proxy::class, $user->getProfileNotify()); - $this->assertFalse($user->getProfileNotify()->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $user->getProfileNotify()); + $this->assertFalse($user->getProfileNotify()->isProxyInitialized()); $user->getProfileNotify()->setLastName('Malarz'); $this->dm->flush(); @@ -352,7 +353,7 @@ public function testSortReferenceManyOwningSide() /** * @expectedException \Doctrine\ODM\MongoDB\DocumentNotFoundException - * @expectedExceptionMessage The "Proxies\__CG__\Doctrine\ODM\MongoDB\Tests\Functional\DocumentWithArrayId" document with identifier {"identifier":2} could not be found. + * @expectedExceptionMessage The "Doctrine\ODM\MongoDB\Tests\Functional\DocumentWithArrayId" document with identifier {"identifier":2} could not be found. */ public function testDocumentNotFoundExceptionWithArrayId() { @@ -377,12 +378,12 @@ public function testDocumentNotFoundExceptionWithArrayId() ); $test = $this->dm->find(get_class($test), $test->id); - $test->referenceOne->__load(); + $test->referenceOne->initializeProxy(); } /** * @expectedException \Doctrine\ODM\MongoDB\DocumentNotFoundException - * @expectedExceptionMessage The "Proxies\__CG__\Documents\Profile" document with identifier "abcdefabcdefabcdefabcdef" could not be found. + * @expectedExceptionMessage The "Documents\Profile" document with identifier "abcdefabcdefabcdefabcdef" could not be found. */ public function testDocumentNotFoundExceptionWithObjectId() { @@ -408,12 +409,12 @@ public function testDocumentNotFoundExceptionWithObjectId() $user = $this->dm->find(get_class($user), $user->getId()); $profile = $user->getProfile(); - $profile->__load(); + $profile->initializeProxy(); } /** * @expectedException \Doctrine\ODM\MongoDB\DocumentNotFoundException - * @expectedExceptionMessage The "Proxies\__CG__\Doctrine\ODM\MongoDB\Tests\Functional\DocumentWithMongoBinDataId" document with identifier "testbindata" could not be found. + * @expectedExceptionMessage The "Doctrine\ODM\MongoDB\Tests\Functional\DocumentWithMongoBinDataId" document with identifier "testbindata" could not be found. */ public function testDocumentNotFoundExceptionWithMongoBinDataId() { @@ -438,7 +439,7 @@ public function testDocumentNotFoundExceptionWithMongoBinDataId() ); $test = $this->dm->find(get_class($test), $test->id); - $test->referenceOne->__load(); + $test->referenceOne->initializeProxy(); } public function testDocumentNotFoundEvent() @@ -474,7 +475,7 @@ public function testDocumentNotFoundEvent() $this->dm->getEventManager()->addEventListener(Events::documentNotFound, new DocumentNotFoundListener($closure)); - $profile->__load(); + $profile->initializeProxy(); } } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/SimpleReferencesTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/SimpleReferencesTest.php index d150f97049..18151ab3ab 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/SimpleReferencesTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/SimpleReferencesTest.php @@ -8,6 +8,7 @@ use Documents\SimpleReferenceUser; use Documents\User; use MongoDB\BSON\ObjectId; +use ProxyManager\Proxy\GhostObjectInterface; use stdClass; use function current; use function end; @@ -79,10 +80,11 @@ public function testProxy() $this->assertNotNull($test); $this->assertNotNull($test->getUser()); - $this->assertInstanceOf('Proxies\__CG__\Documents\User', $test->getUser()); - $this->assertFalse($test->getUser()->__isInitialized__); + $this->assertInstanceOf(User::class, $test->getUser()); + $this->assertInstanceOf(GhostObjectInterface::class, $test->getUser()); + $this->assertFalse($test->getUser()->isProxyInitialized()); $this->assertEquals('jwage', $test->getUser()->getUsername()); - $this->assertTrue($test->getUser()->__isInitialized__); + $this->assertTrue($test->getUser()->isProxyInitialized()); } public function testPersistentCollectionOwningSide() diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH520Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH520Test.php index ce6621b5c4..596a5a6781 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH520Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH520Test.php @@ -7,7 +7,7 @@ use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; -use Doctrine\ODM\MongoDB\Proxy\Proxy; +use ProxyManager\Proxy\GhostObjectInterface; class GH520Test extends BaseTest { @@ -27,8 +27,8 @@ public function testPrimeWithGetSingleResult() $document = $query->getSingleResult(); - $this->assertInstanceOf(Proxy::class, $document->ref); - $this->assertTrue($document->ref->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $document->ref); + $this->assertTrue($document->ref->isProxyInitialized()); } public function testPrimeWithGetSingleResultWillNotPrimeEntireResultSet() diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH593Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH593Test.php index b6769b56fc..53f69429b6 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH593Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH593Test.php @@ -7,8 +7,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ODM\MongoDB\DocumentNotFoundException; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Tests\BaseTest; +use ProxyManager\Proxy\GhostObjectInterface; use function iterator_to_array; class GH593Test extends BaseTest @@ -56,16 +56,16 @@ public function testReferenceManyOwningSidePreparesFilterCriteria() */ $this->assertCount(2, $user1following); - $this->assertInstanceOf(Proxy::class, $user1following[0]); - $this->assertTrue($user1following[0]->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $user1following[0]); + $this->assertTrue($user1following[0]->isProxyInitialized()); $this->assertEquals($user2->getId(), $user1following[0]->getId()); - $this->assertInstanceOf(Proxy::class, $user1following[1]); - $this->assertFalse($user1following[1]->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $user1following[1]); + $this->assertFalse($user1following[1]->isProxyInitialized()); $this->assertEquals($user3->getId(), $user1following[1]->getId()); $this->expectException(DocumentNotFoundException::class); - $user1following[1]->__load(); + $user1following[1]->initializeProxy(); } public function testReferenceManyInverseSidePreparesFilterCriteria() diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH602Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH602Test.php index 043c421d3b..cd3beb9ac7 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH602Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH602Test.php @@ -7,8 +7,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ODM\MongoDB\DocumentNotFoundException; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Tests\BaseTest; +use ProxyManager\Proxy\GhostObjectInterface; use function iterator_to_array; class GH602Test extends BaseTest @@ -47,16 +47,16 @@ public function testReferenceManyOwningSidePreparesFilterCriteriaForDifferentCla */ $this->assertCount(2, $user1likes); - $this->assertInstanceOf(Proxy::class, $user1likes[0]); - $this->assertTrue($user1likes[0]->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $user1likes[0]); + $this->assertTrue($user1likes[0]->isProxyInitialized()); $this->assertEquals($thing1->getId(), $user1likes[0]->getId()); - $this->assertInstanceOf(Proxy::class, $user1likes[1]); - $this->assertFalse($user1likes[1]->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $user1likes[1]); + $this->assertFalse($user1likes[1]->isProxyInitialized()); $this->assertEquals($thing2->getId(), $user1likes[1]->getId()); $this->expectException(DocumentNotFoundException::class); - $user1likes[1]->__load(); + $user1likes[1]->initializeProxy(); } public function testReferenceManyInverseSidePreparesFilterCriteriaForDifferentClass() diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php index 78cb0bb518..ee7d2d3db9 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH852Test.php @@ -8,8 +8,8 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Criteria; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use MongoDB\BSON\Binary; +use ProxyManager\Proxy\GhostObjectInterface; use function get_class; class GH852Test extends BaseTest @@ -48,24 +48,24 @@ public function testA(Closure $idGenerator) $this->assertEquals($idGenerator('parent'), $parent->id); $this->assertEquals('parent', $parent->name); - $this->assertInstanceOf(Proxy::class, $parent->refOne); - $this->assertFalse($parent->refOne->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $parent->refOne); + $this->assertFalse($parent->refOne->isProxyInitialized()); $this->assertEquals($idGenerator('childA'), $parent->refOne->id); $this->assertEquals('childA', $parent->refOne->name); - $this->assertTrue($parent->refOne->__isInitialized()); + $this->assertTrue($parent->refOne->isProxyInitialized()); $this->assertCount(2, $parent->refMany); /* These proxies will be initialized when we first access the collection * by DocumentPersister::loadReferenceManyCollectionOwningSide(). */ - $this->assertInstanceOf(Proxy::class, $parent->refMany[0]); - $this->assertTrue($parent->refMany[0]->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $parent->refMany[0]); + $this->assertTrue($parent->refMany[0]->isProxyInitialized()); $this->assertEquals($idGenerator('childB'), $parent->refMany[0]->id); $this->assertEquals('childB', $parent->refMany[0]->name); - $this->assertInstanceOf(Proxy::class, $parent->refMany[1]); - $this->assertTrue($parent->refMany[1]->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $parent->refMany[1]); + $this->assertTrue($parent->refMany[1]->isProxyInitialized()); $this->assertEquals($idGenerator('childC'), $parent->refMany[1]->id); $this->assertEquals('childC', $parent->refMany[1]->name); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH936Test.php b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH936Test.php index d6106c93e0..a77f07bd04 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH936Test.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH936Test.php @@ -7,8 +7,8 @@ use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; use Doctrine\ODM\MongoDB\Events; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Tests\BaseTest; +use ProxyManager\Proxy\GhostObjectInterface; class GH936Test extends BaseTest { @@ -27,7 +27,7 @@ public function testRemoveCascadesThroughProxyDocuments() $foo = $this->dm->find(GH936Document::class, $foo->id); - $this->assertInstanceOf(Proxy::class, $foo->ref); + $this->assertInstanceOf(GhostObjectInterface::class, $foo->ref); $this->dm->remove($foo); $this->dm->flush(); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/HydratorTest.php b/tests/Doctrine/ODM/MongoDB/Tests/HydratorTest.php index 1b0c759687..462840f54d 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/HydratorTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/HydratorTest.php @@ -7,8 +7,8 @@ use DateTime; use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use Doctrine\ODM\MongoDB\PersistentCollection; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Query\Query; +use ProxyManager\Proxy\GhostObjectInterface; class HydratorTest extends BaseTest { @@ -39,8 +39,8 @@ public function testHydrator() $this->assertInstanceOf(DateTime::class, $user->birthdate); $this->assertInstanceOf(HydrationClosureReferenceOne::class, $user->referenceOne); $this->assertInstanceOf(PersistentCollection::class, $user->referenceMany); - $this->assertInstanceOf(Proxy::class, $user->referenceMany[0]); - $this->assertInstanceOf(Proxy::class, $user->referenceMany[1]); + $this->assertInstanceOf(GhostObjectInterface::class, $user->referenceMany[0]); + $this->assertInstanceOf(GhostObjectInterface::class, $user->referenceMany[1]); $this->assertInstanceOf(PersistentCollection::class, $user->embedMany); $this->assertEquals('jon', $user->embedOne->name); $this->assertEquals('jon', $user->embedMany[0]->name); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataFactoryTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataFactoryTest.php index 055d4e9ac3..b74e3653c6 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataFactoryTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataFactoryTest.php @@ -8,7 +8,6 @@ use Doctrine\ODM\MongoDB\Configuration; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory; -use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver; use Doctrine\ODM\MongoDB\Tests\BaseTest; use Doctrine\ODM\MongoDB\Tests\Mocks\DocumentManagerMock; use InvalidArgumentException; @@ -32,6 +31,7 @@ public function testGetMetadataForSingleClass() // SUT $cmf = new ClassMetadataFactoryTestSubject(); $cmf->setMetadataFor(TestDocument1::class, $cm1); + $cmf->setDocumentManager($this->dm); // Prechecks $this->assertEquals([], $cm1->parentClasses); @@ -47,28 +47,6 @@ public function testGetMetadataForSingleClass() $this->assertTrue($cm1->hasField('name')); } - public function testHasGetMetadataNamespaceSeparatorIsNotNormalized() - { - require_once __DIR__ . '/Documents/GlobalNamespaceDocument.php'; - - $driver = AnnotationDriver::create(__DIR__ . '/Documents'); - - $dm = $this->getMockDocumentManager($driver); - - $cmf = new ClassMetadataFactory(); - $cmf->setConfiguration($dm->getConfiguration()); - $cmf->setDocumentManager($dm); - - $m1 = $cmf->getMetadataFor('DoctrineGlobal_Article'); - $h1 = $cmf->hasMetadataFor('DoctrineGlobal_Article'); - $h2 = $cmf->hasMetadataFor('\DoctrineGlobal_Article'); - $m2 = $cmf->getMetadataFor('\DoctrineGlobal_Article'); - - $this->assertNotSame($m1, $m2); - $this->assertFalse($h2); - $this->assertTrue($h1); - } - protected function getMockDocumentManager($driver) { $config = new Configuration(); diff --git a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php index c5056183d0..6dd286e09b 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/Mapping/ClassMetadataTest.php @@ -8,7 +8,6 @@ use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Mapping\MappingException; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Repository\DocumentRepository; use Doctrine\ODM\MongoDB\Tests\BaseTest; use Doctrine\ODM\MongoDB\Utility\CollectionHelper; @@ -20,6 +19,7 @@ use Documents\SpecialUser; use Documents\User; use Documents\UserRepository; +use ProxyManager\Proxy\GhostObjectInterface; use ReflectionClass; use stdClass; use function array_merge; @@ -328,8 +328,8 @@ public function testGetFieldValueInitializesProxy() $metadata = $this->dm->getClassMetadata(Album::class); $this->assertEquals($document->getName(), $metadata->getFieldValue($proxy, 'name')); - $this->assertInstanceOf(Proxy::class, $proxy); - $this->assertTrue($proxy->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $proxy); + $this->assertTrue($proxy->isProxyInitialized()); } public function testGetFieldValueOfIdentifierDoesNotInitializeProxy() @@ -343,8 +343,8 @@ public function testGetFieldValueOfIdentifierDoesNotInitializeProxy() $metadata = $this->dm->getClassMetadata(Album::class); $this->assertEquals($document->getId(), $metadata->getFieldValue($proxy, 'id')); - $this->assertInstanceOf(Proxy::class, $proxy); - $this->assertFalse($proxy->__isInitialized()); + $this->assertInstanceOf(GhostObjectInterface::class, $proxy); + $this->assertFalse($proxy->isProxyInitialized()); } public function testSetFieldValue() @@ -365,7 +365,7 @@ public function testSetFieldValueWithProxy() $this->dm->clear(); $proxy = $this->dm->getReference(Album::class, $document->getId()); - $this->assertInstanceOf(Proxy::class, $proxy); + $this->assertInstanceOf(GhostObjectInterface::class, $proxy); $metadata = $this->dm->getClassMetadata(Album::class); $metadata->setFieldValue($proxy, 'name', 'nevermind'); @@ -374,7 +374,7 @@ public function testSetFieldValueWithProxy() $this->dm->clear(); $proxy = $this->dm->getReference(Album::class, $document->getId()); - $this->assertInstanceOf(Proxy::class, $proxy); + $this->assertInstanceOf(GhostObjectInterface::class, $proxy); $this->assertEquals('nevermind', $proxy->getName()); } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php b/tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php index a1b747ac14..2c82576a53 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php @@ -11,6 +11,7 @@ use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory; use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver; use Doctrine\ODM\MongoDB\Persisters\DocumentPersister; +use Doctrine\ODM\MongoDB\Proxy\ClassNameResolver; use Doctrine\ODM\MongoDB\SchemaManager; use Doctrine\ODM\MongoDB\Tests\Mocks\DocumentManagerMock; use Doctrine\ODM\MongoDB\UnitOfWork; @@ -756,10 +757,11 @@ private function getMockDocumentManager() : DocumentManagerMock $em = $this->createMock(EventManager::class); - $dm = new DocumentManagerMock(); - $dm->eventManager = $em; - $dm->config = $config; - $dm->client = $this->createMock(Client::class); + $dm = new DocumentManagerMock(); + $dm->eventManager = $em; + $dm->config = $config; + $dm->client = $this->createMock(Client::class); + $dm->classNameResolver = new ClassNameResolver($config); return $dm; } diff --git a/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php b/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php index babe704bcf..80185606f9 100644 --- a/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php +++ b/tests/Doctrine/ODM/MongoDB/Tests/UnitOfWorkTest.php @@ -16,7 +16,6 @@ use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\MongoDBException; use Doctrine\ODM\MongoDB\Persisters\PersistenceBuilder; -use Doctrine\ODM\MongoDB\Proxy\Proxy; use Doctrine\ODM\MongoDB\Tests\Mocks\DocumentPersisterMock; use Doctrine\ODM\MongoDB\Tests\Mocks\ExceptionThrowingListenerMock; use Doctrine\ODM\MongoDB\Tests\Mocks\PreUpdateListenerMock; @@ -30,6 +29,7 @@ use Documents\Functional\NotSaved; use Documents\User; use MongoDB\BSON\ObjectId; +use ProxyManager\Proxy\GhostObjectInterface; use Throwable; use function get_class; use function spl_object_hash; @@ -681,7 +681,7 @@ public function testRecomputeChangesetForUninitializedProxyDoesNotCreateChangese $user = $this->dm->find(ForumUser::class, $id); $this->assertInstanceOf(ForumUser::class, $user); - $this->assertInstanceOf(Proxy::class, $user->getAvatar()); + $this->assertInstanceOf(GhostObjectInterface::class, $user->getAvatar()); $classMetadata = $this->dm->getClassMetadata(ForumAvatar::class); diff --git a/tests/Stubs/DocumentManager.php b/tests/Stubs/DocumentManager.php index 253f00e092..937a17d374 100644 --- a/tests/Stubs/DocumentManager.php +++ b/tests/Stubs/DocumentManager.php @@ -30,7 +30,7 @@ public function setClassMetadata($className, ClassMetadata $class) $this->classMetadatas[$className] = $class; } - public function getClassMetadata($className) + public function getClassMetadata($className) : ClassMetadata { if (! isset($this->classMetadatas[$className])) { throw new InvalidArgumentException('Metadata for class ' . $className . ' doesn\'t exist, try calling ->setClassMetadata() first');