diff --git a/src/Illuminate/Cache/Repository.php b/src/Illuminate/Cache/Repository.php index d0619c7ee00a..0c81c630a3b3 100755 --- a/src/Illuminate/Cache/Repository.php +++ b/src/Illuminate/Cache/Repository.php @@ -283,7 +283,7 @@ protected function putManyForever(array $values) */ public function setMultiple($values, $ttl = null) { - return $this->putMany($values, $ttl); + return $this->putMany(is_array($values) ? $values : iterator_to_array($values), $ttl); } /** diff --git a/tests/Cache/CacheRepositoryTest.php b/tests/Cache/CacheRepositoryTest.php index 5ead0cb50c8d..0753c4c7d2f4 100755 --- a/tests/Cache/CacheRepositoryTest.php +++ b/tests/Cache/CacheRepositoryTest.php @@ -5,6 +5,7 @@ use DateTime; use DateInterval; use Mockery as m; +use ArrayIterator; use DateTimeImmutable; use Illuminate\Support\Carbon; use PHPUnit\Framework\TestCase; @@ -130,15 +131,22 @@ public function testPuttingMultipleItemsInCache() $repo->put(['foo' => 'bar', 'bar' => 'baz'], 1); } - public function testSettingMultipleItemsInCache() + public function testSettingMultipleItemsInCacheArray() { - // Alias of PuttingMultiple $repo = $this->getRepository(); $repo->getStore()->shouldReceive('putMany')->once()->with(['foo' => 'bar', 'bar' => 'baz'], 1)->andReturn(true); $result = $repo->setMultiple(['foo' => 'bar', 'bar' => 'baz'], 1); $this->assertTrue($result); } + public function testSettingMultipleItemsInCacheIterator() + { + $repo = $this->getRepository(); + $repo->getStore()->shouldReceive('putMany')->once()->with(['foo' => 'bar', 'bar' => 'baz'], 1)->andReturn(true); + $result = $repo->setMultiple(new ArrayIterator(['foo' => 'bar', 'bar' => 'baz']), 1); + $this->assertTrue($result); + } + public function testPutWithNullTTLRemembersItemForever() { $repo = $this->getRepository();