Skip to content

Commit 594fa94

Browse files
committed
Merge pull request #16 from guywithnose/master
Add CACHE_MODE_REFRESH
2 parents 4a71758 + aa6f490 commit 594fa94

4 files changed

Lines changed: 44 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor/
22
/coverage/
3+
/clover.xml

src/Client.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ final class Client
3838
*/
3939
const CACHE_MODE_ALL = 3;
4040

41+
/**
42+
* Flag to refresh cache on ALL requests
43+
*
44+
* @const int
45+
*/
46+
const CACHE_MODE_REFRESH = 4;
47+
4148
/**
4249
* Base url of the API server
4350
*
@@ -129,7 +136,11 @@ public function __construct(
129136
Util::throwIfNotType(['string' => [$accessToken, $refreshToken]], true, true);
130137
Util::ensure(
131138
true,
132-
in_array($cacheMode, [self::CACHE_MODE_NONE, self::CACHE_MODE_GET, self::CACHE_MODE_TOKEN, self::CACHE_MODE_ALL], true),
139+
in_array(
140+
$cacheMode,
141+
[self::CACHE_MODE_NONE, self::CACHE_MODE_GET, self::CACHE_MODE_TOKEN, self::CACHE_MODE_ALL, self::CACHE_MODE_REFRESH],
142+
true
143+
),
133144
'\InvalidArgumentException',
134145
['$cacheMode must be a valid cache mode constant']
135146
);
@@ -304,7 +315,7 @@ public function end($handle)
304315
$response = $this->_adapter->end($this->_adapter->start($request));
305316
}
306317

307-
if ($this->_cacheMode & self::CACHE_MODE_GET && $request->getMethod() === 'GET') {
318+
if (($this->_cacheMode === self::CACHE_MODE_REFRESH || $this->_cacheMode & self::CACHE_MODE_GET) && $request->getMethod() === 'GET') {
308319
$this->_cache->set($request, $response);
309320
}
310321

@@ -363,7 +374,7 @@ private function _refreshAccessToken()
363374

364375
list($this->_accessToken, $this->_refreshToken, $expires) = Authentication::parseTokenResponse($response);
365376

366-
if ($this->_cacheMode & self::CACHE_MODE_TOKEN) {
377+
if ($this->_cache === self::CACHE_MODE_REFRESH || $this->_cacheMode & self::CACHE_MODE_TOKEN) {
367378
$this->_cache->set($request, $response, $expires);
368379
}
369380
}

tests/ClientTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,34 @@ public function get_fromCache()
496496
$this->assertEquals($expected, $actual);
497497
}
498498

499+
/**
500+
* @test
501+
* @covers ::__construct
502+
* @covers ::end
503+
* @covers ::startGet
504+
*/
505+
public function get_disabledCache()
506+
{
507+
$cache = new ArrayCache();
508+
$request = new Request('baseUrl/a+url/id', 'not under test');
509+
$unexpected = new Response(200, ['key' => ['value']], ['doesnt' => 'matter']);
510+
$expected = new Response(200, ['Content-Type' => ['application/json']], []);
511+
$cache->set($request, $unexpected);
512+
$authentication = Authentication::createClientCredentials('not under test', 'not under test');
513+
$adapter = $this->getMockBuilder('\DominionEnterprises\Api\Adapter')->setMethods(['start', 'end'])->getMock();
514+
$adapter->expects($this->once())->method('start');
515+
$adapter->expects($this->once())->method('end')->will(
516+
$this->returnValue(new Response(200, ['Content-Type' => ['application/json']], []))
517+
);
518+
$client = new Client($adapter, $authentication, 'baseUrl', Client::CACHE_MODE_REFRESH, $cache, 'foo');
519+
$actual = $client->end($client->startGet('a url', 'id'));
520+
$this->assertEquals($expected, $actual);
521+
$this->assertEquals($expected, $cache->get($request));
522+
$client = new Client(new TokenAdapter(), $authentication, 'baseUrl', Client::CACHE_MODE_GET, $cache);
523+
$actual = $client->end($client->startGet('a url', 'id'));
524+
$this->assertEquals($expected, $actual);
525+
}
526+
499527
/**
500528
* @test
501529
* @covers ::__construct

tests/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM nubs/phpunit
22

33
USER root
44

5-
RUN pacman --sync --refresh --noconfirm --noprogressbar --quiet && pacman --sync --noconfirm --noprogressbar --quiet php-mongo
5+
RUN pacman-key --populate archlinux && pacman-key --refresh-keys && pacman --sync --refresh --noconfirm --noprogressbar --quiet && pacman --sync --refresh --sysupgrade --noconfirm --noprogressbar --quiet && pacman-db-upgrade && pacman --sync --noconfirm --noprogressbar --quiet php-mongo
66

77
USER build
88

0 commit comments

Comments
 (0)