public function request($method, $url, $body = null, $header = array())
 {
     $url = str_replace('https://api.tinify.com', '', $url);
     $key = $this->get_key($method, $url);
     if (isset($this->handlers[$key])) {
         $handler = $this->handlers[$key];
         $status = $handler['status'];
         $body = isset($handler['body']) ? $handler['body'] : '';
         $headers = isset($handler['headers']) ? $handler['headers'] : array();
         if (isset($headers['compression-count'])) {
             \Tinify\Tinify::setCompressionCount(intval($headers['compression-count']));
         }
         $isError = $status <= 199 || $status >= 300;
         $isJson = true;
         if ($isJson || $isError) {
             $body = json_decode($body);
         }
         if ($isError) {
             throw \Tinify\Exception::create($body->message, $body->error, $status);
         }
         return (object) array('body' => $body, 'headers' => $headers);
     } else {
         throw new Exception('No handler for ' . $key);
     }
 }
 public function testSaveUpdatesCompressionCount()
 {
     Tinify\Tinify::setCompressionCount(6);
     $this->image->setDestinationSubdir("my_image_type");
     $this->image->setBaseFile("example.png");
     $this->image->saveFile();
     $status = $this->getObjectManager()->get("Tinify\\Magento\\Model\\Config\\ConnectionStatus");
     $this->assertEquals(6, $status->getCompressionCount());
 }
 protected function setUp()
 {
     parent::setUp();
     $this->useFilesystemRoot();
     $this->loadArea("adminhtml");
     $this->config = $this->getObjectManager()->get("Tinify\\Magento\\Model\\Config");
     $this->messageList = $this->getObjectManager()->get("Magento\\Framework\\Notification\\MessageList");
     $this->configModel = $this->getObjectManager()->get("Magento\\Config\\Model\\Config");
     AspectMock\Test::double("Tinify\\Client", ["request" => function () {
         throw new Tinify\ClientException("error");
     }]);
     Tinify\Tinify::setCompressionCount(6);
 }
 public function create_key($email, $options)
 {
     try {
         $this->last_error_code = 0;
         $this->set_request_options(\Tinify\Tinify::getClient(\Tinify\Tinify::ANONYMOUS));
         \Tinify\createKey($email, $options);
     } catch (\Tinify\Exception $err) {
         $this->last_error_code = $err->status;
         throw new Tiny_Exception($err->getMessage(), get_class($err), $err->status);
     }
 }
 public function testUpdateCompressionCountUpdatesCount()
 {
     Tinify\Tinify::setCompressionCount(18);
     $this->cache->expects($this->once())->method("save")->with(serialize(["compression_count" => 18]), "tinify_status");
     $this->status->updateCompressionCount();
 }