|
3 | 3 | namespace TraderInteractive\Api; |
4 | 4 |
|
5 | 5 | use Fig\Http\Message\StatusCodeInterface as StatusCodes; |
| 6 | +use Helmich\MongoMock\MockCollection; |
6 | 7 | use SubjectivePHP\Psr\SimpleCache\InMemoryCache; |
7 | 8 | use DominionEnterprises\Util\Arrays; |
8 | 9 | use DominionEnterprises\Util\Http; |
9 | 10 | use GuzzleHttp\Psr7\Request; |
10 | 11 | use GuzzleHttp\Psr7\Response as Psr7Response; |
11 | 12 | use PHPUnit\Framework\TestCase; |
12 | 13 | use Psr\Http\Message\RequestInterface; |
| 14 | +use SubjectivePHP\Psr\SimpleCache\MongoCache; |
| 15 | +use SubjectivePHP\Psr\SimpleCache\RedisCache; |
13 | 16 |
|
14 | 17 | /** |
15 | 18 | * Unit tests for the Client class |
@@ -932,6 +935,44 @@ function (RequestInterface $request) { |
932 | 935 | $this->assertEquals($expected, Response::fromPsr7Response($actual)); |
933 | 936 | } |
934 | 937 |
|
| 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 | + |
935 | 976 | /** |
936 | 977 | * Verify client uses in memory token only if originially pulled from cache |
937 | 978 | * |
|
0 commit comments