5 * Commands which are useful for unit tests.
7 namespace Drush\Commands;
9 use Drupal\Core\DrupalKernel;
10 use Drupal\Core\Site\Settings;
11 use Drush\Boot\AutoloaderAwareTrait;
14 class TestFixtureCommands
17 use AutoloaderAwareTrait;
24 // unit-drush-dependency: Command depending on an unknown commandfile
27 * No-op command, used to test completion for commands that start the same as other commands.
28 * We don't do completion in Drush core any longer, but keeping as a placeholder for now.
32 public function unit()
37 * Works like php-eval. Used for testing $command_specific context.
42 public function drushUnitEval($code)
44 return eval($code . ';');
48 * Run a batch process.
53 public function drushUnitBatch()
55 // Reduce php memory/time limits to test backend respawn.
60 [[$this, '_drushUnitBatchOperation'], []],
62 'finished' => [$this, '_drushUnitBatchFinished'],
63 // 'file' => Doesn't work for us. Drupal 7 enforces this path
64 // to be relative to DRUPAL_ROOT.
65 // @see _batch_process().
68 \drush_backend_batch_process();
70 // Print the batch output.
71 \drush_backend_output();
74 public function _drushUnitBatchOperation(&$context)
76 $context['message'] = "!!! ArrayObject does its job.";
78 for ($i = 0; $i < 5; $i++) {
79 \drush_print("Iteration $i");
81 $context['finished'] = 1;
84 public function _drushUnitBatchFinished()
86 // Restore php limits.
91 * Return options as function result.
92 * @command unit-return-options
94 public function drushUnitReturnOptions($arg = '', $options = ['x' => 'y', 'data' => [], 'format' => 'yaml'])
96 unset($options['format']);
101 * Return original argv as function result.
102 * @command unit-return-argv
104 public function drushUnitReturnArgv(array $args)
110 * Clears the dependency injection container.
112 * Intended for testing cases that require the container to be rebuilt from
115 * @command unit-invalidate-container
118 public function drushUnitInvalidateContainer()
120 $autoloader = $this->loadDrupalAutoloader(DRUPAL_ROOT);
121 $request = Drush::bootstrap()->getRequest();
122 $sitePath = DrupalKernel::findSitePath($request);
124 // Perform early bootstrap. This includes dynamic configuration of PHP,
125 // setting the error and exception handlers etc.
126 DrupalKernel::bootEnvironment();
128 // Initialize database connections and apply configuration from
130 Settings::initialize(DRUPAL_ROOT, $sitePath, $autoloader);
132 $kernel = new DrupalKernel('prod', $autoloader);
133 $kernel->setSitePath($sitePath);
135 // We need to boot the kernel in order to load the service that can
136 // delete the compiled container from the cache backend.
138 $kernel->invalidateContainer();
142 * Loads the Drupal autoloader and returns the instance.
144 * @see \Drush\Commands\core\CacheCommands::loadDrupalAutoloader()
146 protected function loadDrupalAutoloader($drupal_root)
148 static $autoloader = false;
150 $autoloadFilePath = $drupal_root .'/autoload.php';
151 if (!$autoloader && file_exists($autoloadFilePath)) {
152 $autoloader = require $autoloadFilePath;
155 if ($autoloader === true) {
156 // The autoloader was already required. Assume that Drush and Drupal share an autoloader per
157 // "Point autoload.php to the proper vendor directory" - https://www.drupal.org/node/2404989
158 $autoloader = $this->autoloader();