diff --git a/lib/RetailCrm/Methods/V5/Files.php b/lib/RetailCrm/Methods/V5/Files.php index bc711ec..e5bb08e 100644 --- a/lib/RetailCrm/Methods/V5/Files.php +++ b/lib/RetailCrm/Methods/V5/Files.php @@ -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] ); } } diff --git a/tests/RetailCrm/Tests/Methods/Version5/ApiClientFilesTest.php b/tests/RetailCrm/Tests/Methods/Version5/ApiClientFilesTest.php index e588180..c78a4a7 100644 --- a/tests/RetailCrm/Tests/Methods/Version5/ApiClientFilesTest.php +++ b/tests/RetailCrm/Tests/Methods/Version5/ApiClientFilesTest.php @@ -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' => []]); + } }