Merge pull request #81 from Neur0toxine/master
[fix] fileDownload and fileUpload method works properly now
This commit is contained in:
commit
30dfcc7c68
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,6 +2,8 @@
|
||||
/bin
|
||||
composer.lock
|
||||
composer.phar
|
||||
coverage.xml
|
||||
test-report.xml
|
||||
phpunit.xml
|
||||
.idea
|
||||
.DS_Store
|
||||
|
@ -29,8 +29,13 @@ use RetailCrm\Http\Client;
|
||||
*/
|
||||
abstract class AbstractLoader
|
||||
{
|
||||
/** @var string|null */
|
||||
protected $siteCode;
|
||||
|
||||
/** @var \RetailCrm\Http\Client */
|
||||
protected $client;
|
||||
|
||||
/** @var string */
|
||||
protected $crmUrl;
|
||||
|
||||
/**
|
||||
|
@ -75,7 +75,7 @@ class Client
|
||||
*
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function makeRequest(
|
||||
public function makeRawRequest(
|
||||
$path,
|
||||
$method,
|
||||
array $parameters = [],
|
||||
@ -119,7 +119,7 @@ class Client
|
||||
curl_setopt($curlHandler, CURLOPT_POST, true);
|
||||
|
||||
if ('/files/upload' == $path) {
|
||||
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters['file']);
|
||||
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, file_get_contents($parameters['file']));
|
||||
} else {
|
||||
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters);
|
||||
}
|
||||
@ -143,4 +143,29 @@ class Client
|
||||
|
||||
return new ApiResponse($statusCode, $responseBody);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make HTTP request and deserialize JSON body (throws exception otherwise)
|
||||
*
|
||||
* @param string $path request url
|
||||
* @param string $method (default: 'GET')
|
||||
* @param array $parameters (default: array())
|
||||
* @param bool $fullPath (default: false)
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @throws CurlException
|
||||
* @throws InvalidJsonException
|
||||
*
|
||||
* @return ApiResponse
|
||||
*/
|
||||
public function makeRequest(
|
||||
$path,
|
||||
$method,
|
||||
array $parameters = [],
|
||||
$fullPath = false
|
||||
) {
|
||||
return $this->makeRawRequest($path, $method, $parameters, $fullPath)->asJsonResponse();
|
||||
}
|
||||
}
|
||||
|
@ -150,7 +150,7 @@ trait Files
|
||||
public function fileDownload($id)
|
||||
{
|
||||
/* @noinspection PhpUndefinedMethodInspection */
|
||||
return $this->client->makeRequest(
|
||||
return $this->client->makeRawRequest(
|
||||
sprintf('/files/%s/download', $id),
|
||||
"GET"
|
||||
);
|
||||
|
@ -51,9 +51,17 @@ class ApiResponse implements \ArrayAccess
|
||||
{
|
||||
$this->statusCode = (int) $statusCode;
|
||||
$this->rawResponse = $responseBody;
|
||||
}
|
||||
|
||||
if (!empty($responseBody)) {
|
||||
$response = json_decode($responseBody, true);
|
||||
/**
|
||||
* Deserialize JSON from raw response body
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function asJsonResponse()
|
||||
{
|
||||
if (!empty($this->rawResponse)) {
|
||||
$response = json_decode($this->rawResponse, true);
|
||||
|
||||
if (!$response && JSON_ERROR_NONE !== ($error = json_last_error())) {
|
||||
throw new InvalidJsonException(
|
||||
@ -64,6 +72,8 @@ class ApiResponse implements \ArrayAccess
|
||||
|
||||
$this->response = $response;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -93,6 +93,22 @@ class ClientTest extends TestCase
|
||||
$response = $client->makeRequest('/orders', Client::METHOD_GET);
|
||||
|
||||
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
|
||||
static::assertNotEmpty($response->getResponseBody());
|
||||
static::assertNotEmpty($response->getResponse());
|
||||
static::assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @group client
|
||||
*/
|
||||
public function testRequestSuccessNoDeserialization()
|
||||
{
|
||||
$client = static::getClient();
|
||||
$response = $client->makeRawRequest('/orders', Client::METHOD_GET);
|
||||
|
||||
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
|
||||
static::assertNotEmpty($response->getResponseBody());
|
||||
static::assertEmpty($response->getResponse());
|
||||
static::assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
}
|
||||
|
@ -308,7 +308,7 @@ class ApiClientCustomersCorporateTest extends TestCase
|
||||
{
|
||||
$client = static::getApiClient();
|
||||
$response = $client->request->customersCorporateAddresses($ids['externalId'], ['name' => 'name'], 1, 20);
|
||||
|
||||
|
||||
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
|
||||
static::assertTrue(
|
||||
$response->isSuccessful(),
|
||||
|
@ -40,7 +40,7 @@ class ApiResponseTest extends TestCase
|
||||
'Response object created'
|
||||
);
|
||||
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
static::assertInstanceOf(
|
||||
'RetailCrm\Response\ApiResponse',
|
||||
$response,
|
||||
@ -54,7 +54,20 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testJsonInvalid()
|
||||
{
|
||||
new ApiResponse(400, '{ "asdf": }');
|
||||
(new ApiResponse(400, '{ "asdf": }'))->asJsonResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* @group response
|
||||
*/
|
||||
public function testJsonInvalidNoDeserialize()
|
||||
{
|
||||
$response = new ApiResponse(400, '{ "asdf": }');
|
||||
static::assertInstanceOf(
|
||||
'RetailCrm\Response\ApiResponse',
|
||||
$response,
|
||||
'Response object created'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -69,7 +82,7 @@ class ApiResponseTest extends TestCase
|
||||
'Response object returns the right status code'
|
||||
);
|
||||
|
||||
$response = new ApiResponse(460, '{ "success": false }');
|
||||
$response = (new ApiResponse(460, '{ "success": false }'))->asJsonResponse();
|
||||
static::assertEquals(
|
||||
460,
|
||||
$response->getStatusCode(),
|
||||
@ -88,7 +101,7 @@ class ApiResponseTest extends TestCase
|
||||
'Request was successful'
|
||||
);
|
||||
|
||||
$response = new ApiResponse(460, '{ "success": false }');
|
||||
$response = (new ApiResponse(460, '{ "success": false }'))->asJsonResponse();
|
||||
static::assertFalse(
|
||||
$response->isSuccessful(),
|
||||
'Request was failed'
|
||||
@ -100,7 +113,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testMagicCall()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
static::assertEquals(
|
||||
true,
|
||||
$response->isSuccessful(),
|
||||
@ -125,7 +138,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testMagicCallException2()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
/* @noinspection PhpUndefinedMethodInspection */
|
||||
$response->getSomeSuccess();
|
||||
}
|
||||
@ -135,7 +148,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testMagicGet()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
static::assertEquals(
|
||||
true,
|
||||
$response->success,
|
||||
@ -160,7 +173,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testMagicGetException2()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
/* @noinspection PhpUndefinedFieldInspection */
|
||||
$response->someSuccess;
|
||||
}
|
||||
@ -170,7 +183,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testArrayGet()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
static::assertEquals(
|
||||
true,
|
||||
$response['success'],
|
||||
@ -194,7 +207,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testArrayGetException2()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
$response['someSuccess'];
|
||||
}
|
||||
|
||||
@ -203,7 +216,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testArrayIsset()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
|
||||
static::assertTrue(
|
||||
isset($response['success']),
|
||||
@ -222,7 +235,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testArraySetException1()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
$response['success'] = 'a';
|
||||
}
|
||||
|
||||
@ -232,7 +245,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testArraySetException2()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
$response['sssssssuccess'] = 'a';
|
||||
}
|
||||
|
||||
@ -242,7 +255,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testArrayUnsetException1()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
unset($response['success']);
|
||||
}
|
||||
|
||||
@ -252,7 +265,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testArrayUnsetException2()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
unset($response['sssssssuccess']);
|
||||
}
|
||||
|
||||
@ -261,7 +274,7 @@ class ApiResponseTest extends TestCase
|
||||
*/
|
||||
public function testMagicIsset()
|
||||
{
|
||||
$response = new ApiResponse(201, '{ "success": true }');
|
||||
$response = (new ApiResponse(201, '{ "success": true }'))->asJsonResponse();
|
||||
|
||||
static::assertTrue(
|
||||
isset($response->success),
|
||||
|
Loading…
Reference in New Issue
Block a user