You are here

class LingotekApiUnitTest in Lingotek Translation 8

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

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

Hierarchy

Expanded class hierarchy of LingotekApiUnitTest

File

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

Namespace

Drupal\Tests\lingotek\Unit\Remote
View source
class LingotekApiUnitTest extends UnitTestCase {

  /**
   * @var \Drupal\lingotek\Remote\LingotekApi
   */
  protected $lingotek_api;

  /**
   * @var \Drupal\lingotek\Remote\LingotekHttpInterface
   */
  protected $client;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->client = $this
      ->getMockBuilder('\\Drupal\\lingotek\\Remote\\LingotekHttpInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $logger = $this
      ->getMockBuilder('\\Psr\\Log\\LoggerInterface')
      ->getMock();
    $this->lingotek_api = new LingotekApi($this->client, $logger);
  }

  /**
   * @covers ::getDocument
   */
  public function testGetDocument() {

    // Ensure that the right call is done for testing if a document is imported.
    $response = $this
      ->getMockBuilder('\\Psr\\Http\\Message\\ResponseInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $this->client
      ->expects($this
      ->at(0))
      ->method('get')
      ->with('/api/document/fancy-document-id/status')
      ->will($this
      ->returnValue($response));
    $this->lingotek_api
      ->getDocument('fancy-document-id');
  }

  /**
   * @covers ::addTranslation
   */
  public function testAddTranslation() {

    // Ensure that the workflow is set when it's need to be.
    $response = $this
      ->getMockBuilder('\\Psr\\Http\\Message\\ResponseInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $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
      ->returnValue($response));
    $this->client
      ->expects($this
      ->at(1))
      ->method('post')
      ->with('/api/document/fancy-document-id/translation', [
      'locale_code' => 'es_ES',
    ])
      ->will($this
      ->returnValue($response));
    $this->lingotek_api
      ->addTranslation('fancy-document-id', 'es_ES', 'my_workflow');
    $this->lingotek_api
      ->addTranslation('fancy-document-id', 'es_ES', NULL);
  }

  /**
   * @covers ::addTranslation
   */
  public function testAddTranslationWithException() {

    // Ensure that the workflow is set when it's need to be.
    $request = $this
      ->getMockBuilder('\\Psr\\Http\\Message\\RequestInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $response = $this
      ->getMockBuilder('\\Psr\\Http\\Message\\ResponseInterface')
      ->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://cms.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.');
  }

  /**
   * @covers ::deleteDocument
   */
  public function testDeleteDocument() {
    $response = $this
      ->getMockBuilder('\\Psr\\Http\\Message\\ResponseInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $response
      ->expects($this
      ->any())
      ->method('getStatusCode')
      ->willReturn(Response::HTTP_ACCEPTED);
    $this->client
      ->expects($this
      ->at(0))
      ->method('delete')
      ->with('/api/document/fancy-document-id')
      ->will($this
      ->returnValue($response));
    $response = $this->lingotek_api
      ->deleteDocument('fancy-document-id');
    $this
      ->assertEquals($response
      ->getStatusCode(), Response::HTTP_ACCEPTED);
  }

  /**
   * @covers ::deleteDocument
   */
  public function testDeleteDocumentThatDoesntExist() {
    $this->client
      ->expects($this
      ->at(0))
      ->method('delete')
      ->with('/api/document/fancy-document-id')
      ->will($this
      ->throwException(new \Exception('', Response::HTTP_NOT_FOUND)));
    $response = $this->lingotek_api
      ->deleteDocument('fancy-document-id');
    $this
      ->assertEquals($response
      ->getStatusCode(), Response::HTTP_NOT_FOUND);
  }

  /**
   * @covers ::getCommunities
   */
  public function testGetCommunities() {

    // Ensure that the limit is set.
    $response = $this
      ->getMockBuilder('\\Psr\\Http\\Message\\ResponseInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $this->client
      ->expects($this
      ->once())
      ->method('get')
      ->with('/api/community', [
      'limit' => 100,
    ])
      ->will($this
      ->returnValue($response));
    $this->lingotek_api
      ->getCommunities();
  }

  /**
   * @covers ::getProjects
   */
  public function testGetProjects() {

    // Ensure that the limit and the community_id are set.
    $response = $this
      ->getMockBuilder('\\Psr\\Http\\Message\\ResponseInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $this->client
      ->expects($this
      ->once())
      ->method('get')
      ->with('/api/project', [
      'community_id' => 'my_community_id',
      'limit' => 1000,
    ])
      ->will($this
      ->returnValue($response));
    $this->lingotek_api
      ->getProjects('my_community_id');
  }

  /**
   * @covers ::getVaults
   */
  public function testGetVaults() {

    // Ensure that the limit is set and the community_id is ignored.
    $response = $this
      ->getMockBuilder('\\Psr\\Http\\Message\\ResponseInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $this->client
      ->expects($this
      ->once())
      ->method('get')
      ->with('/api/vault', [
      'limit' => 100,
      'is_owned' => 'TRUE',
    ])
      ->will($this
      ->returnValue($response));
    $this->lingotek_api
      ->getVaults('community_id');
  }

  /**
   * @covers ::getWorkflows
   */
  public function testGetWorkflows() {
    $community_id = 'my_community_id';

    // Ensure that the limit is set.
    $response = $this
      ->getMockBuilder('\\Psr\\Http\\Message\\ResponseInterface')
      ->disableOriginalConstructor()
      ->getMock();
    $this->client
      ->expects($this
      ->once())
      ->method('get')
      ->with('/api/workflow', [
      'community_id' => $community_id,
      'limit' => 1000,
    ])
      ->will($this
      ->returnValue($response));
    $this->lingotek_api
      ->getWorkflows($community_id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LingotekApiUnitTest::$client protected property
LingotekApiUnitTest::$lingotek_api protected property
LingotekApiUnitTest::setUp protected function Overrides UnitTestCase::setUp
LingotekApiUnitTest::testAddTranslation public function @covers ::addTranslation
LingotekApiUnitTest::testAddTranslationWithException public function @covers ::addTranslation
LingotekApiUnitTest::testDeleteDocument public function @covers ::deleteDocument
LingotekApiUnitTest::testDeleteDocumentThatDoesntExist public function @covers ::deleteDocument
LingotekApiUnitTest::testGetCommunities public function @covers ::getCommunities
LingotekApiUnitTest::testGetDocument public function @covers ::getDocument
LingotekApiUnitTest::testGetProjects public function @covers ::getProjects
LingotekApiUnitTest::testGetVaults public function @covers ::getVaults
LingotekApiUnitTest::testGetWorkflows public function @covers ::getWorkflows
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.