You are here

class AuthApiTest in TMGMT Translator Smartling 8.4

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

Test class for Smartling\AuthApi\AuthTokenProvider.

Hierarchy

  • class \Smartling\Tests\Unit\ApiTestAbstract extends \Smartling\Tests\Unit\PHPUnit_Framework_TestCase

Expanded class hierarchy of AuthApiTest

File

vendor/smartling/api-sdk-php/tests/unit/AuthApiTest.php, line 11

Namespace

Smartling\Tests
View source
class AuthApiTest 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
      ->prepareAuthApiMock();
  }
  private function prepareAuthApiMock() {
    $this->object = $this
      ->getMockBuilder('Smartling\\AuthApi\\AuthTokenProvider')
      ->setMethods(NULL)
      ->setConstructorArgs([
      $this->userIdentifier,
      $this->secretKey,
      $this->client,
    ])
      ->getMock();
  }

  /**
   * Test auth method.
   */
  public function testAuthenticate() {
    $endpointUrl = vsprintf('%s/authenticate', [
      AuthTokenProvider::ENDPOINT_URL,
    ]);
    $this->client
      ->expects(self::once())
      ->method('request')
      ->with('post', $endpointUrl, [
      'headers' => [
        'Accept' => 'application/json',
      ],
      'exceptions' => false,
      'json' => [
        'userIdentifier' => 'SomeUserIdentifier',
        'userSecret' => 'SomeSecretKey',
      ],
    ])
      ->willReturn($this->responseMock);
    $this
      ->invokeMethod($this->object, 'authenticate');
  }

  /**
   * Test auth with invalid credentials.
   *
   * If there are invalid credentials then BaseApiAbstract::sendRequest()
   * returns response with 401 status code. It means that BaseApiAbstract
   * will try to re-authenticate and send request again. But we don't need
   * to re-authenticate if sendRequest() method was called from
   * AuthTokenProvider object (invalid credentials).
   *
   * AuthTokenProvider::sendRequest() - 401 - Invalid credentials.
   * [Some]Api::sendRequest() - 401 - expired access token.
   *
   * @expectedException Smartling\Exceptions\SmartlingApiException
   * @expectedExceptionMessage AuthProvider expected to be instance of AuthApiInterface, type given:NULL
   */
  public function testAuthenticateWithInvalidCredentials() {
    $response_mock = $this
      ->getMockBuilder('GuzzleHttp\\Psr7\\Response')
      ->setMethods(array_merge(self::$responseInterfaceMethods, self::$messageInterfaceMethods))
      ->disableOriginalConstructor()
      ->getMock();
    $response_mock
      ->expects($this
      ->any())
      ->method('getStatusCode')
      ->willReturn(401);
    $client_mock = $this
      ->getMockBuilder('GuzzleHttp\\Client')
      ->setMethods(array_merge(self::$clientInterfaceMethods, self::$hasEmitterInterfaceMethods))
      ->disableOriginalConstructor()
      ->getMock();
    $client_mock
      ->expects(self::once())
      ->method('request')
      ->willReturn($response_mock);
    $auth_api_mock = $this
      ->getMockBuilder('Smartling\\AuthApi\\AuthTokenProvider')
      ->setMethods(NULL)
      ->setConstructorArgs([
      $this->userIdentifier,
      $this->secretKey,
      $client_mock,
    ])
      ->getMock();
    $this
      ->invokeMethod($auth_api_mock, 'authenticate');
  }

}

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.
AuthApiTest::prepareAuthApiMock private function
AuthApiTest::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
AuthApiTest::testAuthenticate public function Test auth method.
AuthApiTest::testAuthenticateWithInvalidCredentials public function Test auth with invalid credentials.