diff --git a/core/lib/Drupal/Component/Scaffold/AllowedPackages.php b/core/lib/Drupal/Component/Scaffold/AllowedPackages.php index 5b0c25bb2d..edfc39bcc3 100644 --- a/core/lib/Drupal/Component/Scaffold/AllowedPackages.php +++ b/core/lib/Drupal/Component/Scaffold/AllowedPackages.php @@ -50,13 +50,13 @@ class AllowedPackages implements PostPackageEventListenerInterface { * The composer object. * @param \Composer\IO\IOInterface $io * IOInterface to write to. - * @param \Drupal\Component\Scaffold\ManageOptions $manageOptions + * @param \Drupal\Component\Scaffold\ManageOptions $manage_options * Manager of the options in the top-level composer.json's 'extra' section. */ - public function __construct(Composer $composer, IOInterface $io, ManageOptions $manageOptions) { + public function __construct(Composer $composer, IOInterface $io, ManageOptions $manage_options) { $this->composer = $composer; $this->io = $io; - $this->manageOptions = $manageOptions; + $this->manageOptions = $manage_options; } /** diff --git a/core/lib/Drupal/Component/Scaffold/GenerateAutoloadReferenceFile.php b/core/lib/Drupal/Component/Scaffold/GenerateAutoloadReferenceFile.php index a485ca3f49..64b240ec42 100644 --- a/core/lib/Drupal/Component/Scaffold/GenerateAutoloadReferenceFile.php +++ b/core/lib/Drupal/Component/Scaffold/GenerateAutoloadReferenceFile.php @@ -27,21 +27,21 @@ private function __construct() { * The name of the package defining the autoload file (the root package). * @param string $web_root * The path to the web root. - * @param string $vendorPath + * @param string $vendor * The path to the vendor directory. * * @return \Drupal\Component\Scaffold\Operations\ScaffoldResult * The result of the autoload file generation. */ - public static function generateAutoload($package_name, $web_root, $vendorPath) { - $autoloadPath = static::autoloadPath($package_name, $web_root); - $location = dirname($autoloadPath->fullPath()); + public static function generateAutoload($package_name, $web_root, $vendor) { + $autoload_path = static::autoloadPath($package_name, $web_root); + $location = dirname($autoload_path->fullPath()); // Calculate the relative path from the webroot (location of the project // autoload.php) to the vendor directory. $fs = new SymfonyFilesystem(); - $relativeVendorPath = $fs->makePathRelative($vendorPath, realpath($location)); - $fs->dumpFile($autoloadPath->fullPath(), static::autoLoadContents($relativeVendorPath)); - return new ScaffoldResult($autoloadPath, TRUE); + $relative_vendor_path = $fs->makePathRelative($vendor, realpath($location)); + $fs->dumpFile($autoload_path->fullPath(), static::autoLoadContents($relative_vendor_path)); + return new ScaffoldResult($autoload_path, TRUE); } /** @@ -65,14 +65,14 @@ protected static function autoloadPath($package_name, $web_root) { /** * Builds the contents of the autoload file. * - * @param string $relativeVendorPath + * @param string $vendor_path * The relative path to vendor. * * @return string * Return the contents for the autoload.php. */ - protected static function autoLoadContents($relativeVendorPath) { - $relativeVendorPath = rtrim($relativeVendorPath, '/'); + protected static function autoLoadContents($vendor_path) { + $vendor_path = rtrim($vendor_path, '/'); return <<startToken = $startToken; - $this->endToken = $endToken; + public function __construct($start_token = '\\[', $end_token = '\\]') { + $this->startToken = $start_token; + $this->endToken = $end_token; } /** diff --git a/core/lib/Drupal/Component/Scaffold/ManageGitIgnore.php b/core/lib/Drupal/Component/Scaffold/ManageGitIgnore.php index 28a37fb9f0..9985f3ca9f 100644 --- a/core/lib/Drupal/Component/Scaffold/ManageGitIgnore.php +++ b/core/lib/Drupal/Component/Scaffold/ManageGitIgnore.php @@ -29,7 +29,7 @@ public function __construct($dir) { /** * Manages gitignore files. * - * @param array $files + * @param \Drupal\Component\Scaffold\Operations\ScaffoldResult[] $files * A list of scaffold results, each of which holds a path and whether * or not that file is managed. * @param \Drupal\Component\Scaffold\ScaffoldOptions $options diff --git a/core/lib/Drupal/Component/Scaffold/Operations/AppendOp.php b/core/lib/Drupal/Component/Scaffold/Operations/AppendOp.php index c4488eac3f..d4d58b1b33 100644 --- a/core/lib/Drupal/Component/Scaffold/Operations/AppendOp.php +++ b/core/lib/Drupal/Component/Scaffold/Operations/AppendOp.php @@ -28,14 +28,14 @@ class AppendOp implements OperationInterface { /** * Constructs an AppendOp. * - * @param \Drupal\Component\Scaffold\ScaffoldFilePath $prependPath + * @param \Drupal\Component\Scaffold\ScaffoldFilePath $prepend_path * The relative path to the prepend file. - * @param \Drupal\Component\Scaffold\ScaffoldFilePath $appendPath + * @param \Drupal\Component\Scaffold\ScaffoldFilePath $append_path * The relative path to the append file. */ - public function __construct(ScaffoldFilePath $prependPath = NULL, ScaffoldFilePath $appendPath = NULL) { - $this->prepend = $prependPath; - $this->append = $appendPath; + public function __construct(ScaffoldFilePath $prepend_path = NULL, ScaffoldFilePath $append_path = NULL) { + $this->prepend = $prepend_path; + $this->append = $append_path; } /** diff --git a/core/lib/Drupal/Component/Scaffold/Operations/OperationCollection.php b/core/lib/Drupal/Component/Scaffold/Operations/OperationCollection.php index 1e513b1e27..48ffdf8ee8 100644 --- a/core/lib/Drupal/Component/Scaffold/Operations/OperationCollection.php +++ b/core/lib/Drupal/Component/Scaffold/Operations/OperationCollection.php @@ -40,7 +40,7 @@ public function __construct(IOInterface $io) { * this will be the same as $scaffold_file->packageName() unless this scaffold * file has been overridden or removed by some other package. * - * @param array $list_of_scaffold_files + * @param \Drupal\Component\Scaffold\ScaffoldFileInfo[] $list_of_scaffold_files * Associative array containing destination => operation mappings. * @param \Drupal\Component\Scaffold\ScaffoldFileInfo $scaffold_file * The scaffold file to use to find a providing package name. @@ -55,16 +55,16 @@ protected function findProvidingPackage(array $list_of_scaffold_files, ScaffoldF if (!array_key_exists($dest_rel_path, $list_of_scaffold_files)) { throw new \RuntimeException("Scaffold file not found in list of all scaffold files."); } - $overridden_scaffold_file = $list_of_scaffold_files[$dest_rel_path]; - return $overridden_scaffold_file->packageName(); + return $list_of_scaffold_files[$dest_rel_path]->packageName(); } /** * Process all of the scaffold files listed in the provided file mappings. + * * @param array $file_mappings * An multidimensional array of file mappings, as returned by * self::getFileMappingsFromPackages(). - * @param \Drupal\Component\Scaffold\Interpolator $locationReplacements + * @param \Drupal\Component\Scaffold\Interpolator $location_replacements * An object with the location mappings (e.g. [web-root]). * @param \Drupal\Component\Scaffold\ScaffoldOptions $options * Configuration options from the top-level composer.json file. @@ -73,8 +73,8 @@ protected function findProvidingPackage(array $list_of_scaffold_files, ScaffoldF * Associative array keyed by destination path and values as the scaffold * result for each scaffolded file. */ - public function process(array $file_mappings, Interpolator $locationReplacements, ScaffoldOptions $options) { - list($list_of_scaffold_files, $resolved_file_mappings) = $this->collateScaffoldFiles($file_mappings, $locationReplacements); + public function process(array $file_mappings, Interpolator $location_replacements, ScaffoldOptions $options) { + list($list_of_scaffold_files, $resolved_file_mappings) = $this->collateScaffoldFiles($file_mappings, $location_replacements); return $this->processScaffoldFiles($list_of_scaffold_files, $resolved_file_mappings, $options); } @@ -84,7 +84,7 @@ public function process(array $file_mappings, Interpolator $locationReplacements * @param array $file_mappings * An multidimensional array of file mappings, as returned by * self::getFileMappingsFromPackages(). - * @param \Drupal\Component\Scaffold\Interpolator $locationReplacements + * @param \Drupal\Component\Scaffold\Interpolator $location_replacements * An object with the location mappings (e.g. [web-root]). * * @return array @@ -92,14 +92,16 @@ public function process(array $file_mappings, Interpolator $locationReplacements * - Associative array containing destination => operation mappings. * - Associative array containing package name => file mappings. */ - protected function collateScaffoldFiles(array $file_mappings, Interpolator $locationReplacements) { + protected function collateScaffoldFiles(array $file_mappings, Interpolator $location_replacements) { $resolved_file_mappings = []; + /** @var \Drupal\Component\Scaffold\ScaffoldFileInfo[] $list_of_scaffold_files */ $list_of_scaffold_files = []; foreach ($file_mappings as $package_name => $package_file_mappings) { foreach ($package_file_mappings as $destination_rel_path => $op) { - $destination = ScaffoldFilePath::destinationPath($package_name, $destination_rel_path, $locationReplacements); + $destination = ScaffoldFilePath::destinationPath($package_name, $destination_rel_path, $location_replacements); // If there was already a scaffolding operation happening at this path, - // use a ConjunctionOp if possible. + // AppendOp operations can be joined to the previous operation using a + // ConjunctionOp. if (isset($list_of_scaffold_files[$destination_rel_path]) && $op instanceof AppendOp) { $op = new ConjunctionOp($list_of_scaffold_files[$destination_rel_path]->op(), $op); } diff --git a/core/lib/Drupal/Component/Scaffold/Operations/OperationFactory.php b/core/lib/Drupal/Component/Scaffold/Operations/OperationFactory.php index ad6f531837..7ede3241d6 100644 --- a/core/lib/Drupal/Component/Scaffold/Operations/OperationFactory.php +++ b/core/lib/Drupal/Component/Scaffold/Operations/OperationFactory.php @@ -34,7 +34,7 @@ public function __construct(Composer $composer) { * * @param \Composer\Package\PackageInterface $package * The package that relative paths will be relative from. - * @param string $dest_rel_path + * @param string $destination * The destination path for the scaffold file. Used only for error messages. * @param mixed $metadata * The metadata for this operation object, which varies by operation type. @@ -46,17 +46,17 @@ public function __construct(Composer $composer) { * Exception thrown when $metadata can not be used to determine a scaffold * operation. */ - public function create(PackageInterface $package, $dest_rel_path, $metadata) { - $metadata = $this->normalizeScaffoldMetadata($dest_rel_path, $metadata); + public function create(PackageInterface $package, $destination, $metadata) { + $metadata = $this->normalizeScaffoldMetadata($destination, $metadata); switch ($metadata['mode']) { case 'skip': return new SkipOp(); case 'replace': - return $this->createReplaceOp($package, $dest_rel_path, $metadata); + return $this->createReplaceOp($package, $destination, $metadata); case 'append': - return $this->createAppendOp($package, $dest_rel_path, $metadata); + return $this->createAppendOp($package, $destination, $metadata); } throw new \RuntimeException("Unknown scaffold operation mode {$metadata['mode']}."); } @@ -68,7 +68,7 @@ public function create(PackageInterface $package, $dest_rel_path, $metadata) { * * @param \Composer\Package\PackageInterface $package * The package that relative paths will be relative from. - * @param string $dest_rel_path + * @param string $destination * The destination path for the scaffold file. Used only for error messages. * @param array $metadata * The metadata for this operation object, i.e. the relative 'path'. @@ -76,7 +76,7 @@ public function create(PackageInterface $package, $dest_rel_path, $metadata) { * @return \Drupal\Component\Scaffold\Operations\OperationInterface * A scaffold replace operation object. */ - protected function createReplaceOp(PackageInterface $package, $dest_rel_path, array $metadata) { + protected function createReplaceOp(PackageInterface $package, $destination, array $metadata) { // If this op does not provide an 'overwrite' value, default it to true. $metadata += ['overwrite' => TRUE]; if (!isset($metadata['path'])) { @@ -84,7 +84,7 @@ protected function createReplaceOp(PackageInterface $package, $dest_rel_path, ar } $package_name = $package->getName(); $package_path = $this->getPackagePath($package); - $source = ScaffoldFilePath::sourcePath($package_name, $package_path, $dest_rel_path, $metadata['path']); + $source = ScaffoldFilePath::sourcePath($package_name, $package_path, $destination, $metadata['path']); $op = new ReplaceOp($source, $metadata['overwrite']); return $op; } @@ -94,7 +94,7 @@ protected function createReplaceOp(PackageInterface $package, $dest_rel_path, ar * * @param \Composer\Package\PackageInterface $package * The package that relative paths will be relative from. - * @param string $dest_rel_path + * @param string $destination * The destination path for the scaffold file. Used only for error messages. * @param array $metadata * The metadata for this operation object, i.e. the relative 'path'. @@ -102,16 +102,16 @@ protected function createReplaceOp(PackageInterface $package, $dest_rel_path, ar * @return \Drupal\Component\Scaffold\Operations\OperationInterface * A scaffold replace operation object. */ - protected function createAppendOp(PackageInterface $package, $dest_rel_path, array $metadata) { + protected function createAppendOp(PackageInterface $package, $destination, array $metadata) { $package_name = $package->getName(); $package_path = $this->getPackagePath($package); $prepend_source_file = NULL; $append_source_file = NULL; if (isset($metadata['prepend'])) { - $prepend_source_file = ScaffoldFilePath::sourcePath($package_name, $package_path, $dest_rel_path, $metadata['prepend']); + $prepend_source_file = ScaffoldFilePath::sourcePath($package_name, $package_path, $destination, $metadata['prepend']); } if (isset($metadata['append'])) { - $append_source_file = ScaffoldFilePath::sourcePath($package_name, $package_path, $dest_rel_path, $metadata['append']); + $append_source_file = ScaffoldFilePath::sourcePath($package_name, $package_path, $destination, $metadata['append']); } $op = new AppendOp($prepend_source_file, $append_source_file); return $op; @@ -149,7 +149,7 @@ protected function getPackagePath(PackageInterface $package) { * - Boolean 'false' means "skip". * - A string means "replace", with the string value becoming the path. * - * @param string $dest_rel_path + * @param string $destination * The destination path for the scaffold file. * @param mixed $value * The metadata for this operation object, which varies by operation type. @@ -157,15 +157,15 @@ protected function getPackagePath(PackageInterface $package) { * @return array * Normalized scaffold metadata. */ - protected function normalizeScaffoldMetadata($dest_rel_path, $value) { + protected function normalizeScaffoldMetadata($destination, $value) { if (is_bool($value)) { if (!$value) { return ['mode' => 'skip']; } - throw new \RuntimeException("File mapping {$dest_rel_path} cannot be given the value 'true'."); + throw new \RuntimeException("File mapping {$destination} cannot be given the value 'true'."); } if (empty($value)) { - throw new \RuntimeException("File mapping {$dest_rel_path} cannot be empty."); + throw new \RuntimeException("File mapping {$destination} cannot be empty."); } if (is_string($value)) { $value = ['path' => $value]; diff --git a/core/lib/Drupal/Component/Scaffold/Operations/ReplaceOp.php b/core/lib/Drupal/Component/Scaffold/Operations/ReplaceOp.php index a980e9d2bf..358f2cdc5f 100644 --- a/core/lib/Drupal/Component/Scaffold/Operations/ReplaceOp.php +++ b/core/lib/Drupal/Component/Scaffold/Operations/ReplaceOp.php @@ -40,26 +40,6 @@ public function __construct(ScaffoldFilePath $sourcePath, $overwrite = TRUE) { $this->overwrite = $overwrite; } - /** - * Gets the source. - * - * @return \Drupal\Component\Scaffold\ScaffoldFilePath - * The source file reference object. - */ - protected function getSource() { - return $this->source; - } - - /** - * Determines whether scaffold file should overwrite files. - * - * @return bool - * Value of the 'overwrite' option. - */ - protected function getOverwrite() { - return $this->overwrite; - } - /** * Copy or Symlink the specified scaffold file. * @@ -70,7 +50,7 @@ public function process(ScaffoldFilePath $destination, IOInterface $io, Scaffold $destination_path = $destination->fullPath(); // Do nothing if overwrite is 'false' and a file already exists at the // destination. - if ($this->getOverwrite() === FALSE && file_exists($destination_path)) { + if ($this->overwrite === FALSE && file_exists($destination_path)) { $interpolator = $destination->getInterpolator(); $io->write($interpolator->interpolate(" - Skip [dest-rel-path] because it already exists and overwrite is false.")); return new ScaffoldResult($destination, FALSE); @@ -99,13 +79,13 @@ public function process(ScaffoldFilePath $destination, IOInterface $io, Scaffold */ protected function copyScaffold(ScaffoldFilePath $destination, IOInterface $io) { $interpolator = $destination->getInterpolator(); - $this->getSource()->addInterpolationData($interpolator); - $success = copy($this->getSource()->fullPath(), $destination->fullPath()); + $this->source->addInterpolationData($interpolator); + $success = copy($this->source->fullPath(), $destination->fullPath()); if (!$success) { throw new \RuntimeException($interpolator->interpolate("Could not copy source file [src-rel-path] to [dest-rel-path]!")); } $io->write($interpolator->interpolate(" - Copy [dest-rel-path] from [src-rel-path]")); - return new ScaffoldResult($destination, $this->getOverwrite()); + return new ScaffoldResult($destination, $this->overwrite); } /** @@ -123,13 +103,13 @@ protected function symlinkScaffold(ScaffoldFilePath $destination, IOInterface $i $interpolator = $destination->getInterpolator(); try { $fs = new Filesystem(); - $fs->relativeSymlink($this->getSource()->fullPath(), $destination->fullPath()); + $fs->relativeSymlink($this->source->fullPath(), $destination->fullPath()); } catch (\Exception $e) { throw new \RuntimeException($interpolator->interpolate("Could not symlink source file [src-rel-path] to [dest-rel-path]!"), [], $e); } $io->write($interpolator->interpolate(" - Link [dest-rel-path] from [src-rel-path]")); - return new ScaffoldResult($destination, $this->getOverwrite()); + return new ScaffoldResult($destination, $this->overwrite); } } diff --git a/core/lib/Drupal/Component/Scaffold/ScaffoldFilePath.php b/core/lib/Drupal/Component/Scaffold/ScaffoldFilePath.php index 35a1428fbe..8e842a68e1 100644 --- a/core/lib/Drupal/Component/Scaffold/ScaffoldFilePath.php +++ b/core/lib/Drupal/Component/Scaffold/ScaffoldFilePath.php @@ -35,7 +35,7 @@ class ScaffoldFilePath { * * @var string */ - protected $relPath; + protected $relativePath; /** * The full path to the file. @@ -59,7 +59,7 @@ class ScaffoldFilePath { public function __construct($path_type, $package_name, $rel_path, $full_path) { $this->type = $path_type; $this->packageName = $package_name; - $this->relPath = $rel_path; + $this->relativePath = $rel_path; $this->fullPath = $full_path; } @@ -80,7 +80,7 @@ public function packageName() { * Relative path to file. */ public function relativePath() { - return $this->relPath; + return $this->relativePath; } /** diff --git a/core/lib/Drupal/Component/Scaffold/ScaffoldOptions.php b/core/lib/Drupal/Component/Scaffold/ScaffoldOptions.php index 91752c3660..ddc86659a0 100644 --- a/core/lib/Drupal/Component/Scaffold/ScaffoldOptions.php +++ b/core/lib/Drupal/Component/Scaffold/ScaffoldOptions.php @@ -60,16 +60,6 @@ public static function create(array $extras) { return new self($options); } - /** - * Creates a scaffold option object with default values. - * - * @return self - * A scaffold options object with default values - */ - public static function defaultOptions() { - return new self([]); - } - /** * Creates a new scaffold options object with some values overridden. * diff --git a/core/tests/Drupal/Tests/Component/Scaffold/ExecTrait.php b/core/tests/Drupal/Tests/Component/Scaffold/ExecTrait.php index 14c44ee6f4..0986961788 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/ExecTrait.php +++ b/core/tests/Drupal/Tests/Component/Scaffold/ExecTrait.php @@ -28,7 +28,7 @@ protected function mustExec($cmd, $cwd, array $env = []) { $process->setTimeout(300)->setIdleTimeout(300)->run(); $exitCode = $process->getExitCode(); if (0 != $exitCode) { - throw new \Exception("Exit code: {$exitCode}\n\n" . $process->getErrorOutput() . "\n\n" . $process->getOutput()); + throw new \RuntimeException("Exit code: {$exitCode}\n\n" . $process->getErrorOutput() . "\n\n" . $process->getOutput()); } return [$process->getOutput(), $process->getErrorOutput()]; } diff --git a/core/tests/Drupal/Tests/Component/Scaffold/Fixtures.php b/core/tests/Drupal/Tests/Component/Scaffold/Fixtures.php index 86dab883e1..de91e9381b 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/Fixtures.php +++ b/core/tests/Drupal/Tests/Component/Scaffold/Fixtures.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\Component\Scaffold; -use Composer\Composer; use Composer\Console\Application; use Composer\Factory; use Composer\IO\BufferIO; @@ -19,20 +18,34 @@ * Convenience class for creating fixtures. */ class Fixtures { + /** * Directories to delete when we are done. * * @var string[] */ protected $tmpDirs = []; + + /** + * A Composer IOInterface to write to. + * + * @var \Composer\IO\IOInterface + */ protected $io; + + /** + * The composer object. + * + * @var \Composer\Composer + */ protected $composer; /** - * Get an IO fixture. + * Gets an IO fixture. * * @return \Composer\IO\IOInterface - * A Composer IOInterface to write to; output may be retrieved via Fixtures::getOutput() + * A Composer IOInterface to write to; output may be retrieved via + * Fixtures::getOutput(). */ public function io() { if (!$this->io) { @@ -42,7 +55,7 @@ public function io() { } /** - * Get the Composer object. + * Gets the Composer object. * * @return \Composer\Composer * The main Composer object, needed by the scaffold Handler, etc. @@ -55,7 +68,7 @@ public function getComposer() { } /** - * Get the output from our io() fixture. + * Gets the output from the io() fixture. * * @return string * Output captured from tests that write to Fixtures::io(). @@ -65,7 +78,9 @@ public function getOutput() { } /** - * Return the path to this project so that it may be injected into composer.json files. + * Gets the path to Scaffold component. + * + * Used to inject the component into composer.json files. * * @return string * Path to the root of this project. @@ -75,7 +90,7 @@ public function projectRoot() { } /** - * Return the path to the project fixtures. + * Gets the path to the project fixtures. * * @return string * Path to project fixtures @@ -85,59 +100,71 @@ public function allFixturesDir() { } /** - * Return the path to one particular project fixture. + * Gets the path to one particular project fixture. + * + * @param string $project_name + * The project name to get the path for. * * @return string - * Path to project fixture + * Path to project fixture. */ public function projectFixtureDir($project_name) { $dir = $this->allFixturesDir() . '/' . $project_name; if (!is_dir($dir)) { - throw new \Exception("Requested fixture project {$project_name} that does not exist."); + throw new \RuntimeException("Requested fixture project {$project_name} that does not exist."); } return $dir; } /** - * Return the path to one particular bin path. + * Gets the path to one particular bin path. + * + * @param string $bin_name + * The bin name to get the path for. * * @return string - * Path to project fixture + * Path to project fixture. */ public function binFixtureDir($bin_name) { $dir = $this->allFixturesDir() . '/scripts/' . $bin_name; if (!is_dir($dir)) { - throw new \Exception("Requested fixture bin dir {$bin_name} that does not exist."); + throw new \RuntimeException("Requested fixture bin dir {$bin_name} that does not exist."); } return $dir; } /** - * Use in place of ScaffoldFilePath::sourcePath to get a path to a source scaffold fixture. + * Gets a path to a source scaffold fixture. + * + * Use in place of ScaffoldFilePath::sourcePath(). * * @param string $project_name - * The name of the project to fetch; $package_name is "fixtures/$project_name". + * The name of the project to fetch; $package_name is + * "fixtures/$project_name". * @param string $source * The name of the asset; path is "assets/$source". - * @param string $destination - * The path to the destination; only used in error messages, not needed for most tests. * * @return \Drupal\Component\Scaffold\ScaffoldFilePath * The full and relative path to the desired asset + * + * @see \Drupal\Component\Scaffold\ScaffoldFilePath::sourcePath() */ - public function sourcePath($project_name, $source, $destination = 'unknown') { + public function sourcePath($project_name, $source) { $package_name = "fixtures/{$project_name}"; $source_rel_path = "assets/{$source}"; $package_path = $this->projectFixtureDir($project_name); - $destination = 'unknown'; - return ScaffoldFilePath::sourcePath($package_name, $package_path, $destination, $source_rel_path); + return ScaffoldFilePath::sourcePath($package_name, $package_path, 'unknown', $source_rel_path); } /** - * Use in place of Handler::getLocationReplacements() to obtain a 'web-root'. + * Gets an Interpolator with 'web-root' and 'package-name' set. + * + * Use in place of ManageOptions::getLocationReplacements(). * * @return \Drupal\Component\Scaffold\Interpolator * An interpolator with location replacements, including 'web-root'. + * + * @see \Drupal\Component\Scaffold\ManageOptions::getLocationReplacements() */ public function getLocationReplacements() { $destinationTmpDir = $this->mkTmpDir(); @@ -147,10 +174,11 @@ public function getLocationReplacements() { } /** - * Use to create a ReplaceOp fixture. + * Creates a ReplaceOp fixture. * * @param string $project_name - * The name of the project to fetch; $package_name is "fixtures/$project_name". + * The name of the project to fetch; $package_name is + * "fixtures/$project_name". * @param string $source * The name of the asset; path is "assets/$source". * @@ -163,15 +191,16 @@ public function replaceOp($project_name, $source) { } /** - * Use to create an AppendOp fixture. + * Creates an AppendOp fixture. * * @param string $project_name - * The name of the project to fetch; $package_name is "fixtures/$project_name". + * The name of the project to fetch; $package_name is + * "fixtures/$project_name". * @param string $source * The name of the asset; path is "assets/$source". * * @return \Drupal\Component\Scaffold\Operations\AppendOp - * An append opperation object. + * An append operation object. */ public function appendOp($project_name, $source) { $source_path = $this->sourcePath($project_name, $source); @@ -179,7 +208,9 @@ public function appendOp($project_name, $source) { } /** - * Use in place of ScaffoldFilePath::destinationPath to get a destination path in a tmp dir. + * Gets a destination path in a tmp dir. + * + * Use in place of ScaffoldFilePath::destinationPath(). * * @param string $destination * Destination path; should be in the form '[web-root]/robots.txt', where @@ -189,11 +220,13 @@ public function appendOp($project_name, $source) { * Location replacements. Obtain via Fixtures::getLocationReplacements() * when creating multiple scaffold destinations. * @param string $package_name - * The name of the fixture package that this path came from. Optional; - * taken from interpolator if not provided. + * (optional) The name of the fixture package that this path came from. + * Taken from interpolator if not provided. * * @return \Drupal\Component\Scaffold\ScaffoldFilePath * A destination scaffold file backed by temporary storage. + * + * @see \Drupal\Component\Scaffold\ScaffoldFilePath::destinationPath() */ public function destinationPath($destination, Interpolator $interpolator = NULL, $package_name = NULL) { $interpolator = $interpolator ?: $this->getLocationReplacements(); @@ -202,7 +235,7 @@ public function destinationPath($destination, Interpolator $interpolator = NULL, } /** - * Generate a path to a temporary location, but do not create the directory. + * Generates a path to a temporary location, but do not create the directory. * * @param string $extraSalt * Extra characters to throw into the md5 to add to name. @@ -217,7 +250,7 @@ public function tmpDir($extraSalt = '') { } /** - * Create a temporary directory. + * Creates a temporary directory. * * @param string $extraSalt * Extra characters to throw into the md5 to add to name. @@ -233,7 +266,7 @@ public function mkTmpDir($extraSalt = '') { } /** - * Call 'tearDown' in any test that copies fixtures to transient locations. + * Calls 'tearDown' in any test that copies fixtures to transient locations. */ public function tearDown() { // Remove any temporary directories that were created. @@ -243,28 +276,28 @@ public function tearDown() { } // Clear out variables from the previous pass. $this->tmpDirs = []; - $this->fixturesDir = NULL; $this->io = NULL; } /** - * Create a temporary copy of all of the fixtures projects into a temp dir. + * Creates a temporary copy of all of the fixtures projects into a temp dir. * * The fixtures remain dirty if they already exist. Individual tests should - * first delete any fixture directory that needs to remain pristine. Since - * all temporary directories are removed in tearDown, this is only an issue - * when a) the FIXTURE_DIR environment variable has been set, or b) tests - * are calling cloneFixtureProjects more than once per test method. + * first delete any fixture directory that needs to remain pristine. Since all + * temporary directories are removed in tearDown, this is only an issue when + * a) the FIXTURE_DIR environment variable has been set, or b) tests are + * calling cloneFixtureProjects more than once per test method. * * @param string $fixturesDir * The directory to place fixtures in. * @param array $replacements - * Key : value mappings for placeholders to replace in composer.json templates. + * Key : value mappings for placeholders to replace in composer.json + * templates. */ public function cloneFixtureProjects($fixturesDir, array $replacements = []) { $filesystem = new Filesystem(); $replacements += ['SYMLINK' => 'true']; - $interpolator = new Interpolator('__', '__', TRUE); + $interpolator = new Interpolator('__', '__'); $interpolator->setData($replacements); $filesystem->copy($this->allFixturesDir(), $fixturesDir); $composer_json_templates = glob($fixturesDir . "/*/composer.json.tmpl"); @@ -280,12 +313,18 @@ public function cloneFixtureProjects($fixturesDir, array $replacements = []) { } /** - * Run the scaffold operation. + * Runs the scaffold operation. + * + * This is equivalent to running `composer composer-scaffold`, but we do the + * equivalent operation by instantiating a Handler object in order to continue + * running in the same process, so that coverage may be calculated for the + * code executed by these tests. * - * This is equivalent to running `composer composer-scaffold`, but we - * do the equivalent operation by instantiating a Handler object in order - * to continue running in the same process, so that coverage may be - * calculated for the code executed by these tests. + * @param string $cwd + * The working directory to run the scaffold command in. + * + * @return string + * Output captured from tests that write to Fixtures::io(). */ public function runScaffold($cwd) { chdir($cwd); @@ -304,8 +343,8 @@ public function runScaffold($cwd) { * @param int $expectedExitCode * The expected exit code; will throw if a different exit code is returned. * - * @return array - * Standard output and standard error from the command + * @return string + * Standard output and standard error from the command. */ public function runComposer($cmd, $cwd, $expectedExitCode = 0) { chdir($cwd); @@ -315,13 +354,13 @@ public function runComposer($cmd, $cwd, $expectedExitCode = 0) { $application->setAutoExit(FALSE); try { $exitCode = $application->run($input, $output); + if ($exitCode != $expectedExitCode) { + print "Command '{$cmd}' - Expected exit code: {$expectedExitCode}, actual exit code: {$exitCode}\n"; + } } catch (\Exception $e) { print "Exception: " . $e->getMessage() . "\n"; } - if ($exitCode != $expectedExitCode) { - print "Command '{$cmd}' - Expected exit code: {$expectedExitCode}, actual exit code: {$exitCode}\n"; - } $output = $output->fetch(); return $output; } diff --git a/core/tests/Drupal/Tests/Component/Scaffold/Functional/ComposerHookTest.php b/core/tests/Drupal/Tests/Component/Scaffold/Functional/ComposerHookTest.php index 12fd9f1166..c48d8688c7 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/Functional/ComposerHookTest.php +++ b/core/tests/Drupal/Tests/Component/Scaffold/Functional/ComposerHookTest.php @@ -25,6 +25,7 @@ class ComposerHookTest extends TestCase { use ExecTrait; use AssertUtilsTrait; + /** * The root of this project. * @@ -34,12 +35,14 @@ class ComposerHookTest extends TestCase { * @var string */ protected $projectRoot; + /** * Directory to perform the tests in. * * @var string */ protected $fixturesDir; + /** * The Symfony FileSystem component. * @@ -47,6 +50,13 @@ class ComposerHookTest extends TestCase { */ protected $fileSystem; + /** + * The Fixtures object. + * + * @var \Drupal\Tests\Component\Scaffold\Fixtures + */ + protected $fixtures; + /** * {@inheritdoc} */ @@ -82,7 +92,7 @@ public function testComposerHooks() { // project is "allowed" in our main fixture project, but not required. // We expect that requiring this library should re-scaffold, resulting // in a changed default.settings.php file. - list($stdout, $stderr) = $this->execComposer("require --no-ansi --no-interaction fixtures/scaffold-override-fixture:dev-master", $sut); + list($stdout,) = $this->execComposer("require --no-ansi --no-interaction fixtures/scaffold-override-fixture:dev-master", $sut); $this->assertScaffoldedFile($sut . '/sites/default/default.settings.php', $is_link, '#scaffolded from the scaffold-override-fixture#'); // Make sure that the appropriate notice informing us that scaffolding // is allowed was printed. @@ -99,11 +109,10 @@ public function testComposerHooks() { $this->assertScaffoldedFile($sut . '/sites/default/default.settings.php', $is_link, '#scaffolded from the scaffold-override-fixture#'); // Run 'composer create-project' to create a new test project called // 'create-project-test', which is a copy of 'fixtures/drupal-drupal'. - $packages = $this->fixturesDir . '/packages.json'; $sut = $this->fixturesDir . '/create-project-test'; $filesystem = new Filesystem(); $filesystem->remove($sut); - list($stdout, $stderr) = $this->execComposer("create-project --repository=packages.json fixtures/drupal-drupal {$sut}", $this->fixturesDir, ['COMPOSER_MIRROR_PATH_REPOS' => 1]); + list($stdout,) = $this->execComposer("create-project --repository=packages.json fixtures/drupal-drupal {$sut}", $this->fixturesDir, ['COMPOSER_MIRROR_PATH_REPOS' => 1]); $this->assertDirectoryExists($sut); $this->assertContains('Scaffolding files for fixtures/drupal-drupal', $stdout); $this->assertScaffoldedFile($sut . '/index.php', FALSE, '#Test version of index.php from drupal/core#'); @@ -113,7 +122,7 @@ public function testComposerHooks() { $this->execComposer("install --no-ansi", $sut); // Require a project that is not allowed to scaffold and confirm that we // get a warning, and it does not scaffold. - list($stdout, $stderr) = $this->execComposer("require --no-ansi --no-interaction fixtures/scaffold-override-fixture:dev-master", $sut); + list($stdout,) = $this->execComposer("require --no-ansi --no-interaction fixtures/scaffold-override-fixture:dev-master", $sut); $this->assertFileNotExists($sut . '/sites/default/default.settings.php'); $this->assertContains('Package fixtures/scaffold-override-fixture has scaffold operations, but it is not allowed in the root-level composer.json file.', $stdout); } diff --git a/core/tests/Drupal/Tests/Component/Scaffold/Functional/ManageGitIgnoreTest.php b/core/tests/Drupal/Tests/Component/Scaffold/Functional/ManageGitIgnoreTest.php index 149063c58a..fc6f63f8c7 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/Functional/ManageGitIgnoreTest.php +++ b/core/tests/Drupal/Tests/Component/Scaffold/Functional/ManageGitIgnoreTest.php @@ -20,6 +20,7 @@ class ManageGitIgnoreTest extends TestCase { use ExecTrait; use AssertUtilsTrait; + /** * The root of this project. * @@ -29,12 +30,14 @@ class ManageGitIgnoreTest extends TestCase { * @var string */ protected $projectRoot; + /** * Directory to perform the tests in. * * @var string */ protected $fixturesDir; + /** * The Symfony FileSystem component. * @@ -42,6 +45,13 @@ class ManageGitIgnoreTest extends TestCase { */ protected $fileSystem; + /** + * The Fixtures object. + * + * @var \Drupal\Tests\Component\Scaffold\Fixtures + */ + protected $fixtures; + /** * {@inheritdoc} */ @@ -60,12 +70,19 @@ protected function tearDown() { } /** - * Create a system-under-test and initialize a git repository for it. + * Creates a system-under-test and initialize a git repository for it. + * + * @param string $fixture_name + * The name of the fixture to use from + * core/tests/Drupal/Tests/Component/Scaffold/fixtures. + * + * @return string + * The path to the fixture directory. */ - protected function createSutWithGit($topLevelProjectDir) { + protected function createSutWithGit($fixture_name) { $is_link = FALSE; $this->fixturesDir = $this->fixtures->tmpDir($this->getName()); - $sut = $this->fixturesDir . '/' . $topLevelProjectDir; + $sut = $this->fixturesDir . '/' . $fixture_name; $replacements = ['SYMLINK' => $is_link ? 'true' : 'false', 'PROJECT_ROOT' => $this->projectRoot]; $this->fixtures->cloneFixtureProjects($this->fixturesDir, $replacements); // .gitignore files will not be managed unless there is a git repository. @@ -75,13 +92,13 @@ protected function createSutWithGit($topLevelProjectDir) { $this->mustExec('git config user.name "Test User"', $sut); $this->mustExec('git add .', $sut); $this->mustExec('git commit -m "Initial commit."', $sut); - // Run composer install, but supress scaffolding. + // Run composer install, but suppress scaffolding. $this->fixtures->runComposer("install --no-ansi --no-scripts", $sut); return $sut; } /** - * Test to see if the scaffold operation correctly manages the .gitignore file. + * Tests scaffold command correctly manages the .gitignore file. */ public function testManageGitIgnore() { // Note that the drupal-composer-drupal-project fixture does not @@ -106,9 +123,9 @@ public function testManageGitIgnore() { update.php web.config EOT; - // At this point we should have a .gitignore file, because although we - // did not explicitly ask for .gitignore tracking, the vendor directory - // is not tracked, so the default in that instance is to manage .gitignore files. + // At this point we should have a .gitignore file, because although we did + // not explicitly ask for .gitignore tracking, the vendor directory is not + // tracked, so the default in that instance is to manage .gitignore files. $this->assertScaffoldedFile($sut . '/docroot/.gitignore', FALSE, '#' . $expected . '#msi'); $this->assertScaffoldedFile($sut . '/docroot/sites/.gitignore', FALSE, '#example.settings.local.php#'); $this->assertScaffoldedFile($sut . '/docroot/sites/default/.gitignore', FALSE, '#default.services.yml#'); @@ -119,12 +136,12 @@ public function testManageGitIgnore() { EOT; // Check to see whether there are any untracked files. We expect that // only the .gitignore files themselves should be untracked. - list($stdout, $stderr) = $this->mustExec('git status --porcelain', $sut); + list($stdout,) = $this->mustExec('git status --porcelain', $sut); $this->assertEquals(trim($expected), trim($stdout)); } /** - * Test to see if scaffold operation does not manage the .gitignore file when disabled. + * Tests scaffold command does not manage the .gitignore file when disabled. */ public function testUnmanagedGitIgnoreWhenDisabled() { // Note that the drupal-drupal fixture has a configuration setting @@ -139,24 +156,26 @@ public function testUnmanagedGitIgnoreWhenDisabled() { } /** - * Test to see if the scaffold operation disables .gitignore management when git not present. + * Tests scaffold command disables .gitignore management when git not present. * - * The scaffold operation should still succeed if there is no 'git' executable. + * The scaffold operation should still succeed if there is no 'git' + * executable. */ public function testUnmanagedGitIgnoreWhenGitNotAvailable() { - // Note that the drupal-composer-drupal-project fixture does not - // have any configuration settings related to .gitignore management. + // Note that the drupal-composer-drupal-project fixture does not have any + // configuration settings related to .gitignore management. $sut = $this->createSutWithGit('drupal-composer-drupal-project'); $this->assertFileNotExists($sut . '/docroot/sites/default/.gitignore'); $this->assertFileNotExists($sut . '/docroot/index.php'); $this->assertFileNotExists($sut . '/docroot/sites/.gitignore'); - // Confirm that 'git' is available (n.b. if it were not, createSutWithGit() would fail). + // Confirm that 'git' is available (n.b. if it were not, createSutWithGit() + // would fail). exec('git --help', $output, $status); $this->assertEquals(0, $status); - // Modify our $PATH so that it begins with a path that contains an executable - // script named 'git' that always exits with 127, as if git were not found. - // Note that we run our tests using process isolation, so we do not need to - // restore the PATH when we are done. + // Modify our $PATH so that it begins with a path that contains an + // executable script named 'git' that always exits with 127, as if git were + // not found. Note that we run our tests using process isolation, so we do + // not need to restore the PATH when we are done. $unavailableGitPath = $this->fixtures->binFixtureDir('disable-git-bin'); chmod($unavailableGitPath . '/git', 0755); putenv('PATH=' . $unavailableGitPath . ':' . getenv('PATH')); diff --git a/core/tests/Drupal/Tests/Component/Scaffold/Functional/ScaffoldTest.php b/core/tests/Drupal/Tests/Component/Scaffold/Functional/ScaffoldTest.php index ec6eae0c28..3e733c2ad2 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/Functional/ScaffoldTest.php +++ b/core/tests/Drupal/Tests/Component/Scaffold/Functional/ScaffoldTest.php @@ -17,7 +17,7 @@ */ class ScaffoldTest extends TestCase { use AssertUtilsTrait; - const FIXTURE_DIR = 'SCAFFOLD_FIXTURE_DIR'; + /** * The root of this project. * @@ -27,18 +27,14 @@ class ScaffoldTest extends TestCase { * @var string */ protected $projectRoot; + /** * Directory to perform the tests in. * * @var string */ protected $fixturesDir; - /** - * The file path to the system under test. - * - * @var string - */ - protected $sut; + /** * The Symfony FileSystem component. * @@ -46,6 +42,13 @@ class ScaffoldTest extends TestCase { */ protected $fileSystem; + /** + * The Fixtures object. + * + * @var \Drupal\Tests\Component\Scaffold\Fixtures + */ + protected $fixtures; + /** * {@inheritdoc} */ @@ -53,7 +56,10 @@ protected function setUp() { $this->fileSystem = new Filesystem(); $this->fixtures = new Fixtures(); $this->projectRoot = $this->fixtures->projectRoot(); - $this->fixturesDir = getenv(self::FIXTURE_DIR); + // The directory used for creating composer projects to test can be + // configured using the SCAFFOLD_FIXTURE_DIR environment variable. Otherwise + // a directory will be created in the system's temporary directory. + $this->fixturesDir = getenv('SCAFFOLD_FIXTURE_DIR'); if (!$this->fixturesDir) { $this->fixturesDir = $this->fixtures->tmpDir($this->getName()); } @@ -68,15 +74,25 @@ protected function tearDown() { } /** - * Create the System-Under-Test. + * Creates the System-Under-Test. + * + * @param string $fixture_name + * The name of the fixture to use from + * core/tests/Drupal/Tests/Component/Scaffold/fixtures. + * @param array $replacements + * Key : value mappings for placeholders to replace in composer.json + * templates. + * + * @return string + * The path to the created System-Under-Test. */ - protected function createSut($topLevelProjectDir, $replacements = []) { - $this->sut = $this->fixturesDir . '/' . $topLevelProjectDir; + protected function createSut($fixture_name, array $replacements = []) { + $sut = $this->fixturesDir . '/' . $fixture_name; // Erase just our sut, to ensure it is clean. Recopy all of the fixtures. - $this->fileSystem->remove($this->sut); + $this->fileSystem->remove($sut); $replacements += ['PROJECT_ROOT' => $this->projectRoot]; $this->fixtures->cloneFixtureProjects($this->fixturesDir, $replacements); - return $this->sut; + return $sut; } /** @@ -95,15 +111,23 @@ public function scaffoldFixturesWithErrorConditionsTestValues() { /** * Tests that scaffold files throw when they have bad values. * + * @param string $fixture_name + * The name of the fixture to use from + * core/tests/Drupal/Tests/Component/Scaffold/fixtures. + * @param string $expected_exception_message + * The expected exception message. + * @param bool $is_link + * Whether or not symlinking should be used. + * * @dataProvider scaffoldFixturesWithErrorConditionsTestValues */ - public function testScaffoldFixturesWithErrorConditions($topLevelProjectDir, $expectedExceptionMessage, $is_link) { - $sut = $this->createSut($topLevelProjectDir, ['SYMLINK' => $is_link ? 'true' : 'false']); + public function testScaffoldFixturesWithErrorConditions($fixture_name, $expected_exception_message, $is_link) { + $sut = $this->createSut($fixture_name, ['SYMLINK' => $is_link ? 'true' : 'false']); // Run composer install to get the dependencies we need to test. - $this->fixtures->runComposer("install --no-ansi --no-scripts", $this->sut); + $this->fixtures->runComposer("install --no-ansi --no-scripts", $sut); // Test scaffold. Expect an error. $this->expectException(\Exception::class); - $this->expectExceptionMessage($expectedExceptionMessage); + $this->expectExceptionMessage($expected_exception_message); $this->fixtures->runScaffold($sut); } @@ -142,16 +166,25 @@ public function scaffoldTestValues() { /** * Tests that scaffold files are correctly moved. * + * @param string $fixture_name + * The name of the fixture to use from + * core/tests/Drupal/Tests/Component/Scaffold/fixtures. + * @param string $assert_function + * The name of the assert function to call after running the scaffold + * command. + * @param bool $is_link + * Whether to use symlinking. + * * @dataProvider scaffoldTestValues */ - public function testScaffold($topLevelProjectDir, $scaffoldAssertions, $is_link) { - $sut = $this->createSut($topLevelProjectDir, ['SYMLINK' => $is_link ? 'true' : 'false']); + public function testScaffold($fixture_name, $assert_function, $is_link) { + $sut = $this->createSut($fixture_name, ['SYMLINK' => $is_link ? 'true' : 'false']); // Run composer install to get the dependencies we need to test. - $this->fixtures->runComposer("install --no-ansi --no-scripts", $this->sut); + $this->fixtures->runComposer("install --no-ansi --no-scripts", $sut); // Test composer:scaffold. - $scaffoldOutput = $this->fixtures->runScaffold($sut); + $this->fixtures->runScaffold($sut); // @todo We could assert that $scaffoldOutput must contain some expected text - call_user_func([$this, $scaffoldAssertions], $sut, $is_link, $topLevelProjectDir); + call_user_func([$this, $assert_function], $sut, $is_link, $fixture_name); } /** @@ -161,7 +194,7 @@ public function testEmptyProject() { $topLevelProjectDir = 'empty-fixture'; $sut = $this->createSut($topLevelProjectDir, ['SYMLINK' => 'false']); // Run composer install to get the dependencies we need to test. - $this->fixtures->runComposer("install --no-ansi --no-scripts", $this->sut); + $this->fixtures->runComposer("install --no-ansi --no-scripts", $sut); // Test composer:scaffold. $scaffoldOutput = $this->fixtures->runScaffold($sut); $this->assertContains('Nothing scaffolded because no packages are allowed in the top-level composer.json file', $scaffoldOutput); @@ -174,12 +207,12 @@ public function testProjectThatScaffoldsEmptyProject() { $topLevelProjectDir = 'project-allowing-empty-fixture'; $sut = $this->createSut($topLevelProjectDir, ['SYMLINK' => 'false']); // Run composer install to get the dependencies we need to test. - $this->fixtures->runComposer("install --no-ansi --no-scripts", $this->sut); + $this->fixtures->runComposer("install --no-ansi --no-scripts", $sut); // Test composer:scaffold. $scaffoldOutput = $this->fixtures->runScaffold($sut); $this->assertContains('The allowed package fixtures/empty-fixture does not provide a file mapping for Composer Scaffold', $scaffoldOutput); $docroot = $sut; - $this->assertCommonDrupalAssetsWereScaffolded($docroot, FALSE, $topLevelProjectDir); + $this->assertCommonDrupalAssetsWereScaffolded($docroot, FALSE); } /** @@ -189,7 +222,7 @@ public function testProjectWithEmptyScaffoldPath() { $topLevelProjectDir = 'project-with-empty-scaffold-path'; $sut = $this->createSut($topLevelProjectDir, ['SYMLINK' => 'false']); // Run composer install to get the dependencies we need to test. - $this->fixtures->runComposer("install --no-ansi --no-scripts", $this->sut); + $this->fixtures->runComposer("install --no-ansi --no-scripts", $sut); // Test composer:scaffold. $this->expectException(\Exception::class); $this->expectExceptionMessage('No scaffold file path given for [web-root]/my-error in package fixtures/project-with-empty-scaffold-path'); @@ -203,7 +236,7 @@ public function testProjectWithIllegalDirScaffold() { $topLevelProjectDir = 'project-with-illegal-dir-scaffold'; $sut = $this->createSut($topLevelProjectDir, ['SYMLINK' => 'false']); // Run composer install to get the dependencies we need to test. - $this->fixtures->runComposer("install --no-ansi --no-scripts", $this->sut); + $this->fixtures->runComposer("install --no-ansi --no-scripts", $sut); // Test composer:scaffold. $this->expectException(\Exception::class); $this->expectExceptionMessage('Scaffold file assets in package fixtures/project-with-illegal-dir-scaffold is a directory; only files may be scaffolded'); @@ -211,34 +244,53 @@ public function testProjectWithIllegalDirScaffold() { } /** - * Asserts that the drupal/assets scaffold files correct for drupal/project layout. + * Asserts that the drupal/assets scaffold files correct for drupal/project. + * + * @param string $sut + * The path to the System-under-Test. + * @param bool $is_link + * Whether or not symlinking is used. + * @param string $project_name + * Not used but this assertion is called dynamically and this argument is + * provided. */ protected function assertDrupalProjectSutWasScaffolded($sut, $is_link, $project_name) { $docroot = $sut . '/docroot'; - $this->assertCommonDrupalAssetsWereScaffolded($docroot, $is_link, $project_name); + $this->assertCommonDrupalAssetsWereScaffolded($docroot, $is_link); $this->assertDefaultSettingsFromScaffoldOverride($docroot, $is_link); $this->assertHtaccessExcluded($docroot); } /** - * Asserts that the drupal/assets scaffold files correct for drupal/drupal layout. + * Asserts that the drupal/assets scaffold files correct for drupal/drupal. + * + * @param string $sut + * The path to the System-under-Test. + * @param bool $is_link + * Whether or not symlinking is used. + * @param string $project_name + * Not used but this assertion is called dynamically and this argument is + * provided. */ protected function assertDrupalDrupalSutWasScaffolded($sut, $is_link, $project_name) { $docroot = $sut; - $this->assertCommonDrupalAssetsWereScaffolded($docroot, $is_link, $project_name); + $this->assertCommonDrupalAssetsWereScaffolded($docroot, $is_link); $this->assertDefaultSettingsFromScaffoldOverride($docroot, $is_link); $this->assertHtaccessExcluded($docroot); } /** - * Ensure that the default settings file was overridden by the test. + * Asserts that the default settings file was overridden by the test. */ protected function assertDefaultSettingsFromScaffoldOverride($docroot, $is_link) { $this->assertScaffoldedFile($docroot . '/sites/default/default.settings.php', $is_link, '#scaffolded from the scaffold-override-fixture#'); } /** - * Ensure that the .htaccess file was excluded by the test. + * Asserts that the .htaccess file was excluded by the test. + * + * @param string $docroot + * The path to the System-under-Test's docroot. */ protected function assertHtaccessExcluded($docroot) { // Ensure that the .htaccess.txt file was not written, as our @@ -247,39 +299,58 @@ protected function assertHtaccessExcluded($docroot) { } /** - * Assert that the appropriate file was replaced. + * Asserts that the appropriate file was replaced. * * Check the drupal/drupal-based project to confirm that the expected file was * replaced, and that files that were not supposed to be replaced remain * unchanged. + * + * @param string $sut + * The path to the System-under-Test. + * @param bool $is_link + * Whether or not symlinking is used. + * @param string $project_name + * The project name expected in the robots.txt file. */ protected function assertDrupalDrupalFileWasReplaced($sut, $is_link, $project_name) { $docroot = $sut; $this->assertScaffoldedFile($docroot . '/replace-me.txt', $is_link, '#from assets that replaces file#'); $this->assertScaffoldedFile($docroot . '/keep-me.txt', $is_link, '#File in drupal-drupal-test-overwrite that is not replaced#'); $this->assertScaffoldedFile($docroot . '/make-me.txt', $is_link, '#from assets that replaces file#'); - $this->assertCommonDrupalAssetsWereScaffolded($docroot, $is_link, $project_name); + $this->assertCommonDrupalAssetsWereScaffolded($docroot, $is_link); $this->assertScaffoldedFile($docroot . '/robots.txt', $is_link, "#{$project_name}#"); } /** - * Confirm that the robots.txt file was prepended / appended as stipulated in the test. + * Asserts that the robots.txt file was prepended / appended as expected. + * + * @param string $sut + * The path to the System-under-Test. + * @param bool $is_link + * Whether or not symlinking is used. + * @param string $project_name + * Not used but this assertion is called dynamically and this argument is + * provided. */ protected function assertDrupalDrupalFileWasAppended($sut, $is_link, $project_name) { $docroot = $sut; $this->assertScaffoldedFile($docroot . '/robots.txt', FALSE, '#in drupal-drupal-test-append composer.json fixture.*This content is prepended to the top of the existing robots.txt fixture.*Test version of robots.txt from drupal/core.*This content is appended to the bottom of the existing robots.txt fixture.*in drupal-drupal-test-append composer.json fixture#ms'); - $this->assertCommonDrupalAssetsWereScaffolded($docroot, $is_link, $project_name); + $this->assertCommonDrupalAssetsWereScaffolded($docroot, $is_link); } /** - * Assert that the scaffold files from drupal/assets are placed as we expect them to be. + * Asserts that the scaffold files from drupal/assets are placed as expected. * * This tests that all assets from drupal/assets were scaffolded, save * for .htaccess, robots.txt and default.settings.php, which are scaffolded * in different ways in different tests. + * + * @param string $docroot + * The path to the System-under-Test's docroot. + * @param bool $is_link + * Whether or not symlinking is used. */ - protected function assertCommonDrupalAssetsWereScaffolded($docroot, $is_link, $project_name) { - $from_project = "#scaffolded from \"file-mappings\" in {$project_name} composer.json fixture#"; + protected function assertCommonDrupalAssetsWereScaffolded($docroot, $is_link) { $from_core = '#from drupal/core#'; // Ensure that the autoload.php file was written. $this->assertFileExists($docroot . '/autoload.php'); diff --git a/core/tests/Drupal/Tests/Component/Scaffold/Integration/AppendOpTest.php b/core/tests/Drupal/Tests/Component/Scaffold/Integration/AppendOpTest.php index 75b8be42d4..e381cd59ce 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/Integration/AppendOpTest.php +++ b/core/tests/Drupal/Tests/Component/Scaffold/Integration/AppendOpTest.php @@ -20,7 +20,7 @@ class AppendOpTest extends TestCase { public function testProcess() { $fixtures = new Fixtures(); $destination = $fixtures->destinationPath('[web-root]/robots.txt'); - $options = ScaffoldOptions::defaultOptions(); + $options = ScaffoldOptions::create([]); // Assert that there is no target file before we run our test. $this->assertFileNotExists($destination->fullPath()); diff --git a/core/tests/Drupal/Tests/Component/Scaffold/Integration/OperationCollectionTest.php b/core/tests/Drupal/Tests/Component/Scaffold/Integration/OperationCollectionTest.php index e4b8b23f71..2a690a0e6f 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/Integration/OperationCollectionTest.php +++ b/core/tests/Drupal/Tests/Component/Scaffold/Integration/OperationCollectionTest.php @@ -20,7 +20,7 @@ class OperationCollectionTest extends TestCase { /** * @covers ::collateScaffoldFiles */ - public function testCoalateScaffoldFiles() { + public function testCollateScaffoldFiles() { $fixtures = new Fixtures(); $locationReplacements = $fixtures->getLocationReplacements(); $file_mappings = [ @@ -41,9 +41,11 @@ public function testCoalateScaffoldFiles() { $sut = new OperationCollection($fixtures->io()); // Test the system under test. list($scaffold_list, $resolved_file_mappings) = $this->callProtected($sut, 'collateScaffoldFiles', [$file_mappings, $locationReplacements]); - // Confirm that the keys of the output are the same as the keys of the input. + // Confirm that the keys of the output are the same as the keys of the + // input. $this->assertEquals(array_keys($file_mappings), array_keys($resolved_file_mappings)); - // Also assert that we have the right ScaffoldFileInfo objects in the destination. + // Also assert that we have the right ScaffoldFileInfo objects in the + // destination. $this->assertResolvedToSameOp('fixtures/drupal-assets-fixture', '[web-root]/index.php', $file_mappings, $scaffold_list, $resolved_file_mappings); $this->assertResolvedToSameOp('fixtures/drupal-profile', '[web-root]/sites/default/default.services.yml', $file_mappings, $scaffold_list, $resolved_file_mappings); $this->assertResolvedToSameOp('fixtures/drupal-drupal', '[web-root]/robots.txt', $file_mappings, $scaffold_list, $resolved_file_mappings); @@ -53,16 +55,27 @@ public function testCoalateScaffoldFiles() { } /** - * Check to see if a given file was not overridden. + * Checks to see if a given file was not overridden. * * The package name in the scaffold list for the provided destination should * match the package name from the specified project. + * + * @param string $project + * The project to check. + * @param string $dest + * The destination to check. + * @param array $file_mappings + * The test file mappings keyed by project and destination. + * @param \Drupal\Component\Scaffold\ScaffoldFileInfo[] $scaffold_list + * The list of scaffolded files keyed by destination. + * @param array $resolved_file_mappings + * The list of resolved file mappings keyed by project and destination. */ - protected function assertResolvedToSameOp($project, $dest, $file_mappings, $scaffold_list, $resolved_file_mappings) { + protected function assertResolvedToSameOp($project, $dest, array $file_mappings, array $scaffold_list, array $resolved_file_mappings) { $resolved_file_info = $resolved_file_mappings[$project][$dest]; $this->assertEquals(get_class($resolved_file_info), ScaffoldFileInfo::class); $resolved_scaffold_op = $resolved_file_info->op(); - // If this is an append op then it will be overridden by a preprocess op + // If this is an append op then it will be part of a conjunction op. $expected = get_class($file_mappings[$project][$dest]); if ($expected == AppendOp::class) { $this->assertEquals(ConjunctionOp::class, get_class($resolved_scaffold_op)); @@ -75,12 +88,21 @@ protected function assertResolvedToSameOp($project, $dest, $file_mappings, $scaf } /** - * Check if a given file was overridden. + * Checks if a given file was overridden. * * Assert that the file in the scaffold list at the specified destination * comes from a different package than the one in the file info. + * + * @param string $project + * The project to check. + * @param string $dest + * The destination to check. + * @param \Drupal\Component\Scaffold\ScaffoldFileInfo[] $scaffold_list + * The list of scaffolded files keyed by destination. + * @param array $resolved_file_mappings + * The list of resolved file mappings keyed by project and destination. */ - protected function assertOverridden($project, $dest, $scaffold_list, $resolved_file_mappings) { + protected function assertOverridden($project, $dest, array $scaffold_list, array $resolved_file_mappings) { $resolved_file_info = $resolved_file_mappings[$project][$dest]; $this->assertEquals(get_class($resolved_file_info), ScaffoldFileInfo::class); $this->assertNotEquals($project, $scaffold_list[$dest]->packageName()); @@ -92,14 +114,14 @@ protected function assertOverridden($project, $dest, $scaffold_list, $resolved_f * @param mixed $obj * The object to inspect. * @param string $methodName - * The name of the method to call + * The name of the method to call. * @param array $args - * The arguments to pass to the protected method + * The arguments to pass to the protected method. * * @return mixed - * The return value from the protected methos. + * The return value from the protected method. */ - protected function callProtected($obj, $methodName, $args = []) { + protected function callProtected($obj, $methodName, array $args = []) { $r = new \ReflectionMethod($obj, $methodName); $r->setAccessible(TRUE); return $r->invokeArgs($obj, $args); diff --git a/core/tests/Drupal/Tests/Component/Scaffold/Integration/ReplaceOpTest.php b/core/tests/Drupal/Tests/Component/Scaffold/Integration/ReplaceOpTest.php index ec568fc8da..a45d25635e 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/Integration/ReplaceOpTest.php +++ b/core/tests/Drupal/Tests/Component/Scaffold/Integration/ReplaceOpTest.php @@ -21,7 +21,7 @@ public function testProcess() { $fixtures = new Fixtures(); $destination = $fixtures->destinationPath('[web-root]/robots.txt'); $source = $fixtures->sourcePath('drupal-assets-fixture', 'robots.txt'); - $options = ScaffoldOptions::defaultOptions(); + $options = ScaffoldOptions::create([]); $sut = new ReplaceOp($source, TRUE); // Assert that there is no target file before we run our test. $this->assertFileNotExists($destination->fullPath()); diff --git a/core/tests/Drupal/Tests/Component/Scaffold/Integration/SkipOpTest.php b/core/tests/Drupal/Tests/Component/Scaffold/Integration/SkipOpTest.php index fb600bd935..27e358e6bf 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/Integration/SkipOpTest.php +++ b/core/tests/Drupal/Tests/Component/Scaffold/Integration/SkipOpTest.php @@ -20,8 +20,7 @@ class SkipOpTest extends TestCase { public function testProcess() { $fixtures = new Fixtures(); $destination = $fixtures->destinationPath('[web-root]/robots.txt'); - $source = $fixtures->sourcePath('drupal-assets-fixture', 'robots.txt'); - $options = ScaffoldOptions::defaultOptions(); + $options = ScaffoldOptions::create([]); $sut = new SkipOp(); // Assert that there is no target file before we run our test. $this->assertFileNotExists($destination->fullPath()); diff --git a/core/tests/Drupal/Tests/Component/Scaffold/fixtures/README.md b/core/tests/Drupal/Tests/Component/Scaffold/fixtures/README.md index e103ffba5c..0c8ab5bf80 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/fixtures/README.md +++ b/core/tests/Drupal/Tests/Component/Scaffold/fixtures/README.md @@ -1,8 +1,12 @@ # Fixtures README -These fixtures are automatically copied to a temporary directory during test runs. After the test run, the fixtures are automatically deleted. +These fixtures are automatically copied to a temporary directory during test +runs. After the test run, the fixtures are automatically deleted. -Set the SCAFFOLD_FIXTURE_DIR environment variable to place the fixtures in a specific location rather than a temporary directory. If this is done, then the fixtures will not be deleted after the test run. This is useful for ad-hoc testing. +Set the SCAFFOLD_FIXTURE_DIR environment variable to place the fixtures in a +specific location rather than a temporary directory. If this is done, then the +fixtures will not be deleted after the test run. This is useful for ad-hoc +testing. Example: diff --git a/core/tests/Drupal/Tests/Component/Scaffold/fixtures/scaffold-override-fixture/assets/override-settings.php b/core/tests/Drupal/Tests/Component/Scaffold/fixtures/scaffold-override-fixture/assets/override-settings.php index f37a684279..064ed7e3f5 100644 --- a/core/tests/Drupal/Tests/Component/Scaffold/fixtures/scaffold-override-fixture/assets/override-settings.php +++ b/core/tests/Drupal/Tests/Component/Scaffold/fixtures/scaffold-override-fixture/assets/override-settings.php @@ -2,5 +2,5 @@ /** * @file - * This is a settings.php fixture file scaffolded from the scaffold-override-fixture. + * A settings.php fixture file scaffolded from the scaffold-override-fixture. */