Skip to content

Commit 53bbe87

Browse files
committed
fix(savedObjects-error-msg): update of the tests
1 parent cddaf5f commit 53bbe87

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

src/Exceptions/ArrayOfObjectsExpected.php

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Exceptions;
4+
5+
final class InvalidArgumentObjectsException extends AlgoliaException
6+
{
7+
public function __construct($message = '', $code = 0, $previous = null)
8+
{
9+
if (!$message) {
10+
$message = 'Please provide an array of objects instead of a single object.';
11+
}
12+
13+
parent::__construct($message, $code, $previous);
14+
}
15+
}

src/SearchIndex.php

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

55
use Algolia\AlgoliaSearch\Config\SearchConfig;
6-
use Algolia\AlgoliaSearch\Exceptions\ArrayOfObjectsExpected;
6+
use Algolia\AlgoliaSearch\Exceptions\InvalidArgumentObjectsException;
77
use Algolia\AlgoliaSearch\Exceptions\MissingObjectId;
88
use Algolia\AlgoliaSearch\Exceptions\NotFoundException;
99
use Algolia\AlgoliaSearch\Exceptions\ObjectNotFoundException;
@@ -312,8 +312,8 @@ protected function splitIntoBatches($action, $objects, $requestOptions = array()
312312
$count = 0;
313313

314314
foreach ($objects as $object) {
315-
if (!is_array($object)) {
316-
throw new ArrayOfObjectsExpected('Please provide an array of objects instead of a single object.');
315+
if (!Helpers::isIterable($object)) {
316+
throw new InvalidArgumentObjectsException();
317317
}
318318

319319
$batch[] = $object;

src/Support/Helpers.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,16 @@ public static function mapObjectIDs($objectIDKey, $objects)
126126
return $object;
127127
}, $objects);
128128
}
129+
130+
/**
131+
* Checks if a variable can be iterated through.
132+
*
133+
* @param mixed $object
134+
*
135+
* @return bool
136+
*/
137+
public static function isIterable($object)
138+
{
139+
return is_array($object) || is_object($object) || $object instanceof \Traversable;
140+
}
129141
}

tests/Integration/SearchIndexTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function testSaveObjectsFails()
8484
try {
8585
$this->index->saveObjects($this->companies[0]);
8686
} catch (\Exception $e) {
87-
$this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\ArrayOfObjectsExpected', $e);
87+
$this->assertInstanceOf('Algolia\AlgoliaSearch\Exceptions\InvalidArgumentObjectsException', $e);
8888
}
8989
}
9090
}

0 commit comments

Comments
 (0)