diff --git a/CHANGELOG.md b/CHANGELOG.md index bbb81249..cbb25de0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ # Change Log All notable changes to this project will be documented in this file. +## v5.2.6 - 2017-03-25 +### Added: + - HttpClient *dontUseProxy()* method to stop sending requests via proxy + +### Fixed: + - Additional info added in pins *related()* method. + - Auth *login()* method always returns true, if proxy doesn't work + ## v5.2.5 - 2017-03-18 ### Fixed: - Pagination fails on last response. diff --git a/README.md b/README.md index 6c099d34..cf47ca45 100644 --- a/README.md +++ b/README.md @@ -817,6 +817,11 @@ $bot->getHttpClient()->useSocksProxy('192.168.1.1', '12345'); $bot->getHttpClient()->useSocksProxy('192.168.1.1', '12345', 'username:password'); ``` +If you need to stop sending requests via proxy: +```php +$bot->getHttpClient()->dontUseProxy(); +``` + ## Custom request settings It is possible to add some additional Curl options for bot requests. For example you can diff --git a/src/Api/Contracts/HttpClient.php b/src/Api/Contracts/HttpClient.php index 92e6d448..9866aeb4 100644 --- a/src/Api/Contracts/HttpClient.php +++ b/src/Api/Contracts/HttpClient.php @@ -82,4 +82,9 @@ public function useProxy($host, $port, $auth = null, $type = null); * @return HttpClient */ public function useSocksProxy($host, $port, $auth = null); + + /** + * @return HttpClient + */ + public function dontUseProxy(); } diff --git a/src/Api/CurlHttpClient.php b/src/Api/CurlHttpClient.php index 1369ec17..fdaf000b 100644 --- a/src/Api/CurlHttpClient.php +++ b/src/Api/CurlHttpClient.php @@ -227,6 +227,8 @@ public function removeCookies() unlink($this->cookieJar); } + $this->cookieJar = null; + return $this; } @@ -316,6 +318,16 @@ public function useProxy($host, $port, $auth = null, $type = null) return $this->setOptions($proxy); } + public function dontUseProxy() + { + unset($this->options[CURLOPT_PROXY]); + unset($this->options[CURLOPT_PROXYPORT]); + unset($this->options[CURLOPT_PROXYTYPE]); + unset($this->options[CURLOPT_PROXYUSERPWD]); + + return $this; + } + /** * @codeCoverageIgnore * @param string $host diff --git a/src/Api/Providers/Auth.php b/src/Api/Providers/Auth.php index 12665999..8bbc556a 100644 --- a/src/Api/Providers/Auth.php +++ b/src/Api/Providers/Auth.php @@ -178,7 +178,7 @@ protected function processLogin($username, $password) $this->post($credentials, UrlBuilder::RESOURCE_LOGIN); - if ($this->response->hasErrors()) return false; + if ($this->response->isEmpty()) return false; $this->request->login(); diff --git a/src/Api/Providers/Pins.php b/src/Api/Providers/Pins.php index 897d8ba1..5a0c1e75 100644 --- a/src/Api/Providers/Pins.php +++ b/src/Api/Providers/Pins.php @@ -2,15 +2,15 @@ namespace seregazhuk\PinterestBot\Api\Providers; +use seregazhuk\PinterestBot\Api\Providers\Core\EntityProvider; use seregazhuk\PinterestBot\Api\Response; -use seregazhuk\PinterestBot\Helpers\FileHelper; -use seregazhuk\PinterestBot\Helpers\Pagination; -use seregazhuk\PinterestBot\Helpers\UrlBuilder; -use seregazhuk\PinterestBot\Api\Traits\Searchable; use seregazhuk\PinterestBot\Api\Traits\CanBeDeleted; +use seregazhuk\PinterestBot\Api\Traits\Searchable; use seregazhuk\PinterestBot\Api\Traits\SendsMessages; use seregazhuk\PinterestBot\Api\Traits\UploadsImages; -use seregazhuk\PinterestBot\Api\Providers\Core\EntityProvider; +use seregazhuk\PinterestBot\Helpers\FileHelper; +use seregazhuk\PinterestBot\Helpers\Pagination; +use seregazhuk\PinterestBot\Helpers\UrlBuilder; class Pins extends EntityProvider { @@ -211,7 +211,7 @@ public function tried($pinId, $limit = Pagination::DEFAULT_LIMIT) * @param int $limit * @return Pagination */ - protected function getAggregatedActivity($pinId, $additionalData = [], $limit) + protected function getAggregatedActivity($pinId, $additionalData = [], $limit) { $aggregatedPinId = $this->getAggregatedPinId($pinId); @@ -240,7 +240,12 @@ public function feed($limit = Pagination::DEFAULT_LIMIT) */ public function related($pinId, $limit = Pagination::DEFAULT_LIMIT) { - return $this->paginate(['pin' => $pinId], UrlBuilder::RESOURCE_RELATED_PINS, $limit); + $requestData = [ + 'pin' => $pinId, + 'add_vase' => true, + ]; + + return $this->paginate($requestData, UrlBuilder::RESOURCE_RELATED_PINS, $limit); } /** diff --git a/src/Helpers/Pagination.php b/src/Helpers/Pagination.php index eb189b6f..5f9e595d 100644 --- a/src/Helpers/Pagination.php +++ b/src/Helpers/Pagination.php @@ -7,7 +7,6 @@ use IteratorAggregate; use seregazhuk\PinterestBot\Api\Contracts\PaginatedResponse; - /** * Class Pagination * Iterate through results of Pinterest Api. By default iterator will return 50 first diff --git a/tests/Bot/CurlHttpClientTest.php b/tests/Bot/CurlHttpClientTest.php index 97899122..3b1b2d5e 100644 --- a/tests/Bot/CurlHttpClientTest.php +++ b/tests/Bot/CurlHttpClientTest.php @@ -36,5 +36,4 @@ public function it_creates_cookies_file_if_doesnt_exist() $this->assertTrue(file_exists($this->getCookiePath('test_name'))); } - } diff --git a/tests/Bot/PaginationTest.php b/tests/Bot/PaginationTest.php index 6c4b3e0f..e36bd9d7 100644 --- a/tests/Bot/PaginationTest.php +++ b/tests/Bot/PaginationTest.php @@ -2,7 +2,6 @@ namespace seregazhuk\tests\Bot; -use Mockery; use PHPUnit_Framework_TestCase; use seregazhuk\PinterestBot\Api\Response; use seregazhuk\tests\Helpers\ResponseHelper; @@ -41,9 +40,7 @@ public function it_always_return_iterator_object() public function it_uses_bookmarks_for_iteration_over_responses() { $pagination = new Pagination(); - $responseData = $this->createPaginatedResponse($this->paginatedResponse); - $responseData['resource'] = ['options' => ['bookmarks' => 'my_bookrmarks']]; - + $responseData = $this->createPaginatedResponse($this->paginatedResponse, 'my_bookrmarks'); $pagination->paginateOver(function() use ($responseData){ return (new Response())->fill($responseData); @@ -63,8 +60,7 @@ public function it_can_skip_n_first_results() $data[] = ['id'=> $value]; } - $responseData = $this->createPaginatedResponse($data); - $responseData['resource'] = ['options' => ['bookmarks' => 'my_bookrmarks']]; + $responseData = $this->createPaginatedResponse($data, 'my_bookrmarks'); $pagination->paginateOver(function() use ($responseData){ return (new Response())->fill($responseData); @@ -85,8 +81,7 @@ public function it_can_limit_results() $data[] = ['id'=> $value]; } - $responseData = $this->createPaginatedResponse($data); - $responseData['resource'] = ['options' => ['bookmarks' => 'my_bookrmarks']]; + $responseData = $this->createPaginatedResponse($data, 'my_bookrmarks'); $pagination = new Pagination(); $pagination->paginateOver(function() use ($responseData){ @@ -108,8 +103,7 @@ public function it_accepts_limit_in_constructor() $data[] = ['id'=> $value]; } - $responseData = $this->createPaginatedResponse($data); - $responseData['resource'] = ['options' => ['bookmarks' => 'my_bookrmarks']]; + $responseData = $this->createPaginatedResponse($data, 'my_bookrmarks'); $pagination = new Pagination($limit); $pagination->paginateOver(function() use ($responseData){ diff --git a/tests/Bot/ResponseTest.php b/tests/Bot/ResponseTest.php index d1628993..34282c9b 100644 --- a/tests/Bot/ResponseTest.php +++ b/tests/Bot/ResponseTest.php @@ -17,7 +17,7 @@ class ResponseTest extends PHPUnit_Framework_TestCase public function it_should_return_data_from_response() { $response = new Response(); - $response->fill($this->createApiResponse(['data' => 'some data'])); + $response->fill($this->createSuccessApiResponse('some data')); $this->assertEquals('some data', $response->getResponseData()); } @@ -26,7 +26,7 @@ public function it_should_return_data_from_response() public function it_should_return_value_by_key_from_response() { $response = new Response(); - $response->fill($this->createApiResponse(['data' => ['key' => 'value']])); + $response->fill($this->createSuccessApiResponse(['key' => 'value'])); $this->assertEquals('value', $response->getResponseData('key')); } @@ -51,7 +51,7 @@ public function it_should_check_empty_responses() $this->assertTrue($response->isEmpty()); $responseWithError = new Response(); - $response->fill($this->createErrorApiResponse('some error')); + $response->fill($this->createErrorApiResponse()); $this->assertTrue($responseWithError->isEmpty()); } @@ -69,9 +69,13 @@ public function it_should_check_responses_with_data() public function it_should_check_responses_with_errors() { $response = new Response(); - $response->fill($this->createErrorApiResponse('some error')); + $response->fill($this->createErrorApiResponse()); $this->assertTrue($response->hasErrors()); + } + /** @test */ + public function it_should_check_responses_without_errors() + { $response = new Response(); $response->fill($this->createSuccessApiResponse()); $this->assertFalse($response->hasErrors()); @@ -82,7 +86,7 @@ public function it_should_check_responses_with_errors() public function it_should_return_bookmarks_string_from_response() { $response = new Response(); - $response->fill(['resource' => ['options' => ['bookmarks' => ['my_bookmarks_string']]]]); + $response->fill($this->createPaginatedResponse([], 'my_bookmarks_string')); $this->assertEquals(['my_bookmarks_string'], $response->getBookmarks()); $response = new Response(); @@ -114,16 +118,10 @@ public function it_should_return_client_info() /** @test */ public function it_should_return_data_and_bookmarks_from_response_with_pagination() { - $dataWithBookmarks = [ - 'resource' => [ - 'options' => ['bookmarks' => ['my_bookmarks_string']] - ], - 'resource_response' => [ - 'data' => 'some data' - ] - ]; $response = new Response(); - $response->fill($dataWithBookmarks); + $response->fill( + $this->createPaginatedResponse('some data', 'my_bookmarks_string') + ); $expected = [ 'data' => 'some data', diff --git a/tests/Bot/SendsMessagesTest.php b/tests/Bot/SendsMessagesTest.php index ae80983e..19e131c1 100644 --- a/tests/Bot/SendsMessagesTest.php +++ b/tests/Bot/SendsMessagesTest.php @@ -17,7 +17,9 @@ public function it_doesnt_allow_to_send_messages_without_specifying_emails_or_us $this->setExpectedException(InvalidRequest::class); /** @var SendsMessages $object */ $object = $this->getMockForTrait(SendsMessages::class); - $object->expects($this->any()) + + $object + ->expects($this->any()) ->method('post'); $object->send(1, 'message', [], []); diff --git a/tests/Helpers/ResponseHelper.php b/tests/Helpers/ResponseHelper.php index d4601e7a..92160265 100644 --- a/tests/Helpers/ResponseHelper.php +++ b/tests/Helpers/ResponseHelper.php @@ -61,16 +61,17 @@ public function createErrorApiResponse($error = 'error') * Create a dummy paginated response. * * @param mixed $response + * @param string $bookmarks * @return array */ - public function createPaginatedResponse($response) + public function createPaginatedResponse($response, $bookmarks = '') { return [ 'resource_response' => [ 'data' => $response, ], 'resource' => [ - 'options' => ['bookmarks' => 'my_bookrmarks'] + 'options' => ['bookmarks' => [$bookmarks]] ] ]; }