From 43ccd9ead6b1558cd2fa3976b63ca43f65a3ebc1 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Thu, 8 Sep 2011 15:54:49 -0300 Subject: [PATCH] tests for DDC-753 --- lib/Doctrine/ORM/Configuration.php | 3 +- lib/Doctrine/ORM/EntityManager.php | 3 +- .../ORM/Mapping/ClassMetadataInfo.php | 5 -- .../Models/DDC753/DDC753CustomRepository.php | 36 ++++++++ .../Models/DDC753/DDC753DefaultRepository.php | 82 +++++++++++++++++++ .../DDC753EntityWithCustomRepository.php | 39 +++++++++ ...DC753EntityWithDefaultCustomRepository.php | 39 +++++++++ .../DDC753EntityWithInvalidRepository.php | 39 +++++++++ .../Models/DDC753/DDC753InvalidRepository.php | 28 +++++++ .../ORM/Functional/EntityRepositoryTest.php | 53 ++++++++++++ 10 files changed, 320 insertions(+), 7 deletions(-) create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753CustomRepository.php create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithCustomRepository.php create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithDefaultCustomRepository.php create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithInvalidRepository.php create mode 100644 tests/Doctrine/Tests/Models/DDC753/DDC753InvalidRepository.php diff --git a/lib/Doctrine/ORM/Configuration.php b/lib/Doctrine/ORM/Configuration.php index 99ee397cc..42ceded0d 100644 --- a/lib/Doctrine/ORM/Configuration.php +++ b/lib/Doctrine/ORM/Configuration.php @@ -526,7 +526,8 @@ class Configuration extends \Doctrine\DBAL\Configuration */ public function setDefaultRepositoryClassName($className) { - if (!is_subclass_of($className, 'Doctrine\Common\Persistence\ObjectRepository')) { + $class = new \ReflectionClass($className); + if (!$class->implementsInterface('Doctrine\Common\Persistence\ObjectRepository')) { throw ORMException::invalidObjectRepository($className); } $this->_attributes['defaultRepositoryClassName'] = $className; diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 0379cc435..9be5b50da 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -576,7 +576,8 @@ class EntityManager implements ObjectManager if ($customRepositoryClassName !== null) { $repository = new $customRepositoryClassName($this, $metadata); } else { - $repository = new EntityRepository($this, $metadata); + $repositoryClass = $this->config->getDefaultRepositoryClassName(); + $repository = new $repositoryClass($this, $metadata); } $this->repositories[$entityName] = $repository; diff --git a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php index e34db3f2d..17255cb23 100644 --- a/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php +++ b/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php @@ -1519,17 +1519,12 @@ class ClassMetadataInfo implements ClassMetadata * Registers a custom repository class for the entity class. * * @param string $mapperClassName The class name of the custom mapper. - * @throws MappingException If not implements Doctrine\Common\Persistence\ObjectRepository */ public function setCustomRepositoryClass($repositoryClassName) { if ($repositoryClassName !== null && strpos($repositoryClassName, '\\') === false && strlen($this->namespace) > 0) { $repositoryClassName = $this->namespace . '\\' . $repositoryClassName; - - if (!is_subclass_of($repositoryClassName, 'Doctrine\Common\Persistence\ObjectRepository')) { - throw MappingException::invalidObjectRepository($repositoryClassName); - } } $this->customRepositoryClassName = $repositoryClassName; } diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753CustomRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753CustomRepository.php new file mode 100644 index 000000000..d277cf8aa --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753CustomRepository.php @@ -0,0 +1,36 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +use Doctrine\ORM\EntityRepository; + +class DDC753CustomRepository extends EntityRepository +{ + + /** + * @return bool + */ + public function isCustomRepository() + { + return true; + } + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php new file mode 100644 index 000000000..85a981c09 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753DefaultRepository.php @@ -0,0 +1,82 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +use Doctrine\ORM\EntityRepository; + +class DDC753DefaultRepository implements \Doctrine\Common\Persistence\ObjectRepository +{ + + /** + * @var Doctrine\ORM\EntityRepository + */ + private $repository; + + /** + * @param EntityManager $em + * @param ClassMetadata $classMetadata + */ + public function __construct($em, $class) + { + $this->repository = new EntityRepository($em, $class); + } + + /** + * {@inheritdoc} + */ + public function find($id) + { + return $this->repository->find($id); + } + + /** + * {@inheritdoc} + */ + public function findAll() + { + return $this->repository->findAll; + } + + /** + * {@inheritdoc} + */ + public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) + { + return $this->repository->findBy($criteria, $orderBy, $limit, $offset); + } + + /** + * {@inheritdoc} + */ + public function findOneBy(array $criteria) + { + return $this->repository->findOneBy($criteria); + } + + /** + * @return bool + */ + public function isDefaultRepository() + { + return true; + } + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithCustomRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithCustomRepository.php new file mode 100644 index 000000000..fa8a77957 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithCustomRepository.php @@ -0,0 +1,39 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +/** + * @Entity(repositoryClass = "Doctrine\Tests\Models\DDC753\DDC753CustomRepository") + */ +class DDC753EntityWithCustomRepository +{ + + /** + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + protected $id; + + /** @column(type="string") */ + protected $name; + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithDefaultCustomRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithDefaultCustomRepository.php new file mode 100644 index 000000000..43f435a98 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithDefaultCustomRepository.php @@ -0,0 +1,39 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +/** + * @Entity() + */ +class DDC753EntityWithDefaultCustomRepository +{ + + /** + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + protected $id; + + /** @column(type="string") */ + protected $name; + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithInvalidRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithInvalidRepository.php new file mode 100644 index 000000000..e7c21762a --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753EntityWithInvalidRepository.php @@ -0,0 +1,39 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +/** + * @Entity(repositoryClass = "\stdClass") + */ +class DDC753EntityWithInvalidRepository +{ + + /** + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + protected $id; + + /** @column(type="string") */ + protected $name; + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/DDC753/DDC753InvalidRepository.php b/tests/Doctrine/Tests/Models/DDC753/DDC753InvalidRepository.php new file mode 100644 index 000000000..9f0937aa1 --- /dev/null +++ b/tests/Doctrine/Tests/Models/DDC753/DDC753InvalidRepository.php @@ -0,0 +1,28 @@ +. + */ + +namespace Doctrine\Tests\Models\DDC753; + +use Doctrine\ORM\EntityRepository; + +class DDC753InvalidRepository +{ + +} \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php index 16d3b7bb4..40133c3cf 100644 --- a/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/EntityRepositoryTest.php @@ -433,5 +433,58 @@ class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertSame($usersAsc[0], $usersDesc[1]); $this->assertSame($usersAsc[1], $usersDesc[0]); } + + + /** + * @group DDC-753 + */ + public function testDefaultRepositoryClassName() + { + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); + $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); + + $repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithDefaultCustomRepository'); + $this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository", $repos); + $this->assertTrue($repos->isDefaultRepository()); + + + $repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithCustomRepository'); + $this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753CustomRepository", $repos); + $this->assertTrue($repos->isCustomRepository()); + + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository"); + $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\ORM\EntityRepository"); + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); + + } + + + /** + * @group DDC-753 + * @expectedException Doctrine\ORM\ORMException + * @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. it must implement Doctrine\Common\Persistence\ObjectRepository. + */ + public function testSetDefaultRepositoryInvalidClassError() + { + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); + $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository"); + } + + + /** + * @group DDC-753 + * @todo check it is necessary throws exception when a repository is not a Doctrine\Common\Persistence\ObjectRepository + * ClassMetadataInfo#setCustomRepositoryClass + */ + public function testEntityWithInvalidRepositoryError() + { + $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository"); + $repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithInvalidRepository'); + + $this->assertInstanceOf("\stdClass", $repos); + + $this->markTestIncomplete(); + } }