You are here

class ContextApiTest in TMGMT Translator Smartling 8.4

Same name and namespace in other branches
  1. 8.2 api-sdk-php/tests/unit/ContextApiTest.php \Smartling\Tests\ContextApiTest
  2. 8.2 vendor/smartling/api-sdk-php/tests/unit/ContextApiTest.php \Smartling\Tests\ContextApiTest
  3. 8.3 vendor/smartling/api-sdk-php/tests/unit/ContextApiTest.php \Smartling\Tests\ContextApiTest

Test class for Smartling\Context\ContextApi.

Hierarchy

Expanded class hierarchy of ContextApiTest

File

vendor/smartling/api-sdk-php/tests/unit/ContextApiTest.php, line 16

Namespace

Smartling\Tests
View source
class ContextApiTest extends ApiTestAbstract {

  /**
   * Sets up the fixture, for example, opens a network connection.
   * This method is called before a test is executed.
   */
  protected function setUp() {
    parent::setUp();
    $this
      ->prepareContextApiMock();
  }
  private function prepareContextApiMock() {
    $this->object = $this
      ->getMockBuilder('Smartling\\Context\\ContextApi')
      ->setMethods([
      'readFile',
    ])
      ->setConstructorArgs([
      $this->projectId,
      $this->client,
      null,
      ContextApi::ENDPOINT_URL,
    ])
      ->getMock();
    $this->object
      ->expects(self::any())
      ->method('readFile')
      ->willReturn($this->streamPlaceholder);
    $this
      ->invokeMethod($this->object, 'setAuth', [
      $this->authProvider,
    ]);
  }

  /**
   * @covers \Smartling\Context\ContextApi::uploadContext
   */
  public function testUploadContext() {
    $params = new UploadContextParameters();
    $params
      ->setContent('./tests/resources/context.html');
    $params
      ->setName('test_context.html');
    $endpointUrl = vsprintf('%s/%s/contexts', [
      ContextApi::ENDPOINT_URL,
      $this->projectId,
    ]);
    $this->client
      ->expects(self::once())
      ->method('request')
      ->with('post', $endpointUrl, [
      'headers' => [
        'Accept' => 'application/json',
        'Authorization' => vsprintf('%s %s', [
          $this->authProvider
            ->getTokenType(),
          $this->authProvider
            ->getAccessToken(),
        ]),
        'X-SL-Context-Source' => $this
          ->invokeMethod($this->object, 'getXSLContextSourceHeader'),
      ],
      'exceptions' => FALSE,
      'multipart' => [
        [
          'name' => 'content',
          'contents' => $this->streamPlaceholder,
        ],
        [
          'name' => 'name',
          'contents' => 'test_context.html',
        ],
      ],
    ])
      ->willReturn($this->responseMock);
    $this->object
      ->uploadContext($params);
  }

  /**
   * @covers \Smartling\Context\ContextApi::uploadContext
   */
  public function testMatchContext() {
    $fileUri = './tests/resources/context.html';
    $contextUid = 'someContextUid';
    $params = new MatchContextParameters();
    $params
      ->setContentFileUri($fileUri);
    $endpointUrl = vsprintf('%s/%s/contexts/%s/match/async', [
      ContextApi::ENDPOINT_URL,
      $this->projectId,
      $contextUid,
    ]);
    $this->client
      ->expects(self::once())
      ->method('request')
      ->with('post', $endpointUrl, [
      'headers' => [
        'Accept' => 'application/json',
        'Authorization' => vsprintf('%s %s', [
          $this->authProvider
            ->getTokenType(),
          $this->authProvider
            ->getAccessToken(),
        ]),
        'X-SL-Context-Source' => $this
          ->invokeMethod($this->object, 'getXSLContextSourceHeader'),
      ],
      'exceptions' => FALSE,
      'json' => [
        'contentFileUri' => $fileUri,
      ],
    ])
      ->willReturn($this->responseMock);
    $this->object
      ->matchContext($contextUid, $params);
  }

