From f0d10675e3e38d5973497143c3b37102080500cc Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Wed, 2 Oct 2019 16:05:32 +0300 Subject: [PATCH 1/5] Use correct Guzzle client From the dependencies one, instead of dev deps --- src/Api/GuzzleHttpClient.php | 28 ++++---------------------- src/Api/Requests/JSONRequest.php | 4 ++-- src/Api/Requests/Request.php | 13 +++++------- src/Api/Requests/RequestsContainer.php | 28 +++++++++++++++++++++----- src/Api/Requests/XMLRequest.php | 4 ++-- src/Contracts/HttpClient.php | 26 ------------------------ src/SmsIntel.php | 13 ++---------- 7 files changed, 38 insertions(+), 78 deletions(-) delete mode 100644 src/Contracts/HttpClient.php diff --git a/src/Api/GuzzleHttpClient.php b/src/Api/GuzzleHttpClient.php index c770051..fe11427 100644 --- a/src/Api/GuzzleHttpClient.php +++ b/src/Api/GuzzleHttpClient.php @@ -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 @@ -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(); } /** @@ -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(); } } diff --git a/src/Api/Requests/JSONRequest.php b/src/Api/Requests/JSONRequest.php index e8e86a3..6dfc439 100644 --- a/src/Api/Requests/JSONRequest.php +++ b/src/Api/Requests/JSONRequest.php @@ -147,7 +147,7 @@ protected function makeEndPoint($action) */ protected function formatRequestBody(array $requestBody) { - return $requestBody; + return ['json' => $requestBody]; } /** @@ -158,4 +158,4 @@ protected function parseResponse($response) { return json_decode($response, true); } -} \ No newline at end of file +} diff --git a/src/Api/Requests/Request.php b/src/Api/Requests/Request.php index e97e2dd..c79a997 100644 --- a/src/Api/Requests/Request.php +++ b/src/Api/Requests/Request.php @@ -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; @@ -26,7 +23,7 @@ abstract class Request */ protected $password; - public function __construct(HttpClient $http) + public function __construct(GuzzleHttpClient $http) { $this->client = $http; } @@ -95,4 +92,4 @@ abstract protected function parseResponse($response); * @return string */ abstract protected function makeEndPoint($action); -} \ No newline at end of file +} diff --git a/src/Api/Requests/RequestsContainer.php b/src/Api/Requests/RequestsContainer.php index cab1a56..b07f57d 100644 --- a/src/Api/Requests/RequestsContainer.php +++ b/src/Api/Requests/RequestsContainer.php @@ -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; @@ -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; @@ -126,4 +144,4 @@ protected function buildRequest($className) ->newInstanceArgs([$this->http]) ->setCredentials($this->login, $this->password); } -} \ No newline at end of file +} diff --git a/src/Api/Requests/XMLRequest.php b/src/Api/Requests/XMLRequest.php index e5bcc27..c744d35 100644 --- a/src/Api/Requests/XMLRequest.php +++ b/src/Api/Requests/XMLRequest.php @@ -111,7 +111,7 @@ protected function makeEndPoint($action) */ protected function formatRequestBody(array $requestBody) { - return (new XMLFormatter($requestBody))->toXml(); + return ['body' => (new XMLFormatter($requestBody))->toXml()]; } /** @@ -125,4 +125,4 @@ protected function parseResponse($response) $xml = simplexml_load_string($response); return (array)$xml->children(); } -} \ No newline at end of file +} diff --git a/src/Contracts/HttpClient.php b/src/Contracts/HttpClient.php deleted file mode 100644 index 8999f63..0000000 --- a/src/Contracts/HttpClient.php +++ /dev/null @@ -1,26 +0,0 @@ - Date: Wed, 2 Oct 2019 16:22:20 +0300 Subject: [PATCH 2/5] Update tests --- composer.json | 5 +-- src/Api/GuzzleHttpClient.php | 2 +- tests/GuzzleHttpAdapterTest.php | 39 ++++++++++-------------- tests/Requests/JSONRequestTest.php | 15 +++++++-- tests/Requests/RequestTest.php | 16 +++++----- tests/Requests/RequestsContainerTest.php | 7 +++-- tests/Requests/XMLRequestTest.php | 2 +- tests/SmsIntelTest.php | 3 +- tests/XMLFormatterTest.php | 3 +- 9 files changed, 49 insertions(+), 43 deletions(-) diff --git a/composer.json b/composer.json index f0359b0..482bb1b 100644 --- a/composer.json +++ b/composer.json @@ -22,9 +22,10 @@ "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": { diff --git a/src/Api/GuzzleHttpClient.php b/src/Api/GuzzleHttpClient.php index fe11427..6978a1a 100644 --- a/src/Api/GuzzleHttpClient.php +++ b/src/Api/GuzzleHttpClient.php @@ -40,6 +40,6 @@ public function get($uri, $params = []) */ public function post($uri, $body = []) { - return $this->client->post($uri, $body)->getBody(); + return $this->client->post($uri, $body)->getBody(); } } diff --git a/tests/GuzzleHttpAdapterTest.php b/tests/GuzzleHttpAdapterTest.php index 52e5a84..65d4db6 100644 --- a/tests/GuzzleHttpAdapterTest.php +++ b/tests/GuzzleHttpAdapterTest.php @@ -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() @@ -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); } @@ -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(); @@ -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; } } diff --git a/tests/Requests/JSONRequestTest.php b/tests/Requests/JSONRequestTest.php index 5d28dfb..e734a5f 100644 --- a/tests/Requests/JSONRequestTest.php +++ b/tests/Requests/JSONRequestTest.php @@ -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 @@ -151,6 +154,14 @@ 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') @@ -158,7 +169,7 @@ protected function setHttpClientMockExpectations($requestEndpoint, $requestParam \Mockery::on(function($endpoint) use ($requestEndpoint) { return strpos($endpoint, $requestEndpoint) !== false; }), - $requestParams - )->andReturn(''); + ['json' => $requestParams] + )->andReturn($mockResponse); } } diff --git a/tests/Requests/RequestTest.php b/tests/Requests/RequestTest.php index c891df4..b97f88d 100644 --- a/tests/Requests/RequestTest.php +++ b/tests/Requests/RequestTest.php @@ -4,20 +4,23 @@ use Mockery; use Mockery\Mock; +use PHPUnit\Framework\TestCase; +use seregazhuk\SmsIntel\Api\GuzzleHttpClient; use seregazhuk\SmsIntel\Api\Requests\Request; -use seregazhuk\SmsIntel\Contracts\HttpClient; use seregazhuk\SmsIntel\Api\Requests\XMLRequest; use seregazhuk\SmsIntel\Api\Requests\JSONRequest; -abstract class RequestTest extends \PHPUnit_Framework_TestCase +abstract class RequestTest extends TestCase { + use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; + /** * @var string */ protected $requestClass; /** - * @var HttpClient|Mock + * @var GuzzleHttpClient|Mock */ protected $httpClient; @@ -63,11 +66,6 @@ protected function appendCredentialsToRequestParams($requestParams) ); } - protected function tearDown() - { - Mockery::close(); - } - /** * @param string $requestEndpoint * @param array $requestParams @@ -85,6 +83,6 @@ protected function getRequestClass() protected function createHttpClientMock() { - $this->httpClient = Mockery::mock(HttpClient::class); + $this->httpClient = Mockery::mock(GuzzleHttpClient::class); } } diff --git a/tests/Requests/RequestsContainerTest.php b/tests/Requests/RequestsContainerTest.php index 0e5ff1e..a0f9ab1 100644 --- a/tests/Requests/RequestsContainerTest.php +++ b/tests/Requests/RequestsContainerTest.php @@ -3,12 +3,13 @@ namespace seregazhuk\tests\Requests; use Mockery; -use seregazhuk\SmsIntel\Contracts\HttpClient; +use PHPUnit\Framework\TestCase; +use seregazhuk\SmsIntel\Api\GuzzleHttpClient; use seregazhuk\SmsIntel\Api\Requests\XMLRequest; use seregazhuk\SmsIntel\Api\Requests\JSONRequest; use seregazhuk\SmsIntel\Api\Requests\RequestsContainer; -class RequestsContainerTest extends \PHPUnit_Framework_TestCase +class RequestsContainerTest extends TestCase { /** @test */ public function it_returns_an_instance_of_request_object() @@ -54,7 +55,7 @@ public function it_throws_exception_when_resolving_request_by_wrong_action() protected function createContainerObject() { return new RequestsContainer( - Mockery::mock(HttpClient::class), 'login', 'password' + Mockery::mock(GuzzleHttpClient::class), 'login', 'password' ); } diff --git a/tests/Requests/XMLRequestTest.php b/tests/Requests/XMLRequestTest.php index 8aa6804..a76542c 100644 --- a/tests/Requests/XMLRequestTest.php +++ b/tests/Requests/XMLRequestTest.php @@ -100,7 +100,7 @@ protected function setHttpClientMockExpectations($requestEndpoint, $requestParam Mockery::on(function($endpoint) use ($requestEndpoint) { return strpos($endpoint, $requestEndpoint) !== false; }), - (new XMLFormatter($requestParams))->toXml() + ['body' => (new XMLFormatter($requestParams))->toXml()] ) ->andReturn(''); diff --git a/tests/SmsIntelTest.php b/tests/SmsIntelTest.php index 04eeb4a..fdd19cd 100644 --- a/tests/SmsIntelTest.php +++ b/tests/SmsIntelTest.php @@ -2,10 +2,11 @@ namespace seregazhuk\tests; +use PHPUnit\Framework\TestCase; use seregazhuk\SmsIntel\SmsIntel; use seregazhuk\SmsIntel\Api\Requests\RequestsContainer; -class SmsIntelTest extends \PHPUnit_Framework_TestCase +class SmsIntelTest extends TestCase { /** @test */ public function it_creates_an_instance_of_requests_container() diff --git a/tests/XMLFormatterTest.php b/tests/XMLFormatterTest.php index f1ad05e..c722808 100644 --- a/tests/XMLFormatterTest.php +++ b/tests/XMLFormatterTest.php @@ -2,9 +2,10 @@ namespace seregazhuk\tests; +use PHPUnit\Framework\TestCase; use seregazhuk\SmsIntel\Formatters\XMLFormatter; -class XMLFormatterTest extends \PHPUnit_Framework_TestCase +class XMLFormatterTest extends TestCase { /** @test */ public function it_formats_request_params_to_xml() From d290f34ba93cfe7fe0221c9080aa8583765886e6 Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Wed, 2 Oct 2019 16:25:50 +0300 Subject: [PATCH 3/5] Update PHP version --- .travis.yml | 4 ++-- composer.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 898f7f8..8ed4586 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: php php: - - 5.6 + - 7.1 addons: code_climate: @@ -15,4 +15,4 @@ before_script: script: phpunit after_script: - - vendor/bin/test-reporter \ No newline at end of file + - vendor/bin/test-reporter diff --git a/composer.json b/composer.json index 482bb1b..166c4c2 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ } ], "require": { - "php": ">=5.6.0", + "php": ">=7.1", "ext-curl": "*", "guzzlehttp/guzzle": "^6.2" }, From 87fb112bff282df6ab8a4178fac5959ad84e4290 Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Wed, 2 Oct 2019 16:27:22 +0300 Subject: [PATCH 4/5] Update change log --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8ac50d..a51d120 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From efef7115a3ffc0836e6fe62a09fcc60564fa7beb Mon Sep 17 00:00:00 2001 From: Zhuk Sergey Date: Wed, 2 Oct 2019 16:31:52 +0300 Subject: [PATCH 5/5] Fix phpunit version --- .travis.yml | 4 ++-- composer.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8ed4586..3e5ee77 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: php php: - - 7.1 + - 7.0 addons: code_climate: @@ -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 diff --git a/composer.json b/composer.json index 166c4c2..f18ad9b 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ } ], "require": { - "php": ">=7.1", + "php": ">=7.0", "ext-curl": "*", "guzzlehttp/guzzle": "^6.2" },