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

Commit

Permalink
Merge remote-tracking branch 'github/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
seregazhuk committed May 13, 2017
2 parents d6d6d3d + 43c4e43 commit cae8ae9
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 7 deletions.
15 changes: 11 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Change Log
All notable changes to this project will be documented in this file.

## v5.3.9 - 2017-05-07
### Fixed:
- Boards *forMe()* and Pinners *followers*() methods

### Added:
- httpClient *usesProxy* method

## v5.3.8 - 2017-05-07
### Fixed:
- boards *info()* method for boards with spaces in their names
Expand All @@ -12,16 +19,16 @@ All notable changes to this project will be documented in this file.

## v5.3.6 - 2017-05-01
### Added
- bot wait() method
- bot *wait()* method

## v5.3.5 - 2017-04-23
### Fixed:
- Bot getClientInfo() method
- Bot *getClientInfo()* method

## v5.3.4 - 2017-04-23
### Fixed:
- Boards forMe() method
- Pinners followers() uses limit
- Boards *forMe()* method
- Pinners *followers()* uses limit

## v5.3.3 - 2017-04-22
### Updated:
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,13 @@ If you need to stop sending requests via proxy:
$bot->getHttpClient()->dontUseProxy();
```

Check if bot uses proxy:
```php
if($bot->getHttpClient()->usesProxy()) {
// ...
}
```

## 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 @@ -87,4 +87,9 @@ public function useSocksProxy($host, $port, $auth = null);
* @return HttpClient
*/
public function dontUseProxy();

/**
* @return bool
*/
public function usesProxy();
}
8 changes: 8 additions & 0 deletions src/Api/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ public function dontUseProxy()
return $this;
}

/**
* @return bool
*/
public function usesProxy()
{
return isset($this->options[CURLOPT_PROXY]);
}

/**
* @codeCoverageIgnore
* @param string $host
Expand Down
3 changes: 2 additions & 1 deletion src/Api/ProvidersContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function __get($provider)
*
* @return Provider
*/
protected function getProvider($provider)
public function getProvider($provider)
{
$provider = strtolower($provider);

Expand Down Expand Up @@ -222,6 +222,7 @@ public function getResponse()
}

/**
* @codeCoverageIgnore
* Creates a timeout
* @param int $seconds
* @return $this
Expand Down
24 changes: 24 additions & 0 deletions tests/Bot/CurlHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,28 @@ public function it_creates_cookies_file_if_doesnt_exist()

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

/** @test */
public function it_can_use_proxy_settings()
{
$client = new CurlHttpClient(new Cookies());

$this->assertFalse($client->usesProxy());

$client->useProxy('192.168.1.1', '1235');

$this->assertTrue($client->usesProxy());
}

/** @test */
public function it_removes_proxy_settings()
{
$client = new CurlHttpClient(new Cookies());

$client->useProxy('192.168.1.1', '1235');

$client->dontUseProxy();

$this->assertFalse($client->usesProxy());
}
}
34 changes: 32 additions & 2 deletions tests/Bot/ProvidersContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class ProvidersContainerTest extends PHPUnit_Framework_TestCase

public function setUp()
{
$this->response = Mockery::mock(Response::class);
$this->request = Mockery::mock(Request::class);
$this->response = Mockery::mock(Response::class)->makePartial();
$this->request = Mockery::mock(Request::class)->makePartial();

$this->container = new ProvidersContainer(
$this->request, $this->response
Expand Down Expand Up @@ -75,6 +75,26 @@ public function it_delegates_client_info_to_response()
$this->assertEquals($clientInfo, $this->container->getClientInfo());
}

/** @test */
public function it_can_reload_client_info()
{
$clientInfo = ['info'];
$this->response
->shouldReceive('getClientInfo')
->once()
->andReturn(null);

$this->request->shouldReceive('exec')->once();

$this->response
->shouldReceive('getClientInfo')
->once()
->andReturn($clientInfo);


$this->assertEquals($clientInfo, $this->container->getClientInfo());
}

/** @test */
public function it_delegates_last_error_to_response()
{
Expand All @@ -101,6 +121,16 @@ public function it_should_return_last_message_from_response()
$this->assertEquals($error['code'], $this->container->getLastError());
}

/** @test */
public function it_should_return_null_if_there_was_no_error_in_response()
{
$this->response
->shouldReceive('getLastError')
->andReturn(false);

$this->assertNull($this->container->getLastError());
}

/** @test */
public function it_should_return_last_error_code_from_response()
{
Expand Down
82 changes: 82 additions & 0 deletions tests/Bot/ResolvesCurrentUsernameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace seregazhuk\tests\Bot\Api;

use Mockery;
use PHPUnit_Framework_TestCase;
use seregazhuk\PinterestBot\Api\Request;
use seregazhuk\PinterestBot\Api\Response;
use seregazhuk\PinterestBot\Api\ProvidersContainer;
use seregazhuk\PinterestBot\Api\Providers\Core\Provider;
use seregazhuk\PinterestBot\Api\Traits\ResolvesCurrentUsername;

/**
* Class ProviderTest.
*/
class ResolvesCurrentUsernameTest extends PHPUnit_Framework_TestCase
{
protected function tearDown()
{
Mockery::close();
}

/** @test */
public function it_calls_user_provider_to_get_current_user_name()
{
$username = 'John Doe';

$provider = $this->makeProvider($this->makeRequest($username));

$this->assertEquals($username, $provider->getCurrentUserName());
}

/**
* @param Request $request
* @return DummyUsernameProvider
*/
protected function makeProvider(Request $request)
{
$container = new ProvidersContainer($request, new Response());

/** @var DummyUsernameProvider $provider */
$provider = Mockery::mock(DummyUsernameProvider::class, [$container])->makePartial();

return $provider;
}

protected function makeRequest($username)
{
/** @var Request $request */
$request = Mockery::mock(Request::class)
->shouldReceive('exec')
->andReturn(json_encode($this->makePinterestProfileResponse($username)))
->getMock();

$request->shouldReceive('isLoggedIn')->andReturn(true);

return $request;
}

/**
* @param $username
* @return array
*/
protected function makePinterestProfileResponse($username)
{
return ['resource_response' => ['data' => ['username' => $username]]];
}
}


class DummyUsernameProvider extends Provider {

use ResolvesCurrentUsername;

/**
* @return array|bool|Response
*/
public function getCurrentUserName()
{
return $this->resolveCurrentUsername();
}
}

0 comments on commit cae8ae9

Please sign in to comment.