Skip to content

Commit d32d5af

Browse files
committed
Add client test using mongo cache
1 parent 12808af commit d32d5af

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

tests/ClientTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
namespace TraderInteractive\Api;
44

55
use Fig\Http\Message\StatusCodeInterface as StatusCodes;
6+
use Helmich\MongoMock\MockCollection;
67
use SubjectivePHP\Psr\SimpleCache\InMemoryCache;
78
use DominionEnterprises\Util\Arrays;
89
use DominionEnterprises\Util\Http;
910
use GuzzleHttp\Psr7\Request;
1011
use GuzzleHttp\Psr7\Response as Psr7Response;
1112
use PHPUnit\Framework\TestCase;
1213
use Psr\Http\Message\RequestInterface;
14+
use SubjectivePHP\Psr\SimpleCache\MongoCache;
15+
use SubjectivePHP\Psr\SimpleCache\RedisCache;
1316

1417
/**
1518
* Unit tests for the Client class
@@ -932,6 +935,44 @@ function (RequestInterface $request) {
932935
$this->assertEquals($expected, Response::fromPsr7Response($actual));
933936
}
934937

938+
/**
939+
* @test
940+
* @covers ::end
941+
*/
942+
public function useMongoCache()
943+
{
944+
$hasBeenCalled = false;
945+
$adapter = new FakeAdapter(
946+
function (RequestInterface $request) use (&$hasBeenCalled) {
947+
if (substr_count($request->getUri(), 'token') == 1) {
948+
return new Psr7Response(
949+
200,
950+
['Content-Type' => ['application/json']],
951+
json_encode(['access_token' => 'token', 'expires_in' => 1])
952+
);
953+
}
954+
955+
if ($hasBeenCalled) {
956+
throw new \Exception('Adapter called twice');
957+
}
958+
959+
$hasBeenCalled = true;
960+
if (substr_count($request->getUri(), 'a+url') == 1) {
961+
return new Psr7Response(200, ['header' => ['value']], json_encode(['doesnt' => 'matter']));
962+
}
963+
}
964+
);
965+
$collection = new MockCollection('cache');
966+
$cache = CacheFactory::make(MongoCache::class, ['collection' => $collection]);
967+
$client = new Client($adapter, $this->getAuthentication(), 'baseUrl', Client::CACHE_MODE_GET, $cache);
968+
$expected = $client->get('a url', 'id');
969+
$actual = $cache->get('GET|baseUrl_FSLASH_a+url_FSLASH_id|');
970+
$this->assertEquals($expected, Response::fromPsr7Response($actual));
971+
972+
$expected = $client->get('a url', 'id');
973+
$this->assertEquals($expected, Response::fromPsr7Response($actual));
974+
}
975+
935976
/**
936977
* Verify client uses in memory token only if originially pulled from cache
937978
*

0 commit comments

Comments
 (0)