diff --git a/README.md b/README.md index 0dc222a..cad609f 100644 --- a/README.md +++ b/README.md @@ -12,9 +12,9 @@ against the interface and third parties to provide alternate implementations. ### get ```php - $cache - ->get('foo') - ->then('var_dump'); +$cache + ->get('foo') + ->then('var_dump'); ``` This example fetches the value of the key `foo` and passes it to the @@ -25,7 +25,7 @@ If the key `foo` does not exist, the promise will be rejected. ### set ```php - $cache->set('foo', 'bar'); +$cache->set('foo', 'bar'); ``` This example eventually sets the value of the key `foo` to `bar`. If it @@ -35,7 +35,7 @@ it, it may take a while. ### remove ```php - $cache->remove('foo'); +$cache->remove('foo'); ``` This example eventually removes the key `foo` from the cache. As with `set`, @@ -49,10 +49,10 @@ A common use case of caches is to attempt fetching a cached value and as a fallback retrieve it from the original data source if not found. Here is an example of that: ```php - $cache - ->get('foo') - ->then(null, 'getFooFromDb') - ->then('var_dump'); +$cache + ->get('foo') + ->then(null, 'getFooFromDb') + ->then('var_dump'); ``` First an attempt is made to retrieve the value of `foo`. A promise rejection @@ -69,24 +69,24 @@ chain will correctly fall back, and provide the value in both cases. To expand on the fallback get example, often you want to set the value on the cache after fetching it from the data source. ```php - $cache +$cache + ->get('foo') + ->then(null, array($this, 'getAndCacheFooFromDb')) + ->then('var_dump'); + +public function getAndCacheFooFromDb() +{ + return $this->db ->get('foo') - ->then(null, array($this, 'getAndCacheFooFromDb')) - ->then('var_dump'); - - public function getAndCacheFooFromDb() - { - return $this->db - ->get('foo') - ->then(array($this, 'cacheFooFromDb')); - } - - public function cacheFooFromDb($foo) - { - $this->cache->set('foo', $foo); - - return $foo; - } + ->then(array($this, 'cacheFooFromDb')); +} + +public function cacheFooFromDb($foo) +{ + $this->cache->set('foo', $foo); + + return $foo; +} ``` By using chaining you can easily conditionally cache the value if it is fetched from the database. diff --git a/composer.json b/composer.json index bb3031e..1a65f1e 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,14 @@ "react/promise": "~2.0" }, "autoload": { - "psr-4": { "React\\Cache\\": "src\\" } + "psr-4": { + "React\\Cache\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "React\\Tests\\Cache\\": "tests" + } }, "extra": { "branch-alias": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cba6d4d..56b70fd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,16 +1,16 @@ + backupStaticAttributes="false" + colors="true" + convertErrorsToExceptions="true" + convertNoticesToExceptions="true" + convertWarningsToExceptions="true" + processIsolation="false" + stopOnFailure="false" + syntaxCheck="false" + bootstrap="vendor/autoload.php" + > ./tests/ diff --git a/src/ArrayCache.php b/src/ArrayCache.php index 03dcc15..f8917ac 100644 --- a/src/ArrayCache.php +++ b/src/ArrayCache.php @@ -6,8 +6,14 @@ class ArrayCache implements CacheInterface { - private $data = array(); + /** + * @var array + */ + protected $data = array(); + /** + * {@inheritdoc} + */ public function get($key) { if (!isset($this->data[$key])) { @@ -17,11 +23,17 @@ public function get($key) return Promise\resolve($this->data[$key]); } + /** + * {@inheritdoc} + */ public function set($key, $value) { $this->data[$key] = $value; } + /** + * {@inheritdoc} + */ public function remove($key) { unset($this->data[$key]); diff --git a/src/CacheInterface.php b/src/CacheInterface.php index fd5f2d5..126f7d4 100644 --- a/src/CacheInterface.php +++ b/src/CacheInterface.php @@ -2,12 +2,25 @@ namespace React\Cache; +use React\Promise\PromiseInterface; + interface CacheInterface { - // @return React\Promise\PromiseInterface + /** + * @param string $key + * + * @return PromiseInterface + */ public function get($key); + /** + * @param string $key + * @param mixed $value + */ public function set($key, $value); + /** + * @param string $key + */ public function remove($key); } diff --git a/tests/ArrayCacheTest.php b/tests/ArrayCacheTest.php index eec3739..f3178a3 100644 --- a/tests/ArrayCacheTest.php +++ b/tests/ArrayCacheTest.php @@ -6,14 +6,19 @@ class ArrayCacheTest extends TestCase { - private $cache; + /** + * @var ArrayCache + */ + protected $cache; public function setUp() { $this->cache = new ArrayCache(); } - /** @test */ + /** + * @test + */ public function getShouldRejectPromiseForNonExistentKey() { $this->cache @@ -24,13 +29,16 @@ public function getShouldRejectPromiseForNonExistentKey() ); } - /** @test */ + /** + * @test + */ public function setShouldSetKey() { $this->cache ->set('foo', 'bar'); $success = $this->createCallableMock(); + $success ->expects($this->once()) ->method('__invoke') @@ -41,7 +49,9 @@ public function setShouldSetKey() ->then($success); } - /** @test */ + /** + * @test + */ public function removeShouldRemoveKey() { $this->cache diff --git a/tests/TestCase.php b/tests/TestCase.php index 93a04fb..933da34 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,11 +2,20 @@ namespace React\Tests\Cache; -class TestCase extends \PHPUnit_Framework_TestCase +use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_TestCase; + +class TestCase extends PHPUnit_Framework_TestCase { + /** + * @param int $amount + * + * @return PHPUnit_Framework_MockObject_MockObject + */ protected function expectCallableExactly($amount) { $mock = $this->createCallableMock(); + $mock ->expects($this->exactly($amount)) ->method('__invoke'); @@ -14,9 +23,21 @@ protected function expectCallableExactly($amount) return $mock; } + /** + * @return PHPUnit_Framework_MockObject_MockObject + */ + protected function createCallableMock() + { + return $this->getMock('React\Tests\Cache\CallableStub'); + } + + /** + * @return PHPUnit_Framework_MockObject_MockObject + */ protected function expectCallableOnce() { $mock = $this->createCallableMock(); + $mock ->expects($this->once()) ->method('__invoke'); @@ -24,18 +45,17 @@ protected function expectCallableOnce() return $mock; } + /** + * @return PHPUnit_Framework_MockObject_MockObject + */ protected function expectCallableNever() { $mock = $this->createCallableMock(); + $mock ->expects($this->never()) ->method('__invoke'); return $mock; } - - protected function createCallableMock() - { - return $this->getMock('React\Tests\Cache\CallableStub'); - } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index 108d8e8..0000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,7 +0,0 @@ -addPsr4('React\\Tests\\Cache\\', __DIR__);