You are here

public function LingotekApiUnitTest::testAddTranslationWithException in Lingotek Translation 3.1.x

Same name and namespace in other branches
  1. 8 tests/src/Unit/Remote/LingotekApiUnitTest.php \Drupal\Tests\lingotek\Unit\Remote\LingotekApiUnitTest::testAddTranslationWithException()
  2. 8.2 tests/src/Unit/Remote/LingotekApiUnitTest.php \Drupal\Tests\lingotek\Unit\Remote\LingotekApiUnitTest::testAddTranslationWithException()
  3. 4.0.x tests/src/Unit/Remote/LingotekApiUnitTest.php \Drupal\Tests\lingotek\Unit\Remote\LingotekApiUnitTest::testAddTranslationWithException()
  4. 3.0.x tests/src/Unit/Remote/LingotekApiUnitTest.php \Drupal\Tests\lingotek\Unit\Remote\LingotekApiUnitTest::testAddTranslationWithException()
  5. 3.2.x tests/src/Unit/Remote/LingotekApiUnitTest.php \Drupal\Tests\lingotek\Unit\Remote\LingotekApiUnitTest::testAddTranslationWithException()
  6. 3.3.x tests/src/Unit/Remote/LingotekApiUnitTest.php \Drupal\Tests\lingotek\Unit\Remote\LingotekApiUnitTest::testAddTranslationWithException()
  7. 3.4.x tests/src/Unit/Remote/LingotekApiUnitTest.php \Drupal\Tests\lingotek\Unit\Remote\LingotekApiUnitTest::testAddTranslationWithException()
  8. 3.5.x tests/src/Unit/Remote/LingotekApiUnitTest.php \Drupal\Tests\lingotek\Unit\Remote\LingotekApiUnitTest::testAddTranslationWithException()
  9. 3.6.x tests/src/Unit/Remote/LingotekApiUnitTest.php \Drupal\Tests\lingotek\Unit\Remote\LingotekApiUnitTest::testAddTranslationWithException()
  10. 3.7.x tests/src/Unit/Remote/LingotekApiUnitTest.php \Drupal\Tests\lingotek\Unit\Remote\LingotekApiUnitTest::testAddTranslationWithException()
  11. 3.8.x tests/src/Unit/Remote/LingotekApiUnitTest.php \Drupal\Tests\lingotek\Unit\Remote\LingotekApiUnitTest::testAddTranslationWithException()

@covers ::addTranslation

File

tests/src/Unit/Remote/LingotekApiUnitTest.php, line 74

Class

LingotekApiUnitTest
@coversDefaultClass \Drupal\lingotek\Remote\LingotekApi @group lingotek @preserveGlobalState disabled

Namespace

Drupal\Tests\lingotek\Unit\Remote

Code

public function testAddTranslationWithException() {

  // Ensure that the workflow is set when it's need to be.
  $request = $this
    ->getMockBuilder(RequestInterface::class)
    ->disableOriginalConstructor()
    ->getMock();
  $response = $this
    ->getMockBuilder(ResponseInterface::class)
    ->disableOriginalConstructor()
    ->getMock();
  $response
    ->expects($this
    ->any())
    ->method('getStatusCode')
    ->willReturn(Response::HTTP_BAD_REQUEST);
  $response
    ->expects($this
    ->any())
    ->method('getBody')
    ->willReturn(json_encode([
    'messages' => [
      'Translation (es_ES) already exists.',
    ],
  ]));
  $this->client
    ->expects($this
    ->at(0))
    ->method('post')
    ->with('/api/document/fancy-document-id/translation', [
    'locale_code' => 'es_ES',
    'workflow_id' => 'my_workflow',
  ])
    ->will($this
    ->throwException(new ClientException('Client error: `POST https://myaccount.lingotek.com/api/document/700e102b-b0ad-4ddf-9da1-73c62d587abc/translation` resulted in a `400 Bad Request` response:
{"messages":["Translation (es_ES) already exists."]}', $request, $response)));
  $response = $this->lingotek_api
    ->addTranslation('fancy-document-id', 'es_ES', 'my_workflow');
  $this
    ->assertEquals($response
    ->getStatusCode(), Response::HTTP_CREATED, 'If the translation existed, we succeed instead of failing.');
}