Version 1
[yaffs-website] / vendor / drush / drush / tests / unit.drush.inc
1 <?php
2
3 /**
4  * @file
5  *   Commands which are useful for unit tests.
6  */
7
8 /**
9  * Implementation of hook_drush_command().
10  */
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,
15   );
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',
20   );
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',
25   );
26   $items['unit-batch'] = array(
27     'description' => 'Run a batch process.',
28     'bootstrap' => DRUSH_BOOTSTRAP_MAX,
29   );
30   $items['unit-return-options'] = array(
31     'description' => 'Return options as function result.',
32     'bootstrap' => DRUSH_BOOTSTRAP_NONE,
33   );
34   $items['unit-return-argv'] = array(
35     'description' => 'Return original argv as function result.',
36     'bootstrap' => DRUSH_BOOTSTRAP_NONE,
37   );
38   $items['missing-callback'] = array(
39     'description' => 'Command with no callback function, to test error reporting.',
40     'bootstrap' => DRUSH_BOOTSTRAP_NONE,
41   );
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'),
46   );
47   return $items;
48 }
49
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__);}
60
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__);
66   }
67 }
68
69 function drush_unit_post_unit_invoke() {
70   // Record that this hook was called.
71   unit_invoke_log(__FUNCTION__);
72
73   // Make sure we enter into rollback.
74   drush_set_error('');
75 }
76
77 /**
78  * The final invoke hook. Emit the call history.
79  * Cannot use 'exit' as it does not fire in rollback scenario.
80  */
81 function drush_unit_invoke_validate_rollback() {
82   unit_invoke_log(__FUNCTION__);
83   print json_encode(unit_invoke_log());
84 }
85
86 function unit_invoke_log($function = NULL) {
87   static $called = array();
88   if ($function) {
89     $called[] = $function;
90   }
91   else {
92     return $called;
93   }
94 }
95
96 /**
97  * Command callback.
98  */
99 function drush_unit_batch() {
100   // Reduce php memory/time limits to test backend respawn.
101   // TODO.
102
103   $batch = array(
104     'operations' => array(
105        array('_drush_unit_batch_operation', array()),
106     ),
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().
111   );
112   batch_set($batch);
113   drush_backend_batch_process();
114
115   // Print the batch output.
116   drush_backend_output();
117 }
118
119 function _drush_unit_batch_operation(&$context) {
120   $context['message'] = "!!! ArrayObject does its job.";
121
122   for ($i = 0; $i < 5; $i++) {
123     drush_print("Iteration $i");
124   }
125   $context['finished'] = 1;
126 }
127
128 function _drush_unit_batch_finished() {
129   // Restore php limits.
130   // TODO.
131 }
132
133 // Return all of the option values passed in to this routine, minus the
134 // global options.
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]);
139   }
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']);
144   }
145   return $all_option_values;
146 }
147
148 // Return all of the original arguments passed to this script
149 function drush_unit_return_argv() {
150   return drush_get_context('argv');
151 }