5 * Commands which are useful for unit tests.
9 * Implementation of hook_drush_command().
11 function unit_drush_command() {
12 $items['unit'] = array(
13 'description' => 'No-op command, used to test completion for commands that start the same as other commands.',
14 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
16 $items['unit-eval'] = array(
17 'description' => 'Works like php-eval. Used for testing $command_specific context.',
18 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
19 'callback' => 'drush_core_php_eval',
21 $items['unit-invoke'] = array(
22 'description' => 'Return an array indicating which invoke hooks got called.',
23 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
24 'callback' => 'drush_unit_invoke_primary',
26 $items['unit-batch'] = array(
27 'description' => 'Run a batch process.',
28 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
30 $items['unit-return-options'] = array(
31 'description' => 'Return options as function result.',
32 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
34 $items['unit-return-argv'] = array(
35 'description' => 'Return original argv as function result.',
36 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
38 $items['missing-callback'] = array(
39 'description' => 'Command with no callback function, to test error reporting.',
40 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
42 $items['unit-drush-dependency'] = array(
43 'description' => 'Command depending on an unknown commandfile.',
44 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
45 'drush dependencies' => array('unknown-commandfile'),
50 // Implement each invoke hook with the same single line of code.
51 // That line records that the hook was called.
52 function drush_unit_invoke_init() {unit_invoke_log(__FUNCTION__);}
53 function drush_unit_invoke_validate() {unit_invoke_log(__FUNCTION__);}
54 function drush_unit_pre_unit_invoke() {unit_invoke_log(__FUNCTION__);}
55 // Primary callback is not invoked when command specifies a 'callback'.
56 // function drush_unit_invoke() {unit_invoke_log(__FUNCTION__);}
57 function drush_unit_invoke_primary() {unit_invoke_log(__FUNCTION__);}
58 function drush_unit_pre_unit_invoke_rollback() {unit_invoke_log(__FUNCTION__);}
59 function drush_unit_post_unit_invoke_rollback() {unit_invoke_log(__FUNCTION__);}
61 // Record that hook_drush_init() fired.
62 function unit_drush_init() {
63 $command = drush_get_command();
64 if ($command['command'] == 'unit-invoke') {
65 unit_invoke_log(__FUNCTION__);
69 function drush_unit_post_unit_invoke() {
70 // Record that this hook was called.
71 unit_invoke_log(__FUNCTION__);
73 // Make sure we enter into rollback.
78 * The final invoke hook. Emit the call history.
79 * Cannot use 'exit' as it does not fire in rollback scenario.
81 function drush_unit_invoke_validate_rollback() {
82 unit_invoke_log(__FUNCTION__);
83 print json_encode(unit_invoke_log());
86 function unit_invoke_log($function = NULL) {
87 static $called = array();
89 $called[] = $function;
99 function drush_unit_batch() {
100 // Reduce php memory/time limits to test backend respawn.
104 'operations' => array(
105 array('_drush_unit_batch_operation', array()),
107 'finished' => '_drush_unit_batch_finished',
108 // 'file' => Doesn't work for us. Drupal 7 enforces this path
109 // to be relative to DRUPAL_ROOT.
110 // @see _batch_process().
113 drush_backend_batch_process();
115 // Print the batch output.
116 drush_backend_output();
119 function _drush_unit_batch_operation(&$context) {
120 $context['message'] = "!!! ArrayObject does its job.";
122 for ($i = 0; $i < 5; $i++) {
123 drush_print("Iteration $i");
125 $context['finished'] = 1;
128 function _drush_unit_batch_finished() {
129 // Restore php limits.
133 // Return all of the option values passed in to this routine, minus the
135 function drush_unit_return_options() {
136 $all_option_values = array_merge(drush_get_context('cli'), drush_get_context('stdin'));
137 foreach (drush_get_global_options() as $key => $info) {
138 unset($all_option_values[$key]);
140 if (isset($all_option_values['log-message'])) {
141 drush_log($all_option_values['log-message'], 'warning');
142 drush_log("done", 'warning');
143 unset($all_option_values['log-message']);
145 return $all_option_values;
148 // Return all of the original arguments passed to this script
149 function drush_unit_return_argv() {
150 return drush_get_context('argv');