diff --git a/iloveimg-php/init.php b/iloveimg-php/init.php index ebb83d27..a6a057a5 100755 --- a/iloveimg-php/init.php +++ b/iloveimg-php/init.php @@ -7,7 +7,7 @@ require_once __DIR__ . '/src/Request/Response.php'; require_once __DIR__ . '/src/Request/Request.php'; require_once __DIR__ . '/src/Request/Body.php'; - +require_once __DIR__ . '/src/Element.php'; //Exceptions require_once __DIR__ . '/src/Exceptions/ExtendedException.php'; require_once __DIR__ . '/src/Exceptions/DownloadException.php'; @@ -20,7 +20,6 @@ //Iloveimg require_once __DIR__ . '/src/Iloveimg.php'; require_once __DIR__ . '/src/ImageTask.php'; -require_once __DIR__ . '/src/Element.php'; //Specific processes require_once __DIR__ . '/src/CompressImageTask.php'; diff --git a/iloveimg-php/readme.md b/iloveimg-php/readme.md index d15d0cda..09a64e0c 100644 --- a/iloveimg-php/readme.md +++ b/iloveimg-php/readme.md @@ -23,7 +23,7 @@ PHP 7.1 and later. You can install the library via [Composer](http://getcomposer.org/). Run the following command: ```bash -composer require iloveimg/iloveimg-php +composer require ilovepdf/iloveimg-php ``` To use the library, use Composer's [autoload](https://getcomposer.org/doc/00-intro.md#autoloading): @@ -59,4 +59,4 @@ See samples folder. ## Documentation -Please see https://developer.iloveimg.com/docs for up-to-date documentation. \ No newline at end of file +Please see https://developer.iloveimg.com/docs for up-to-date documentation. diff --git a/iloveimg-php/samples/chained_task.php b/iloveimg-php/samples/chained_task.php new file mode 100644 index 00000000..0501287c --- /dev/null +++ b/iloveimg-php/samples/chained_task.php @@ -0,0 +1,34 @@ +newTask('rotate'); + +// file var keeps info about server file id, name... +// it can be used latter to cancel file +$file = $rotateTask->addFile('/path/to/file/document.jpg'); +$file->setRotation(90); + +// run the task +$rotateTask->execute(); + +//and create a new task from last action +$compressTask = $rotateTask->next('compress'); +$compressTask->setCompressionLevel('extreme'); + +// process files +$compressTask->execute(); + +// and finally download file. If no path is set, it will be downloaded on current folder +$compressTask->download(); \ No newline at end of file diff --git a/iloveimg-php/samples/compress_advanced.php b/iloveimg-php/samples/compress_advanced.php new file mode 100644 index 00000000..a029f7bd --- /dev/null +++ b/iloveimg-php/samples/compress_advanced.php @@ -0,0 +1,33 @@ +addFile('/path/to/file/document.jpg'); + +// we can set rotate to file +$file->setRotation(90); + +// set compression level +$myTask->setCompressionLevel('extreme'); + +// and set name for output file. +// the task will set the correct file extension for you. +$myTask->setOutputFilename('lowlow_compression'); + +// process files +$myTask->execute(); + +// and finally download file. If no path is set, it will be downloaded on current folder +$myTask->download('path/to/download'); \ No newline at end of file diff --git a/iloveimg-php/samples/compress_basic.php b/iloveimg-php/samples/compress_basic.php new file mode 100644 index 00000000..77ee90b8 --- /dev/null +++ b/iloveimg-php/samples/compress_basic.php @@ -0,0 +1,22 @@ +addFile('/path/to/file/document.jpg'); + +// process files +$myTask->execute(); + +// and finally download file. If no path is set, it will be downloaded on current folder +$myTask->download(); diff --git a/iloveimg-php/samples/get_remaining_files.php b/iloveimg-php/samples/get_remaining_files.php new file mode 100644 index 00000000..f29270a2 --- /dev/null +++ b/iloveimg-php/samples/get_remaining_files.php @@ -0,0 +1,26 @@ +getRemainingFiles(); + + +//print your remaining files +echo $remainingFiles; + +//only start new process if you have enough files +if($remainingFiles>0) { + //start the task + $myTask = $iloveimg->newTask('merge'); +} \ No newline at end of file diff --git a/iloveimg-php/samples/repair_advanced.php b/iloveimg-php/samples/repair_advanced.php new file mode 100644 index 00000000..e283a305 --- /dev/null +++ b/iloveimg-php/samples/repair_advanced.php @@ -0,0 +1,27 @@ +addFile('/path/to/file/document.jpg'); + + +// and set name for output file. +// the task will set the correct file extension for you. +$myTask->setOutputFilename('repaired_file'); + +// process files +$myTask->execute(); + +// and finally download file. If no path is set, it will be downloaded on current folder +$myTask->download('path/to/download'); \ No newline at end of file diff --git a/iloveimg-php/samples/repair_basic.php b/iloveimg-php/samples/repair_basic.php new file mode 100644 index 00000000..028f7852 --- /dev/null +++ b/iloveimg-php/samples/repair_basic.php @@ -0,0 +1,22 @@ +addFile('/path/to/file/document.jpg'); + +// process files +$myTask->execute(); + +// and finally download file. If no path is set, it will be downloaded on current folder +$myTask->download(); \ No newline at end of file diff --git a/iloveimg-php/samples/resize_advanced.php b/iloveimg-php/samples/resize_advanced.php new file mode 100644 index 00000000..1336bed9 --- /dev/null +++ b/iloveimg-php/samples/resize_advanced.php @@ -0,0 +1,41 @@ +addFile('/path/to/file/document.jpg'); + + +//set resize mode to pixels +$myTask->setResizeMode('pixels'); + +//resize to fit into a 1024px square +$myTask->setPixelsWidth('1024'); +$myTask->setPixelsHeight('1024'); + +//do not make it bigger +$myTask->setNoEnlargeIfSmaller(true); + +//queep relation +$myTask->setMaintainRatio(true); + + +// and set name for output file. +// the task will set the correct file extension for you. +$myTask->setOutputFilename('repaired_file'); + +// process files +$myTask->execute(); + +// and finally download file. If no path is set, it will be downloaded on current folder +$myTask->download('path/to/download'); \ No newline at end of file diff --git a/iloveimg-php/samples/resize_basic.php b/iloveimg-php/samples/resize_basic.php new file mode 100644 index 00000000..76b6713c --- /dev/null +++ b/iloveimg-php/samples/resize_basic.php @@ -0,0 +1,26 @@ +addFile('/path/to/file/document.jpg'); + +//set resize mode to percentage and resize 50% +$myTask->setResizeMode('percentage'); +$myTask->setPercentage('50'); + +// process files +$myTask->execute(); + +// and finally download file. If no path is set, it will be downloaded on current folder +$myTask->download(); \ No newline at end of file diff --git a/iloveimg-php/samples/rotate_advanced.php b/iloveimg-php/samples/rotate_advanced.php new file mode 100644 index 00000000..b145eb01 --- /dev/null +++ b/iloveimg-php/samples/rotate_advanced.php @@ -0,0 +1,29 @@ +addFile('/path/to/file/document.jpg'); + +// set the rotation, in degrees +$file->setRotation(90); + +// and set name for output file. +// the task will set the correct file extension for you. +$myTask->setOutputFilename('rotated_file'); + +// process files +$myTask->execute(); + +// and finally download file. If no path is set, it will be downloaded on current folder +$myTask->download('path/to/download'); \ No newline at end of file diff --git a/iloveimg-php/samples/rotate_basic.php b/iloveimg-php/samples/rotate_basic.php new file mode 100644 index 00000000..a61dfe70 --- /dev/null +++ b/iloveimg-php/samples/rotate_basic.php @@ -0,0 +1,25 @@ +addFile('/path/to/file/document.jpg'); + +// set the rotation, in degrees +$file->setRotation(90); + +// process files +$myTask->execute(); + +// and finally download file. If no path is set, it will be downloaded on current folder +$myTask->download(); \ No newline at end of file diff --git a/iloveimg-php/samples/try_catch_errors.php b/iloveimg-php/samples/try_catch_errors.php new file mode 100644 index 00000000..8aecdcdf --- /dev/null +++ b/iloveimg-php/samples/try_catch_errors.php @@ -0,0 +1,54 @@ +newTask('compress'); + + // or you can call task class directly, this set the same tool as before + $myTask = new \Iloveimg\CompressimageImageTask('project_public_id','project_secret_key'); + + + // file var keeps info about server file id, name... + // it can be used latter to cancel file + $file = $myTask->addFile('/path/to/file/document.jpg'); + $file2 = $myTask->addFile('/path/to/file/document2.jpg'); + + // and set name for output file. + // in this case it will output a zip file, so we set the package name. + $myTask->setPackagedFilename('compress_documents'); + + // and name for splitted document (inside the zip file) + $myTask->setOutputFilename('compressed'); + + // process files + $myTask->execute(); + + // and finally download file. If no path is set, it will be downloaded on current folder + $myTask->download('path/to/download'); + +} catch (\Iloveimg\Exceptions\StartException $e) { + echo "An error occured on start: " . $e->getMessage() . " "; +} catch (\Iloveimg\Exceptions\AuthException $e) { + echo "An error occured on auth: " . $e->getMessage() . " "; + echo implode(', ', $e->getErrors()); +} catch (\Iloveimg\Exceptions\UploadException $e) { + echo "An error occured on upload: " . $e->getMessage() . " "; + echo implode(', ', $e->getErrors()); +} catch (\Iloveimg\Exceptions\ProcessException $e) { + echo "An error occured on process: " . $e->getMessage() . " "; + echo implode(', ', $e->getErrors()); +} catch (\Exception $e) { + echo "An error occured: " . $e->getMessage(); +} \ No newline at end of file diff --git a/iloveimg-php/samples/watermark_advanced.php b/iloveimg-php/samples/watermark_advanced.php new file mode 100644 index 00000000..9134ce2b --- /dev/null +++ b/iloveimg-php/samples/watermark_advanced.php @@ -0,0 +1,58 @@ +addFile('/path/to/file/document.jpg'); + +$watermarkElement = $myTask->addElement(); + +// set the text +$watermarkElement->setText("watermark text"); + + +// set vertical position +$watermarkElement->setGravity("NorthWest"); + +// adjust vertical position +$watermarkElement->setWidthPercent(20); + +// adjust horizontal position +$watermarkElement->setHorizontalPositionAdjustment("100"); + +// set mode to text +$watermarkElement->setFontFamily("Arial"); + +// set mode to text +$watermarkElement->setFontStyle("Italic"); + +// set the font size +$watermarkElement->setFontSize("12"); + +// set color to red +$watermarkElement->setFontColor("#ff0000"); + +// set transparency +$watermarkElement->setTransparency("50"); + + +// and set name for output file. +// the task will set the correct file extension for you. +$myTask->setOutputFilename('watermarked'); + +// process files +$myTask->execute(); + +// and finally download the unlocked file. If no path is set, it will be downloaded on current folder +$myTask->download('path/to/download'); \ No newline at end of file diff --git a/iloveimg-php/samples/watermark_basic.php b/iloveimg-php/samples/watermark_basic.php new file mode 100644 index 00000000..51664248 --- /dev/null +++ b/iloveimg-php/samples/watermark_basic.php @@ -0,0 +1,24 @@ +addFile('/path/to/file/document.jpg'); + +$element = $myTask->addElement(); +$element->setText('watermark text'); + +// process files +$myTask->execute(); + +// and finally download the unlocked file. If no path is set, it will be downloaded on current folder +$myTask->download(); \ No newline at end of file diff --git a/iloveimg-php/samples/webhook_listen.php b/iloveimg-php/samples/webhook_listen.php new file mode 100644 index 00000000..45bcc9f4 --- /dev/null +++ b/iloveimg-php/samples/webhook_listen.php @@ -0,0 +1,19 @@ +setWorkerServer($_POST['server']); +$myTask->setTask($_POST['task']); + +//and download the file +$myTask->download(); \ No newline at end of file diff --git a/iloveimg-php/samples/webhook_send.php b/iloveimg-php/samples/webhook_send.php new file mode 100644 index 00000000..d1d968ba --- /dev/null +++ b/iloveimg-php/samples/webhook_send.php @@ -0,0 +1,23 @@ +addFile('/path/to/file/document.jpg'); + +//set the webhook that will recibe the notification once file is ready to download +$myTask->setWebhook('http://your_url.com'); + + +// We don't download here because with the webhook we ordered the files must be processed in background. +// Notification will be sent once it's ready diff --git a/iloveimg-php/src/Element.php b/iloveimg-php/src/Element.php index afc49d09..d631b1b7 100644 --- a/iloveimg-php/src/Element.php +++ b/iloveimg-php/src/Element.php @@ -98,6 +98,11 @@ class Element */ public $font_style = 'Regular'; + /** + * @var string + */ + public $font_weight = null; + /** * @var string */ @@ -141,7 +146,11 @@ class Element public $layer; + /** + * @var bool + */ public $bold = false; + /** * string * @var @@ -224,6 +233,15 @@ public function setFontStyle($font_style) return $this; } + /** + * @param string $font_weight + */ + public function setFontWeight($font_weight) + { + $this->font_weight = $font_weight; + return $this; + } + /** * @param int $font_size */ @@ -295,7 +313,7 @@ public function setVerticalPositionAdjustment($vertical_position_adjustment) /** * @param int $horizontal_position_adjustment */ - public function setHorizontalAdjustmentPercent($horizontal_adjustment_percent) + public function setHorizontalAdjustmentPercent($horizontal_adjustment_percent): Element { $this->horizontal_adjustment_percent = $horizontal_adjustment_percent; return $this; @@ -305,7 +323,7 @@ public function setHorizontalAdjustmentPercent($horizontal_adjustment_percent) * @param $gravity * @return $this */ - public function setGravity($gravity) + public function setGravity($gravity): Element { $this->checkValues($gravity, $this->gravityValues); @@ -317,7 +335,7 @@ public function setGravity($gravity) * @param int $width_percent * @return $this */ - public function setWidthPercent(int $width_percent) + public function setWidthPercent(int $width_percent): Element { $this->width_percent = $width_percent; return $this; @@ -332,8 +350,18 @@ public function setWidthPercent(int $width_percent) public function checkValues($value, $allowedValues) { if (!in_array($value, $allowedValues)) { - throw new \InvalidArgumentException('Invalid ' . $this->tool . ' value "' . $value . '". Must be one of: ' . implode(',', $allowedValues)); + throw new \InvalidArgumentException('Invalid value "' . $value . '". Must be one of: ' . implode(',', $allowedValues)); } } + /** + * @param bool $mosaic + * @return Element + */ + public function setMosaic(bool $mosaic): Element + { + $this->mosaic = $mosaic; + return $this; + } + } \ No newline at end of file diff --git a/iloveimg-php/src/Iloveimg.php b/iloveimg-php/src/Iloveimg.php index c8ab19f6..a4761798 100644 --- a/iloveimg-php/src/Iloveimg.php +++ b/iloveimg-php/src/Iloveimg.php @@ -34,7 +34,7 @@ class Iloveimg // @var string|null The version of the Iloveimg API to use for requests. public static $apiVersion = 'v1'; - const VERSION = 'wp.img.1.0.0'; + const VERSION = 'php.1.1.16'; public $token = null; @@ -187,6 +187,9 @@ public function sendRequest($method, $endpoint, $body = null, $start = false) throw new AuthException($response->body->name, $response->code, null, $response); } if ($endpoint == 'upload') { + if(is_string($response->body)){ + throw new UploadException("Upload error", $response->code, null, $response); + } throw new UploadException($response->body->error->message, $response->code, null, $response); } elseif ($endpoint == 'process') { throw new ProcessException($response->body->error->message, $response->code, null, $response); @@ -316,6 +319,15 @@ public function verifySsl($verify) Request::verifyHost($verify); } + + + /** + * @param $follow + */ + public function followLocation($follow){ + Request::followLocation($follow); + } + private function getUpdatedInfo() { $data = array('v' => self::VERSION); diff --git a/iloveimg-php/src/ImageTask.php b/iloveimg-php/src/ImageTask.php index cbc4fc86..548fc5af 100644 --- a/iloveimg-php/src/ImageTask.php +++ b/iloveimg-php/src/ImageTask.php @@ -179,6 +179,10 @@ public function addFileFromUrl($url) */ public function uploadFile($task, $filepath) { + if(!file_exists($filepath)){ + throw new \InvalidArgumentException('File '.$filepath.' does not exists'); + } + $data = array('task' => $task, 'v' => self::VERSION); $files = array('file' => $filepath); $body = Request\Body::multipart($data, $files); diff --git a/iloveimg-php/src/Request/Request.php b/iloveimg-php/src/Request/Request.php index 9a631ac9..6675e97b 100644 --- a/iloveimg-php/src/Request/Request.php +++ b/iloveimg-php/src/Request/Request.php @@ -13,8 +13,9 @@ class Request private static $socketTimeout = null; private static $verifyPeer = true; private static $verifyHost = true; + private static $followLocation = true; - private static $auth = array ( + private static $auth = array( 'user' => '', 'pass' => '', 'method' => CURLAUTH_BASIC @@ -25,7 +26,7 @@ class Request 'tunnel' => false, 'address' => false, 'type' => CURLPROXY_HTTP, - 'auth' => array ( + 'auth' => array( 'user' => '', 'pass' => '', 'method' => CURLAUTH_BASIC @@ -67,6 +68,17 @@ public static function verifyHost($enabled) return self::$verifyHost = $enabled; } + /** + * Follow location option + * + * @param bool $enabled enable follow location, by default is true + * @return bool + */ + public static function followLocation($enabled) + { + return self::$followLocation = $enabled; + } + /** * Set a timeout * @@ -302,25 +314,25 @@ public static function buildHTTPCurlQuery($data, $parent = false) /** * Send a cURL request - * @param \Iloveimg\Method|string $method HTTP method to use + * @param \Ilovepdf\Method|string $method HTTP method to use * @param string $url URL to send the request to * @param mixed $body request body * @param array $headers additional headers to send * @param string $username Authentication username (deprecated) * @param string $password Authentication password (deprecated) - * @throws \Iloveimg\Exception if a cURL error occurs * @return Response + * @throws \Ilovepdf\Exception if a cURL error occurs */ public static function send($method, $url, $body = null, $headers = array(), $username = null, $password = null) { self::$handle = curl_init(); if ($method !== Method::GET) { - if ($method === Method::POST) { - curl_setopt(self::$handle, CURLOPT_POST, true); - } else { - curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method); - } + if ($method === Method::POST) { + curl_setopt(self::$handle, CURLOPT_POST, true); + } else { + curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, $method); + } curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body); } elseif (is_array($body)) { @@ -336,7 +348,6 @@ public static function send($method, $url, $body = null, $headers = array(), $us $curl_base_options = [ CURLOPT_URL => self::encodeUrl($url), CURLOPT_RETURNTRANSFER => true, - CURLOPT_FOLLOWLOCATION => true, CURLOPT_MAXREDIRS => 10, CURLOPT_HTTPHEADER => self::getFormattedHeaders($headers), CURLOPT_HEADER => true, @@ -349,6 +360,10 @@ public static function send($method, $url, $body = null, $headers = array(), $us curl_setopt_array(self::$handle, self::mergeCurlOptions($curl_base_options, self::$curlOpts)); + if (self::$followLocation == true) { + @curl_setopt(self::$handle, CURLOPT_FOLLOWLOCATION, true); + } + if (self::$socketTimeout !== null) { curl_setopt(self::$handle, CURLOPT_TIMEOUT, self::$socketTimeout); } @@ -372,38 +387,38 @@ public static function send($method, $url, $body = null, $headers = array(), $us if (!empty(self::$auth['user'])) { curl_setopt_array(self::$handle, array( - CURLOPT_HTTPAUTH => self::$auth['method'], - CURLOPT_USERPWD => self::$auth['user'] . ':' . self::$auth['pass'] + CURLOPT_HTTPAUTH => self::$auth['method'], + CURLOPT_USERPWD => self::$auth['user'] . ':' . self::$auth['pass'] )); } if (self::$proxy['address'] !== false) { curl_setopt_array(self::$handle, array( - CURLOPT_PROXYTYPE => self::$proxy['type'], - CURLOPT_PROXY => self::$proxy['address'], - CURLOPT_PROXYPORT => self::$proxy['port'], + CURLOPT_PROXYTYPE => self::$proxy['type'], + CURLOPT_PROXY => self::$proxy['address'], + CURLOPT_PROXYPORT => self::$proxy['port'], CURLOPT_HTTPPROXYTUNNEL => self::$proxy['tunnel'], - CURLOPT_PROXYAUTH => self::$proxy['auth']['method'], - CURLOPT_PROXYUSERPWD => self::$proxy['auth']['user'] . ':' . self::$proxy['auth']['pass'] + CURLOPT_PROXYAUTH => self::$proxy['auth']['method'], + CURLOPT_PROXYUSERPWD => self::$proxy['auth']['user'] . ':' . self::$proxy['auth']['pass'] )); } - $response = curl_exec(self::$handle); - $error = curl_error(self::$handle); - $info = self::getInfo(); + $response = curl_exec(self::$handle); + $error = curl_error(self::$handle); + $info = self::getInfo(); if ($error) { - if(strpos($error,'SSL certificate problem')){ - throw new \Exception($error.' Try using method verifySsl to false: "$iloveimg->verifySsl(false)"'); + if (strpos($error, 'SSL certificate problem')) { + throw new \Exception($error . ' Try using method verifySsl to false: "$ilovepdf->verifySsl(false)"'); } throw new \Exception($error); } // Split the full response in its headers and body $header_size = $info['header_size']; - $header = substr($response, 0, $header_size); - $body = substr($response, $header_size); - $httpCode = $info['http_code']; + $header = substr($response, 0, $header_size); + $body = substr($response, $header_size); + $httpCode = $info['http_code']; return new Response($httpCode, $body, $header, self::$jsonOpts); } @@ -428,7 +443,7 @@ public static function getFormattedHeaders($headers) { $formattedHeaders = array(); - $combinedHeaders = array_change_key_case(array_merge(self::$defaultHeaders, (array) $headers)); + $combinedHeaders = array_change_key_case(array_merge(self::$defaultHeaders, (array)$headers)); foreach ($combinedHeaders as $key => $val) { $formattedHeaders[] = self::getHeaderString($key, $val); @@ -458,7 +473,7 @@ private static function getArrayFromQuerystring($query) /** * Ensure that a URL is encoded and safe to use with cURL - * @param string $url URL to encode + * @param string $url URL to encode * @return string */ private static function encodeUrl($url) @@ -466,10 +481,10 @@ private static function encodeUrl($url) $url_parsed = parse_url($url); $scheme = $url_parsed['scheme'] . '://'; - $host = $url_parsed['host']; - $port = (isset($url_parsed['port']) ? $url_parsed['port'] : null); - $path = (isset($url_parsed['path']) ? $url_parsed['path'] : null); - $query = (isset($url_parsed['query']) ? $url_parsed['query'] : null); + $host = $url_parsed['host']; + $port = (isset($url_parsed['port']) ? $url_parsed['port'] : null); + $path = (isset($url_parsed['path']) ? $url_parsed['path'] : null); + $query = (isset($url_parsed['query']) ? $url_parsed['query'] : null); if ($query !== null) { $query = '?' . http_build_query(self::getArrayFromQuerystring($query)); diff --git a/iloveimg-php/tests/Iloveimg/FileTest.php b/iloveimg-php/tests/Iloveimg/FileTest.php new file mode 100644 index 00000000..0a230dde --- /dev/null +++ b/iloveimg-php/tests/Iloveimg/FileTest.php @@ -0,0 +1,61 @@ +getFileOptions(); + $this->assertEquals($options['server_filename'], $serverFilename); + $this->assertEquals($options['filename'], $filename); + } + + + /** + * @test + */ + public function testCanSetRotation(){ + $rotation = 90; + $file = new File('file_server', 'file_name'); + $file->setRotation($rotation); + $options = $file->getFileOptions(); + $this->assertEquals($options['rotate'], $rotation); + } + + /** + * @test + * @expectedException \InvalidArgumentException + */ + public function testSetRotationWithNotAllowedParamTrowsError(){ + $rotation = '900'; + $file = new File('file_server', 'file_name'); + $file->setRotation($rotation); + } + + /* + * @test + * @expectedException \InvalidArgumentException + */ + public function testEmptyServerFilenameTrowException(){ + $file = new File('', 'some_filename'); + $this->assertEquals($file->filename, 'some_filename'); + } + + /* + * @test + * @expectedException \InvalidArgumentException + */ + public function testEmptyFilenameTrowException(){ + $file = new File('server_filename', ''); + $this->assertEquals($file->server_filename, 'server_filename'); + } +} \ No newline at end of file diff --git a/iloveimg-php/tests/Iloveimg/IloveTest.php b/iloveimg-php/tests/Iloveimg/IloveTest.php new file mode 100755 index 00000000..6532c04f --- /dev/null +++ b/iloveimg-php/tests/Iloveimg/IloveTest.php @@ -0,0 +1,50 @@ +publicKey, $this->secretKey); + $iloveimgTest = new Iloveimg($this->publicKeyTest, $this->secretKeyTest); + + $this->assertEquals($iloveimg->getPublicKey(), $this->publicKey); + $this->assertEquals($iloveimgTest->getPublicKey(), $this->publicKeyTest); + } + + /** + * @test + */ + public function testIloveimgEmptyParams() + { + $iloveimg = new Iloveimg(); + $iloveimgTest = new Iloveimg(); + + $iloveimg->setApiKeys($this->publicKey, $this->secretKey); + $iloveimgTest->setApiKeys($this->publicKeyTest, $this->secretKeyTest); + + + $this->assertEquals($iloveimg->getPublicKey(), $this->publicKey); + $this->assertEquals($iloveimgTest->getPublicKey(), $this->publicKeyTest); + } +} diff --git a/iloveimg-php/tests/Iloveimg/IloveimgTest.php b/iloveimg-php/tests/Iloveimg/IloveimgTest.php new file mode 100644 index 00000000..e06464ef --- /dev/null +++ b/iloveimg-php/tests/Iloveimg/IloveimgTest.php @@ -0,0 +1,131 @@ +iloveimg = new Iloveimg(); + $this->iloveimg->setApiKeys($this->publicKey, $this->secretKey); + } + + /** + * @test + */ + public function testShouldHaveSecretKey() + { + $secretKey = $this->iloveimg->getSecretKey(); + $this->assertEquals($this->secretKey, $secretKey); + } + + /** + * @test + */ + public function testShouldHavePublictKey() + { + $publicKey = $this->iloveimg->getPublicKey(); + $this->assertEquals($this->publicKey, $publicKey); + } + + /** + * @test + */ + public function testCanSetApiKeys() + { + $public = "public"; + $secret = "private"; + $this->iloveimg->setApiKeys($public, $secret); + $this->assertEquals($public, $this->iloveimg->getPublicKey()); + $this->assertEquals($secret, $this->iloveimg->getSecretKey()); + } + + /** + * @test + */ + public function testCanGetJwt() + { + $jwt = $this->iloveimg->getJWT(); + $this->assertNotNull($jwt, "jwt should not be null"); + } + + /** + * @test + * @expectedException \Exception + */ + public function testEmptyTaskShouldThrowException() + { + $task = $this->iloveimg->newTask(""); + $this->assertNotNull($task); + } + + /** + * @test + * @expectedException \InvalidArgumentException + */ + public function testNotExistingTaskShouldThrowException() + { + $this->iloveimg->newTask("tralara"); + } + + /** + * @test + */ + public function testEncryptSetDefaultKey() + { + $this->iloveimg->setFileEncryption(true); + $this->assertNotNull($this->iloveimg->getEncrytKey()); + $this->assertEquals(strlen($this->iloveimg->getEncrytKey()), 32); + } + + + /** + * @test + */ + public function testCanSetEncrypt() + { + $key = '1234123412341234'; + $this->iloveimg->setFileEncryption(true, $key ); + $this->assertEquals($this->iloveimg->getEncrytKey(), $key); + } + + /** + * @test + */ + public function testUnsetEncryptRemovesKey() + { + $key = '1234123412341234'; + $this->iloveimg->setFileEncryption(true, $key ); + $this->iloveimg->setFileEncryption(false); + $this->assertNull($this->iloveimg->getEncrytKey()); + } + + + /** + * @test + * @dataProvider invalidKeys + * @expectedException \InvalidArgumentException + */ + public function testWrongEncryptKeyThrowsException($key) + { + $this->iloveimg->setFileEncryption(true, $key ); + } + + + public function invalidKeys() + { + return [ + ['1234'], + ['asdfqwe'], + ]; + } +} \ No newline at end of file diff --git a/iloveimg-php/tests/bootstrap.no_autoload.php b/iloveimg-php/tests/bootstrap.no_autoload.php new file mode 100755 index 00000000..4e9dcfc6 --- /dev/null +++ b/iloveimg-php/tests/bootstrap.no_autoload.php @@ -0,0 +1,3 @@ +