Skip to content

Commit

Permalink
Merge pull request #24 from seregazhuk/use-correct-guzzle-client-from…
Browse files Browse the repository at this point in the history
…-dependencies

Use correct guzzle client from dependencies
  • Loading branch information
seregazhuk authored Oct 2, 2019
2 parents 407c79f + efef711 commit c152a39
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 124 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: php

php:
- 5.6
- 7.0

addons:
code_climate:
Expand All @@ -12,7 +12,7 @@ before_script:
- composer install --prefer-source --no-interaction --dev
- composer dump-autoload

script: phpunit
script: vendor/bin/phpunit

after_script:
- vendor/bin/test-reporter
- vendor/bin/test-reporter
7 changes: 7 additions & 0 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.

## v0.2.1 - 2019-10-02
### Updated:
- PHP 7.0 required

### Fixed:
- Uses Guzzle from required dependencies not from the dev ones

## v0.2.0 - 2017-05-25
### Updated:
- Refactoring
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
}
],
"require": {
"php": ">=5.6.0",
"php": ">=7.0",
"ext-curl": "*",
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
"phpunit/phpunit": "^4.0",
"phpunit/phpunit": "^6.0",
"phpunit/php-code-coverage": "^5.2",
"codeclimate/php-test-reporter": "^0.3.2",
"mockery/mockery": "^0.9.5"
"mockery/mockery": "^1.2"
},
"autoload": {
"psr-4": {
Expand Down
28 changes: 4 additions & 24 deletions src/Api/GuzzleHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace seregazhuk\SmsIntel\Api;

use Guzzle\Http\ClientInterface;
use seregazhuk\SmsIntel\Contracts\HttpClient;
use GuzzleHttp\ClientInterface;

class GuzzleHttpClient implements HttpClient
class GuzzleHttpClient
{
/**
* @var ClientInterface
Expand All @@ -31,11 +30,7 @@ public function get($uri, $params = [])
$uri .= '?' . http_build_query($params);
}

return $this
->client
->get($uri)
->send()
->getBody(true);
return (string)$this->client->get($uri)->getBody();
}

/**
Expand All @@ -45,21 +40,6 @@ public function get($uri, $params = [])
*/
public function post($uri, $body = [])
{
return $this
->client
->post($uri, [], $body)
->send()
->getBody(true);
}

/**
* @param string $url
* @return $this
*/
public function setBaseUrl($url)
{
$this->client->setBaseUrl($url);

return $this;
return $this->client->post($uri, $body)->getBody();
}
}
4 changes: 2 additions & 2 deletions src/Api/Requests/JSONRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected function makeEndPoint($action)
*/
protected function formatRequestBody(array $requestBody)
{
return $requestBody;
return ['json' => $requestBody];
}

/**
Expand All @@ -158,4 +158,4 @@ protected function parseResponse($response)
{
return json_decode($response, true);
}
}
}
13 changes: 5 additions & 8 deletions src/Api/Requests/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@

namespace seregazhuk\SmsIntel\Api\Requests;

use seregazhuk\SmsIntel\Contracts\HttpClient;
use GuzzleHttp\Client;
use seregazhuk\SmsIntel\Api\GuzzleHttpClient;

abstract class Request
{
/**
* @var array
*/
static public $allowedMethod = [];

/**
* @var HttpClient
* @var Client
*/
protected $client;

Expand All @@ -26,7 +23,7 @@ abstract class Request
*/
protected $password;

public function __construct(HttpClient $http)
public function __construct(GuzzleHttpClient $http)
{
$this->client = $http;
}
Expand Down Expand Up @@ -95,4 +92,4 @@ abstract protected function parseResponse($response);
* @return string
*/
abstract protected function makeEndPoint($action);
}
}
28 changes: 23 additions & 5 deletions src/Api/Requests/RequestsContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,33 @@

namespace seregazhuk\SmsIntel\Api\Requests;

use GuzzleHttp\Client;
use ReflectionClass;
use seregazhuk\SmsIntel\Contracts\HttpClient;
use seregazhuk\SmsIntel\Api\GuzzleHttpClient;
use seregazhuk\SmsIntel\Exceptions\WrongRequest;

/**
* @method send(string|array $phoneNumber, string $from, string $message) To send message to one phone number
* @method getGroups
* @method editGroup
* @method addContact
* @method getContacts
* @method createGroup
* @method getPhoneInfo
* @method requestSource
* @method removeContact
*
* @method cancel(int $smsId) Cancel sms by id
* @method getBalance
* @method checkCoupon
* @method getReportBySms
* @method getReportBySource
* @method getReportByNumber
*/
class RequestsContainer
{

/**
* @var HttpClient
* @var Client
*/
protected $http;

Expand All @@ -29,7 +47,7 @@ class RequestsContainer
*/
protected $requests = [];

public function __construct(HttpClient $http, $login, $password)
public function __construct(GuzzleHttpClient $http, $login, $password)
{
$this->http = $http;
$this->login = $login;
Expand Down Expand Up @@ -126,4 +144,4 @@ protected function buildRequest($className)
->newInstanceArgs([$this->http])
->setCredentials($this->login, $this->password);
}
}
}
4 changes: 2 additions & 2 deletions src/Api/Requests/XMLRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected function makeEndPoint($action)
*/
protected function formatRequestBody(array $requestBody)
{
return (new XMLFormatter($requestBody))->toXml();
return ['body' => (new XMLFormatter($requestBody))->toXml()];
}

