Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Implement first idea of the cache provider #488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests and move CacheStrategy folder outside
  • Loading branch information
Baachi committed Feb 1, 2016
commit b27aefd98aa2b2eeac0e98d2c19bf63db49e0ead
3 changes: 2 additions & 1 deletion 3 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"require": {
"php": ">=5.4.0",
"egeloen/http-adapter": "~0.8|~1.0",
"igorw/get-in": "~1.0"
"igorw/get-in": "~1.0",
"psr/cache": "^1.0"
},
"require-dev": {
"geoip2/geoip2": "~2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

namespace Geocoder\Provider\CacheStrategy;
namespace Geocoder\CacheStrategy;

class Expire implements Stragegy
use Psr\Cache\CacheItemPoolInterface;

class Expire implements Strategy
{
private $cache;

Expand All @@ -14,9 +16,8 @@ public function __construct(CacheItemPoolInterface $cache, $ttl = null)
$this->ttl = $ttl;
}

public function invoke($key, callable $function, CacheItemPoolInterface $cache)
public function invoke($key, callable $function)
{
$key = $this->generateKey($address);
$item = $this->cache->getItem($key);

if ($item->isHit()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

namespace Geocoder\Provider\CacheStrategy;
namespace Geocoder\CacheStrategy;

use Psr\Cache\CacheItemPoolInterface;

class StaleIfError implements Stragegy
class StaleIfError implements Strategy
{
private $cache;

Expand All @@ -18,7 +18,7 @@ public function __construct(CacheItemPoolInterface $cache, $ttl = null)

public function invoke($key, callable $function)
{
$item = $this->cache->get($key);
$item = $this->cache->getItem($key);

try {
$data = call_user_func($function);
Expand Down
8 changes: 8 additions & 0 deletions 8 src/Geocoder/CacheStrategy/Strategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Geocoder\CacheStrategy;

interface Strategy
{
function invoke($key, callable $function);
}
9 changes: 4 additions & 5 deletions 9 src/Geocoder/Provider/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

namespace Geocoder\Provider;

use CacheStrategy\Strategy;
use Geocoder\Exception\InvalidCredentials;
use Geocoder\CacheStrategy\Strategy;

/**
* @author Markus Bachmann <markus.bachmann@bachi.biz>
Expand Down Expand Up @@ -57,7 +56,7 @@ public function geocode($address)
*/
public function reverse($latitude, $longitude)
{
$key = $this->generateKey($latitude.$longitude);
$key = $this->generateKey(serialize([$latitude, $longitude]));

return $this->strategy->invoke($key, function() use ($latitude, $longitude) {
return $this->delegate->reverse($latitude, $longitude);
Expand Down Expand Up @@ -91,12 +90,12 @@ public function getName()
/**
* Generate a key.
*
* @param mixed $value
* @param string $value
*
* @return string
*/
private function generateKey($value)
{
return 'geocoder_'.sha1(serialize($value));
return 'geocoder_'.sha1($value);
}
}
8 changes: 0 additions & 8 deletions 8 src/Geocoder/Provider/CacheStrategy/Strategy.php

This file was deleted.

52 changes: 52 additions & 0 deletions 52 tests/Geocoder/Tests/CacheStrategy/ExpireCacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Geocoder\Tests\CacheStrategy;

use Geocoder\CacheStrategy\Expire;

class ExpireTest extends \PHPUnit_Framework_TestCase
{
private $strategy;

protected function setUp()
{
$this->pool = $this->prophesize('Psr\Cache\CacheItemPoolInterface');
$this->strategy = new Expire($this->pool->reveal(), 100);
}

public function testInvokeWithCache()
{
$item = $this->prophesize('Psr\Cache\CacheItemInterface');

$item->isHit()->willReturn(true);
$item->get()->willReturn('test');
$item->set()->shouldNotBeCalled();

$this->pool->getItem('foo')->willReturn($item->reveal());

$data = $this->strategy->invoke('foo', function() {});

return $this->assertEquals('test', $data);
}

public function testInvokeWithoutCache()
{
$item = $this->prophesize('Psr\Cache\CacheItemInterface');

$item->isHit()->willReturn(false);
$item->get()->shouldNotBeCalled();
$item->set('test')->shouldBeCalled();
$item->expiresAfter(100)->shouldBeCalled();

$item = $item->reveal();

$this->pool->getItem('foo')->willReturn($item);
$this->pool->save($item)->willReturn(true);

$data = $this->strategy->invoke('foo', function() {
return 'test';
});

return $this->assertEquals('test', $data);
}
}
71 changes: 71 additions & 0 deletions 71 tests/Geocoder/Tests/CacheStrategy/StaleIfErrorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Geocoder\Tests\CacheStrategy;

use Geocoder\CacheStrategy\StaleIfError;

class StaleIfErrorTest extends \PHPUnit_Framework_TestCase
{
private $strategy;

protected function setUp()
{
$this->pool = $this->prophesize('Psr\Cache\CacheItemPoolInterface');
$this->strategy = new StaleIfError($this->pool->reveal(), 100);
}

public function testValidResponse()
{
$item = $this->prophesize('Psr\Cache\CacheItemInterface');

$item->expiresAfter(100)->shouldBeCalled();
$item->set('test')->shouldBeCalled();

$item->get()->shouldNotBeCalled();

$item = $item->reveal();

$this->pool->getItem('foo')->willReturn($item);
$this->pool->save($item)->willReturn(true);

$data = $this->strategy->invoke('foo', function() {
return 'test';
});

$this->assertEquals('test', $data);
}

public function testCatchExceptionAndReturnCache()
{
$item = $this->prophesize('Psr\Cache\CacheItemInterface');
$item->isHit()->willReturn(true);
$item->get()->willReturn('test');

$item = $item->reveal();

$this->pool->getItem('foo')->willReturn($item);
$this->pool->save()->shouldNotBeCalled();

$data = $this->strategy->invoke('foo', function() {
throw new \Exception();
});

$this->assertEquals('test', $data);
}

/**
* @expectedException \Exception
*/
public function testCatchExceptionAndHaveNoCache()
{
$item = $this->prophesize('Psr\Cache\CacheItemInterface');
$item->isHit()->willReturn(false);
$item->get()->shouldNotBeCalled();

$this->pool->getItem('foo')->willReturn($item->reveal());

$this->strategy->invoke('foo', function() {
throw new \Exception();
});
}
}
57 changes: 57 additions & 0 deletions 57 tests/Geocoder/Tests/Provider/CacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Geocoder\Tests\Provider;

use Prophecy\Argument;
use Geocoder\Provider\Cache;

class CacheTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
$this->strategy = $this->prophesize('Geocoder\CacheStrategy\Strategy');
$this->delegate = $this->prophesize('Geocoder\Provider\Provider');

$this->provider = new Cache($this->strategy->reveal(), $this->delegate->reveal());
}


public function testGeocode()
{
$this->strategy->invoke(
'geocoder_3bd614cd786c546800755606b56dfce3863cffa6',
Argument::type('Closure')
)->willReturn('foo');

$this->assertEquals('foo', $this->provider->geocode('Alexander Platz 1, Berlin'));
}

public function testReverse()
{
$this->strategy->invoke(
'geocoder_20889549b60b05ddbac1f40d34259d8dee5f9835',
Argument::type('Closure')
)->willReturn('foo');

$this->assertEquals('foo', $this->provider->reverse(55.56688, 78.51426));
}

public function testGetName()
{
$this->assertEquals('cache', $this->provider->getName());
}

public function testLimit()
{
$this->delegate->limit(20)->shouldBeCalled();

$this->provider->limit(20);
}

public function testGetLimit()
{
$this->delegate->getLimit()->willReturn(20);

$this->assertEquals(20, $this->provider->getLimit());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.