From 456b2430517d695e4b7c19835141e630ffe78de2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Mrozi=C5=84ski?= Date: Fri, 3 Mar 2017 20:20:28 +0100 Subject: [PATCH 1/5] Return Promise for set and remove --- src/ArrayCache.php | 16 ++++++++++++++++ src/CacheInterface.php | 16 +++++++++++++++- tests/ArrayCacheTest.php | 13 +++++++++++-- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/ArrayCache.php b/src/ArrayCache.php index 03dcc15..978a389 100644 --- a/src/ArrayCache.php +++ b/src/ArrayCache.php @@ -3,11 +3,16 @@ namespace React\Cache; use React\Promise; +use React\Promise\PromiseInterface; class ArrayCache implements CacheInterface { private $data = array(); + /** + * @param $key + * @return PromiseInterface + */ public function get($key) { if (!isset($this->data[$key])) { @@ -17,13 +22,24 @@ public function get($key) return Promise\resolve($this->data[$key]); } + /** + * @param $key + * @param $value + * @return PromiseInterface + */ public function set($key, $value) { $this->data[$key] = $value; + return new Promise\FulfilledPromise(true); } + /** + * @param $key + * @return PromiseInterface + */ public function remove($key) { unset($this->data[$key]); + return new Promise\FulfilledPromise(true); } } diff --git a/src/CacheInterface.php b/src/CacheInterface.php index fd5f2d5..c8d4a55 100644 --- a/src/CacheInterface.php +++ b/src/CacheInterface.php @@ -2,12 +2,26 @@ namespace React\Cache; +use React\Promise\PromiseInterface; + interface CacheInterface { - // @return React\Promise\PromiseInterface + /** + * @param $key + * @return PromiseInterface + */ public function get($key); + /** + * @param $key + * @param $value + * @return PromiseInterface + */ public function set($key, $value); + /** + * @param $key + * @return PromiseInterface + */ public function remove($key); } diff --git a/tests/ArrayCacheTest.php b/tests/ArrayCacheTest.php index eec3739..059de9a 100644 --- a/tests/ArrayCacheTest.php +++ b/tests/ArrayCacheTest.php @@ -6,6 +6,9 @@ class ArrayCacheTest extends TestCase { + /** + * @var ArrayCache + */ private $cache; public function setUp() @@ -27,8 +30,11 @@ public function getShouldRejectPromiseForNonExistentKey() /** @test */ public function setShouldSetKey() { - $this->cache + $setPromise = $this->cache ->set('foo', 'bar'); + $setPromise->then(function ($true) { + $this->assertTrue($true); + }); $success = $this->createCallableMock(); $success @@ -47,8 +53,11 @@ public function removeShouldRemoveKey() $this->cache ->set('foo', 'bar'); - $this->cache + $removePromise = $this->cache ->remove('foo'); + $removePromise->then(function ($true) { + $this->assertTrue($true); + }); $this->cache ->get('foo') From 5dc49c149caa137f4ff37d81ab81cf1558d3b809 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Mrozi=C5=84ski?= Date: Fri, 3 Mar 2017 20:37:29 +0100 Subject: [PATCH 2/5] Fix tests for PHP 5.3 --- tests/ArrayCacheTest.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/ArrayCacheTest.php b/tests/ArrayCacheTest.php index 059de9a..2699d3b 100644 --- a/tests/ArrayCacheTest.php +++ b/tests/ArrayCacheTest.php @@ -32,8 +32,9 @@ public function setShouldSetKey() { $setPromise = $this->cache ->set('foo', 'bar'); - $setPromise->then(function ($true) { - $this->assertTrue($true); + $that = $this; + $setPromise->then(function ($true) use ($that) { + $that->assertTrue($true); }); $success = $this->createCallableMock(); @@ -55,8 +56,9 @@ public function removeShouldRemoveKey() $removePromise = $this->cache ->remove('foo'); - $removePromise->then(function ($true) { - $this->assertTrue($true); + $that = $this; + $removePromise->then(function ($true) use ($that) { + $that->assertTrue($true); }); $this->cache From 214113b00b1324eaab519a206dd8c721f4e17810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Mrozi=C5=84ski?= Date: Sat, 4 Mar 2017 17:00:15 +0100 Subject: [PATCH 3/5] Remove unnecessary doc blocks --- src/ArrayCache.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/src/ArrayCache.php b/src/ArrayCache.php index 978a389..ff2a06d 100644 --- a/src/ArrayCache.php +++ b/src/ArrayCache.php @@ -3,16 +3,11 @@ namespace React\Cache; use React\Promise; -use React\Promise\PromiseInterface; class ArrayCache implements CacheInterface { private $data = array(); - /** - * @param $key - * @return PromiseInterface - */ public function get($key) { if (!isset($this->data[$key])) { @@ -22,21 +17,12 @@ public function get($key) return Promise\resolve($this->data[$key]); } - /** - * @param $key - * @param $value - * @return PromiseInterface - */ public function set($key, $value) { $this->data[$key] = $value; return new Promise\FulfilledPromise(true); } - /** - * @param $key - * @return PromiseInterface - */ public function remove($key) { unset($this->data[$key]); From 92f264583f0238cf09326b30655d7aaf0d02f74c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Mrozi=C5=84ski?= Date: Mon, 6 Mar 2017 15:38:32 +0100 Subject: [PATCH 4/5] Remove params doc blocks --- src/CacheInterface.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/CacheInterface.php b/src/CacheInterface.php index c8d4a55..34211a2 100644 --- a/src/CacheInterface.php +++ b/src/CacheInterface.php @@ -7,20 +7,16 @@ interface CacheInterface { /** - * @param $key * @return PromiseInterface */ public function get($key); /** - * @param $key - * @param $value * @return PromiseInterface */ public function set($key, $value); /** - * @param $key * @return PromiseInterface */ public function remove($key); From ca6a8f95f4ed51a1135423f9723ccd28947abd5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Mrozi=C5=84ski?= Date: Mon, 6 Mar 2017 15:48:37 +0100 Subject: [PATCH 5/5] Changes in tests --- tests/ArrayCacheTest.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tests/ArrayCacheTest.php b/tests/ArrayCacheTest.php index 2699d3b..383490b 100644 --- a/tests/ArrayCacheTest.php +++ b/tests/ArrayCacheTest.php @@ -32,10 +32,14 @@ public function setShouldSetKey() { $setPromise = $this->cache ->set('foo', 'bar'); - $that = $this; - $setPromise->then(function ($true) use ($that) { - $that->assertTrue($true); - }); + + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->identicalTo(true)); + + $setPromise->then($mock); $success = $this->createCallableMock(); $success @@ -56,10 +60,14 @@ public function removeShouldRemoveKey() $removePromise = $this->cache ->remove('foo'); - $that = $this; - $removePromise->then(function ($true) use ($that) { - $that->assertTrue($true); - }); + + $mock = $this->createCallableMock(); + $mock + ->expects($this->once()) + ->method('__invoke') + ->with($this->identicalTo(true)); + + $removePromise->then($mock); $this->cache ->get('foo')