/** * Set up for all tests. * * @return void */ public function setUp() { \Chadicus\FunctionRegistry::reset(__NAMESPACE__, ['date']); \Chadicus\FunctionRegistry::set(__NAMESPACE__, 'time', function () { return 1; }); }
/** * Verify cache is removed when expired. * * @test * @covers ::__construct * @covers ::set * @covers ::get * * @return void */ public function getExpired() { $cache = new ArrayCache(); $request = new Request('not under test', 'not under test', [], []); $cache->set($request, new Response(200, [], [])); $this->assertNotNull($cache->get($request)); \Chadicus\FunctionRegistry::set(__NAMESPACE__, 'time', function () { return strtotime('+1 year'); }); $this->assertNull($cache->get($request)); }
/** * Verify behavior when json_last_error returns a value other than JSON_ERROR_NONE. * * @test * @covers ::send * @expectedException \Exception * @expectedExceptionMessage Unable to parse response: Syntax error * * @return void */ public function sendInvalidJsonInResult() { \Chadicus\FunctionRegistry::set(__NAMESPACE__, 'curl_init', function () { return true; }); \Chadicus\FunctionRegistry::set(__NAMESPACE__, 'curl_setopt_array', function ($curl, array $options) { return true; }); \Chadicus\FunctionRegistry::set(__NAMESPACE__, 'curl_exec', function ($curl) { // contains syntax error return "HTTP/1.1 200 OK\r\nContent-Length: 4\r\nContent-Type: application/json\r\n\n{xx}}"; }); \Chadicus\FunctionRegistry::set(__NAMESPACE__, 'curl_error', function ($curl) { return ''; }); \Chadicus\FunctionRegistry::set(__NAMESPACE__, 'curl_getinfo', function ($curl, $option) { if ($option === CURLINFO_HEADER_SIZE) { return 69; } if ($option === CURLINFO_HTTP_CODE) { return 200; } }); (new CurlAdapter())->send(new Request('not under test', 'get', [], [])); }
/** * Verify MongoCache cannot be instantiated when the mongo extension is not loaded. * * @test * @covers ::__construct * @expectedException \RuntimeException * @expectedExceptionMessage The mongo extension is required for MongoCache * * @return void */ public function constructMongoNotLoaded() { if (!\extension_loaded('mongo')) { $this->markTestSkipped('The mongo extension not available'); return; } \Chadicus\FunctionRegistry::set(__NAMESPACE__, 'extension_loaded', function ($name) { return false; }); new MongoCache(self::getMongoCollection()); }
/** * Verify basic functionality of set(). * * @test * @covers ::set * @covers ::get * * @return void */ public function setAndGet() { FunctionRegistry::set(__NAMESPACE__, 'strtolower', '\\Chadicus\\Tests\\FunctionRegistryTest::staticCallable'); $this->assertSame('\\Chadicus\\Tests\\FunctionRegistryTest::staticCallable', FunctionRegistry::get('strtolower')); }
/** * Verify cache is removed when expired. * * @test * @covers ::__construct * @covers ::set * @covers ::get * * @return void */ public function getExpired() { \Chadicus\FunctionRegistry::set(__NAMESPACE__, 'time', function () { return strtotime('-1 year'); }); $collection = self::getMongoCollection(); $cache = new MongoCache($collection); $request = new Request('not under test', 'not under test', [], []); $cache->set($request, new Response(200, [], [])); $this->assertNotNull($cache->get($request)); $endTime = \time() + 60; while (\time() <= $endTime) { if ($collection->count() === 0) { break; } \usleep(500000); } if ($collection->count() !== 0) { $this->markTestSkipped('Mongo index took too long'); return; } $this->assertNull($cache->get($request)); }
/** * Verfiy response is return from cache. * * @test * @covers ::get * * @return void */ public function getSetsCache() { \Chadicus\FunctionRegistry::set(__NAMESPACE__, 'time', function () { return 1; }); $hash = md5('1aPrivateKeyaPublicKey'); $request = new Request(Client::BASE_URL . "a+Resource/1?apikey=aPublicKey&ts=1&hash={$hash}", 'GET'); $cache = new Cache\ArrayCache(); $adapter = new FakeAdapter(); $client = new Client('aPrivateKey', 'aPublicKey', $adapter, $cache); $response = $client->get('a Resource', 1); $cachedResponse = $cache->get($request); $this->assertSame($response->getHttpCode(), $cachedResponse->getHttpCode()); $this->assertSame($response->getHeaders(), $cachedResponse->getHeaders()); $this->assertSame($response->getBody(), $cachedResponse->getBody()); }