From caa0cce6159011f0b18d9a86333788343a7e4915 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Fri, 30 Nov 2018 06:53:47 +0100 Subject: [PATCH] Better error message when missing document mapping ODM port of https://github.com/doctrine/DoctrineBundle/pull/825. --- Repository/ServiceDocumentRepository.php | 8 ++++ .../Document/TestUnmappedDocument.php | 7 +++ .../TestUnmappedDocumentRepository.php | 15 ++++++ Tests/ServiceRepositoryTest.php | 48 ++++++++++++------- 4 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 Tests/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Document/TestUnmappedDocument.php create mode 100644 Tests/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Repository/TestUnmappedDocumentRepository.php diff --git a/Repository/ServiceDocumentRepository.php b/Repository/ServiceDocumentRepository.php index 321fc600..0b692a99 100644 --- a/Repository/ServiceDocumentRepository.php +++ b/Repository/ServiceDocumentRepository.php @@ -5,6 +5,7 @@ use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\DocumentRepository; +use LogicException; /** * Optional DocumentRepository base class with a simplified constructor (for autowiring). @@ -30,6 +31,13 @@ public function __construct(ManagerRegistry $registry, $documentClass) /** @var DocumentManager $manager */ $manager = $registry->getManagerForClass($documentClass); + if ($manager === null) { + throw new LogicException(sprintf( + 'Could not find the document manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this document’s metadata.', + $documentClass + )); + } + parent::__construct($manager, $manager->getUnitOfWork(), $manager->getClassMetadata($documentClass)); } } diff --git a/Tests/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Document/TestUnmappedDocument.php b/Tests/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Document/TestUnmappedDocument.php new file mode 100644 index 00000000..5396c77f --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/Bundles/RepositoryServiceBundle/Document/TestUnmappedDocument.php @@ -0,0 +1,7 @@ +markTestSkipped('Doctrine MongoDB ODM is not available.'); - } - public function testRepositoryServiceWiring() - { - $container = new ContainerBuilder(new ParameterBag([ + $this->container = new ContainerBuilder(new ParameterBag([ 'kernel.name' => 'app', 'kernel.debug' => false, 'kernel.bundles' => ['RepositoryServiceBundle' => RepositoryServiceBundle::class], @@ -43,9 +45,9 @@ public function testRepositoryServiceWiring() 'kernel.environment' => 'test', 'kernel.root_dir' => __DIR__ . '/../../../../', // src dir ])); - $container->setDefinition('annotation_reader', new Definition(AnnotationReader::class)); + $this->container->setDefinition('annotation_reader', new Definition(AnnotationReader::class)); $extension = new DoctrineMongoDBExtension(); - $container->registerExtension($extension); + $this->container->registerExtension($extension); $extension->load([[ 'connections' => ['default' => []], @@ -58,40 +60,52 @@ public function testRepositoryServiceWiring() ], ], ]], - ]], $container); + ]], $this->container); - $def = $container->register(TestCustomServiceRepoRepository::class, TestCustomServiceRepoRepository::class) + $def = $this->container->register(TestCustomServiceRepoRepository::class, TestCustomServiceRepoRepository::class) ->setPublic(false); // create a public alias so we can use it below for testing - $container->setAlias('test_alias__' . TestCustomServiceRepoRepository::class, new Alias(TestCustomServiceRepoRepository::class, true)); + $this->container->setAlias('test_alias__' . TestCustomServiceRepoRepository::class, new Alias(TestCustomServiceRepoRepository::class, true)); $def->setAutowired(true); $def->setAutoconfigured(true); - $container->addCompilerPass(new ServiceRepositoryCompilerPass()); - $container->compile(); + $this->container->addCompilerPass(new ServiceRepositoryCompilerPass()); + $this->container->compile(); + } - $em = $container->get('doctrine_mongodb.odm.document_manager'); + public function testRepositoryServiceWiring() + { + $dm = $this->container->get('doctrine_mongodb.odm.document_manager'); // traditional custom class repository - $customClassRepo = $em->getRepository(TestCustomClassRepoDocument::class); + $customClassRepo = $dm->getRepository(TestCustomClassRepoDocument::class); $this->assertInstanceOf(TestCustomClassRepoRepository::class, $customClassRepo); // a smoke test, trying some methods $this->assertSame(TestCustomClassRepoDocument::class, $customClassRepo->getClassName()); $this->assertInstanceOf(Builder::class, $customClassRepo->createQueryBuilder()); // generic DocumentRepository - $genericRepository = $em->getRepository(TestDefaultRepoDocument::class); + $genericRepository = $dm->getRepository(TestDefaultRepoDocument::class); $this->assertInstanceOf(DocumentRepository::class, $genericRepository); - $this->assertSame($genericRepository, $genericRepository = $em->getRepository(TestDefaultRepoDocument::class)); + $this->assertSame($genericRepository, $genericRepository = $dm->getRepository(TestDefaultRepoDocument::class)); // a smoke test, trying one of the methods $this->assertSame(TestDefaultRepoDocument::class, $genericRepository->getClassName()); // custom service repository - $customServiceRepo = $em->getRepository(TestCustomServiceRepoDocument::class); - $this->assertSame($customServiceRepo, $container->get('test_alias__' . TestCustomServiceRepoRepository::class)); + $customServiceRepo = $dm->getRepository(TestCustomServiceRepoDocument::class); + $this->assertSame($customServiceRepo, $this->container->get('test_alias__' . TestCustomServiceRepoRepository::class)); // a smoke test, trying some methods $this->assertSame(TestCustomServiceRepoDocument::class, $customServiceRepo->getClassName()); $this->assertInstanceOf(Builder::class, $customServiceRepo->createQueryBuilder()); } + + /** + * @expectedException LogicException + * @expectedExceptionMessage Could not find the document manager for class "Fixtures\Bundles\RepositoryServiceBundle\Document\TestUnmappedDocument". Check your Doctrine configuration to make sure it is configured to load this document’s metadata. + */ + public function testInstantiatingServiceRepositoryForUnmappedClass() + { + new TestUnmappedDocumentRepository($this->container->get('doctrine_mongodb')); + } }