Skip to content

Commit

Permalink
Fix all issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sayan goswami committed Jun 5, 2023
1 parent 58aeb72 commit 2553cd5
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/Domain/Composer/Version/DrupalCoreVersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Enum/EnvVarEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion src/Event/CiEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
15 changes: 10 additions & 5 deletions src/Helper/Log/GoogleApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand All @@ -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' => [
Expand Down Expand Up @@ -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' => [
Expand Down
12 changes: 11 additions & 1 deletion tests/Console/Command/Ci/CiRunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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 {
Expand Down
117 changes: 117 additions & 0 deletions tests/Helper/Log/GoogleApiClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Acquia\Orca\Tests\Helper\Log;

use Acquia\Orca\Domain\Composer\Version\DrupalCoreVersionResolver;
use Acquia\Orca\Enum\DrupalCoreVersionEnum;
use Acquia\Orca\Helper\Log\GoogleApiClient;
use Acquia\Orca\Tests\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
* @property \Acquia\Orca\Domain\Composer\Version\DrupalCoreVersionResolver|\Prophecy\Prophecy\ObjectProphecy $version
*/
class GoogleApiClientTest extends TestCase {

protected DrupalCoreVersionResolver|ObjectProphecy $version;

/**
* @var \Prophecy\Prophecy\ObjectProphecy|\Symfony\Contracts\HttpClient\HttpClientInterface
*/
private HttpClientInterface|ObjectProphecy $httpClient;

/**
* @var \Prophecy\Prophecy\ObjectProphecy|\Symfony\Component\Console\Style\SymfonyStyle
*/
private ObjectProphecy|SymfonyStyle $output;

/**
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
protected function setUp(): void {

$this->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);
}

}

0 comments on commit 2553cd5

Please sign in to comment.