From acc8b61cd151382ddf23967abc93b68d3f5816ac Mon Sep 17 00:00:00 2001 From: Lars Strojny Date: Mon, 26 Nov 2012 01:03:46 +0100 Subject: [PATCH] Adding EntityManagerDecorator base class as an extension point for EntityManager --- .../ORM/Decorator/EntityManagerDecorator.php | 271 ++++++++++++++++++ lib/Doctrine/ORM/EntityManager.php | 4 +- lib/Doctrine/ORM/EntityManagerInterface.php | 60 ++++ .../Decorator/EntityManagerDecoratorTest.php | 66 +++++ 4 files changed, 399 insertions(+), 2 deletions(-) create mode 100644 lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php create mode 100644 lib/Doctrine/ORM/EntityManagerInterface.php create mode 100644 tests/Doctrine/Tests/ORM/Decorator/EntityManagerDecoratorTest.php diff --git a/lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php b/lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php new file mode 100644 index 000000000..dc123118f --- /dev/null +++ b/lib/Doctrine/ORM/Decorator/EntityManagerDecorator.php @@ -0,0 +1,271 @@ +. + */ + +namespace Doctrine\ORM\Decorator; + +use Doctrine\DBAL\LockMode; +use Doctrine\ORM\Query\ResultSetMapping; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\Common\Persistence\ObjectManagerDecorator; + +/** + * Base class for EntityManager decorators + * + * @since 2.4 + * @author Lars Strojny wrapped = $wrapped; + } + + /** + * {@inheritdoc} + */ + public function getConnection() + { + return $this->wrapped->getConnection(); + } + + /** + * {@inheritdoc} + */ + public function getExpressionBuilder() + { + return $this->wrapped->getExpressionBuilder(); + } + + /** + * {@inheritdoc} + */ + public function beginTransaction() + { + return $this->wrapped->beginTransaction(); + } + + /** + * {@inheritdoc} + */ + public function transactional($func) + { + return $this->wrapped->transactional($func); + } + + /** + * {@inheritdoc} + */ + public function commit() + { + return $this->wrapped->commit(); + } + + /** + * {@inheritdoc} + */ + public function rollback() + { + return $this->wrapped->rollback(); + } + + /** + * {@inheritdoc} + */ + public function createQuery($dql = '') + { + return $this->wrapped->createQuery($dql); + } + + /** + * {@inheritdoc} + */ + public function createNamedQuery($name) + { + return $this->wrapped->createNamedQuery($name); + } + + /** + * {@inheritdoc} + */ + public function createNativeQuery($sql, ResultSetMapping $rsm) + { + return $this->wrapped->createNativeQuery($sql, $rsm); + } + + /** + * {@inheritdoc} + */ + public function createNamedNativeQuery($name) + { + return $this->wrapped->createNamedNativeQuery($name); + } + + /** + * {@inheritdoc} + */ + public function createQueryBuilder() + { + return $this->wrapped->createQueryBuilder(); + } + + /** + * {@inheritdoc} + */ + public function getReference($entityName, $id) + { + return $this->wrapped->getReference($entityName, $id); + } + + /** + * {@inheritdoc} + */ + public function getPartialReference($entityName, $identifier) + { + return $this->wrapped->getPartialReference($entityName, $identifier); + } + + /** + * {@inheritdoc} + */ + public function close() + { + return $this->wrapped->close(); + } + + /** + * {@inheritdoc} + */ + public function copy($entity, $deep = false) + { + return $this->wrapped->copy($entity, $deep); + } + + /** + * {@inheritdoc} + */ + public function lock($entity, $lockMode, $lockVersion = null) + { + return $this->wrapped->lock($entity, $lockMode, $lockVersion); + } + + /** + * {@inheritdoc} + */ + public function find($entityName, $id, $lockMode = LockMode::NONE, $lockVersion = null) + { + return $this->wrapped->find($entityName, $id, $lockMode, $lockVersion); + } + + /** + * {@inheritdoc} + */ + public function flush($entity = null) + { + return $this->wrapped->flush($entity); + } + + /** + * {@inheritdoc} + */ + public function getEventManager() + { + return $this->wrapped->getEventManager(); + } + + /** + * {@inheritdoc} + */ + public function getConfiguration() + { + return $this->wrapped->getConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function isOpen() + { + return $this->wrapped->isOpen(); + } + + /** + * {@inheritdoc} + */ + public function getUnitOfWork() + { + return $this->wrapped->getUnitOfWork(); + } + + /** + * {@inheritdoc} + */ + public function getHydrator($hydrationMode) + { + return $this->wrapped->getHydrator($hydrationMode); + } + + /** + * {@inheritdoc} + */ + public function newHydrator($hydrationMode) + { + return $this->wrapped->newHydrator($hydrationMode); + } + + /** + * {@inheritdoc} + */ + public function getProxyFactory() + { + return $this->wrapped->getProxyFactory(); + } + + /** + * {@inheritdoc} + */ + public function getFilters() + { + return $this->wrapped->getFilters(); + } + + /** + * {@inheritdoc} + */ + public function isFiltersStateClean() + { + return $this->wrapped->isFiltersStateClean(); + } + + /** + * {@inheritdoc} + */ + public function hasFilters() + { + return $this->wrapped->hasFilters(); + } +} diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 4da4abed4..53cbf1926 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -40,7 +40,7 @@ use Doctrine\Common\Util\ClassUtils; * @author Jonathan Wage * @author Roman Borschel */ -class EntityManager implements ObjectManager +/* final */class EntityManager implements EntityManagerInterface { /** * The used Configuration. @@ -286,7 +286,7 @@ class EntityManager implements ObjectManager * * @return \Doctrine\ORM\Query */ - public function createQuery($dql = "") + public function createQuery($dql = '') { $query = new Query($this); diff --git a/lib/Doctrine/ORM/EntityManagerInterface.php b/lib/Doctrine/ORM/EntityManagerInterface.php new file mode 100644 index 000000000..d72f7cd0c --- /dev/null +++ b/lib/Doctrine/ORM/EntityManagerInterface.php @@ -0,0 +1,60 @@ +. + */ + +namespace Doctrine\ORM; + +use Doctrine\Common\Persistence\ObjectManager; +use Doctrine\DBAL\LockMode; +use Doctrine\ORM\Query\ResultSetMapping; + +/** + * EntityManager interface + * + * @since 2.4 + * @author Lars Strojny wrapped = $this->getMock('Doctrine\ORM\EntityManagerInterface'); + $this->decorator = $this->getMockBuilder('Doctrine\ORM\Decorator\EntityManagerDecorator') + ->setConstructorArgs(array($this->wrapped)) + ->setMethods(null) + ->getMock(); + } + + public function getMethodParameters() + { + $class = new \ReflectionClass('Doctrine\ORM\EntityManager'); + + $methods = array(); + foreach ($class->getMethods() as $method) { + if ($method->isConstructor() || $method->isStatic() || !$method->isPublic()) { + continue; + } + + /** Special case EntityManager::createNativeQuery() */ + if ($method->getName() === 'createNativeQuery') { + $methods[] = array($method->getName(), array('name', new ResultSetMapping())); + continue; + } + + if ($method->getNumberOfRequiredParameters() === 0) { + $methods[] = array($method->getName(), array()); + } elseif ($method->getNumberOfRequiredParameters() > 0) { + $methods[] = array($method->getName(), array_fill(0, $method->getNumberOfRequiredParameters(), 'req') ?: array()); + } + if ($method->getNumberOfParameters() != $method->getNumberOfRequiredParameters()) { + $methods[] = array($method->getName(), array_fill(0, $method->getNumberOfParameters(), 'all') ?: array()); + } + } + + return $methods; + } + + /** + * @dataProvider getMethodParameters + */ + public function testAllMethodCallsAreDelegatedToTheWrappedInstance($method, array $parameters) + { + $stub = $this->wrapped + ->expects($this->once()) + ->method($method) + ->will($this->returnValue('INNER VALUE FROM ' . $method)); + + call_user_func_array(array($stub, 'with'), $parameters); + + $this->assertSame('INNER VALUE FROM ' . $method, call_user_func_array(array($this->decorator, $method), $parameters)); + } +}