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

updated method

This commit is contained in:
Akolzin Dmitry 2020-09-24 13:55:04 +03:00
parent e68a23885e
commit 2672fcdebf
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' => []]);
}
}