Skip to content

Commit

Permalink
Better error message when missing document mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Nov 30, 2018
1 parent 13d6b1d commit e548a56
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 23 deletions.
8 changes: 8 additions & 0 deletions Repository/ServiceDocumentRepository.php
Expand Up @@ -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).
Expand All @@ -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));
}
}
@@ -0,0 +1,7 @@
<?php

namespace Fixtures\Bundles\RepositoryServiceBundle\Document;

class TestUnmappedDocument
{
}
@@ -0,0 +1,15 @@
<?php

namespace Fixtures\Bundles\RepositoryServiceBundle\Repository;

use Doctrine\Bundle\MongoDBBundle\ManagerRegistry;
use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepository;
use Fixtures\Bundles\RepositoryServiceBundle\Document\TestUnmappedDocument;

class TestUnmappedDocumentRepository extends ServiceDocumentRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, TestUnmappedDocument::class);
}
}
54 changes: 31 additions & 23 deletions Tests/ServiceRepositoryTest.php
Expand Up @@ -14,38 +14,34 @@
use Fixtures\Bundles\RepositoryServiceBundle\Document\TestDefaultRepoDocument;
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomClassRepoRepository;
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestCustomServiceRepoRepository;
use Fixtures\Bundles\RepositoryServiceBundle\Repository\TestUnmappedDocumentRepository;
use Fixtures\Bundles\RepositoryServiceBundle\RepositoryServiceBundle;
use LogicException;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

class ServiceRepositoryTest extends TestCase
{
/** @var ContainerBuilder */
private $container;

protected function setUp()
{
parent::setUp();

if (class_exists(DocumentManager::class)) {
return;
}

$this->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],
'kernel.cache_dir' => sys_get_temp_dir(),
'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' => []],
Expand All @@ -58,40 +54,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'));
}
}

0 comments on commit e548a56

Please sign in to comment.