Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #255 from seregazhuk/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
seregazhuk authored Mar 25, 2017
2 parents 3875a23 + aba9979 commit 792ea63
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 37 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Api/Contracts/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
12 changes: 12 additions & 0 deletions src/Api/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ public function removeCookies()
unlink($this->cookieJar);
}

$this->cookieJar = null;

return $this;
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Providers/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
19 changes: 12 additions & 7 deletions src/Api/Providers/Pins.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Helpers/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/Bot/CurlHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ public function it_creates_cookies_file_if_doesnt_exist()

$this->assertTrue(file_exists($this->getCookiePath('test_name')));
}

}
14 changes: 4 additions & 10 deletions tests/Bot/PaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace seregazhuk\tests\Bot;

use Mockery;
use PHPUnit_Framework_TestCase;
use seregazhuk\PinterestBot\Api\Response;
use seregazhuk\tests\Helpers\ResponseHelper;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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){
Expand All @@ -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){
Expand Down
26 changes: 12 additions & 14 deletions tests/Bot/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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'));
}
Expand All @@ -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());
}
Expand All @@ -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());
Expand All @@ -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();
Expand Down Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion tests/Bot/SendsMessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [], []);
Expand Down
5 changes: 3 additions & 2 deletions tests/Helpers/ResponseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
]
];
}
Expand Down

0 comments on commit 792ea63

Please sign in to comment.