  /**
   * @covers \Smartling\Context\ContextApi::uploadAndMatchContext
   */
  public function testUploadAndMatchContext() {
    $fileUri = './tests/resources/context.html';
    $matchParams = new MatchContextParameters();
    $matchParams
      ->setContentFileUri($fileUri);
    $params = new UploadContextParameters();
    $params
      ->setContent($fileUri);
    $params
      ->setMatchParams($matchParams);
    $endpointUrl = vsprintf('%s/%s/contexts/upload-and-match-async', [
      ContextApi::ENDPOINT_URL,
      $this->projectId,
    ]);
    $this->client
      ->expects(self::once())
      ->method('request')
      ->with('post', $endpointUrl, [
      'headers' => [
        'Accept' => 'application/json',
        'Authorization' => vsprintf('%s %s', [
          $this->authProvider
            ->getTokenType(),
          $this->authProvider
            ->getAccessToken(),
        ]),
        'X-SL-Context-Source' => $this
          ->invokeMethod($this->object, 'getXSLContextSourceHeader'),
      ],
      'exceptions' => FALSE,
      'multipart' => [
        [
          'name' => 'content',
          'contents' => $this->streamPlaceholder,
        ],
        [
          'name' => 'matchParams',
          'contents' => '{"contentFileUri":".\\/tests\\/resources\\/context.html"}',
          'headers' => [
            'Content-Type' => 'application/json',
          ],
        ],
      ],
    ])
      ->willReturn($this->responseMock);
    $this->object
      ->uploadAndMatchContext($params);
  }

  /**
   * @covers \Smartling\Context\ContextApi::getMissingResources
   */
  public function testGetMatchStatus() {
    $matchId = 'test_match_id';
    $endpointUrl = vsprintf('%s/%s/match/%s', [
      ContextApi::ENDPOINT_URL,
      $this->projectId,
      $matchId,
    ]);
    $this->client
      ->expects(self::once())
      ->method('request')
      ->with('get', $endpointUrl, [
      'headers' => [
        'Accept' => 'application/json',
        'Authorization' => vsprintf('%s %s', [
          $this->authProvider
            ->getTokenType(),
          $this->authProvider
            ->getAccessToken(),
        ]),
        'X-SL-Context-Source' => $this
          ->invokeMethod($this->object, 'getXSLContextSourceHeader'),
      ],
      'exceptions' => FALSE,
      'query' => [],
    ])
      ->willReturn($this->responseMock);
    $this->object
      ->getMatchStatus($matchId);
  }

  /**
   * @covers \Smartling\Context\ContextApi::getMissingResources
   */
  public function testGetMissingResources() {
    $offset = 'some_offset';
    $params = new MissingResourcesParameters();
    $params
      ->setOffset($offset);
    $endpointUrl = vsprintf('%s/%s/missing-resources', [
      ContextApi::ENDPOINT_URL,
      $this->projectId,
    ]);
    $this->client
      ->expects(self::once())
      ->method('request')
      ->with('get', $endpointUrl, [
      'headers' => [
        'Accept' => 'application/json',
        'Authorization' => vsprintf('%s %s', [
          $this->authProvider
            ->getTokenType(),
          $this->authProvider
            ->getAccessToken(),
        ]),
        'X-SL-Context-Source' => $this
          ->invokeMethod($this->object, 'getXSLContextSourceHeader'),
      ],
      'exceptions' => FALSE,
      'query' => [
        'offset' => $offset,
      ],
    ])
      ->willReturn($this->responseMock);
    $this->object
      ->getMissingResources($params);
  }

