public checkUpdate ( ) : integer | null | ||
Résultat | integer | null | The actual cached version if a newer version is available, null otherwise |
public function checkUpdate()
{
$success = null;
$cachedVersion = $this->getCache()->getItem('browscap.version', false, $success);
if (!$cachedVersion) {
// could not load version from cache
$this->getLogger()->info('there is no cached version available, please update from remote');
return 0;
}
$uri = (new IniLoader())->getRemoteVersionUrl();
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $this->getClient()->get($uri, ['connect_timeout' => $this->connectTimeout]);
if ($response->getStatusCode() !== 200) {
throw new FetcherException('an error occured while fetching version data from URI ' . $uri . ': StatusCode was ' . $response->getStatusCode());
}
try {
$remoteVersion = $response->getBody()->getContents();
} catch (\Exception $e) {
throw new FetcherException('an error occured while fetching version data from URI ' . $uri . ': StatusCode was ' . $response->getStatusCode(), 0, $e);
}
if (!$remoteVersion) {
// could not load remote version
$this->getLogger()->info('could not load version from remote location');
return 0;
}
if ($cachedVersion && $remoteVersion && $remoteVersion <= $cachedVersion) {
// no newer version available
$this->getLogger()->info('there is no newer version available');
return null;
}
$this->getLogger()->info('a newer version is available, local version: ' . $cachedVersion . ', remote version: ' . $remoteVersion);
return (int) $cachedVersion;
}
/** * */ public function testCheckUpdateWithNewerVersion() { $logger = $this->getMockBuilder(\Monolog\Logger::class)->disableOriginalConstructor()->getMock(); $this->object->setLogger($logger); $body = $this->getMockBuilder(\GuzzleHttp\Psr7\Stream::class)->disableOriginalConstructor()->setMethods(['getContents'])->getMock(); $body->expects(self::once())->method('getContents')->will(self::returnValue(6001)); $response = $this->getMockBuilder(\GuzzleHttp\Psr7\Response::class)->disableOriginalConstructor()->setMethods(['getStatusCode', 'getBody'])->getMock(); $response->expects(self::once())->method('getStatusCode')->will(self::returnValue(200)); $response->expects(self::once())->method('getBody')->will(self::returnValue($body)); $client = $this->getMockBuilder(\GuzzleHttp\Client::class)->disableOriginalConstructor()->setMethods(['get'])->getMock(); $client->expects(self::once())->method('get')->will(self::returnValue($response)); $this->object->setClient($client); $map = [['browscap.time', false, null, null], ['browscap.version', false, null, 6000]]; $cache = $this->getMockBuilder(\BrowscapPHP\Cache\BrowscapCache::class)->disableOriginalConstructor()->setMethods(['getItem', 'hasItem', 'setItem'])->getMock(); $cache->expects(self::any())->method('getItem')->will(self::returnValueMap($map)); $cache->expects(self::any())->method('hasItem')->will(self::returnValue(true)); $cache->expects(self::never())->method('setItem')->will(self::returnValue(false)); $this->object->setCache($cache); self::assertSame(6000, $this->object->checkUpdate()); }