Skip to content

Commit

Permalink
PHPUnit : Refactoring Tests (Use local resource)
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Jan 30, 2024
1 parent 13b202b commit 25a11a8
Show file tree
Hide file tree
Showing 13 changed files with 351,026 additions and 67 deletions.
3 changes: 2 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999

DATABASE_URL="mysql://root:[email protected]:3306/qanightlyresults?serverVersion=8.0.23&charset=utf8mb4"
QANB_TOKEN='AZERTY'
QANB_TOKEN='AZERTY'
#QANB_REPORT_PATH='./tests/resources/'
8 changes: 5 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:
- name: Inject data for AutoUpgrade
run: |
DATE=$(date -d "1 day ago" +"%Y-%m-%d")
php bin/console nightly:import \
php bin/console nightly:import:mocha \
-p cli \
-c autoupgrade \
--env test \
Expand All @@ -110,11 +110,13 @@ jobs:
- name: Inject data for Core
run: |
DATE=$(date -d "1 day ago" +"%Y-%m-%d")
php bin/console nightly:import \
php bin/console nightly:import:mocha \
-p chromium \
-c functional \
--env test \
$DATE-develop.json
- name: Run phpunit
run: ./vendor/bin/phpunit
run: ./vendor/bin/phpunit
env:
QANB_REPORT_PATH: './tests/resources/'
3 changes: 2 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
QANB_GCPURL: 'https://storage.googleapis.com/prestashop-core-nightly/'
QANB_REPORT_PATH: '%env(default:QANB_GCPURL:QANB_REPORT_PATH)%'
QANB_TOKEN: '%env(QANB_TOKEN)%'

services:
Expand All @@ -13,7 +14,7 @@ services:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$nightlyGCPUrl: '%QANB_GCPURL%'
$nightlyReportPath: '%QANB_REPORT_PATH%'
$nightlyToken: '%QANB_TOKEN%'

# makes classes in src/ available to be used as services
Expand Down
22 changes: 11 additions & 11 deletions src/Command/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@

namespace App\Command;

use App\Service\ReportImporter;
use App\Service\ReportMochaImporter;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(name: 'nightly:import')]
#[AsCommand(name: 'nightly:import:mocha')]
class ImportCommand extends Command
{
private ReportImporter $reportImporter;
private ReportMochaImporter $reportImporter;

private string $nightlyGCPUrl;
private string $nightlyReportPath;

public function __construct(ReportImporter $reportImporter, string $nightlyGCPUrl)
public function __construct(ReportMochaImporter $reportImporter, string $nightlyReportPath)
{
parent::__construct();

$this->reportImporter = $reportImporter;
$this->nightlyGCPUrl = $nightlyGCPUrl;
$this->nightlyReportPath = $nightlyReportPath;
}

protected function configure(): void
{
$this
->addArgument('filename', InputArgument::REQUIRED)
->addOption('platform', 'p', InputOption::VALUE_REQUIRED, '', ReportImporter::FILTER_PLATFORMS[0], ReportImporter::FILTER_PLATFORMS)
->addOption('campaign', 'c', InputOption::VALUE_REQUIRED, '', ReportImporter::FILTER_CAMPAIGNS[0], ReportImporter::FILTER_CAMPAIGNS)
->addOption('platform', 'p', InputOption::VALUE_REQUIRED, '', ReportMochaImporter::FILTER_PLATFORMS[0], ReportMochaImporter::FILTER_PLATFORMS)
->addOption('campaign', 'c', InputOption::VALUE_REQUIRED, '', ReportMochaImporter::FILTER_CAMPAIGNS[0], ReportMochaImporter::FILTER_CAMPAIGNS)
;
}

Expand All @@ -47,7 +47,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::FAILURE;
}

$fileContent = @file_get_contents($this->nightlyGCPUrl . 'reports/' . $input->getArgument('filename'));
$fileContent = @file_get_contents($this->nightlyReportPath . 'reports/' . $input->getArgument('filename'));
if (!$fileContent) {
$output->writeln('<error>Unable to retrieve content from GCP URL</error>');

Expand All @@ -62,7 +62,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$startDate = \DateTime::createFromFormat(
ReportImporter::FORMAT_DATE_MOCHA6,
ReportMochaImporter::FORMAT_DATE_MOCHA6,
$jsonContent->stats->start
);

Expand All @@ -73,7 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$matchesVersion[1],
$startDate,
$jsonContent,
ReportImporter::FORMAT_DATE_MOCHA6
ReportMochaImporter::FORMAT_DATE_MOCHA6
);

