示例#1
0
 /**
  * Prepare the Guzzle object for a request.
  * 
  * @return $this Self object for chaining.
  */
 public function prepare()
 {
     if (!$this->guzzle_client) {
         $this->guzzle_client = new Client($this->api_target, $this->guzzle_options);
         if ($this->cache) {
             if ($this->cache == 'file') {
                 if (!$this->cache_directory) {
                     throw new ProgrammesPlantException("No cache directory set.");
                 }
                 $this->cache_object = new FilesystemCache($this->cache_directory);
             } elseif ($this->cache == 'memory') {
                 $this->cache_object = new ArrayCache();
             }
             $adapter = new DoctrineCacheAdapter($this->cache_object);
             $this->cache_plugin = new CachePlugin(array('adapter' => $adapter, 'can_cache' => new CallbackCanCacheStrategy(function (Request $request) {
                 return $request->canCache();
             }, function (Response $response) {
                 $can = $response->isSuccessful() && $response->canCache();
                 if ($can) {
                     try {
                         API::json_decode($response->getBody(true));
                     } catch (JSONDecode $e) {
                         $can = false;
                     }
                 }
                 return $can;
             })));
             $this->guzzle_client->addSubscriber($this->cache_plugin);
         }
     }
     return $this;
 }
 /**
  * @dataProvider json_decodeErrorsProvider
  */
 public function testjson_decodeThrowsExceptionWithCorrectJSONDecodeErrorMessage($string, $expected_message)
 {
     $pp = new PP('http://example.com');
     $this->setExpectedException('ProgrammesPlant\\JSONDecode', 'We cannot decode invalid JSON,  json_decode reports: ' . $expected_message . "\nString: " . $string);
     $pp->json_decode($string);
 }