Skip to content

Commit

Permalink
On type update throw an document not found error
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Thinardon committed Dec 21, 2018
1 parent 2967d38 commit 1df7f4b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Exception/DocumentNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Biig\Component\Elasticsearch\Exception;

class DocumentNotFoundException extends \RuntimeException
{
public function __construct(\Throwable $previous = null)
{
parent::__construct(
'Document not found',
0,
$previous
);
}
}
12 changes: 11 additions & 1 deletion src/Indexation/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Biig\Component\Elasticsearch\Indexation;

use Biig\Component\Elasticsearch\Exception\DocumentNotFoundException;
use Biig\Component\Elasticsearch\Exception\NoElasticaTypeAvailable;
use Biig\Component\Elasticsearch\Indexation\Doctrine\SimplePaginator;
use Elastica\Document;
use Elastica\Exception\ResponseException;
use Elastica\Type;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
Expand Down Expand Up @@ -54,7 +56,15 @@ public function update($object, $id)
throw new NoElasticaTypeAvailable();
}

$this->type->updateDocument(new Document($id, $object));
try {
$this->type->updateDocument(new Document($id, $object));
} catch (ResponseException $exception) {
if ('document_missing_exception' === $exception->getResponse()->getFullError()['type']) {
throw new DocumentNotFoundException($exception);
}

throw $exception;
}
}

public function stageForInsert(array $object, $id = null)
Expand Down
65 changes: 65 additions & 0 deletions tests/Indexation/AbstractTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Biig\Component\Elasticsearch\Test\Indexation;

use Biig\Component\Elasticsearch\Indexation\AbstractIndex;
use Biig\Component\Elasticsearch\Indexation\AbstractType;
use Biig\Component\Elasticsearch\Indexation\Doctrine\SimplePaginator;
use Biig\Component\Elasticsearch\Indexation\Hydrator\Hydrator;
use Biig\Component\Elasticsearch\Indexation\Hydrator\HydratorFactory;
use Biig\Component\Elasticsearch\Indexation\IndexInterface;
use Biig\Component\Elasticsearch\Mapping\IndexBuilder;
use Elastica\Client;
use Elastica\Exception\ElasticsearchException;
use Elastica\Exception\ResponseException;
use Elastica\Index;
use Elastica\Response;
use Elastica\Type;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Log\LoggerInterface;

class AbstractTypeTest extends TestCase
{
/**
* @var LoggerInterface
*/
private $logger;

public function setUp()
{
$this->logger = $this->prophesize(LoggerInterface::class);
}

/**
* @expectedException \Biig\Component\Elasticsearch\Exception\DocumentNotFoundException
*/
public function testItAcceptTypes()
{
$type = $this->prophesize(Type::class);
$response = $this->prophesize(Response::class);
$exception = $this->prophesize(ResponseException::class);
$exception->getResponse()->willReturn($response->reveal());
$response->getFullError()->willReturn(['type' => 'document_missing_exception']);

$fooType = new FooType($this->logger->reveal());
$fooType->setType($type->reveal());

$type->updateDocument(Argument::any())->willThrow($exception->reveal());

$fooType->update(new \stdClass(), 'foo');
}

}

class FooType extends AbstractType
{
public function getPaginator(): SimplePaginator
{
}

public function getName(): string
{
return 'bar';
}
}

0 comments on commit 1df7f4b

Please sign in to comment.