2880a6ad1b86f200a8d26326dfc5fa211232572d
[yaffs-website] / vendor / drush / drush / tests / Commands / TestFixtureCommands.php
1 <?php
2
3 /**
4  * @file
5  *   Commands which are useful for unit tests.
6  */
7 namespace Drush\Commands;
8
9 use Drupal\Core\DrupalKernel;
10 use Drupal\Core\Site\Settings;
11 use Drush\Boot\AutoloaderAwareTrait;
12 use Drush\Drush;
13
14 class TestFixtureCommands
15 {
16
17     use AutoloaderAwareTrait;
18
19   // Obsolete:
20   //   unit-invoke
21   //   missing-callback
22   //
23   // Future:
24   //   unit-drush-dependency: Command depending on an unknown commandfile
25
26   /**
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.
29    *
30    * @command unit
31    */
32     public function unit()
33     {
34     }
35
36   /**
37    * Works like php-eval. Used for testing $command_specific context.
38    *
39    * @command unit-eval
40    * @bootstrap max
41    */
42     public function drushUnitEval($code)
43     {
44         return eval($code . ';');
45     }
46
47   /**
48    * Run a batch process.
49    *
50    * @command unit-batch
51    * @bootstrap max
52    */
53     public function drushUnitBatch()
54     {
55         // Reduce php memory/time limits to test backend respawn.
56         // TODO.
57
58         $batch = [
59         'operations' => [
60          [[$this, '_drushUnitBatchOperation'], []],
61         ],
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().
66         ];
67         \batch_set($batch);
68         \drush_backend_batch_process();
69
70         // Print the batch output.
71         \drush_backend_output();
72     }
73
74     public function _drushUnitBatchOperation(&$context)
75     {
76         $context['message'] = "!!! ArrayObject does its job.";
77
78         for ($i = 0; $i < 5; $i++) {
79             \drush_print("Iteration $i");
80         }
81         $context['finished'] = 1;
82     }
83
84     public function _drushUnitBatchFinished()
85     {
86         // Restore php limits.
87         // TODO.
88     }
89
90   /**
91    * Return options as function result.
92    * @command unit-return-options
93    */
94     public function drushUnitReturnOptions($arg = '', $options = ['x' => 'y', 'data' => [], 'format' => 'yaml'])
95     {
96         unset($options['format']);
97         return $options;
98     }
99
100   /**
101    * Return original argv as function result.
102    * @command unit-return-argv
103    */
104     public function drushUnitReturnArgv(array $args)
105     {
106         return $args;
107     }
108
109     /**
110      * Clears the dependency injection container.
111      *
112      * Intended for testing cases that require the container to be rebuilt from
113      * scratch.
114      *
115      * @command unit-invalidate-container
116      * @bootstrap site
117      */
118     public function drushUnitInvalidateContainer()
119     {
120         $autoloader = $this->loadDrupalAutoloader(DRUPAL_ROOT);
121         $request = Drush::bootstrap()->getRequest();
122         $sitePath = DrupalKernel::findSitePath($request);
123
124         // Perform early bootstrap. This includes dynamic configuration of PHP,
125         // setting the error and exception handlers etc.
126         DrupalKernel::bootEnvironment();
127
128         // Initialize database connections and apply configuration from
129         // settings.php.
130         Settings::initialize(DRUPAL_ROOT, $sitePath, $autoloader);
131
132         $kernel = new DrupalKernel('prod', $autoloader);
133         $kernel->setSitePath($sitePath);
134
135         // We need to boot the kernel in order to load the service that can
136         // delete the compiled container from the cache backend.
137         $kernel->boot();
138         $kernel->invalidateContainer();
139     }
140
141     /**
142      * Loads the Drupal autoloader and returns the instance.
143      *
144      * @see \Drush\Commands\core\CacheCommands::loadDrupalAutoloader()
145      */
146     protected function loadDrupalAutoloader($drupal_root)
147     {
148         static $autoloader = false;
149
150         $autoloadFilePath = $drupal_root .'/autoload.php';
151         if (!$autoloader && file_exists($autoloadFilePath)) {
152             $autoloader = require $autoloadFilePath;
153         }
154
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();
159         }
160
161         return $autoloader;
162     }
163 }