From 2553cd51ef7d0acafce53ce5cef5c414aecd3dd0 Mon Sep 17 00:00:00 2001 From: sayan goswami Date: Tue, 30 May 2023 18:27:36 +0530 Subject: [PATCH] Fix all issues --- .../Version/DrupalCoreVersionResolver.php | 3 + src/Enum/EnvVarEnum.php | 9 ++ src/Event/CiEvent.php | 2 +- src/Helper/Log/GoogleApiClient.php | 15 ++- tests/Console/Command/Ci/CiRunCommandTest.php | 12 +- tests/Helper/Log/GoogleApiClientTest.php | 117 ++++++++++++++++++ 6 files changed, 151 insertions(+), 7 deletions(-) create mode 100644 tests/Helper/Log/GoogleApiClientTest.php diff --git a/src/Domain/Composer/Version/DrupalCoreVersionResolver.php b/src/Domain/Composer/Version/DrupalCoreVersionResolver.php index 70c72fe17..c2cd8ea71 100644 --- a/src/Domain/Composer/Version/DrupalCoreVersionResolver.php +++ b/src/Domain/Composer/Version/DrupalCoreVersionResolver.php @@ -426,6 +426,9 @@ private function assertNextMajorLatestMinorBetaOrLaterExists(): void { $is_alpha = strpos($stability, "alpha"); $is_dev = strpos($stability, "dev"); if ($is_alpha || $is_dev) { + // Assigning to null as beta or later version does not exist. + $this->nextMajorLatestMinorBetaOrLater = NULL; + $message = "No next major, latest minor beta-or-later Drupal core version exists."; throw new OrcaVersionNotFoundException($message); } diff --git a/src/Enum/EnvVarEnum.php b/src/Enum/EnvVarEnum.php index 19a73a734..42b76e4a3 100644 --- a/src/Enum/EnvVarEnum.php +++ b/src/Enum/EnvVarEnum.php @@ -46,6 +46,12 @@ class EnvVarEnum extends Enum { public const ORCA_FIXTURE_PROFILE = 'ORCA_FIXTURE_PROFILE'; + public const ORCA_GOOGLE_API_CLIENT_ID = 'ORCA_GOOGLE_API_CLIENT_ID'; + + public const ORCA_GOOGLE_API_CLIENT_SECRET = 'ORCA_GOOGLE_API_CLIENT_SECRET'; + + public const ORCA_GOOGLE_API_REFRESH_TOKEN = 'ORCA_GOOGLE_API_REFRESH_TOKEN'; + public const ORCA_JOB = 'ORCA_JOB'; public const ORCA_PACKAGES_CONFIG = 'ORCA_PACKAGES_CONFIG'; @@ -101,6 +107,9 @@ public static function descriptions(): array { self::ORCA_ENABLE_NIGHTWATCH => 'Whether or not to run Nightwatch.js tests', self::ORCA_FIXTURE_DIR => 'The directory ORCA uses for test fixtures', self::ORCA_FIXTURE_PROFILE => 'The Drupal installation profile ORCA installs in fixtures', + self::ORCA_GOOGLE_API_CLIENT_ID => 'The Google API Client ID', + self::ORCA_GOOGLE_API_CLIENT_SECRET => 'The Google API CLIENT SECRET', + self::ORCA_GOOGLE_API_REFRESH_TOKEN => 'The Google API Refresh Token', self::ORCA_JOB => 'The name of the ORCA CI job', self::ORCA_PACKAGES_CONFIG => 'The path to a config file to completely replace the list of packages ORCA installs in fixtures and runs tests on', self::ORCA_PACKAGES_CONFIG_ALTER => 'The path to a config file to alter the main list of packages ORCA installs in fixtures and runs tests on', diff --git a/src/Event/CiEvent.php b/src/Event/CiEvent.php index 590a34aa8..9bbf30371 100644 --- a/src/Event/CiEvent.php +++ b/src/Event/CiEvent.php @@ -5,7 +5,7 @@ use Symfony\Contracts\EventDispatcher\Event; /** - * CiFailureEvent is called when an ORCA job fails. + * This event is called when an ORCA job completes. */ class CiEvent extends Event { diff --git a/src/Helper/Log/GoogleApiClient.php b/src/Helper/Log/GoogleApiClient.php index ecb58a099..37d562138 100644 --- a/src/Helper/Log/GoogleApiClient.php +++ b/src/Helper/Log/GoogleApiClient.php @@ -94,11 +94,10 @@ public function __construct(HttpClientInterface $http_client, */ public function postData(array $data): void { - // @todo skip tests that have versions defined but are not running. + // Skip tests that have versions defined but are not running. // If version is null for ex: STATIC_CODE_ANALYSIS jobs then send data // as it is. if (is_null($data['version'])) { - // @todo specify something appropriate here. $data['version'] = 'NA'; } elseif (!$this->version->existsPredefined($data['version'])) { @@ -114,6 +113,9 @@ public function postData(array $data): void { $spread_sheet_id = "1CllNKp9W1x0t_B3kKJhsJa5lMAevpxTSgIid4aOz2cE"; $sheet_id = "Sheet1"; $access_token = $this->getToken(); + if (is_null($access_token)) { + return; + } $options = [ 'auth_bearer' => $access_token, 'headers' => [ @@ -163,10 +165,13 @@ public function postData(array $data): void { /** * Gets the access token. - * - * @throws \Acquia\Orca\Exception\OrcaHttpException */ - private function getToken(): string { + public function getToken(): ?string { + + if (is_null($this->googleApiClientId) || is_null($this->googleApiClientSecret) || is_null($this->googleApiRefreshToken)) { + $this->output->comment("Operation unsuccessful!! API keys not found... "); + return NULL; + } $options = [ 'headers' => [ diff --git a/tests/Console/Command/Ci/CiRunCommandTest.php b/tests/Console/Command/Ci/CiRunCommandTest.php index 4265cabe3..5ae661e23 100644 --- a/tests/Console/Command/Ci/CiRunCommandTest.php +++ b/tests/Console/Command/Ci/CiRunCommandTest.php @@ -17,6 +17,7 @@ use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; use Symfony\Component\Console\Command\Command; +use Symfony\Component\EventDispatcher\EventDispatcher; /** * @property \Acquia\Orca\Domain\Ci\CiJobFactory|\Prophecy\Prophecy\ObjectProphecy $ciJobFactory @@ -33,6 +34,7 @@ class CiRunCommandTest extends CommandTestBase { protected AbstractCiJob|ObjectProphecy $ciJob; protected CiRunOptionsFactory|ObjectProphecy $ciRunOptionsFactory; protected CiRunOptions|ObjectProphecy $ciRunOptions; + protected EventDispatcher|ObjectProphecy $eventDispatcher; protected function setUp(): void { $this->ciRunOptions = $this->prophesize(CiRunOptions::class); @@ -41,16 +43,24 @@ protected function setUp(): void { ->create(Argument::any()) ->willReturn($this->ciRunOptions->reveal()); $this->ciJob = $this->prophesize(CiTestJob::class); + $this->ciJob + ->jobName() + ->willReturn(new CiJobEnum(CiJobEnum::STATIC_CODE_ANALYSIS)); $this->ciJobFactory = $this->prophesize(CiJobFactory::class); $this->ciJobFactory ->create($this->validJob()) ->willReturn($this->ciJob->reveal()); + $this->eventDispatcher = $this->prophesize(EventDispatcher::class); + $this->eventDispatcher + ->dispatch(Argument::cetera()) + ->willReturn(new \stdClass()); } protected function createCommand(): Command { $ci_run_options_factory = $this->ciRunOptionsFactory->reveal(); $ci_job_factory = $this->ciJobFactory->reveal(); - return new CiRunCommand($ci_job_factory, $ci_run_options_factory); + $event_dispatcher = $this->eventDispatcher->reveal(); + return new CiRunCommand($ci_job_factory, $ci_run_options_factory, $event_dispatcher); } private function validSutName(): string { diff --git a/tests/Helper/Log/GoogleApiClientTest.php b/tests/Helper/Log/GoogleApiClientTest.php new file mode 100644 index 000000000..76c8f9c25 --- /dev/null +++ b/tests/Helper/Log/GoogleApiClientTest.php @@ -0,0 +1,117 @@ +version = $this->prophesize(DrupalCoreVersionResolver::class); + + $response = $this->prophesize(ResponseInterface::class); + $response->getStatusCode() + ->willReturn(200); + $response->toArray() + ->willReturn([ + 'access_token' => 'sample token', + 'updates' => [ + 'updatedData' => [ + 'values' => [ + '0' => ['Sample Data', 'Sample Data'], + ], + ], + ], + ]); + $this->httpClient = $this->prophesize(HttpClientInterface::class); + $this->httpClient + ->request(Argument::cetera()) + ->willReturn($response); + $this->output = $this->prophesize(SymfonyStyle::class); + + } + + protected function createGoogleClient(): GoogleApiClient { + $http_client = $this->httpClient->reveal(); + $output_symfony = $this->output->reveal(); + $version_resolver = $this->version->reveal(); + $client_id = "Sample Client"; + $client_secret = "Sample Secret"; + $refresh_token = "Refresh Token"; + return new GoogleApiClient($http_client, $output_symfony, $version_resolver, $client_id, $client_secret, $refresh_token); + } + + /** + * @throws \Acquia\Orca\Exception\OrcaHttpException + * @throws \Acquia\Orca\Exception\OrcaVersionNotFoundException + */ + public function testPostDataWithNullVersion(): void { + + $data = [ + 'job' => 'STATIC_CODE_ANALYSIS', + 'phase' => 'script', + 'sut' => 'drupal/example', + 'status' => 'PASS', + 'version' => NULL, + ]; + $this->version + ->resolvePredefined(Argument::any()) + ->shouldNotBeCalled(); + + $google_client = $this->createGoogleClient(); + $google_client->postData($data); + } + + /** + * @throws \Acquia\Orca\Exception\OrcaHttpException + * @throws \Acquia\Orca\Exception\OrcaVersionNotFoundException + */ + public function testPostDataWithLatestDrupalVersion(): void { + + $data = [ + 'job' => 'INTEGRATED_TEST_ON_CURRENT', + 'phase' => 'script', + 'sut' => 'drupal/example', + 'status' => 'PASS', + 'version' => DrupalCoreVersionEnum::CURRENT(), + ]; + $this->version + ->existsPredefined(Argument::any()) + ->willReturn(TRUE); + $this->version + ->resolvePredefined(Argument::any()) + ->shouldBeCalledOnce(); + + $google_client = $this->createGoogleClient(); + self::assertIsString($google_client->getToken()); + $google_client->postData($data); + } + +}