Skip to content

Commit

Permalink
Invalid translateBatch response when $strings is not zero indexed arr…
Browse files Browse the repository at this point in the history
…ay. (#321)
  • Loading branch information
povils authored and dwsupplee committed Jan 24, 2017
1 parent 985f916 commit 65b3e97
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Translate/TranslateClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public function translateBatch(array $strings, array $options = [])
]);

$translations = [];
$strings = array_values($strings);

if (isset($response['data']['translations'])) {
foreach ($response['data']['translations'] as $key => $translation) {
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/Translate/TranslateClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,37 @@ public function testTranslateBatch()
$this->assertEquals($expected2, $translations[1]);
}

public function testTranslateBatchWithNotZeroIndexedInput()
{
$expected1 = $this->getTranslateExpectedData('translate', 'translated', 'en');
$expected2 = $this->getTranslateExpectedData('translate2', 'translated2', 'en');
$stringsToTranslate = [1 => $expected1['input'], 2 => $expected2['input']];

$target = 'de';
$this->connection
->listTranslations([
'target' => $target,
'q' => $stringsToTranslate,
'key' => $this->key,
'model' => 'base'
])
->willReturn([
'data' => [
'translations' => [
$this->getTranslateApiData($expected1['text'], $expected1['source']),
$this->getTranslateApiData($expected2['text'], $expected2['source'])
]
]
])
->shouldBeCalledTimes(1);
$client = new TranslateTestClient(['key' => $this->key, 'target' => $target]);
$client->setConnection($this->connection->reveal());
$translations = $client->translateBatch($stringsToTranslate, ['model' => 'base']);

$this->assertEquals($expected1, $translations[0]);
$this->assertEquals($expected2, $translations[1]);
}

public function testDetectLanguage()
{
$expected = $this->getDetectionExpectedData('text', 'en', .5);
Expand Down

0 comments on commit 65b3e97

Please sign in to comment.