return Command::SUCCESS;
Expand Down
10 changes: 5 additions & 5 deletions src/Controller/HealthCheckController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public function __construct(ExecutionRepository $executionRepository)
}

#[Route('/healthcheck', methods: ['GET'])]
public function check(string $nightlyGCPUrl): JsonResponse
public function check(string $nightlyReportPath): JsonResponse
{
$data = [
'database' => true,
'gcp' => true,
'gcp' => false,
];

// Check database
Expand All @@ -36,9 +36,9 @@ public function check(string $nightlyGCPUrl): JsonResponse
}

// Check GCP
$gcpCall = @file_get_contents($nightlyGCPUrl);
if (!$gcpCall) {
$data['gcp'] = false;
$gcpCall = @file_get_contents($nightlyReportPath);
if ($gcpCall || !is_dir($nightlyReportPath)) {
$data['gcp'] = true;
}

return $this->json($data);
Expand Down
22 changes: 11 additions & 11 deletions src/Controller/ImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Controller;

use App\Repository\ExecutionRepository;
use App\Service\ReportImporter;
use App\Service\ReportMochaImporter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -14,11 +14,11 @@ class ImportController extends AbstractController
{
private ExecutionRepository $executionRepository;

private ReportImporter $reportImporter;
private ReportMochaImporter $reportImporter;

private string $nightlyToken;

private string $nightlyGCPUrl;
private string $nightlyReportPath;

private ?string $filename;

Expand All @@ -34,14 +34,14 @@ class ImportController extends AbstractController

public function __construct(
ExecutionRepository $executionRepository,
ReportImporter $reportImporter,
ReportMochaImporter $reportImporter,
string $nightlyToken,
string $nightlyGCPUrl
string $nightlyReportPath
) {
$this->executionRepository = $executionRepository;
$this->reportImporter = $reportImporter;
$this->nightlyToken = $nightlyToken;
$this->nightlyGCPUrl = $nightlyGCPUrl;
$this->nightlyReportPath = $nightlyReportPath;
}

#[Route('/hook/reports/import', methods: ['GET'])]
Expand All @@ -59,7 +59,7 @@ public function importReport(Request $request): JsonResponse
$this->version,
$this->startDate,
$this->jsonContent,
ReportImporter::FORMAT_DATE_MOCHA6
ReportMochaImporter::FORMAT_DATE_MOCHA6
);

return new JsonResponse([
Expand Down Expand Up @@ -102,7 +102,7 @@ private function checkAuth(Request $request): ?JsonResponse
], Response::HTTP_BAD_REQUEST);
}

