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

fix(saveObjects-error-msg): refine the error thrown by the method #584

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Exceptions/InvalidArgumentObjectsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

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)
chloelbn marked this conversation as resolved.
Show resolved Hide resolved
{
if ('' === $message) {
$message = 'Please provide an array of objects instead of a single object.';
}

parent::__construct($message, $code, $previous);
}
}
5 changes: 5 additions & 0 deletions src/SearchIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Algolia\AlgoliaSearch;

use Algolia\AlgoliaSearch\Config\SearchConfig;
use Algolia\AlgoliaSearch\Exceptions\InvalidArgumentObjectsException;
use Algolia\AlgoliaSearch\Exceptions\MissingObjectId;
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
use Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException;
Expand Down Expand Up @@ -311,6 +312,10 @@ protected function splitIntoBatches($action, $objects, $requestOptions = array()
$count = 0;

foreach ($objects as $object) {
if (!Helpers::isIterable($object)) {
throw new InvalidArgumentObjectsException();
}

$batch[] = $object;
$count++;

Expand Down
12 changes: 12 additions & 0 deletions src/Support/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,16 @@ public static function mapObjectIDs($objectIDKey, $objects)
return $object;
}, $objects);
}

/**
* Checks if a variable can be iterated through.
*
* @param mixed $object
*
* @return bool
*/
public static function isIterable($object)
{
return is_array($object) || $object instanceof \Traversable;
}
}
40 changes: 24 additions & 16 deletions tests/Integration/SearchIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@

class SearchIndexTest extends AlgoliaIntegrationTestCase
{
protected $index;

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

if (!isset(static::$indexes['main'])) {
static::$indexes['main'] = self::safeName('general-index-mgmt');
}

$this->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',
Expand All @@ -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);

Expand All @@ -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\InvalidArgumentObjectsException', $e);
}
}
}