1
0
mirror of synced 2024-11-21 21:06:07 +03:00

Updated fileEdit method

This commit is contained in:
Alex Lushpai 2020-09-24 14:02:25 +03:00 committed by GitHub
commit d566d2b344
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 4 deletions

View File

@ -159,27 +159,57 @@ trait Files
/**
* Edit file data
*
* @param int $id file ID
* @param array $file file data
*
* $file = [
* 'filename' => 'Test file',
* 'attachment' => [
* [
* 'customer' => [
* 'id' => 1
* ],
* 'order' => [
* 'id' => 1
* ]
* ]
* ]
* ];
*
* @throws \InvalidArgumentException
* @throws \RetailCrm\Exception\CurlException
* @throws \RetailCrm\Exception\InvalidJsonException
*
* @return \RetailCrm\Response\ApiResponse
*/
public function fileEdit(array $file)
public function fileEdit($id, array $file)
{
if (empty($id)) {
throw new \InvalidArgumentException(
'Parameter `id` can`t be blank'
);
}
if (empty($file)) {
throw new \InvalidArgumentException(
'Parameter `file` must contains a data'
);
}
$allowedFields = ['filename', 'attachment'];
foreach (array_keys($file) as $field) {
if (!in_array($field, $allowedFields)) {
throw new \InvalidArgumentException(
'Invalid structure of `file` parameter'
);
}
}
/* @noinspection PhpUndefinedMethodInspection */
return $this->client->makeRequest(
sprintf('/files/%s/edit', $file['id']),
sprintf('/files/%s/edit', $id),
"POST",
['file' => json_encode($file)]
['file' => json_encode($file), 'id' => $id]
);
}
}

View File

@ -46,7 +46,6 @@ class ApiClientFilesTest extends TestCase
*/
public function testFileUpload()
{
$client = static::getApiClient();
$response = $client->request->fileUpload(__DIR__ . '/../../../Tests/Resources/Report.pdf');
@ -67,6 +66,14 @@ class ApiClientFilesTest extends TestCase
sleep(1);
$response = $client->request->fileEdit($fileId, ['filename' => 'Test file']);
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
static::assertEquals(200, $response->getStatusCode());
static::assertTrue($response->isSuccessful());
sleep(1);
$response = $client->request->fileDelete($fileId);
static::assertInstanceOf('RetailCrm\Response\ApiResponse', $response);
@ -75,4 +82,12 @@ class ApiClientFilesTest extends TestCase
sleep(1);
}
public function testFileEditFailure()
{
static::expectExceptionObject(new \InvalidArgumentException('Invalid structure of `file` parameter'));
$client = static::getApiClient();
$client->request->fileEdit(1, ['file' => []]);
}
}