Skip to content
Snippets Groups Projects
Commit be0b99b7 authored by Ted Bowman's avatar Ted Bowman
Browse files

Issue #3308886 by tedbow: Consolidate Test fixture logic

parent 80cf7125
No related branches found
No related tags found
1 merge request!467Issue #3308886: Consolidate Test fixture logic
Showing
with 111 additions and 65 deletions
......@@ -8,15 +8,12 @@ use Drupal\package_manager\Exception\StageValidationException;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Tests\package_manager\Kernel\TestPathFactory;
use Drupal\Tests\package_manager\Kernel\TestStageTrait;
use Drupal\Tests\package_manager\Traits\InfoYmlConverterTrait;
/**
* Base class for kernel tests of the Automatic Updates Extensions module.
*/
abstract class AutomaticUpdatesExtensionsKernelTestBase extends AutomaticUpdatesKernelTestBase {
use InfoYmlConverterTrait;
/**
* {@inheritdoc}
*/
......@@ -47,7 +44,6 @@ abstract class AutomaticUpdatesExtensionsKernelTestBase extends AutomaticUpdates
protected function createVirtualProject(?string $source_dir = NULL): void {
$source_dir = $source_dir ?? __DIR__ . '/../../fixtures/fake-site';
parent::createVirtualProject($source_dir);
$this->renameVfsInfoYmlFiles();
}
/**
......
......@@ -3,7 +3,6 @@
namespace Drupal\Tests\automatic_updates_extensions\Kernel;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
use Drupal\Tests\package_manager\Traits\InfoYmlConverterTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
/**
......@@ -14,7 +13,6 @@ use Drupal\Tests\user\Traits\UserCreationTrait;
class ExtensionUpdaterTest extends AutomaticUpdatesKernelTestBase {
use UserCreationTrait;
use InfoYmlConverterTrait;
/**
* {@inheritdoc}
......@@ -42,7 +40,6 @@ class ExtensionUpdaterTest extends AutomaticUpdatesKernelTestBase {
$this->setCurrentUser($user);
$this->createVirtualProject(__DIR__ . '/../../fixtures/fake-site');
$this->renameVfsInfoYmlFiles();
}
/**
......
......@@ -85,7 +85,8 @@ class PackagesInstalledWithComposerValidatorTest extends AutomaticUpdatesExtensi
*/
public function testPreApplyException(string $stage_dir, array $expected_results): void {
$active_dir = __DIR__ . '/../../../fixtures/packages_installed_with_composer_validator/active';
$this->useComposerFixturesFiles($active_dir, $stage_dir);
$this->copyFixtureFolderToActiveDirectory($active_dir);
$this->copyFixtureFolderToStageDirectoryOnApply($stage_dir);
$this->assertUpdateResults(['my_module' => '9.8.1'], $expected_results, PreApplyEvent::class);
}
......
......@@ -131,7 +131,8 @@ class UpdateReleaseValidatorTest extends AutomaticUpdatesExtensionsKernelTestBas
'drupal' => __DIR__ . '/../../../../../tests/fixtures/release-history/drupal.9.8.2.xml',
]);
$active_dir = __DIR__ . '/../../../fixtures/update_release_validator/active';
$this->useComposerFixturesFiles($active_dir, $stage_dir);
$this->copyFixtureFolderToActiveDirectory($active_dir);
$this->copyFixtureFolderToStageDirectoryOnApply($stage_dir);
if ($error_expected) {
$expected_results = [
ValidationResult::createError(
......
......@@ -58,6 +58,9 @@ abstract class BypassedStagerServiceBase {
/**
* If a fixture path has been set, mirrors it to the given path.
*
* Files in the destination directory but not in the source directory will
* not be deleted.
*
* @param \PhpTuf\ComposerStager\Domain\Value\Path\PathInterface $destination
* The path to which the fixture files should be mirrored.
*/
......@@ -67,7 +70,7 @@ abstract class BypassedStagerServiceBase {
if ($fixture_path && is_dir($fixture_path)) {
$this->fileSystem->mirror($fixture_path, $destination->resolve(), NULL, [
'override' => TRUE,
'delete' => TRUE,
'delete' => FALSE,
]);
}
}
......
......@@ -4,7 +4,7 @@ namespace Drupal\Tests\package_manager\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\package_manager\ComposerUtility;
use Drupal\Tests\package_manager\Traits\InfoYmlConverterTrait;
use Drupal\Tests\package_manager\Traits\FixtureUtilityTrait;
use org\bovigo\vfs\vfsStream;
/**
......@@ -14,7 +14,7 @@ use org\bovigo\vfs\vfsStream;
*/
class ComposerUtilityTest extends KernelTestBase {
use InfoYmlConverterTrait;
use FixtureUtilityTrait;
/**
* {@inheritdoc}
......@@ -28,9 +28,8 @@ class ComposerUtilityTest extends KernelTestBase {
parent::setUp();
$fixture = vfsStream::newDirectory('fixture');
vfsStream::copyFromFileSystem(__DIR__ . '/../../fixtures/project_package_conversion', $fixture);
$this->vfsRoot->addChild($fixture);
$this->renameVfsInfoYmlFiles();
static::copyFixtureFilesTo(__DIR__ . '/../../fixtures/project_package_conversion', $fixture->url());
}
/**
......
......@@ -12,6 +12,7 @@ use Drupal\package_manager\Exception\StageException;
use Drupal\package_manager\Exception\StageValidationException;
use Drupal\package_manager\Stage;
use Drupal\package_manager_bypass\Beginner;
use Drupal\Tests\package_manager\Traits\FixtureUtilityTrait;
use Drupal\Tests\package_manager\Traits\ValidationTestTrait;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
......@@ -19,9 +20,6 @@ use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Utils;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
use org\bovigo\vfs\visitor\vfsStreamAbstractVisitor;
use PhpTuf\ComposerStager\Domain\Value\Path\PathInterface;
use PhpTuf\ComposerStager\Infrastructure\Factory\Path\PathFactoryInterface;
use PhpTuf\ComposerStager\Infrastructure\Value\Path\AbstractPath;
......@@ -33,6 +31,7 @@ use Symfony\Component\DependencyInjection\Definition;
*/
abstract class PackageManagerKernelTestBase extends KernelTestBase {
use FixtureUtilityTrait;
use ValidationTestTrait;
/**
......@@ -218,31 +217,8 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
// Create the active directory and copy its contents from a fixture.
$active_dir = vfsStream::newDirectory('active');
$this->vfsRoot->addChild($active_dir);
vfsStream::copyFromFileSystem($source_dir, $active_dir);
// Because we can't commit physical `.git` directories into the fixture, use
// a visitor to traverse the virtual file system and rename all `_git`
// directories to `.git`.
vfsStream::inspect(new class () extends vfsStreamAbstractVisitor {
/**
* {@inheritdoc}
*/
public function visitFile(vfsStreamFile $file) {}
/**
* {@inheritdoc}
*/
public function visitDirectory(vfsStreamDirectory $dir) {
if ($dir->getName() === '_git') {
$dir->rename('.git');
}
foreach ($dir->getChildren() as $child) {
$this->visit($child);
}
}
});
$active_dir = $active_dir->url();
static::copyFixtureFilesTo($source_dir, $active_dir);
// Create a staging root directory alongside the active directory.
$stage_dir = vfsStream::newDirectory('stage');
......@@ -251,7 +227,6 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
// Ensure the path locator points to the virtual active directory. We assume
// that is its own web root and that the vendor directory is at its top
// level.
$active_dir = $active_dir->url();
/** @var \Drupal\package_manager_bypass\PathLocator $path_locator */
$path_locator = $this->container->get('package_manager.path_locator');
$path_locator->setPaths($active_dir, $active_dir . '/vendor', '', $stage_dir->url());
......@@ -279,36 +254,31 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
}
/**
* Copies composer fixture files to active and stage directories.
* Copies a fixture directory into the active directory.
*
* @param string $active_fixture_dir
* Path to fixture active directory from which the files will be copied.
* @param string|null $stage_fixture_dir
* (optional) Path to fixture staged directory from which the files will be
* copied. If not provided $active_dir_to_set will be used as the fixture
* staged directory also.
*/
protected function useComposerFixturesFiles(string $active_fixture_dir, string $stage_fixture_dir = NULL) {
$this->assertFileIsReadable("$active_fixture_dir/active.installed.json");
protected function copyFixtureFolderToActiveDirectory(string $active_fixture_dir) {
$active_dir = $this->container->get('package_manager.path_locator')
->getProjectRoot();
copy("$active_fixture_dir/active.installed.json", "$active_dir/vendor/composer/installed.json");
static::copyFixtureFilesTo($active_fixture_dir, $active_dir);
}
// Before any other pre-apply listener runs, replaced the staged
// `vendor/composer/installed.json` with the fixture's
// `staged.installed.json`.
if ($stage_fixture_dir === NULL) {
$stage_fixture_dir = $active_fixture_dir;
}
if (!file_exists("$stage_fixture_dir/staged.installed.json")) {
return;
}
$this->assertFileIsReadable("$stage_fixture_dir/staged.installed.json");
$listener = function (PreApplyEvent $event) use ($stage_fixture_dir): void {
copy("$stage_fixture_dir/staged.installed.json", $event->getStage()->getStageDirectory() . "/vendor/composer/installed.json");
/**
* Copies a fixture directory into the stage directory on apply.
*
* @param string $fixture_dir
* Path to fixture directory from which the files will be copied.
*/
protected function copyFixtureFolderToStageDirectoryOnApply(string $fixture_dir) {
/** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */
$event_dispatcher = $this->container->get('event_dispatcher');
$listener = function (PreApplyEvent $event) use ($fixture_dir): void {
static::copyFixtureFilesTo($fixture_dir, $event->getStage()->getStageDirectory());
};
$this->container->get('event_dispatcher')
->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX);
$event_dispatcher->addListener(PreApplyEvent::class, $listener, PHP_INT_MAX);
}
/**
......
<?php
namespace Drupal\Tests\package_manager\Traits;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Iterator\RecursiveDirectoryIterator;
/**
* A utility for all things fixtures.
*/
trait FixtureUtilityTrait {
/**
* Mirrors a fixture directory to the given path.
*
* Files not in the source fixture directory will not be deleted from
* destination directory. After copying the files to the destination directory
* the files and folders will be converted so that can be used in the tests.
* The conversion includes:
* - Renaming '_git' directories to '.git'
* - Renaming files ending in '.info.yml.hide' to remove '.hide'.
*
* @param string $source_path
* The source path.
* @param string $destination_path
* The path to which the fixture files should be mirrored.
*/
protected static function copyFixtureFilesTo(string $source_path, string $destination_path): void {
(new Filesystem())->mirror($source_path, $destination_path, NULL, [
'override' => TRUE,
'delete' => FALSE,
]);
static::renameInfoYmlFiles($destination_path);
static::renameGitDirectories($destination_path);
}
/**
* Renames all files that end with .info.yml.hide.
*
* @param string $dir
* The directory to be iterated through.
*/
protected static function renameInfoYmlFiles(string $dir) {
// Construct the iterator.
$it = new RecursiveDirectoryIterator($dir, \RecursiveIteratorIterator::SELF_FIRST);
// Loop through files and rename them.
foreach (new \RecursiveIteratorIterator($it) as $file) {
if ($file->getExtension() == 'hide') {
rename($file->getPathname(), $dir . DIRECTORY_SEPARATOR .
$file->getRelativePath() . DIRECTORY_SEPARATOR . str_replace(".hide", "", $file->getFilename()));
}
}
}
/**
* Renames _git directories to .git.
*
* @param string $dir
* The directory to be iterated through.
*/
private static function renameGitDirectories(string $dir) {
$iter = new \RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST,
\RecursiveIteratorIterator::CATCH_GET_CHILD
);
/** @var \Symfony\Component\Finder\SplFileInfo $file */
foreach ($iter as $file) {
if ($file->isDir() && $file->getFilename() === '_git' && $file->getRelativePathname()) {
rename(
$file->getPathname(),
$file->getPath() . DIRECTORY_SEPARATOR . '.git'
);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment