From 797a2002886b86064031d113666ecd4dd9fe46ae Mon Sep 17 00:00:00 2001 From: Chloe Liban Date: Mon, 9 Sep 2019 16:03:51 +0200 Subject: [PATCH 1/3] fix(saveObjects-error-msg): refine the error thrown by the method --- src/Exceptions/ArrayOfObjectsExpected.php | 7 ++++ src/SearchIndex.php | 5 +++ tests/Integration/SearchIndexTest.php | 40 ++++++++++++++--------- 3 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 src/Exceptions/ArrayOfObjectsExpected.php diff --git a/src/Exceptions/ArrayOfObjectsExpected.php b/src/Exceptions/ArrayOfObjectsExpected.php new file mode 100644 index 000000000..103c69086 --- /dev/null +++ b/src/Exceptions/ArrayOfObjectsExpected.php @@ -0,0 +1,7 @@ +index = static::getClient()->initIndex(static::$indexes['main']); } public function testIndexDoesNotExist() { - $index = static::getClient()->initIndex(static::$indexes['main']); - - self::assertFalse($index->exists()); + self::assertFalse($this->index->exists()); } public function testIndexExists() { - $index = static::getClient()->initIndex(static::$indexes['main']); - - $index + $this->index ->saveObject( array( 'firstname' => 'Jimmie', @@ -35,27 +35,26 @@ public function testIndexExists() ) ->wait(); - self::assertTrue($index->exists()); + self::assertTrue($this->index->exists()); } public function testFindObject() { - $index = static::getClient()->initIndex(static::$indexes['main']); - $index->clearObjects(); - $index->saveObjects($this->companies); + $this->index->clearObjects(); + $this->index->saveObjects($this->companies); - $res = $index->search('Algolia'); + $res = $this->index->search('Algolia'); $this->assertEquals(SearchIndex::getObjectPosition($res, 'nicolas-dessaigne'), 0); $this->assertEquals(SearchIndex::getObjectPosition($res, 'julien-lemoine'), 1); $this->assertEquals(SearchIndex::getObjectPosition($res, ''), -1); try { - $index->findObject(function () { return false; }); + $this->index->findObject(function () { return false; }); } catch (\Exception $e) { $this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException', $e); } - $found = $index->findObject(function () { return true; }); + $found = $this->index->findObject(function () { return true; }); $this->assertEquals($found['position'], 0); $this->assertEquals($found['page'], 0); @@ -64,19 +63,28 @@ public function testFindObject() }; try { - $index->findObject($callback, array('query' => 'algolia')); + $this->index->findObject($callback, array('query' => 'algolia')); } catch (\Exception $e) { $this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException', $e); } try { - $index->findObject($callback, array('query' => '', 'paginate' => false, 'hitsPerPage' => 5)); + $this->index->findObject($callback, array('query' => '', 'paginate' => false, 'hitsPerPage' => 5)); } catch (\Exception $e) { $this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException', $e); } - $obj = $index->findObject($callback, array('query' => '', 'paginate' => true, 'hitsPerPage' => 5)); + $obj = $this->index->findObject($callback, array('query' => '', 'paginate' => true, 'hitsPerPage' => 5)); $this->assertEquals($obj['position'], 0); $this->assertEquals($obj['page'], 2); } + + public function testSaveObjectsFails() + { + try { + $this->index->saveObjects($this->companies[0]); + } catch (\Exception $e) { + $this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\ArrayOfObjectsExpected', $e); + } + } } From b98a4fa8b0d4f6ba9ba7cf48649e64f0d04263dc Mon Sep 17 00:00:00 2001 From: Chloe Liban Date: Mon, 9 Sep 2019 17:48:33 +0200 Subject: [PATCH 2/3] fix(savedObjects-error-msg): update of the tests --- src/Exceptions/ArrayOfObjectsExpected.php | 7 ------- .../InvalidArgumentObjectsException.php | 15 +++++++++++++++ src/SearchIndex.php | 6 +++--- src/Support/Helpers.php | 12 ++++++++++++ tests/Integration/SearchIndexTest.php | 2 +- 5 files changed, 31 insertions(+), 11 deletions(-) delete mode 100644 src/Exceptions/ArrayOfObjectsExpected.php create mode 100644 src/Exceptions/InvalidArgumentObjectsException.php diff --git a/src/Exceptions/ArrayOfObjectsExpected.php b/src/Exceptions/ArrayOfObjectsExpected.php deleted file mode 100644 index 103c69086..000000000 --- a/src/Exceptions/ArrayOfObjectsExpected.php +++ /dev/null @@ -1,7 +0,0 @@ -index->saveObjects($this->companies[0]); } catch (\Exception $e) { - $this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\ArrayOfObjectsExpected', $e); + $this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\InvalidArgumentObjectsException', $e); } } } From 3a08a880873b6b79e859765f64e9b66a10abd57d Mon Sep 17 00:00:00 2001 From: Chloe Liban Date: Tue, 10 Sep 2019 11:18:23 +0200 Subject: [PATCH 3/3] fix(saveObjects-error-msg): fix PR --- src/Exceptions/InvalidArgumentObjectsException.php | 11 ++++++++++- src/Support/Helpers.php | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Exceptions/InvalidArgumentObjectsException.php b/src/Exceptions/InvalidArgumentObjectsException.php index c32198abd..e3de4a300 100644 --- a/src/Exceptions/InvalidArgumentObjectsException.php +++ b/src/Exceptions/InvalidArgumentObjectsException.php @@ -2,11 +2,20 @@ namespace Algolia\AlgoliaSearch\Exceptions; +use Throwable; + final class InvalidArgumentObjectsException extends AlgoliaException { + /** + * InvalidArgumentObjectsException constructor. + * + * @param string $message The Exception message to throw + * @param int $code The Exception code + * @param Throwable $previous The previous throwable used for the exception chaining + */ public function __construct($message = '', $code = 0, $previous = null) { - if (!$message) { + if ('' === $message) { $message = 'Please provide an array of objects instead of a single object.'; } diff --git a/src/Support/Helpers.php b/src/Support/Helpers.php index 754da40c9..3f28cb6c4 100644 --- a/src/Support/Helpers.php +++ b/src/Support/Helpers.php @@ -136,6 +136,6 @@ public static function mapObjectIDs($objectIDKey, $objects) */ public static function isIterable($object) { - return is_array($object) || is_object($object) || $object instanceof \Traversable; + return is_array($object) || $object instanceof \Traversable; } }