Skip to content

Commit 12a1682

Browse files
committed
fix(saveObjects-error-msg): refine the error thrown by the method
1 parent c78634e commit 12a1682

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Exceptions;
4+
5+
class ArrayOfObjectsExpected extends AlgoliaException
6+
{
7+
}

src/SearchIndex.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch;
44

55
use Algolia\AlgoliaSearch\Config\SearchConfig;
6+
use Algolia\AlgoliaSearch\Exceptions\ArrayOfObjectsExpected;
67
use Algolia\AlgoliaSearch\Exceptions\MissingObjectId;
78
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
89
use Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException;
@@ -311,6 +312,10 @@ protected function splitIntoBatches($action, $objects, $requestOptions = array()
311312
$count = 0;
312313

313314
foreach ($objects as $object) {
315+
if (!is_array($object)) {
316+
throw new ArrayOfObjectsExpected('Please provide an array of objects instead of a single object.');
317+
}
318+
314319
$batch[] = $object;
315320
$count++;
316321

tests/Integration/SearchIndexTest.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66

77
class SearchIndexTest extends AlgoliaIntegrationTestCase
88
{
9+
protected $index;
10+
911
protected function setUp()
1012
{
1113
parent::setUp();
1214

1315
if (!isset(static::$indexes['main'])) {
1416
static::$indexes['main'] = self::safeName('general-index-mgmt');
1517
}
18+
19+
$this->index = static::getClient()->initIndex(static::$indexes['main']);
1620
}
1721

1822
public function testIndexDoesNotExist()
1923
{
20-
$index = static::getClient()->initIndex(static::$indexes['main']);
21-
22-
self::assertFalse($index->exists());
24+
self::assertFalse($this->index->exists());
2325
}
2426

2527
public function testIndexExists()
2628
{
27-
$index = static::getClient()->initIndex(static::$indexes['main']);
28-
29-
$index
29+
$this->index
3030
->saveObject(
3131
array(
3232
'firstname' => 'Jimmie',
@@ -35,27 +35,26 @@ public function testIndexExists()
3535
)
3636
->wait();
3737

38-
self::assertTrue($index->exists());
38+
self::assertTrue($this->index->exists());
3939
}
4040

4141
public function testFindObject()
4242
{
43-
$index = static::getClient()->initIndex(static::$indexes['main']);
44-
$index->clearObjects();
45-
$index->saveObjects($this->companies);
43+
$this->index->clearObjects();
44+
$this->index->saveObjects($this->companies);
4645

47-
$res = $index->search('Algolia');
46+
$res = $this->index->search('Algolia');
4847
$this->assertEquals(SearchIndex::getObjectPosition($res, 'nicolas-dessaigne'), 0);
4948
$this->assertEquals(SearchIndex::getObjectPosition($res, 'julien-lemoine'), 1);
5049
$this->assertEquals(SearchIndex::getObjectPosition($res, ''), -1);
5150

5251
try {
53-
$index->findObject(function () { return false; });
52+
$this->index->findObject(function () { return false; });
5453
} catch (\Exception $e) {
5554
$this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException', $e);
5655
}
5756

58-
$found = $index->findObject(function () { return true; });
57+
$found = $this->index->findObject(function () { return true; });
5958
$this->assertEquals($found['position'], 0);
6059
$this->assertEquals($found['page'], 0);
6160

@@ -64,19 +63,28 @@ public function testFindObject()
6463
};
6564

6665
try {
67-
$index->findObject($callback, array('query' => 'algolia'));
66+
$this->index->findObject($callback, array('query' => 'algolia'));
6867
} catch (\Exception $e) {
6968
$this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException', $e);
7069
}
7170

7271
try {
73-
$index->findObject($callback, array('query' => '', 'paginate' => false, 'hitsPerPage' => 5));
72+
$this->index->findObject($callback, array('query' => '', 'paginate' => false, 'hitsPerPage' => 5));
7473
} catch (\Exception $e) {
7574
$this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException', $e);
7675
}
7776

78-
$obj = $index->findObject($callback, array('query' => '', 'paginate' => true, 'hitsPerPage' => 5));
77+
$obj = $this->index->findObject($callback, array('query' => '', 'paginate' => true, 'hitsPerPage' => 5));
7978
$this->assertEquals($obj['position'], 0);
8079
$this->assertEquals($obj['page'], 2);
8180
}
81+
82+
public function testSaveObjectsFails()
83+
{
84+
try {
85+
$this->index->saveObjects($this->companies[0]);
86+
} catch (\Exception $e) {
87+
$this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\ArrayOfObjectsExpected', $e);
88+
}
89+
}
8290
}

0 commit comments

Comments
 (0)