You are here

class SalesforceControllerTest in Salesforce Suite 8.3

@coversDefaultClass \Drupal\salesforce\Controller\SalesforceController @group salesforce

Hierarchy

Expanded class hierarchy of SalesforceControllerTest

File

tests/src/Unit/SalesforceControllerTest.php, line 22

Namespace

Drupal\Tests\salesforce\Unit
View source
class SalesforceControllerTest extends UnitTestCase {

  /**
   * Set up for each test.
   */
  public function setUp() {
    parent::setUp();
    $this->example_url = 'https://example.com';
    $this->httpClient = $this
      ->getMock('\\GuzzleHttp\\Client', [
      'post',
    ]);
    $this->httpClient
      ->expects($this
      ->once())
      ->method('post')
      ->willReturn(new Response());
    $this->configFactory = $this
      ->getMockBuilder('\\Drupal\\Core\\Config\\ConfigFactory')
      ->disableOriginalConstructor()
      ->getMock();
    $this->state = $this
      ->getMockBuilder('\\Drupal\\Core\\State\\State')
      ->disableOriginalConstructor()
      ->getMock();
    $this->cache = $this
      ->getMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
    $this->json = $this
      ->getMock(Json::CLASS);
    $this->time = $this
      ->getMock(TimeInterface::CLASS);
    $args = [
      $this->httpClient,
      $this->configFactory,
      $this->state,
      $this->cache,
      $this->json,
      $this->time,
    ];
    $this->client = $this
      ->getMock(RestClient::class, [
      'getConsumerKey',
      'getConsumerSecret',
      'getAuthCallbackUrl',
      'getAuthTokenUrl',
      'handleAuthResponse',
    ], $args);
    $this->client
      ->expects($this
      ->once())
      ->method('getConsumerKey')
      ->willReturn($this
      ->randomMachineName());
    $this->client
      ->expects($this
      ->once())
      ->method('getConsumerSecret')
      ->willReturn($this
      ->randomMachineName());
    $this->client
      ->expects($this
      ->once())
      ->method('getAuthCallbackUrl')
      ->willReturn($this->example_url);
    $this->client
      ->expects($this
      ->once())
      ->method('getAuthTokenUrl')
      ->willReturn($this->example_url);
    $this->client
      ->expects($this
      ->once())
      ->method('handleAuthResponse')
      ->willReturn($this->client);
    $this->request = new Request([
      'code' => $this
        ->randomMachineName(),
    ]);
    $this->request_stack = $this
      ->getMock(RequestStack::class);
    $this->request_stack
      ->expects($this
      ->exactly(2))
      ->method('getCurrentRequest')
      ->willReturn($this->request);
    $this->url_generator = $this
      ->prophesize(MetadataBubblingUrlGenerator::class);
    $this->url_generator
      ->generateFromRoute('salesforce.authorize', [], [
      "absolute" => TRUE,
    ], FALSE)
      ->willReturn('foo/bar');
    $container = new ContainerBuilder();
    $container
      ->set('salesforce.client', $this->client);
    $container
      ->set('http_client', $this->httpClient);
    $container
      ->set('request_stack', $this->request_stack);
    $container
      ->set('url.generator', $this->url_generator
      ->reveal());
    $container
      ->set('datetime.time', $this->time);
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::oauthCallback
   */
  public function testOauthCallback() {
    $this->controller = $this
      ->getMock(SalesforceController::class, [
      'successMessage',
    ], [
      $this->client,
      $this->httpClient,
      $this->url_generator
        ->reveal(),
    ]);
    $this->controller
      ->expects($this
      ->once())
      ->method('successMessage')
      ->willReturn(NULL);
    $expected = new RedirectResponse('foo/bar');
    $actual = $this->controller
      ->oauthCallback();
    $this
      ->assertEquals($expected
      ->getTargetUrl(), $actual
      ->getTargetUrl());
  }

}

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.
SalesforceControllerTest::setUp public function Set up for each test. Overrides UnitTestCase::setUp
SalesforceControllerTest::testOauthCallback public function @covers ::oauthCallback
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.