From 30157eda548c8e5cdc3415e8818964d897fe66a6 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Wed, 19 Jul 2017 16:20:26 +0200 Subject: [PATCH] handle a non existing key in array cache store --- src/Illuminate/Cache/ArrayStore.php | 3 ++- tests/Cache/CacheArrayStoreTest.php | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Cache/ArrayStore.php b/src/Illuminate/Cache/ArrayStore.php index d2f9140dc7cc..8901a489b209 100644 --- a/src/Illuminate/Cache/ArrayStore.php +++ b/src/Illuminate/Cache/ArrayStore.php @@ -50,7 +50,8 @@ public function put($key, $value, $minutes) */ public function increment($key, $value = 1) { - $this->storage[$key] = ((int) $this->storage[$key]) + $value; + $this->storage[$key] = ! isset($this->storage[$key]) + ? $value : ((int) $this->storage[$key]) + $value; return $this->storage[$key]; } diff --git a/tests/Cache/CacheArrayStoreTest.php b/tests/Cache/CacheArrayStoreTest.php index 4e95f1a0ec65..957e69d3931f 100755 --- a/tests/Cache/CacheArrayStoreTest.php +++ b/tests/Cache/CacheArrayStoreTest.php @@ -45,6 +45,13 @@ public function testValuesCanBeIncremented() $this->assertEquals(2, $store->get('foo')); } + public function testNonExistingKeysCanBeIncremented() + { + $store = new ArrayStore; + $store->increment('foo'); + $this->assertEquals(1, $store->get('foo')); + } + public function testValuesCanBeDecremented() { $store = new ArrayStore;