Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimisation du temps des tests fonctionnels #1643

Merged
merged 1 commit into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"ext-json": "*",
"ext-libxml": "*",
"ext-openssl": "*",
"ext-pdo": "*",
"algolia/algoliasearch-client-php": "^3.4",
"beberlei/assert": "^2.9",
"captioning/captioning": "^2.6",
Expand Down Expand Up @@ -98,6 +99,7 @@
"behat/mink-extension": "^2.3",
"behat/mink-goutte-driver": "^1.2",
"friendsofphp/php-cs-fixer": "~2",
"ifsnop/mysqldump-php": "^2.12",
"rector/rector": "^2.0",
"smalot/pdfparser": "^0.19.0"
},
Expand Down
61 changes: 60 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 53 additions & 8 deletions tests/behat/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,31 @@
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Exception\ExpectationException;
use Behat\MinkExtension\Context\MinkContext;
use Ifsnop\Mysqldump\Mysqldump;
use Smalot\PdfParser\Parser;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;

class FeatureContext implements Context
{
public const MAILCATCHER_URL = 'http://mailcatcher:1080';
private const MAILCATCHER_URL = 'http://mailcatcher:1080';
private const DB_NAME = 'web';

private MinkContext $minkContext;

private array $pdfPages = [];

private PDO $database;
private Mysqldump $dumper;
private string $dbDumpKey;

public function __construct()
{
$this->database = new PDO('mysql:host=dbtest;dbname=' . self::DB_NAME, 'root', 'root');
$this->dumper = new Mysqldump('mysql:host=dbtest;dbname=' . self::DB_NAME, 'root', 'root');
$this->dbDumpKey = $this->computeDbDumpKey();
}

/**
* @BeforeScenario
*/
Expand All @@ -34,11 +47,19 @@ public function gatherContexts(BeforeScenarioScope $scope): void
/**
* @BeforeScenario @reloadDbWithTestData
*/
public function beforeScenarioReloadDb(): void
public function beforeScenarioReloadDatabase(): void
{
$this->resetDb();
$this->migrateDb();
$this->seedRun();

$dbDumpFile = sprintf(__DIR__ . '/../../../var/cache/test/db_dump_%s.sql', $this->dbDumpKey);
if (!is_file($dbDumpFile)) {
$this->migrateDb();
$this->seedRun();

$this->dumper->start($dbDumpFile);
} else {
$this->restoreDb($dbDumpFile);
}
}

/**
Expand All @@ -59,11 +80,35 @@ public function beforeScenarioClearAllSponsorFiles(): void
$filesystem->remove(Event::getSponsorFileDir());
}

private function computeDbDumpKey(): string
{
$finder = new Symfony\Component\Finder\Finder();
$files = $finder->files()->in([
__DIR__ . '/../../../db/migrations/',
__DIR__ . '/../../../db/seeds/',
]);

$key = '';
foreach ($files as $file) {
$key .= md5_file($file->getRealPath());
}

return md5($key);
}

private function restoreDb(string $dbDumpFile): void
{
if (false === $this->database->exec(file_get_contents($dbDumpFile))) {
throw new RuntimeException(implode(' ', $this->database->errorInfo()));
}
}

private function resetDb(): void
{
$pdo = new \PDO('mysql:host=dbtest', 'root', 'root');
$pdo->exec('DROP DATABASE IF EXISTS web');
$pdo->exec('CREATE DATABASE web');
$sql = sprintf('DROP DATABASE IF EXISTS %1$s; CREATE DATABASE %1$s; USE %1$s;', self::DB_NAME);
if (false === $this->database->exec($sql)) {
throw new RuntimeException(implode(' ', $this->database->errorInfo()));
}
}

private function migrateDb(): void
Expand Down Expand Up @@ -175,7 +220,7 @@ public function selectHasValues(string $field, string $expectedValuesJson): void
* @Then The :field field should have the following selected value :expectedValue
* @throws ExpectationException
*/
public function selectHasForCurrentSelectedValue(string $field, string $expectedValue) :void
public function selectHasForCurrentSelectedValue(string $field, string $expectedValue): void
{
$node = $this->minkContext->assertSession()->fieldExists($field);
$options = $node->findAll('css', 'option');
Expand Down
Loading