You are here

class RestClientTest in Salesforce Suite 8.3

Same name in this branch
  1. 8.3 tests/src/Unit/RestClientTest.php \Drupal\Tests\salesforce\Unit\RestClientTest
  2. 8.3 modules/salesforce_encrypt/tests/src/Unit/RestClientTest.php \Drupal\Tests\salesforce_encrypt\Unit\RestClientTest

@coversDefaultClass \Drupal\salesforce_encrypt\Rest\RestClient @group salesforce

Hierarchy

Expanded class hierarchy of RestClientTest

File

modules/salesforce_encrypt/tests/src/Unit/RestClientTest.php, line 22

Namespace

Drupal\Tests\salesforce_encrypt\Unit
View source
class RestClientTest extends UnitTestCase {
  public static $modules = [
    'key',
    'encrypt',
    'salesforce',
    'salesforce_encrypt',
  ];
  protected $httpClient;
  protected $configFactory;
  protected $state;
  protected $cache;
  protected $json;
  protected $time;
  protected $encryption;
  protected $profileManager;
  protected $lock;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->accessToken = 'foo';
    $this->refreshToken = 'bar';
    $this->identity = [
      'zee' => 'bang',
    ];
    $this->httpClient = $this
      ->getMock(Client::CLASS);
    $this->configFactory = $this
      ->getMockBuilder(ConfigFactory::CLASS)
      ->disableOriginalConstructor()
      ->getMock();
    $this->state = $this
      ->getMockBuilder(State::CLASS)
      ->disableOriginalConstructor()
      ->getMock();
    $this->cache = $this
      ->createMock(CacheBackendInterface::CLASS);
    $this->json = $this
      ->createMock('Drupal\\Component\\Serialization\\Json');
    $this->encryption = $this
      ->createMock(EncryptServiceInterface::CLASS);
    $this->profileManager = $this
      ->createMock(EncryptionProfileManagerInterface::CLASS);
    $this->lock = $this
      ->createMock(LockBackendInterface::CLASS);
    $this->encryptionProfile = $this
      ->createMock(EncryptionProfileInterface::CLASS);
    $this->json = $this
      ->createMock(Json::CLASS);
    $this->time = $this
      ->createMock(TimeInterface::CLASS);
    $this->client = $this
      ->getMockBuilder(RestClient::CLASS)
      ->setMethods([
      'doGetEncryptionProfile',
    ])
      ->setConstructorArgs([
      $this->httpClient,
      $this->configFactory,
      $this->state,
      $this->cache,
      $this->json,
      $this->time,
      $this->encryption,
      $this->profileManager,
      $this->lock,
    ])
      ->getMock();
  }

  /**
   * @covers ::encrypt
   *
   * encrypt is protected, so we get at it through ::getAccessToken
   * This test covers the case where access token is NULL.
   */
  public function testEncryptNull() {

    // Test unencrypted.
    $this->state
      ->expects($this
      ->any())
      ->method('get')
      ->willReturn(NULL);
    $this->client
      ->expects($this
      ->any())
      ->method('doGetEncryptionProfile')
      ->willReturn(NULL);
    $this
      ->assertFalse($this->client
      ->getAccessToken());
  }

  /**
   * @covers ::encrypt
   *
   * This test covers the case where access token is not NULL.
   */
  public function testEncryptNotNull() {

    // Test unencrypted.
    $this->state
      ->expects($this
      ->any())
      ->method('get')
      ->willReturn('not null');
    $this->client
      ->expects($this
      ->any())
      ->method('doGetEncryptionProfile')
      ->willReturn($this->encryptionProfile);
    $this->encryption
      ->expects($this
      ->any())
      ->method('decrypt')
      ->willReturn($this->accessToken);
    $this
      ->assertEquals($this->accessToken, $this->client
      ->getAccessToken());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
RestClientTest::$cache protected property
RestClientTest::$configFactory protected property
RestClientTest::$encryption protected property
RestClientTest::$httpClient protected property
RestClientTest::$json protected property
RestClientTest::$lock protected property
RestClientTest::$modules public static property
RestClientTest::$profileManager protected property
RestClientTest::$state protected property
RestClientTest::$time protected property
RestClientTest::setUp public function Overrides UnitTestCase::setUp
RestClientTest::testEncryptNotNull public function @covers ::encrypt
RestClientTest::testEncryptNull public function @covers ::encrypt
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.