From 917d17fe30e4557cf2135122bb64a9ae63512051 Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Thu, 16 Mar 2017 20:23:03 +0300 Subject: [PATCH 01/10] fix: Keywords provider --- CHANGELOG.md | 5 +++++ src/Api/Contracts/PaginatedResponse.php | 2 +- src/Api/Providers/Core/Provider.php | 2 +- src/Api/Providers/Keywords.php | 2 +- src/Api/Response.php | 4 ++-- tests/Bot/ResponseTest.php | 2 +- 6 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0fd0724f..a15d95fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Change Log All notable changes to this project will be documented in this file. +## v5.2.5 - 2017-03-16 +### Fixed: + - Keywords provider fixed + - Response *getResponseData()* always returns array + ## v5.2.4 - 2017-03-16 ### Added: - Pinners *tried* pins. diff --git a/src/Api/Contracts/PaginatedResponse.php b/src/Api/Contracts/PaginatedResponse.php index 3c510a19..3deb9bac 100644 --- a/src/Api/Contracts/PaginatedResponse.php +++ b/src/Api/Contracts/PaginatedResponse.php @@ -17,7 +17,7 @@ public function getBookmarks(); public function hasBookmarks(); /** - * @return array|bool + * @return array */ public function getResponseData(); diff --git a/src/Api/Providers/Core/Provider.php b/src/Api/Providers/Core/Provider.php index 1542b414..c97f04b0 100644 --- a/src/Api/Providers/Core/Provider.php +++ b/src/Api/Providers/Core/Provider.php @@ -64,7 +64,7 @@ protected function post($requestOptions, $resourceUrl, $returnResponse = false) * * @param array $requestOptions * @param string $resourceUrl - * @return array|bool|Response + * @return array|Response */ protected function get(array $requestOptions = [], $resourceUrl = '') { diff --git a/src/Api/Providers/Keywords.php b/src/Api/Providers/Keywords.php index c0ce0a95..352472ee 100644 --- a/src/Api/Providers/Keywords.php +++ b/src/Api/Providers/Keywords.php @@ -20,7 +20,7 @@ public function recommendedFor($query) 'query' => $query, ]; - $result = $this->get($requestOptions, UrlBuilder::getSearchUrl()); + $result = $this->get($requestOptions, UrlBuilder::RESOURCE_SEARCH); return $this->getKeywordsFromRequest($result); } diff --git a/src/Api/Response.php b/src/Api/Response.php index 57351f93..97eb4772 100644 --- a/src/Api/Response.php +++ b/src/Api/Response.php @@ -57,12 +57,12 @@ public function fillFromJson($json) * Check if specified data exists in response. * * @param null $key - * @return array|bool + * @return array */ public function getResponseData($key = null) { if ($this->hasErrors()) { - return false; + return []; } return $this->parseResponseData($key); diff --git a/tests/Bot/ResponseTest.php b/tests/Bot/ResponseTest.php index d1628993..8923c3bf 100644 --- a/tests/Bot/ResponseTest.php +++ b/tests/Bot/ResponseTest.php @@ -37,7 +37,7 @@ public function it_should_return_false_on_error_response() $response = new Response(); $response->fill($this->createErrorApiResponse('some error')); - $this->assertFalse($response->getResponseData()); + $this->assertEmpty($response->getResponseData()); $lastError = $response->getLastError(); $this->assertEquals('some error', $lastError['message']); From da86de3b34b47379d5a4b12a1e0ecb754340d9fe Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Thu, 16 Mar 2017 20:56:55 +0300 Subject: [PATCH 02/10] fix: Response --- CHANGELOG.md | 6 +----- src/Api/Contracts/PaginatedResponse.php | 2 +- src/Api/Providers/Core/Provider.php | 2 +- src/Api/Response.php | 4 ++-- src/Helpers/Pagination.php | 21 ++++++++++++++++----- tests/Bot/ResponseTest.php | 2 +- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a15d95fa..21eb02af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,6 @@ # Change Log All notable changes to this project will be documented in this file. -## v5.2.5 - 2017-03-16 -### Fixed: - - Keywords provider fixed - - Response *getResponseData()* always returns array - ## v5.2.4 - 2017-03-16 ### Added: - Pinners *tried* pins. @@ -15,6 +10,7 @@ All notable changes to this project will be documented in this file. - Pins *activity()* and *tried()* methods always return Pagination object. - Boards *forUser* method returns detailed information for every board. If no boards are available it returns empty array. + - Pagination processes *getResponseData()* as array ## v5.2.3 - 2017-01-24 ### Fixed: diff --git a/src/Api/Contracts/PaginatedResponse.php b/src/Api/Contracts/PaginatedResponse.php index 3deb9bac..3c510a19 100644 --- a/src/Api/Contracts/PaginatedResponse.php +++ b/src/Api/Contracts/PaginatedResponse.php @@ -17,7 +17,7 @@ public function getBookmarks(); public function hasBookmarks(); /** - * @return array + * @return array|bool */ public function getResponseData(); diff --git a/src/Api/Providers/Core/Provider.php b/src/Api/Providers/Core/Provider.php index c97f04b0..1542b414 100644 --- a/src/Api/Providers/Core/Provider.php +++ b/src/Api/Providers/Core/Provider.php @@ -64,7 +64,7 @@ protected function post($requestOptions, $resourceUrl, $returnResponse = false) * * @param array $requestOptions * @param string $resourceUrl - * @return array|Response + * @return array|bool|Response */ protected function get(array $requestOptions = [], $resourceUrl = '') { diff --git a/src/Api/Response.php b/src/Api/Response.php index 97eb4772..57351f93 100644 --- a/src/Api/Response.php +++ b/src/Api/Response.php @@ -57,12 +57,12 @@ public function fillFromJson($json) * Check if specified data exists in response. * * @param null $key - * @return array + * @return array|bool */ public function getResponseData($key = null) { if ($this->hasErrors()) { - return []; + return false; } return $this->parseResponseData($key); diff --git a/src/Helpers/Pagination.php b/src/Helpers/Pagination.php index 0283a42d..cc93e5a2 100644 --- a/src/Helpers/Pagination.php +++ b/src/Helpers/Pagination.php @@ -5,7 +5,8 @@ use Traversable; use EmptyIterator; use IteratorAggregate; -use seregazhuk\PinterestBot\Api\Response; +use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse; + /** * Class Pagination @@ -135,15 +136,14 @@ protected function processCallback() { $this->resultsNum = 0; $this->processed = 0; - $callback = $this->callback; while (true) { - /** @var Response $response */ - $response = $callback(); + /** @var PaginatedResponse $response */ + $response = call_user_func($this->callback); if ($response->isEmpty()) return; - foreach ($response->getResponseData() as $result) { + foreach ($this->getResultsFromResponse($response) as $result) { $this->processed++; if ($this->processed > $this->offset) { @@ -157,4 +157,15 @@ protected function processCallback() if (!$response->hasBookmarks()) return; } } + + /** + * @param $response + * @return array + */ + protected function getResultsFromResponse(PaginatedResponse $response) + { + $results = $response->getResponseData(); + + return $results ? : []; + } } diff --git a/tests/Bot/ResponseTest.php b/tests/Bot/ResponseTest.php index 8923c3bf..d1628993 100644 --- a/tests/Bot/ResponseTest.php +++ b/tests/Bot/ResponseTest.php @@ -37,7 +37,7 @@ public function it_should_return_false_on_error_response() $response = new Response(); $response->fill($this->createErrorApiResponse('some error')); - $this->assertEmpty($response->getResponseData()); + $this->assertFalse($response->getResponseData()); $lastError = $response->getLastError(); $this->assertEquals('some error', $lastError['message']); From 8a98738176319df4831a3a4b1d72ea0fe7f61031 Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Thu, 16 Mar 2017 21:04:49 +0300 Subject: [PATCH 03/10] fix: Pagination --- src/Helpers/Pagination.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helpers/Pagination.php b/src/Helpers/Pagination.php index cc93e5a2..aa68edc0 100644 --- a/src/Helpers/Pagination.php +++ b/src/Helpers/Pagination.php @@ -166,6 +166,6 @@ protected function getResultsFromResponse(PaginatedResponse $response) { $results = $response->getResponseData(); - return $results ? : []; + return is_array($results) ? : []; } } From 2e74ddcdee145f1637ab79bef4e13ed80f3f9464 Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Thu, 16 Mar 2017 21:08:22 +0300 Subject: [PATCH 04/10] fix: Pagination --- src/Helpers/Pagination.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helpers/Pagination.php b/src/Helpers/Pagination.php index aa68edc0..1a4aab57 100644 --- a/src/Helpers/Pagination.php +++ b/src/Helpers/Pagination.php @@ -166,6 +166,6 @@ protected function getResultsFromResponse(PaginatedResponse $response) { $results = $response->getResponseData(); - return is_array($results) ? : []; + return $results === false ? [] : $results; } } From 131fa56f2b9ca5f46466e08fa6d3e997249a5c89 Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Sat, 18 Mar 2017 09:32:55 +0300 Subject: [PATCH 05/10] fix: Pagination fails on last response --- CHANGELOG.md | 4 ++++ src/Api/Providers/Core/Provider.php | 9 ++++----- src/Api/Traits/HandlesRequest.php | 3 ++- tests/Bot/PaginationTest.php | 12 ++++++++++++ tests/Bot/ProviderTest.php | 12 +----------- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21eb02af..bbb81249 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. +## v5.2.5 - 2017-03-18 +### Fixed: + - Pagination fails on last response. + ## v5.2.4 - 2017-03-16 ### Added: - Pinners *tried* pins. diff --git a/src/Api/Providers/Core/Provider.php b/src/Api/Providers/Core/Provider.php index 1542b414..12f8d1fb 100644 --- a/src/Api/Providers/Core/Provider.php +++ b/src/Api/Providers/Core/Provider.php @@ -64,9 +64,10 @@ protected function post($requestOptions, $resourceUrl, $returnResponse = false) * * @param array $requestOptions * @param string $resourceUrl + * @param bool $returnResponse * @return array|bool|Response */ - protected function get(array $requestOptions = [], $resourceUrl = '') + protected function get(array $requestOptions = [], $resourceUrl = '', $returnResponse = false) { $query = Request::createQuery( $requestOptions, @@ -75,9 +76,7 @@ protected function get(array $requestOptions = [], $resourceUrl = '') $this->execute($resourceUrl . '?' . $query); - return $this->response->hasBookmarks() ? - $this->response : - $this->response->getResponseData(); + return $returnResponse ? $this->response : $this->response->getResponseData(); } @@ -123,7 +122,7 @@ public function isLoggedIn() protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LIMIT) { return $this->paginateCustom(function() use ($data, $resourceUrl) { - return $this->get($data, $resourceUrl); + return $this->get($data, $resourceUrl, true); })->take($limit); } diff --git a/src/Api/Traits/HandlesRequest.php b/src/Api/Traits/HandlesRequest.php index 93747977..e64a920d 100644 --- a/src/Api/Traits/HandlesRequest.php +++ b/src/Api/Traits/HandlesRequest.php @@ -22,7 +22,8 @@ abstract protected function post($requestOptions, $resourceUrl, $returnResponse * * @param array $requestOptions * @param string $resourceUrl + * @param bool $returnResponse * @return array|bool|Response */ - abstract protected function get(array $requestOptions = [], $resourceUrl = ''); + abstract protected function get(array $requestOptions = [], $resourceUrl = '', $returnResponse = false); } \ No newline at end of file diff --git a/tests/Bot/PaginationTest.php b/tests/Bot/PaginationTest.php index c63ab854..33f5405c 100644 --- a/tests/Bot/PaginationTest.php +++ b/tests/Bot/PaginationTest.php @@ -2,6 +2,7 @@ namespace seregazhuk\tests\Bot; +use Mockery; use PHPUnit_Framework_TestCase; use seregazhuk\PinterestBot\Api\Response; use seregazhuk\tests\Helpers\ResponseHelper; @@ -119,6 +120,17 @@ public function it_accepts_limit_in_constructor() $this->assertEquals($expected, $pagination->toArray()); } + + /** @test */ + public function it_stops_when_response_is_empty() + { + $pagination = new Pagination(); + $pagination->paginateOver(function(){ + return (new Response())->fill([]); + }); + + $this->assertCount(0, $pagination->toArray()); + } } diff --git a/tests/Bot/ProviderTest.php b/tests/Bot/ProviderTest.php index e771608c..9916accd 100644 --- a/tests/Bot/ProviderTest.php +++ b/tests/Bot/ProviderTest.php @@ -20,7 +20,7 @@ protected function tearDown() } /** @test */ - public function it_returns_data_for_response_without_pagination() + public function it_returns_data_for_response() { $response = ['resource_response' => ['data' => 'value']]; @@ -31,16 +31,6 @@ public function it_returns_data_for_response_without_pagination() $this->assertEquals($responseData, $provider->visitPage()); } - /** @test */ - public function it_returns_response_object_if_it_has_pagination() - { - $response = ['resource' => ['options' => ['bookmarks' => 'bookmarks_string']]]; - - $provider = $this->makeProvider($response); - - $this->assertInstanceOf(Response::class, $provider->visitPage()); - } - /** * @param $response * @return Mockery\Mock|Provider From 434bdaff168378a2fee8394bd2d3ff4a31c978c0 Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Sat, 18 Mar 2017 10:24:03 +0300 Subject: [PATCH 06/10] add: Provider tests --- src/Api/Providers/Core/Provider.php | 2 + src/Helpers/Pagination.php | 2 +- tests/Bot/PaginationTest.php | 2 +- tests/Bot/ProviderTest.php | 75 ++++++++++++++++++++++++++--- tests/Helpers/ResponseHelper.php | 10 ++-- 5 files changed, 78 insertions(+), 13 deletions(-) diff --git a/src/Api/Providers/Core/Provider.php b/src/Api/Providers/Core/Provider.php index 12f8d1fb..f9fb23ad 100644 --- a/src/Api/Providers/Core/Provider.php +++ b/src/Api/Providers/Core/Provider.php @@ -127,6 +127,8 @@ protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LI } /** + * Accepts callback which should return PaginatedResponse object. + * * @param callable $callback * @param int $limit * @return Pagination diff --git a/src/Helpers/Pagination.php b/src/Helpers/Pagination.php index 1a4aab57..eb189b6f 100644 --- a/src/Helpers/Pagination.php +++ b/src/Helpers/Pagination.php @@ -54,7 +54,7 @@ public function __construct($limit = self::DEFAULT_LIMIT) } /** - * Sets a callback to make requests. Should be a closure. + * Sets a callback to make requests. Callback should return PaginatedResponse object. * * @param callable $callback * @return $this diff --git a/tests/Bot/PaginationTest.php b/tests/Bot/PaginationTest.php index 33f5405c..6c4b3e0f 100644 --- a/tests/Bot/PaginationTest.php +++ b/tests/Bot/PaginationTest.php @@ -19,7 +19,7 @@ class PaginationTest extends PHPUnit_Framework_TestCase public function it_returns_first_result_when_no_bookmarks() { $pagination = new Pagination(); - $responseData = $this->createPaginatedResponse($this->paginatedResponse); + $responseData = $this->createSuccessApiResponse($this->paginatedResponse); $pagination->paginateOver(function() use ($responseData){ return (new Response())->fill($responseData); diff --git a/tests/Bot/ProviderTest.php b/tests/Bot/ProviderTest.php index 9916accd..855f73a4 100644 --- a/tests/Bot/ProviderTest.php +++ b/tests/Bot/ProviderTest.php @@ -6,6 +6,7 @@ use PHPUnit_Framework_TestCase; use seregazhuk\PinterestBot\Api\Request; use seregazhuk\PinterestBot\Api\Response; +use seregazhuk\tests\Helpers\ResponseHelper; use seregazhuk\PinterestBot\Api\Providers\Core\Provider; /** @@ -13,6 +14,7 @@ */ class ProviderTest extends PHPUnit_Framework_TestCase { + use ResponseHelper; protected function tearDown() { @@ -31,18 +33,77 @@ public function it_returns_data_for_response() $this->assertEquals($responseData, $provider->visitPage()); } + /** @test */ + public function it_should_return_response_object_for_pagination() + { + $paginatedResponse = $this->createPaginatedResponse($this->paginatedResponse); + + $request = $this->makeRequest($paginatedResponse, 2); + $request + ->shouldReceive('exec') + ->once() + ->andReturn(json_encode([])); + + /** @var DummyProvider $provider */ + $provider = Mockery::mock(DummyProvider::class, [$request, new Response()]) + ->makePartial(); + + $provider->dummyPaginate(['test' => 'test'], 'http://example.com')->toArray(); + } + + /** @test */ + public function it_should_clear_response_before_pagination() + { + $response = Mockery::mock(Response::class) + ->shouldReceive('clear') + ->once() + ->getMock() + ->makePartial(); + + /** @var DummyProvider $provider */ + $provider = Mockery::mock(DummyProvider::class, [$this->makeRequest([]), $response]) + ->makePartial(); + + $provider->dummyPaginate(['test' => 'test'], 'http://example.com')->toArray(); + } + + /** + * @param mixed $response + * @param int $times + * @return Mockery\Mock|Provider|DummyProvider + */ + protected function makeProvider($response, $times = 1) + { + $request = $this->makeRequest($response, $times); + + return Mockery::mock(DummyProvider::class, [$request, new Response()]) + ->makePartial(); + } + /** - * @param $response - * @return Mockery\Mock|Provider + * @param mixed $response + * @param int $times + * @return Mockery\MockInterface */ - protected function makeProvider($response) + protected function makeRequest($response, $times = 1) { - $request = Mockery::mock(Request::class) + return Mockery::mock(Request::class) ->shouldReceive('exec') + ->times($times) ->andReturn(json_encode($response)) ->getMock(); - - return Mockery::mock(Provider::class, [$request, new Response()]) - ->makePartial(); } } + +class DummyProvider extends Provider { + + /** + * @param mixed $data + * @param string $resourceUrl + * @return \seregazhuk\PinterestBot\Helpers\Pagination + */ + public function dummyPaginate($data, $resourceUrl) + { + return $this->paginate($data, $resourceUrl); + } +} \ No newline at end of file diff --git a/tests/Helpers/ResponseHelper.php b/tests/Helpers/ResponseHelper.php index 0779e0b9..d4601e7a 100644 --- a/tests/Helpers/ResponseHelper.php +++ b/tests/Helpers/ResponseHelper.php @@ -2,8 +2,6 @@ namespace seregazhuk\tests\Helpers; -use seregazhuk\PinterestBot\Helpers\Pagination; - /** * Class ResponseHelper. * @@ -34,11 +32,12 @@ public function createApiResponse($data = []) /** * Create a success dummy response. * + * @param mixed $data * @return array */ - public function createSuccessApiResponse() + public function createSuccessApiResponse($data = 'success') { - return $this->createApiResponse(['data' => 'success']); + return $this->createApiResponse(['data' => $data]); } /** @@ -70,6 +69,9 @@ public function createPaginatedResponse($response) 'resource_response' => [ 'data' => $response, ], + 'resource' => [ + 'options' => ['bookmarks' => 'my_bookrmarks'] + ] ]; } } From ff805c1a5091454bdca590379c0d896682f51abc Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Sat, 18 Mar 2017 10:50:34 +0300 Subject: [PATCH 07/10] add: Provider tests --- tests/Bot/ProviderTest.php | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/Bot/ProviderTest.php b/tests/Bot/ProviderTest.php index 855f73a4..6f774a5d 100644 --- a/tests/Bot/ProviderTest.php +++ b/tests/Bot/ProviderTest.php @@ -67,6 +67,32 @@ public function it_should_clear_response_before_pagination() $provider->dummyPaginate(['test' => 'test'], 'http://example.com')->toArray(); } + /** @test */ + public function it_should_return_response_object_if_required_for_get_request() + { + $provider = $this->makeProvider([]); + + $this->assertInstanceOf(Response::class, $provider->dummyGet(true)); + } + + /** @test */ + public function it_should_return_response_object_if_required_for_post_request() + { + $provider = $this->makeProvider([]); + + $this->assertInstanceOf(Response::class, $provider->dummyPost(true)); + } + + /** @test */ + public function it_should_return_bool_if_required_for_post_request() + { + $response = ['resource_response' => ['data' => 'value']]; + + $provider = $this->makeProvider($response); + + $this->assertTrue($provider->dummyPost(false)); + } + /** * @param mixed $response * @param int $times @@ -106,4 +132,22 @@ public function dummyPaginate($data, $resourceUrl) { return $this->paginate($data, $resourceUrl); } + + /** + * @param bool $returnResponse + * @return array|bool|Response + */ + public function dummyGet($returnResponse) + { + return $this->get([], '', $returnResponse); + } + + /** + * @param bool $returnResponse + * @return array|bool|Response + */ + public function dummyPost($returnResponse) + { + return $this->post([], '', $returnResponse); + } } \ No newline at end of file From 9868b17cfc8472937f5ff01863bc569e0c39496f Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Sat, 18 Mar 2017 11:09:57 +0300 Subject: [PATCH 08/10] upd: test coverage config --- phpunit.xml | 18 ++++++++++-------- src/Api/Providers/Core/Provider.php | 3 ++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 79682783..2409b6b5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -19,14 +19,16 @@ ./src/ - /src/Api/Providers/Topics.php - /src/Api/Providers/Boards.php - /src/Api/Providers/Comments.php - /src/Api/Providers/Inbox.php - /src/Api/Providers/Pins.php - /src/Api/Providers/Pinners.php - /src/Api/Providers/Interests.php - /src/Api/Providers/User.php + ./src/Api/Providers/Core/EntityProvider.php + ./src/Api/Providers/Topics.php + ./src/Api/Providers/Boards.php + ./src/Api/Providers/Comments.php + ./src/Api/Providers/Inbox.php + ./src/Api/Providers/Pins.php + ./src/Api/Providers/Pinners.php + ./src/Api/Providers/Password.php + ./src/Api/Providers/Interests.php + ./src/Api/Providers/User.php diff --git a/src/Api/Providers/Core/Provider.php b/src/Api/Providers/Core/Provider.php index f9fb23ad..4a8e3b3d 100644 --- a/src/Api/Providers/Core/Provider.php +++ b/src/Api/Providers/Core/Provider.php @@ -121,7 +121,8 @@ public function isLoggedIn() */ protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LIMIT) { - return $this->paginateCustom(function() use ($data, $resourceUrl) { + return $this + ->paginateCustom(function() use ($data, $resourceUrl) { return $this->get($data, $resourceUrl, true); })->take($limit); } From dde8519e9b156ed0f1d97b6f2f690e763cb7e45e Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Sat, 18 Mar 2017 11:21:05 +0300 Subject: [PATCH 09/10] upd: Provider refactoring --- phpunit.xml | 2 ++ src/Api/Providers/Auth.php | 4 ++-- src/Api/Providers/Core/Provider.php | 14 +++++++------- src/Api/Providers/Password.php | 2 +- src/Api/Providers/Pins.php | 12 ++++++------ src/Api/Traits/HandlesRequest.php | 6 ++---- tests/Bot/ProviderTest.php | 27 +++++---------------------- 7 files changed, 25 insertions(+), 42 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 2409b6b5..ebd811c6 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -29,6 +29,8 @@ ./src/Api/Providers/Password.php ./src/Api/Providers/Interests.php ./src/Api/Providers/User.php + ./src/Api/Providers/Auth.php + ./src/Api/Providers/Keywords.php diff --git a/src/Api/Providers/Auth.php b/src/Api/Providers/Auth.php index b47e51e9..12665999 100644 --- a/src/Api/Providers/Auth.php +++ b/src/Api/Providers/Auth.php @@ -176,9 +176,9 @@ protected function processLogin($username, $password) 'password' => $password, ]; - $response = $this->post($credentials, UrlBuilder::RESOURCE_LOGIN, true); + $this->post($credentials, UrlBuilder::RESOURCE_LOGIN); - if ($response->hasErrors()) return false; + if ($this->response->hasErrors()) return false; $this->request->login(); diff --git a/src/Api/Providers/Core/Provider.php b/src/Api/Providers/Core/Provider.php index 4a8e3b3d..edb6771d 100644 --- a/src/Api/Providers/Core/Provider.php +++ b/src/Api/Providers/Core/Provider.php @@ -45,17 +45,16 @@ public function __construct(Request $request, Response $response) * * @param array $requestOptions * @param string $resourceUrl - * @param bool $returnResponse * * @return Response|bool */ - protected function post($requestOptions, $resourceUrl, $returnResponse = false) + protected function post($requestOptions, $resourceUrl) { $postString = Request::createQuery($requestOptions); $this->execute($resourceUrl, $postString); - return $returnResponse ? $this->response : $this->response->isOk(); + return $this->response->isOk(); } @@ -64,10 +63,9 @@ protected function post($requestOptions, $resourceUrl, $returnResponse = false) * * @param array $requestOptions * @param string $resourceUrl - * @param bool $returnResponse * @return array|bool|Response */ - protected function get(array $requestOptions = [], $resourceUrl = '', $returnResponse = false) + protected function get(array $requestOptions = [], $resourceUrl = '') { $query = Request::createQuery( $requestOptions, @@ -76,7 +74,7 @@ protected function get(array $requestOptions = [], $resourceUrl = '', $returnRes $this->execute($resourceUrl . '?' . $query); - return $returnResponse ? $this->response : $this->response->getResponseData(); + return $this->response->getResponseData(); } @@ -123,7 +121,9 @@ protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LI { return $this ->paginateCustom(function() use ($data, $resourceUrl) { - return $this->get($data, $resourceUrl, true); + $this->get($data, $resourceUrl); + + return $this->response; })->take($limit); } diff --git a/src/Api/Providers/Password.php b/src/Api/Providers/Password.php index 2f773b44..08045cb3 100644 --- a/src/Api/Providers/Password.php +++ b/src/Api/Providers/Password.php @@ -54,7 +54,7 @@ public function reset($link, $newPassword) 'expiration' => $query['e'], ]; - return $this->post($request, UrlBuilder::RESOURCE_RESET_PASSWORD_UPDATE, true); + return $this->post($request, UrlBuilder::RESOURCE_RESET_PASSWORD_UPDATE); } /** diff --git a/src/Api/Providers/Pins.php b/src/Api/Providers/Pins.php index fa1925e9..897d8ba1 100644 --- a/src/Api/Providers/Pins.php +++ b/src/Api/Providers/Pins.php @@ -84,9 +84,9 @@ public function create($imageUrl, $boardId, $description = '', $link = '') 'board_id' => $boardId, ]; - return $this - ->post($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN, true) - ->getResponseData(); + $this->post($requestOptions, UrlBuilder::RESOURCE_CREATE_PIN); + + return $this->response->getResponseData(); } /** @@ -140,9 +140,9 @@ public function repin($repinId, $boardId, $description = '') 'pin_id' => $repinId, ]; - return $this - ->post($requestOptions, UrlBuilder::RESOURCE_REPIN, true) - ->getResponseData(); + $this->post($requestOptions, UrlBuilder::RESOURCE_REPIN); + + return $this->response->getResponseData(); } /** diff --git a/src/Api/Traits/HandlesRequest.php b/src/Api/Traits/HandlesRequest.php index e64a920d..5cdecaee 100644 --- a/src/Api/Traits/HandlesRequest.php +++ b/src/Api/Traits/HandlesRequest.php @@ -11,19 +11,17 @@ trait HandlesRequest * * @param array $requestOptions * @param string $resourceUrl - * @param bool $returnResponse * * @return Response|bool */ - abstract protected function post($requestOptions, $resourceUrl, $returnResponse = false); + abstract protected function post($requestOptions, $resourceUrl); /** * Executes a GET request to Pinterest API. * * @param array $requestOptions * @param string $resourceUrl - * @param bool $returnResponse * @return array|bool|Response */ - abstract protected function get(array $requestOptions = [], $resourceUrl = '', $returnResponse = false); + abstract protected function get(array $requestOptions = [], $resourceUrl = ''); } \ No newline at end of file diff --git a/tests/Bot/ProviderTest.php b/tests/Bot/ProviderTest.php index 6f774a5d..ed2483bf 100644 --- a/tests/Bot/ProviderTest.php +++ b/tests/Bot/ProviderTest.php @@ -67,21 +67,6 @@ public function it_should_clear_response_before_pagination() $provider->dummyPaginate(['test' => 'test'], 'http://example.com')->toArray(); } - /** @test */ - public function it_should_return_response_object_if_required_for_get_request() - { - $provider = $this->makeProvider([]); - - $this->assertInstanceOf(Response::class, $provider->dummyGet(true)); - } - - /** @test */ - public function it_should_return_response_object_if_required_for_post_request() - { - $provider = $this->makeProvider([]); - - $this->assertInstanceOf(Response::class, $provider->dummyPost(true)); - } /** @test */ public function it_should_return_bool_if_required_for_post_request() @@ -90,7 +75,7 @@ public function it_should_return_bool_if_required_for_post_request() $provider = $this->makeProvider($response); - $this->assertTrue($provider->dummyPost(false)); + $this->assertTrue($provider->dummyPost()); } /** @@ -134,20 +119,18 @@ public function dummyPaginate($data, $resourceUrl) } /** - * @param bool $returnResponse * @return array|bool|Response */ - public function dummyGet($returnResponse) + public function dummyGet() { - return $this->get([], '', $returnResponse); + return $this->get([], ''); } /** - * @param bool $returnResponse * @return array|bool|Response */ - public function dummyPost($returnResponse) + public function dummyPost() { - return $this->post([], '', $returnResponse); + return $this->post([], ''); } } \ No newline at end of file From 87868c53542d700c23a45ee50dbcd3e2d2f1da1c Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Sat, 18 Mar 2017 11:23:56 +0300 Subject: [PATCH 10/10] upd: code stye --- src/Api/Providers/Core/Provider.php | 2 +- src/Api/Traits/HandlesRequest.php | 2 +- src/Api/Traits/Searchable.php | 2 +- tests/Bot/ProviderTest.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Api/Providers/Core/Provider.php b/src/Api/Providers/Core/Provider.php index edb6771d..0f78c54d 100644 --- a/src/Api/Providers/Core/Provider.php +++ b/src/Api/Providers/Core/Provider.php @@ -120,7 +120,7 @@ public function isLoggedIn() protected function paginate($data, $resourceUrl, $limit = Pagination::DEFAULT_LIMIT) { return $this - ->paginateCustom(function() use ($data, $resourceUrl) { + ->paginateCustom(function () use ($data, $resourceUrl) { $this->get($data, $resourceUrl); return $this->response; diff --git a/src/Api/Traits/HandlesRequest.php b/src/Api/Traits/HandlesRequest.php index 5cdecaee..4f282652 100644 --- a/src/Api/Traits/HandlesRequest.php +++ b/src/Api/Traits/HandlesRequest.php @@ -24,4 +24,4 @@ abstract protected function post($requestOptions, $resourceUrl); * @return array|bool|Response */ abstract protected function get(array $requestOptions = [], $resourceUrl = ''); -} \ No newline at end of file +} diff --git a/src/Api/Traits/Searchable.php b/src/Api/Traits/Searchable.php index 230841c5..e6c93a9b 100644 --- a/src/Api/Traits/Searchable.php +++ b/src/Api/Traits/Searchable.php @@ -62,7 +62,7 @@ protected function execSearchRequest($query, $scope) */ public function search($query, $limit = Pagination::DEFAULT_LIMIT) { - return $this->paginateCustom(function() use ($query) { + return $this->paginateCustom(function () use ($query) { return $this->execSearchRequest($query, $this->getSearchScope()); })->take($limit); } diff --git a/tests/Bot/ProviderTest.php b/tests/Bot/ProviderTest.php index ed2483bf..f3f7d559 100644 --- a/tests/Bot/ProviderTest.php +++ b/tests/Bot/ProviderTest.php @@ -133,4 +133,4 @@ public function dummyPost() { return $this->post([], ''); } -} \ No newline at end of file +}