Skip to content

Commit

Permalink
Fix for drupal check prophesize method deprecation (#755)
Browse files Browse the repository at this point in the history
  • Loading branch information
divya-intelli authored Nov 11, 2022
1 parent 6e5aa5b commit a902f6c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 64 deletions.
23 changes: 16 additions & 7 deletions tests/src/Unit/Command/CreateEdgeRoleCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Drupal\Core\Logger\LogMessageParserInterface;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Prophecy\Prophet;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -93,6 +94,13 @@ class CreateEdgeRoleCommandTest extends UnitTestCase {
*/
private $outputFormatter;

/**
* The Prophet class.
*
* @var \Prophecy\Prophet
*/
private $prophet;

/**
* {@inheritdoc}
*/
Expand All @@ -102,17 +110,18 @@ protected function setUp(): void {
}

parent::setUp();
$this->cliService = $this->prophesize(CliServiceInterface::class);
$this->logMessageParser = $this->prophesize(LogMessageParserInterface::class);
$this->loggerChannelFactory = $this->prophesize(LoggerChannelFactoryInterface::class);
$this->prophet = new Prophet();
$this->cliService = $this->prophet->prophesize(CliServiceInterface::class);
$this->logMessageParser = $this->prophet->prophesize(LogMessageParserInterface::class);
$this->loggerChannelFactory = $this->prophet->prophesize(LoggerChannelFactoryInterface::class);
$this->createEdgeRoleCommand = new CreateEdgeRoleCommand($this->cliService->reveal(),
$this->logMessageParser->reveal(), $this->loggerChannelFactory->reveal());

$this->input = $this->prophesize(InputInterface::class);
$this->output = $this->prophesize(OutputInterface::class);
$this->io = $this->prophesize(DrupalStyle::class);
$this->input = $this->prophet->prophesize(InputInterface::class);
$this->output = $this->prophet->prophesize(OutputInterface::class);
$this->io = $this->prophet->prophesize(DrupalStyle::class);

$this->outputFormatter = $this->prophesize(OutputFormatterInterface::class)->reveal();
$this->outputFormatter = $this->prophet->prophesize(OutputFormatterInterface::class)->reveal();
$this->output->getFormatter()->willReturn($this->outputFormatter);
$this->output->getVerbosity()->willReturn(OutputInterface::VERBOSITY_DEBUG);
}
Expand Down
88 changes: 49 additions & 39 deletions tests/src/Unit/Command/Util/ApigeeEdgeManagementCliServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Psr7\Response;
use Prophecy\Argument;
use Prophecy\Prophet;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\Console\Style\StyleInterface;

Expand Down Expand Up @@ -82,28 +83,37 @@ class ApigeeEdgeManagementCliServiceTest extends UnitTestCase {
*/
protected $httpClient;

/**
* The Prophet class.
*
* @var \Prophecy\Prophet
*/
private $prophet;

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->httpClient = $this->prophesize(Client::class);

$this->prophet = new Prophet();
$this->httpClient = $this->prophet->prophesize(Client::class);
}

/**
* Call createEdgeRoleForDrupal with null base URL to test default base URL.
*/
public function testCreateEdgeRoleForDrupalCustomRoleAndBaseUrl() {
// Output to user should show role created and permissions set.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->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 = $this->prophet->prophesize(Response::class);
$response_org->getBody()
->shouldBeCalledTimes(1)
->willReturn('{ "name": "' . $this->org . '" }');
Expand All @@ -113,8 +123,8 @@ public function testCreateEdgeRoleForDrupalCustomRoleAndBaseUrl() {
->willReturn($response_org->reveal());

// The role should not exist yet in system.
$request_role = $this->prophesize(RequestInterface::class);
$response_role = $this->prophesize(Response::class);
$request_role = $this->prophet->prophesize(RequestInterface::class);
$response_role = $this->prophet->prophesize(Response::class);
$response_role->getStatusCode()->willReturn(404);
$exception = new ClientException('Forbidden', $request_role->reveal(), $response_role->reveal());
$this->httpClient
Expand All @@ -140,15 +150,15 @@ public function testCreateEdgeRoleForDrupalCustomRoleAndBaseUrl() {
*/
public function testCreateEdgeRoleForDrupalDefaultRoleAndBaseUrl() {
// Output to user should show role created and permissions set.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->prophesize(StyleInterface::class);
$io->success(Argument::exact('Connected to Edge org ' . $this->org . '.'))->shouldBeCalledTimes(1);
$io->success(Argument::containingString('Role ' . ApigeeEdgeManagementCliServiceInterface::DEFAULT_ROLE_NAME . ' is configured.'))->shouldBeCalledTimes(1);
$io->text(Argument::containingString('Role ' . ApigeeEdgeManagementCliServiceInterface::DEFAULT_ROLE_NAME . ' does not exist'))->shouldBeCalledTimes(1);
$io->text(Argument::containingString('Setting permissions on role ' . ApigeeEdgeManagementCliServiceInterface::DEFAULT_ROLE_NAME . '.'))->shouldBeCalledTimes(1);
$io->text(Argument::containingString('/'))->shouldBeCalledTimes(12);

// Org should exist.
$response_org = $this->prophesize(Response::class);
$response_org = $this->prophet->prophesize(Response::class);
$response_org->getBody()
->shouldBeCalledTimes(1)
->willReturn('{ "name": "' . $this->org . '" }');
Expand All @@ -158,8 +168,8 @@ public function testCreateEdgeRoleForDrupalDefaultRoleAndBaseUrl() {
->willReturn($response_org->reveal());

// The role should not exist yet in system.
$request_role = $this->prophesize(RequestInterface::class);
$response_role = $this->prophesize(Response::class);
$request_role = $this->prophet->prophesize(RequestInterface::class);
$response_role = $this->prophet->prophesize(Response::class);
$response_role->getStatusCode()->willReturn(404);
$exception = new ClientException('Forbidden', $request_role->reveal(), $response_role->reveal());
$this->httpClient
Expand All @@ -185,14 +195,14 @@ public function testCreateEdgeRoleForDrupalDefaultRoleAndBaseUrl() {
*/
public function testCreateEdgeRoleForDrupalWhenRoleExistsTestWithForceFlag() {
// Expected to output error if role does not exist.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->prophesize(StyleInterface::class);
$io->success(Argument::exact('Connected to Edge org ' . $this->org . '.'))->shouldBeCalledTimes(1);
$io->text(Argument::containingString('Setting permissions on role ' . $this->roleName . '.'))->shouldBeCalledTimes(1);
$io->text(Argument::containingString('/'))->shouldBeCalledTimes(12);
$io->success(Argument::containingString('Role ' . $this->roleName . ' is configured.'))->shouldBeCalledTimes(1);

// Return organization info.
$response_org = $this->prophesize(Response::class);
$response_org = $this->prophet->prophesize(Response::class);
$response_org->getBody()
->shouldBeCalledTimes(1)
->willReturn('{ "name": "' . $this->org . '" }');
Expand All @@ -202,7 +212,7 @@ public function testCreateEdgeRoleForDrupalWhenRoleExistsTestWithForceFlag() {
->willReturn($response_org->reveal());

// Return existing role.
$response_user_role = $this->prophesize(Response::class);
$response_user_role = $this->prophet->prophesize(Response::class);
$response_user_role->getBody()->willReturn('{ "name": "' . $this->roleName . '" }');
$this->httpClient
->get(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName), Argument::type('array'))
Expand All @@ -227,13 +237,13 @@ public function testCreateEdgeRoleForDrupalWhenRoleExistsTestWithForceFlag() {
*/
public function testCreateEdgeRoleForDrupalWhenRoleExistsTestNoForceFlag() {
// Expected to output error if role does not exist.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->prophesize(StyleInterface::class);
$io->success(Argument::exact('Connected to Edge org ' . $this->org . '.'))->shouldBeCalledTimes(1);
$io->error(Argument::containingString('Role ' . $this->roleName . ' already exists.'))->shouldBeCalledTimes(1);
$io->note(Argument::containingString('Run with --force option'))->shouldBeCalled();

// Return organization info.
$response_org = $this->prophesize(Response::class);
$response_org = $this->prophet->prophesize(Response::class);
$response_org->getBody()
->shouldBeCalledTimes(1)
->willReturn('{ "name": "' . $this->org . '" }');
Expand All @@ -243,7 +253,7 @@ public function testCreateEdgeRoleForDrupalWhenRoleExistsTestNoForceFlag() {
->willReturn($response_org->reveal());

// Return existing role.
$response_user_role = $this->prophesize(Response::class);
$response_user_role = $this->prophet->prophesize(Response::class);
$response_user_role->getBody()->willReturn('{ "name": "' . $this->roleName . '" }');
$this->httpClient
->get(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName), Argument::type('array'))
Expand All @@ -259,13 +269,13 @@ public function testCreateEdgeRoleForDrupalWhenRoleExistsTestNoForceFlag() {
public function testIsValidEdgeCredentialsBadEndpoint() {
// Mimic a invalid response for the call to get org details.
$body = "<h1>not json</h1>";
$response = $this->prophesize(Response::class);
$response = $this->prophet->prophesize(Response::class);
$response->getBody()
->shouldBeCalledTimes(1)
->willReturn($body);

// The user should see an error message.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->prophesize(StyleInterface::class);
$io->error(Argument::containingString('Unable to parse response from GET'))
->shouldBeCalledTimes(1);
$this->httpClient
Expand All @@ -284,8 +294,8 @@ public function testIsValidEdgeCredentialsBadEndpoint() {
*/
public function testIsValidEdgeCredentialsUnauthorized() {
// Invalid password returns unauthorized 403.
$request_role = $this->prophesize(RequestInterface::class);
$response_role = $this->prophesize(Response::class);
$request_role = $this->prophet->prophesize(RequestInterface::class);
$response_role = $this->prophet->prophesize(Response::class);
$response_role->getStatusCode()->willReturn(403);
$exception = new ClientException('Unauthorized', $request_role->reveal(), $response_role->reveal());
$this->httpClient
Expand All @@ -294,7 +304,7 @@ public function testIsValidEdgeCredentialsUnauthorized() {
->shouldBeCalledTimes(1);

// The user should see an error message.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->prophesize(StyleInterface::class);
$io->error(Argument::containingString('Error connecting to Apigee Edge'))
->shouldBeCalledTimes(1);
$io->note(Argument::containingString('may not have the orgadmin role for Apigee Edge org'))
Expand All @@ -310,7 +320,7 @@ public function testIsValidEdgeCredentialsUnauthorized() {
*/
public function testIsValidEdgeCredentialsValid() {
// Org should exist.
$response_org = $this->prophesize(Response::class);
$response_org = $this->prophet->prophesize(Response::class);
$response_org->getBody()
->shouldBeCalledTimes(1)
->willReturn('{ "name": "' . $this->org . '" }');
Expand All @@ -320,7 +330,7 @@ public function testIsValidEdgeCredentialsValid() {
->willReturn($response_org->reveal());

// Errors should not be called.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->prophesize(StyleInterface::class);
$io->error(Argument::type('string'))
->shouldNotBeCalled();
$io->section(Argument::type('string'))
Expand All @@ -342,7 +352,7 @@ public function testIsValidEdgeCredentialsValid() {
*/
public function testDoesRoleExistTrue() {
// Return existing role.
$response_user_role = $this->prophesize(Response::class);
$response_user_role = $this->prophet->prophesize(Response::class);
$response_user_role->getBody()->willReturn('{ "name": "' . $this->roleName . '" }');
$this->httpClient
->get(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName), Argument::type('array'))
Expand All @@ -361,8 +371,8 @@ public function testDoesRoleExistTrue() {
*/
public function testDoesRoleExistNotTrue() {
// The role should not exist in system.
$request_role = $this->prophesize(RequestInterface::class);
$response_role = $this->prophesize(Response::class);
$request_role = $this->prophet->prophesize(RequestInterface::class);
$response_role = $this->prophet->prophesize(Response::class);
$response_role->getStatusCode()->willReturn(404);
$exception = new ClientException('Forbidden', $request_role->reveal(), $response_role->reveal());
$this->httpClient
Expand All @@ -381,8 +391,8 @@ public function testDoesRoleExistNotTrue() {
*/
public function testDoesRoleExistServerErrorThrown() {
// Http client throws exception if network or server error happens.
$request = $this->prophesize(RequestInterface::class);
$response = $this->prophesize(Response::class);
$request = $this->prophet->prophesize(RequestInterface::class);
$response = $this->prophet->prophesize(Response::class);
$response->getStatusCode()->willReturn(500);
$exception = new ServerException('Server error.', $request->reveal(), $response->reveal());
$this->expectException(ServerException::class);
Expand All @@ -399,12 +409,12 @@ public function testDoesRoleExistServerErrorThrown() {
*/
public function testHandleHttpClientExceptions0Code() {
// Error message should output to user.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->prophesize(StyleInterface::class);
$io->error(Argument::containingString('Error connecting to Apigee Edge'))->shouldBeCalledTimes(1);
$io->note(Argument::containingString('Your system may not be able to connect'))->shouldBeCalledTimes(1);

// Create network error.
$exception = $this->prophesize(TransferException::class);
$exception = $this->prophet->prophesize(TransferException::class);

$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient->reveal());
$apigee_edge_management_cli_service->handleHttpClientExceptions($exception->reveal(), $io->reveal(), [$this, 'mockDt'], 'http://api.apigee.com/test', $this->org, $this->email);
Expand All @@ -415,13 +425,13 @@ public function testHandleHttpClientExceptions0Code() {
*/
public function testHandleHttpClientExceptions401Code() {
// Server returns 401 unauthorized.
$request = $this->prophesize(RequestInterface::class);
$response = $this->prophesize(Response::class);
$request = $this->prophet->prophesize(RequestInterface::class);
$response = $this->prophet->prophesize(Response::class);
$response->getStatusCode()->willReturn(401);
$exception = new ClientException('Unauthorized', $request->reveal(), $response->reveal());

// Expect user friendly message displayed about error.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->prophesize(StyleInterface::class);
$io->error(Argument::containingString('Error connecting to Apigee Edge'))->shouldBeCalledTimes(1);
$io->note(Argument::exact('Your username or password is invalid.'))->shouldBeCalledTimes(1);

Expand All @@ -434,13 +444,13 @@ public function testHandleHttpClientExceptions401Code() {
*/
public function testHandleHttpClientExceptions403Code() {
// Server returns 403 forbidden.
$request = $this->prophesize(RequestInterface::class);
$response = $this->prophesize(Response::class);
$request = $this->prophet->prophesize(RequestInterface::class);
$response = $this->prophet->prophesize(Response::class);
$response->getStatusCode()->willReturn(403);
$exception = new ClientException('Forbidden', $request->reveal(), $response->reveal());

// Expect error messages.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->prophesize(StyleInterface::class);
$io->error(Argument::containingString('Error connecting to Apigee Edge'))->shouldBeCalledTimes(1);
$io->note(Argument::containingString('User ' . $this->email . ' may not have the orgadmin role'))->shouldBeCalledTimes(1);

Expand All @@ -453,13 +463,13 @@ public function testHandleHttpClientExceptions403Code() {
*/
public function testHandleHttpClientExceptions302Code() {
// Return a 302 redirection response, which Apigee API would not do.
$request = $this->prophesize(RequestInterface::class);
$response = $this->prophesize(Response::class);
$request = $this->prophet->prophesize(RequestInterface::class);
$response = $this->prophet->prophesize(Response::class);
$response->getStatusCode()->willReturn(302);
$exception = new ClientException('Forbidden', $request->reveal(), $response->reveal());

// User should see error message.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->prophesize(StyleInterface::class);
$io->error(Argument::containingString('Error connecting to Apigee Edge'))->shouldBeCalledTimes(1);
$io->note(Argument::containingString('the url ' . $this->baseUrl . '/test' . ' does not seem to be a valid Apigee Edge endpoint.'))->shouldBeCalledTimes(1);

Expand All @@ -483,7 +493,7 @@ public function testSetDefaultPermissions() {
$method_set_default_permissions->setAccessible(TRUE);

// Create input params.
$io = $this->prophesize(StyleInterface::class);
$io = $this->prophet->prophesize(StyleInterface::class);
$args = [
$io->reveal(),
[$this, 'mockDt'],
Expand Down
Loading

0 comments on commit a902f6c

Please sign in to comment.