You are here

public function ApigeeEdgeManagementCliServiceTest::testCreateEdgeRoleForDrupalCustomRoleAndBaseUrl in Apigee Edge 8

Call createEdgeRoleForDrupal with null base URL to test default base URL.

File

tests/src/Unit/Command/Util/ApigeeEdgeManagementCliServiceTest.php, line 96

Class

ApigeeEdgeManagementCliServiceTest
Test ApigeeEdgeManagementCliService.

Namespace

Drupal\Tests\apigee_edge\Unit\Command\Util

Code

public function testCreateEdgeRoleForDrupalCustomRoleAndBaseUrl() {

  // Output to user should show role created and permissions set.
  $io = $this
    ->prophesize(StyleInterface::class);
  $io
    ->success(Argument::exact('Connected to Edge org ' . $this->org . '.'))
    ->shouldBeCalledTimes(1);
  $io
    ->success(Argument::containingString('Role ' . $this->roleName . ' is configured.'))
    ->shouldBeCalledTimes(1);
  $io
    ->text(Argument::containingString('Role ' . $this->roleName . ' does not exist. Creating role.'))
    ->shouldBeCalledTimes(1);
  $io
    ->text(Argument::containingString('Setting permissions on role ' . $this->roleName . '.'))
    ->shouldBeCalledTimes(1);
  $io
    ->text(Argument::containingString('/'))
    ->shouldBeCalledTimes(12);

  // Org should exist.
  $response_org = $this
    ->prophesize(Response::class);
  $response_org
    ->getBody()
    ->shouldBeCalledTimes(1)
    ->willReturn('{ "name": "' . $this->org . '" }');
  $this->httpClient
    ->get(Argument::exact($this->baseUrl . '/o/' . $this->org), Argument::type('array'))
    ->shouldBeCalledTimes(1)
    ->willReturn($response_org
    ->reveal());

  // The role should not exist yet in system.
  $request_role = $this
    ->prophesize(RequestInterface::class);
  $response_role = $this
    ->prophesize(Response::class);
  $response_role
    ->getStatusCode()
    ->willReturn(404);
  $exception = new ClientException('Forbidden', $request_role
    ->reveal(), $response_role
    ->reveal());
  $this->httpClient
    ->get(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName), Argument::type('array'))
    ->willThrow($exception);

  // The role should be created.
  $this->httpClient
    ->post(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles'), Argument::type('array'))
    ->shouldBeCalledTimes(1);

  // The permissions should be set properly.
  $this->httpClient
    ->post(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName . '/permissions'), Argument::type('array'))
    ->shouldBeCalledTimes(12);
  $apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
    ->reveal());
  $apigee_edge_management_cli_service
    ->createEdgeRoleForDrupal($io
    ->reveal(), [
    $this,
    'mockDt',
  ], $this->org, $this->email, $this->password, $this->baseUrl, $this->roleName, FALSE);
}