Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

document suffix now optional #165

Merged
merged 2 commits into from
Jan 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions DependencyInjection/Compiler/MappingPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,18 @@ public function process(ContainerBuilder $container)
[$repository],
]
);
$container->setDefinition(
sprintf('es.manager.%s.%s', $managerName, substr($repository, strpos($repository, ':') + 1)),
$repositoryDefinition
);

$repository = substr($repository, strpos($repository, ':') + 1);
$repositoryId = sprintf('es.manager.%s.%s', $managerName, $repository);

if (strtolower(substr($repository, -8)) === 'document') {
$container->setAlias(
sprintf('es.manager.%s.%s', $managerName, substr($repository, 0, strlen($repository) - 8)),
$repositoryId
);
}

$container->setDefinition($repositoryId, $repositoryDefinition);
}
}
}
Expand Down
22 changes: 16 additions & 6 deletions ORM/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getRepository($type)
{
$type = is_array($type) ? $type : [$type];

foreach ($type as $selectedType) {
foreach ($type as &$selectedType) {
$this->checkRepositoryType($selectedType);
}

Expand Down Expand Up @@ -174,13 +174,23 @@ public function getTypesMapping()
*
* @throws \InvalidArgumentException
*/
private function checkRepositoryType($type)
private function checkRepositoryType(&$type)
{
if (!array_key_exists($type, $this->getBundlesMapping())) {
$exceptionMessage = "Undefined repository {$type}, valid repositories are: " .
join(', ', array_keys($this->getBundlesMapping())) . '.';
throw new \InvalidArgumentException($exceptionMessage);
$mapping = $this->getBundlesMapping();

if (array_key_exists($type, $mapping)) {
return;
}

if (array_key_exists($type . 'Document', $mapping)) {
$type .= 'Document';

return;
}

$exceptionMessage = "Undefined repository `{$type}`, valid repositories are: `" .
join('`, `', array_keys($this->getBundlesMapping())) . '`.';
throw new \InvalidArgumentException($exceptionMessage);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public function getTestContainerData()
'es.manager.default.bar',
'ONGR\ElasticsearchBundle\ORM\Repository',
],
[
'es.manager.default.color',
'ONGR\ElasticsearchBundle\ORM\Repository',
],
[
'es.manager.default.colordocument',
'ONGR\ElasticsearchBundle\ORM\Repository',
],
[
'es.metadata_collector',
'ONGR\ElasticsearchBundle\Mapping\MetadataCollector',
],
];
}

Expand Down
14 changes: 14 additions & 0 deletions Tests/Functional/ORM/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,20 @@ public function testSuggest($suggesters, $expectedResults)
}
}

/**
* Tests if repository is fetched without suffix.
*/
public function testGetRepositoryWithDoucmentSuffix()
{
$manager = $this->getManager();
$repository = $manager->getRepository('AcmeTestBundle:Color');

$this->assertInstanceOf(
'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\ColorDocument',
$repository->createDocument()
);
}

/**
* Assert suggestion score is set.
*
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/ORM/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testGetRepositories()
* Check if an exception is thrown when an undefined repository is specified.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Undefined repository rep1, valid repositories are: rep2, rep3.
* @expectedExceptionMessage Undefined repository `rep1`, valid repositories are: `rep2`, `rep3`.
*/
public function testGetRepositoriesException()
{
Expand All @@ -83,7 +83,7 @@ public function testGetRepositoriesException()
* Check if an exception is thrown when an undefined repository is specified and only a single rep is specified.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Undefined repository rep1, valid repositories are: rep2, rep3.
* @expectedExceptionMessage Undefined repository `rep1`, valid repositories are: `rep2`, `rep3`.
*/
public function testGetRepositoriesExceptionSingle()
{
Expand Down
26 changes: 26 additions & 0 deletions Tests/app/fixture/Acme/TestBundle/Document/ColorDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document;

use ONGR\ElasticsearchBundle\Annotation as ES;
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
use ONGR\ElasticsearchBundle\Document\DocumentTrait;

/**
* Class ColorDocument.
*
* @ES\Document(type="color")
*/
class ColorDocument implements DocumentInterface
{
use DocumentTrait;
}