1313namespace Geocoder \Plugin \Tests \Plugin ;
1414
1515use Cache \Adapter \Void \VoidCachePool ;
16+ use Generator ;
17+ use Geocoder \Model \Coordinates ;
1618use Geocoder \Plugin \Plugin \CachePlugin ;
1719use Geocoder \Query \GeocodeQuery ;
1820use Geocoder \Query \Query ;
21+ use Geocoder \Query \ReverseQuery ;
1922use PHPUnit \Framework \TestCase ;
2023
2124class CachePluginTest extends TestCase
@@ -50,18 +53,31 @@ public function testPluginMiss()
5053 $ this ->assertEquals ('result ' , $ plugin ->handleQuery ($ query , $ next , $ first ));
5154 }
5255
53- public function testPluginHit ()
56+ public function getQueryProvider (): Generator
5457 {
5558 $ query = GeocodeQuery::create ('foo ' );
56- $ queryString = sha1 ($ query ->__toString ());
59+ $ key = sha1 ($ query ->__toString ());
60+ yield [$ query , $ key ];
61+
62+ $ query = ReverseQuery::create (new Coordinates (12.123456 , 12.123456 ));
63+ $ lessPreciseQuery = $ query ->withCoordinates (new Coordinates (12.1235 , 12.1235 ));
64+ $ key = sha1 ((string ) $ lessPreciseQuery );
65+ yield [$ query , $ key ];
66+ }
67+
68+ /**
69+ * @dataProvider getQueryProvider
70+ */
71+ public function testPluginHit (Query $ query , string $ key )
72+ {
5773 $ cache = $ this ->getMockBuilder (VoidCachePool::class)
5874 ->disableOriginalConstructor ()
5975 ->setMethods (['get ' , 'set ' ])
6076 ->getMock ();
6177
6278 $ cache ->expects ($ this ->once ())
6379 ->method ('get ' )
64- ->with ('v4 ' .$ queryString )
80+ ->with ('v4 ' .$ key )
6581 ->willReturn ('result ' );
6682 $ cache ->expects ($ this ->never ())->method ('set ' );
6783
@@ -72,7 +88,7 @@ public function testPluginHit()
7288 $ this ->fail ('Plugin not call $next on cache hit ' );
7389 };
7490
75- $ plugin = new CachePlugin ($ cache );
91+ $ plugin = new CachePlugin ($ cache, 0 , 4 );
7692 $ this ->assertEquals ('result ' , $ plugin ->handleQuery ($ query , $ next , $ first ));
7793 }
7894}
0 commit comments