Skip to content

Commit 9e4fceb

Browse files
add batch api v2 register batch file
1 parent 9501bd3 commit 9e4fceb

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

src/Batch/BatchApiV2.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class BatchApiV2 extends BaseApiAbstract
1414
public const ENDPOINT_URL = 'https://api.smartling.com/job-batches-api/v2/projects';
1515

1616
private const ACTION_CANCEL_FILE = 'CANCEL_FILE';
17+
private const ACTION_REGISTER_FILE = 'REGISTER_FILE';
1718

1819
public function __construct(
1920
AuthApiInterface $authProvider,
@@ -87,4 +88,22 @@ function array_is_list(array $array): bool
8788
self::HTTP_METHOD_POST,
8889
)['batchUid'];
8990
}
91+
92+
/**
93+
* @throws SmartlingApiException
94+
*/
95+
public function registerBatchFile(string $batchUid, string $fileUri): void
96+
{
97+
if ($batchUid === '') {
98+
throw new \UnexpectedValueException('BatchUid cannot be empty.');
99+
}
100+
$this->sendRequest(
101+
"batches/$batchUid",
102+
$this->getDefaultRequestData('json', [
103+
'action' => self::ACTION_REGISTER_FILE,
104+
'fileUri' => $fileUri,
105+
]),
106+
self::HTTP_METHOD_PUT,
107+
);
108+
}
90109
}

tests/unit/BatchApiV2Test.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,42 @@ public function testBatchNotCreatedFileUrisNotList()
9494
1 => 'fileUri',
9595
]);
9696
}
97+
98+
public function testRegisterBatchFile()
99+
{
100+
$batchUid = 'test_batch_uid';
101+
$fileUri = '/test/file.xml';
102+
103+
$this->client
104+
->expects($this->once())
105+
->method('request')
106+
->with('put', BatchApiV2::ENDPOINT_URL . "/$this->projectId/batches/$batchUid", [
107+
'headers' => [
108+
'Accept' => 'application/json',
109+
'Authorization' => \vsprintf('%s %s', [
110+
$this->authProvider->getTokenType(),
111+
$this->authProvider->getAccessToken(),
112+
]),
113+
],
114+
'exceptions' => false,
115+
'json' => [
116+
'action' => 'REGISTER_FILE',
117+
'fileUri' => $fileUri,
118+
],
119+
])
120+
->willReturn($this->responseMock);
121+
122+
$this->throwPreviousException(function () use ($batchUid, $fileUri) {
123+
(new BatchApiV2($this->authProvider, $this->projectId, null, $this->client))
124+
->registerBatchFile($batchUid, $fileUri);
125+
});
126+
}
127+
128+
public function testRegisterBatchFileEmptyBatchUid()
129+
{
130+
$this->expectException(\UnexpectedValueException::class);
131+
$this->expectExceptionMessage('BatchUid cannot be empty.');
132+
(new BatchApiV2($this->authProvider, $this->projectId, null, $this->client))
133+
->registerBatchFile('', '/test/file.xml');
134+
}
97135
}

0 commit comments

Comments
 (0)