  /**
   * @covers \Smartling\Context\ContextApi::uploadResource
   */
  public function testUploadResource() {
    $resourceId = 'some_resource_id';
    $params = new UploadResourceParameters();
    $params
      ->setFile('./tests/resources/test.png');
    $endpointUrl = vsprintf('%s/%s/resources/%s', [
      ContextApi::ENDPOINT_URL,
      $this->projectId,
      $resourceId,
    ]);
    $this->client
      ->expects(self::once())
      ->method('request')
      ->with('put', $endpointUrl, [
      'headers' => [
        'Accept' => 'application/json',
        'Authorization' => vsprintf('%s %s', [
          $this->authProvider
            ->getTokenType(),
          $this->authProvider
            ->getAccessToken(),
        ]),
        'X-SL-Context-Source' => $this
          ->invokeMethod($this->object, 'getXSLContextSourceHeader'),
      ],
      'exceptions' => FALSE,
      'multipart' => [
        [
          'name' => 'resource',
          'contents' => $this->streamPlaceholder,
        ],
      ],
    ])
      ->willReturn($this->responseMock);
    $this->object
      ->uploadResource($resourceId, $params);
  }

  /**
   * @covers \Smartling\Context\ContextApi::renderContext
   */
  public function testRenderContext() {
    $contextUid = 'someContextUid';
    $endpointUrl = vsprintf('%s/%s/contexts/%s/render', [
      ContextApi::ENDPOINT_URL,
      $this->projectId,
      $contextUid,
    ]);
    $this->client
      ->expects(self::once())
      ->method('request')
      ->with('post', $endpointUrl, [
      'headers' => [
        'Accept' => 'application/json',
        'Authorization' => vsprintf('%s %s', [
          $this->authProvider
            ->getTokenType(),
          $this->authProvider
            ->getAccessToken(),
        ]),
        'X-SL-Context-Source' => $this
          ->invokeMethod($this->object, 'getXSLContextSourceHeader'),
      ],
      'exceptions' => FALSE,
      'form_params' => [],
    ])
      ->willReturn($this->responseMock);
    $this->object
      ->renderContext($contextUid);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ApiTestAbstract::$authProvider protected property
ApiTestAbstract::$client protected property
ApiTestAbstract::$clientInterfaceMethods protected static property
ApiTestAbstract::$hasEmitterInterfaceMethods protected static property
ApiTestAbstract::$messageInterfaceMethods protected static property
ApiTestAbstract::$object protected property
ApiTestAbstract::$projectId protected property
ApiTestAbstract::$requestInterfaceMethods protected static property
ApiTestAbstract::$requestMock protected property
ApiTestAbstract::$responseAsync protected property 1
ApiTestAbstract::$responseInterfaceMethods protected static property
ApiTestAbstract::$responseMock protected property
ApiTestAbstract::$responseWithException protected property
ApiTestAbstract::$secretKey protected property
ApiTestAbstract::$streamPlaceholder protected property
ApiTestAbstract::$userIdentifier protected property
ApiTestAbstract::$validResponse protected property
ApiTestAbstract::invokeMethod protected function Invokes protected or private method of given object.
ApiTestAbstract::JSON_OBJECT_AS_ARRAY constant
ApiTestAbstract::prepareAuthProviderMock protected function
ApiTestAbstract::prepareClientResponseMock protected function
ApiTestAbstract::prepareHttpClientMock protected function
ApiTestAbstract::readProperty protected function Reads protected or private property of given object.
ContextApiTest::prepareContextApiMock private function
ContextApiTest::setUp protected function Sets up the fixture, for example, opens a network connection. This method is called before a test is executed. Overrides ApiTestAbstract::setUp
ContextApiTest::testGetMatchStatus public function @covers \Smartling\Context\ContextApi::getMissingResources
ContextApiTest::testGetMissingResources public function @covers \Smartling\Context\ContextApi::getMissingResources
ContextApiTest::testMatchContext public function @covers \Smartling\Context\ContextApi::uploadContext
ContextApiTest::testRenderContext public function @covers \Smartling\Context\ContextApi::renderContext
ContextApiTest::testUploadAndMatchContext public function @covers \Smartling\Context\ContextApi::uploadAndMatchContext
ContextApiTest::testUploadContext public function @covers \Smartling\Context\ContextApi::uploadContext
ContextApiTest::testUploadResource public function @covers \Smartling\Context\ContextApi::uploadResource