$fileContent = @file_get_contents($this->nightlyGCPUrl . 'reports/' . $this->filename);
$fileContent = @file_get_contents($this->nightlyReportPath . 'reports/' . $this->filename);
if (!$fileContent) {
return new JsonResponse([
'message' => 'Unable to retrieve content from GCP URL',
Expand All @@ -122,12 +122,12 @@ private function checkAuth(Request $request): ?JsonResponse
$this->platform = $request->query->has('platform') ? $request->query->get('platform') : (
$request->query->has('browser') ? $request->query->get('browser') : null
);
$this->platform = in_array($this->platform, ReportImporter::FILTER_PLATFORMS) ? $this->platform : ReportImporter::FILTER_PLATFORMS[0];
$this->platform = in_array($this->platform, ReportMochaImporter::FILTER_PLATFORMS) ? $this->platform : ReportMochaImporter::FILTER_PLATFORMS[0];

$this->campaign = $request->query->has('campaign') ? $request->query->get('campaign') : null;
$this->campaign = in_array($this->campaign, ReportImporter::FILTER_CAMPAIGNS) ? $this->campaign : ReportImporter::FILTER_CAMPAIGNS[0];
$this->campaign = in_array($this->campaign, ReportMochaImporter::FILTER_CAMPAIGNS) ? $this->campaign : ReportMochaImporter::FILTER_CAMPAIGNS[0];

$this->startDate = \DateTime::createFromFormat(ReportImporter::FORMAT_DATE_MOCHA6, $this->jsonContent->stats->start);
$this->startDate = \DateTime::createFromFormat(ReportMochaImporter::FORMAT_DATE_MOCHA6, $this->jsonContent->stats->start);

// Check if there is no similar entry
if (!$force && $this->executionRepository->findOneByNightly($this->version, $this->platform, $this->campaign, $this->startDate->format('Y-m-d'))) {
Expand Down
10 changes: 5 additions & 5 deletions src/Controller/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ class ReportController extends AbstractController

private ReportSuiteBuilder $reportSuiteBuilder;

private string $nightlyGCPUrl;
private string $nightlyReportPath;

public function __construct(
ExecutionRepository $executionRepository,
ReportLister $reportLister,
ReportSuiteBuilder $reportSuiteBuilder,
string $nightlyGCPUrl
string $nightlyReportPath
) {
$this->executionRepository = $executionRepository;
$this->reportLister = $reportLister;
$this->reportSuiteBuilder = $reportSuiteBuilder;
$this->nightlyGCPUrl = $nightlyGCPUrl;
$this->nightlyReportPath = $nightlyReportPath;
}

#[Route('/reports', methods: ['GET'])]
Expand Down Expand Up @@ -66,10 +66,10 @@ public function reports(Request $request): JsonResponse
$date = $execution->getStartDate()->format('Y-m-d');

if (isset($reportListing[$date][$execution->getVersion()]['zip'])) {
$download = $this->nightlyGCPUrl . $reportListing[$date][$execution->getVersion()]['zip'];
$download = $this->nightlyReportPath . $reportListing[$date][$execution->getVersion()]['zip'];
}
if (isset($reportListing[$date][$execution->getVersion()]['xml'])) {
$xml = $this->nightlyGCPUrl . $reportListing[$date][$execution->getVersion()]['xml'];
$xml = $this->nightlyReportPath . $reportListing[$date][$execution->getVersion()]['xml'];
}

$reports[] = [
Expand Down
10 changes: 5 additions & 5 deletions src/Service/ReportLister.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@

class ReportLister
{
public string $url;
public string $nightlyReportPath;

public function __construct(string $nightlyGCPUrl)
public function __construct(string $nightlyReportPath)
{
$this->url = $nightlyGCPUrl;
$this->nightlyReportPath = $nightlyReportPath;
}

/**
* @return array<string, array<string, array<string, string>>>
*/
public function get(): array
{
$return = file_get_contents($this->url);
if (!$return) {
$return = @file_get_contents($this->nightlyReportPath);
if (!$return || !is_dir($this->nightlyReportPath)) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use App\Repository\TestRepository;
use Doctrine\ORM\EntityManagerInterface;

class ReportImporter
class ReportMochaImporter
{
public const FILTER_PLATFORMS = ['chromium', 'firefox', 'webkit', 'cli'];

Expand Down
27 changes: 11 additions & 16 deletions tests/Controller/ImportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@

class ImportControllerTest extends WebTestCase
{
private static string $date = '';

public static function setUpBeforeClass(): void
{
self::$date = date('Y-m-d', strtotime('today'));
}
private const DATE_RESOURCE = '2024-01-30';

public function testReportOkAutoupgrade(): void
{
$client = static::createClient();
$client->request('GET', '/hook/reports/import?filename=autoupgrade_' . self::$date . '-develop.json&token=AZERTY&campaign=autoupgrade&platform=cli');
$client->request('GET', '/hook/reports/import?filename=autoupgrade_' . self::DATE_RESOURCE . '-develop.json&token=AZERTY&campaign=autoupgrade&platform=cli');

$response = $client->getResponse();

Expand Down Expand Up @@ -67,7 +62,7 @@ public function testReportWithParameterToken(): void
public function testReportWithParameterFilename(): void
{
$client = static::createClient();
$client->request('GET', '/hook/reports/import?filename=' . self::$date . '-develop.json');
$client->request('GET', '/hook/reports/import?filename=' . self::DATE_RESOURCE . '-develop.json');
$response = $client->getResponse();

$this->assertEquals(400, $response->getStatusCode());
Expand All @@ -83,7 +78,7 @@ public function testReportWithParameterFilename(): void
public function testReportWithParameterFilenameAndBakToken(): void
{
$client = static::createClient();
$client->request('GET', '/hook/reports/import?filename=' . self::$date . '-develop.json&token=BAD');
$client->request('GET', '/hook/reports/import?filename=' . self::DATE_RESOURCE . '-develop.json&token=BAD');
$response = $client->getResponse();

$this->assertEquals(401, $response->getStatusCode());
Expand All @@ -99,7 +94,7 @@ public function testReportWithParameterFilenameAndBakToken(): void
public function testReportWithNoVersionInFilename(): void
{
$client = static::createClient();
$client->request('GET', '/hook/reports/import?filename=' . self::$date . '.json&token=AZERTY');
$client->request('GET', '/hook/reports/import?filename=' . self::DATE_RESOURCE . '.json&token=AZERTY');
$response = $client->getResponse();

$this->assertEquals(400, $response->getStatusCode());
Expand All @@ -115,7 +110,7 @@ public function testReportWithNoVersionInFilename(): void
public function testReportWithBadVersionInFilename(): void
{
$client = static::createClient();
$client->request('GET', '/hook/reports/import?filename=' . self::$date . '-.json&token=AZERTY');
$client->request('GET', '/hook/reports/import?filename=' . self::DATE_RESOURCE . '-.json&token=AZERTY');
$response = $client->getResponse();

$this->assertEquals(400, $response->getStatusCode());
Expand All @@ -125,13 +120,13 @@ public function testReportWithBadVersionInFilename(): void
$content = $response->getContent();
$content = json_decode($content, true);
$this->assertArrayHasKey('message', $content);
$this->assertEquals('Version found not correct () from filename ' . self::$date . '-.json', $content['message']);
$this->assertEquals('Version found not correct () from filename ' . self::DATE_RESOURCE . '-.json', $content['message']);
}

public function testReportWithNotExistingFilename(): void
{
$client = static::createClient();
$client->request('GET', '/hook/reports/import?filename=' . self::$date . '-truc.json&token=AZERTY');
$client->request('GET', '/hook/reports/import?filename=' . self::DATE_RESOURCE . '-truc.json&token=AZERTY');
$response = $client->getResponse();

$this->assertEquals(400, $response->getStatusCode());
Expand All @@ -147,7 +142,7 @@ public function testReportWithNotExistingFilename(): void
public function testReportOk(): void
{
$client = static::createClient();
$client->request('GET', '/hook/reports/import?filename=' . self::$date . '-develop.json&token=AZERTY');
$client->request('GET', '/hook/reports/import?filename=' . self::DATE_RESOURCE . '-develop.json&token=AZERTY');
$response = $client->getResponse();

$this->assertEquals(200, $response->getStatusCode());
Expand All @@ -165,7 +160,7 @@ public function testReportOk(): void
public function testReportAlreadyExisting(): void
{
$client = static::createClient();
$client->request('GET', '/hook/reports/import?filename=' . self::$date . '-develop.json&token=AZERTY');
$client->request('GET', '/hook/reports/import?filename=' . self::DATE_RESOURCE . '-develop.json&token=AZERTY');
$response = $client->getResponse();

$this->assertEquals(403, $response->getStatusCode());
Expand All @@ -175,6 +170,6 @@ public function testReportAlreadyExisting(): void
$content = $response->getContent();
$content = json_decode($content, true);
$this->assertArrayHasKey('message', $content);
$this->assertEquals('A similar entry was found (criteria: version develop, platform chromium, campaign functional, date ' . self::$date . ').', $content['message']);
$this->assertEquals('A similar entry was found (criteria: version develop, platform chromium, campaign functional, date ' . self::DATE_RESOURCE . ').', $content['message']);
}
}
Loading

0 comments on commit 25a11a8

Please sign in to comment.