From c944ed459dbe56b694c16210723feca3fad466e6 Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Sun, 18 Sep 2016 20:25:16 +0300 Subject: [PATCH 1/3] fix: upload --- src/Api/Request.php | 2 +- src/Api/Response.php | 9 +++++++++ src/Api/Traits/Searchable.php | 7 +++++++ src/Api/Traits/UploadsImages.php | 13 +++++++++++-- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Api/Request.php b/src/Api/Request.php index b51b2f3f..1ac4cc72 100644 --- a/src/Api/Request.php +++ b/src/Api/Request.php @@ -89,7 +89,7 @@ public function upload($pathToFile, $url) * @param string $resourceUrl * @param string $postString * - * @return Response + * @return string */ public function exec($resourceUrl, $postString = '') { diff --git a/src/Api/Response.php b/src/Api/Response.php index 4a73fe44..e899cc71 100644 --- a/src/Api/Response.php +++ b/src/Api/Response.php @@ -56,6 +56,15 @@ public function getData($key = '', $default = null) return $this->getValueByKey($key, $this->data, $default); } + /** + * @param string $key + * @return bool + */ + public function hasData($key = '') + { + return !is_null($this->getValueByKey($key, $this->data)); + } + /** * Parse data from Pinterest Api response. * Data is stored in ['resource_response']['data'] array. diff --git a/src/Api/Traits/Searchable.php b/src/Api/Traits/Searchable.php index d62e85bd..1eec3ce4 100644 --- a/src/Api/Traits/Searchable.php +++ b/src/Api/Traits/Searchable.php @@ -3,6 +3,7 @@ namespace seregazhuk\PinterestBot\Api\Traits; use seregazhuk\PinterestBot\Api\Request; +use seregazhuk\PinterestBot\Api\Response; use seregazhuk\PinterestBot\Helpers\UrlBuilder; use seregazhuk\PinterestBot\Api\SearchResponse; @@ -124,4 +125,10 @@ protected function appendBookMarks($bookmarks, $options) * @return mixed */ abstract protected function getPaginatedResponse(array $params, $limit, $method = 'getPaginatedData'); + + /** + * @param string $res + * @return Response + */ + abstract protected function processResult($res); } diff --git a/src/Api/Traits/UploadsImages.php b/src/Api/Traits/UploadsImages.php index d1c0d081..11f814f7 100644 --- a/src/Api/Traits/UploadsImages.php +++ b/src/Api/Traits/UploadsImages.php @@ -2,12 +2,17 @@ namespace seregazhuk\PinterestBot\Api\Traits; +use seregazhuk\PinterestBot\Api\Request; +use seregazhuk\PinterestBot\Api\Response; use seregazhuk\PinterestBot\Helpers\UrlBuilder; use seregazhuk\PinterestBot\Exceptions\InvalidRequest; /** * Trait UploadsImages * @package seregazhuk\PinterestBot\Api\Traits + * + * @property Request $request + * @property Response $response */ trait UploadsImages { @@ -20,9 +25,13 @@ trait UploadsImages */ public function upload($image) { - $res = $this->request + $result = $this->request ->upload($image, UrlBuilder::IMAGE_UPLOAD); - return $res['success'] ? $res['image_url'] : null; + $this->processResult($result); + + return $this->response->hasData('success') ? + $this->response->getData('image_url') : + null; } } \ No newline at end of file From 9484e7c05a4bcc883d24c3345b0baa24589ff428 Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Sun, 18 Sep 2016 20:31:44 +0300 Subject: [PATCH 2/3] fix: upload tests --- tests/Api/PinsTest.php | 6 +++++- tests/Api/UserTest.php | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/Api/PinsTest.php b/tests/Api/PinsTest.php index 4b4c31f3..2e29928a 100644 --- a/tests/Api/PinsTest.php +++ b/tests/Api/PinsTest.php @@ -81,7 +81,11 @@ public function it_should_upload_images_when_creating_pin_with_local_image() $image = 'image.jpg'; $this->request ->shouldReceive('upload') - ->withArgs([$image, UrlBuilder::IMAGE_UPLOAD]); + ->withArgs([$image, UrlBuilder::IMAGE_UPLOAD]) + ->andReturn(json_encode([ + 'success' => true, + 'image_url' => 'http://example.com/example.jpg' + ])); $response = $this->createPinCreationResponse(); $this->setResponseExpectation($response); diff --git a/tests/Api/UserTest.php b/tests/Api/UserTest.php index 2a0ab2e7..7fb48598 100644 --- a/tests/Api/UserTest.php +++ b/tests/Api/UserTest.php @@ -50,7 +50,11 @@ public function it_should_upload_image_when_editing_profile_with_local_image() ]; $this->request ->shouldReceive('upload') - ->withArgs([$attributes['profile_image'], UrlBuilder::IMAGE_UPLOAD]); + ->withArgs([$attributes['profile_image'], UrlBuilder::IMAGE_UPLOAD]) + ->andReturn(json_encode([ + 'success' => true, + 'image_url' => 'http://example.com/example.jpg' + ])); $this->setSuccessResponse(); $this->assertTrue($this->provider->profile($attributes)); From a56cdfc973a5d4169a98b8d52bd237614e4009ce Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Sun, 18 Sep 2016 20:32:27 +0300 Subject: [PATCH 3/3] fix: upload tests --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 204d3260..610b6cf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Change Log All notable changes to this project will be documented in this file. +## [4.11.2] - 2016-09-18 +### Fixed: + - Images upload + ## [4.11.1] - 2016-09-18 ### Added: - Bot *getClientInfo* method.