/**
Expand All @@ -125,4 +125,4 @@ protected function parseResponse($response)
$xml = simplexml_load_string($response);
return (array)$xml->children();
}
}
}
26 changes: 0 additions & 26 deletions src/Contracts/HttpClient.php

This file was deleted.

13 changes: 2 additions & 11 deletions src/SmsIntel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace seregazhuk\SmsIntel;

use Guzzle\Http\Client;
use GuzzleHttp\Client;
use seregazhuk\SmsIntel\Api\GuzzleHttpClient;
use seregazhuk\SmsIntel\Contracts\HttpClient;
use seregazhuk\SmsIntel\Api\Requests\RequestsContainer;
use seregazhuk\SmsIntel\Exceptions\AuthenticationFailed;

Expand All @@ -21,20 +20,12 @@ public static function create($login, $password)
self::checkCredentials($login, $password);

return new RequestsContainer(
self::createHttpAdapter(),
new GuzzleHttpClient(new Client()),
$login,
$password
);
}

/**
* @return HttpClient
*/
protected static function createHttpAdapter()
{
return new GuzzleHttpClient(new Client());
}

/**
* @param string $login
* @param string $password
Expand Down
39 changes: 16 additions & 23 deletions tests/GuzzleHttpAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,18 @@

namespace seregazhuk\tests;

use Guzzle\Http\ClientInterface;
use Guzzle\Stream\StreamInterface;
use GuzzleHttp\ClientInterface;
use Guzzle\Http\Message\RequestInterface;
use Guzzle\Http\Message\Response;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use seregazhuk\SmsIntel\Api\GuzzleHttpClient;

class GuzzleHttpAdapterTest extends \PHPUnit_Framework_TestCase
class GuzzleHttpAdapterTest extends TestCase
{

/** @test */
public function it_sets_base_url_for_http_client()
{
$baseUrl = 'http://example.com';

$http = Mockery::mock(ClientInterface::class)
->shouldReceive('setBaseUrl')
->with($baseUrl)
->getMock();

$guzzleAdapter = new GuzzleHttpClient($http);
$guzzleAdapter->setBaseUrl($baseUrl);
}
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;

/** @test */
public function it_executes_get_request_on_http_client()
Expand All @@ -38,7 +28,6 @@ public function it_executes_get_request_on_http_client()
->andReturn($this->createRequestMock())
->getMock();


$guzzleAdapter = new GuzzleHttpClient($http);
$guzzleAdapter->get($url, $queryParams);
}
Expand All @@ -51,7 +40,7 @@ public function it_executes_post_request_on_http_client()

$http = Mockery::mock(ClientInterface::class)
->shouldReceive('post')
->with($url, [], $params)
->with($url, $params)
->andReturn($this->createRequestMock())
->getMock();

Expand All @@ -64,10 +53,14 @@ public function it_executes_post_request_on_http_client()
*/
protected function createRequestMock()
{
$requestMock = Mockery::mock(RequestInterface::class)
->shouldReceive('send')
->andReturn(new Response(200))
->getMock();
return $requestMock;
/** @var Mock $mockContent */
$mockContent = Mockery::mock(StreamInterface::class);
$mockContent->shouldReceive('getContents')->andReturn('{}');

/** @var Mock $mockResponse */
$mockResponse = Mockery::mock(ResponseInterface::class);
$mockResponse->shouldReceive('getBody')->andReturn($mockContent);

return $mockResponse;
}
}
15 changes: 13 additions & 2 deletions tests/Requests/JSONRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace seregazhuk\tests\Requests;

use Guzzle\Stream\StreamInterface;
use Mockery;
use Psr\Http\Message\ResponseInterface;
use seregazhuk\SmsIntel\Api\Requests\JSONRequest;

class JSONRequestTest extends RequestTest
Expand Down Expand Up @@ -151,14 +154,22 @@ public function it_returns_account_info()
*/
protected function setHttpClientMockExpectations($requestEndpoint, $requestParams)
{
/** @var Mock $mockContent */
$mockContent = Mockery::mock(StreamInterface::class);
$mockContent->shouldReceive('getContents')->andReturn('{}');

/** @var Mock $mockResponse */
$mockResponse = Mockery::mock(ResponseInterface::class);
$mockResponse->shouldReceive('getBody')->andReturn($mockContent);

$this
->httpClient
->shouldReceive('post')
->with(
\Mockery::on(function($endpoint) use ($requestEndpoint) {
return strpos($endpoint, $requestEndpoint) !== false;
}),
$requestParams
)->andReturn('');
['json' => $requestParams]
)->andReturn($mockResponse);
}
}
Loading

0 comments on commit c152a39

Please sign in to comment.