From 1df7f4bccc2ff34af38a0fe2b8ec02619f8b048e Mon Sep 17 00:00:00 2001 From: Alexis Thinardon Date: Fri, 21 Dec 2018 16:05:29 +0100 Subject: [PATCH] On type update throw an document not found error --- src/Exception/DocumentNotFoundException.php | 15 +++++ src/Indexation/AbstractType.php | 12 +++- tests/Indexation/AbstractTypeTest.php | 65 +++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/Exception/DocumentNotFoundException.php create mode 100644 tests/Indexation/AbstractTypeTest.php diff --git a/src/Exception/DocumentNotFoundException.php b/src/Exception/DocumentNotFoundException.php new file mode 100644 index 0000000..32f523a --- /dev/null +++ b/src/Exception/DocumentNotFoundException.php @@ -0,0 +1,15 @@ +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) diff --git a/tests/Indexation/AbstractTypeTest.php b/tests/Indexation/AbstractTypeTest.php new file mode 100644 index 0000000..5f91c11 --- /dev/null +++ b/tests/Indexation/AbstractTypeTest.php @@ -0,0 +1,65 @@ +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'; + } +}