unit.drush.inc

  1. 8.0.x tests/unit.drush.inc
  2. 6.x tests/unit.drush.inc
  3. 7.x tests/unit.drush.inc
  4. 4.x tests/unit.drush.inc
  5. 5.x tests/unit.drush.inc
  6. master tests/unit.drush.inc

Functions

File

tests/unit.drush.inc
View source
  1. <?php
  2. // $Id$
  3. /*
  4. * @file
  5. * Commands which are useful for unit tests.
  6. */
  7. /**
  8. * Implementation of hook_drush_command().
  9. */
  10. function unit_drush_command() {
  11. $items['unit'] = array(
  12. 'description' => 'No-op command, used to test completion for commands that start the same as other commands.',
  13. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  14. );
  15. $items['unit-eval'] = array(
  16. 'description' => 'Works like php-eval. Used for testing $command_specific context.',
  17. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  18. 'callback' => 'drush_core_php_eval', // Note - no invoke hooks.
  19. );
  20. $items['unit-invoke'] = array(
  21. 'description' => 'Return an array indicating which invoke hooks got called.',
  22. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  23. );
  24. $items['unit-batch'] = array(
  25. 'description' => 'Run a batch process.',
  26. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  27. );
  28. $items['unit-return-options'] = array(
  29. 'description' => 'Return options as function result.',
  30. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  31. );
  32. $items['unit-return-argv'] = array(
  33. 'description' => 'Return original argv as function result.',
  34. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  35. );
  36. $items['missing-callback'] = array(
  37. 'description' => 'Command with no callback function, to test error reporting.',
  38. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  39. );
  40. $items['unit-drush-dependency'] = array(
  41. 'description' => 'Command depending on an unknown commandfile.',
  42. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  43. 'drush dependencies' => array('unknown-commandfile'),
  44. );
  45. return $items;
  46. }
  47. // Implement each invoke hook with the same single line of code.
  48. // That line records that the hook was called.
  49. function drush_unit_invoke_init() {unit_invoke_log(__FUNCTION__);}
  50. function drush_unit_invoke_validate() {unit_invoke_log(__FUNCTION__);}
  51. function drush_unit_pre_unit_invoke() {unit_invoke_log(__FUNCTION__);}
  52. function drush_unit_invoke() {unit_invoke_log(__FUNCTION__);}
  53. function drush_unit_pre_unit_invoke_rollback() {unit_invoke_log(__FUNCTION__);}
  54. function drush_unit_post_unit_invoke_rollback() {unit_invoke_log(__FUNCTION__);}
  55. // Record that hook_drush_init() fired.
  56. function unit_drush_init() {
  57. $command = drush_get_command();
  58. if ($command['command'] == 'unit-invoke') {
  59. unit_invoke_log(__FUNCTION__);
  60. }
  61. }
  62. function drush_unit_post_unit_invoke() {
  63. // Record that this hook was called.
  64. unit_invoke_log(__FUNCTION__);
  65. // Make sure we enter into rollback.
  66. drush_set_error('');
  67. }
  68. /*
  69. * The final invoke hook. Emit the call history.
  70. * Cannot use 'exit' as it does not fire in rollback scenario.
  71. */
  72. function drush_unit_invoke_validate_rollback() {
  73. unit_invoke_log(__FUNCTION__);
  74. print json_encode(unit_invoke_log());
  75. }
  76. function unit_invoke_log($function = NULL) {
  77. static $called = array();
  78. if ($function) {
  79. $called[] = $function;
  80. }
  81. else {
  82. return $called;
  83. }
  84. }
  85. /**
  86. * Command callback.
  87. */
  88. function drush_unit_batch() {
  89. // Reduce php memory/time limits to test backend respawn.
  90. // TODO.
  91. $batch = array(
  92. 'operations' => array(
  93. array('_drush_unit_batch_operation', array()),
  94. ),
  95. 'finished' => '_drush_unit_batch_finished',
  96. // 'file' => Doesn't work for us. Drupal 7 enforces this path
  97. // to be relative to DRUPAL_ROOT.
  98. // @see _batch_process().
  99. );
  100. batch_set($batch);
  101. drush_backend_batch_process();
  102. // Print the batch output.
  103. drush_backend_output();
  104. }
  105. function _drush_unit_batch_operation(&$context) {
  106. $context['message'] = "!!! ArrayObject does its job.";
  107. for ($i = 0; $i < 5; $i++) {
  108. drush_print("Iteration $i");
  109. }
  110. $context['finished'] = 1;
  111. }
  112. function _drush_unit_batch_finished() {
  113. // Restore php limits.
  114. // TODO.
  115. }
  116. // Return all of the option values passed in to this routine, minus the
  117. // global options.
  118. function drush_unit_return_options() {
  119. $all_option_values = array_merge(drush_get_context('cli'), drush_get_context('stdin'));
  120. foreach (drush_get_global_options() as $key => $info) {
  121. unset($all_option_values[$key]);
  122. }
  123. if (isset($all_option_values['log-message'])) {
  124. drush_log($all_option_values['log-message'], 'warning');
  125. drush_log("done", 'warning');
  126. unset($all_option_values['log-message']);
  127. }
  128. return $all_option_values;
  129. }
  130. // Return all of the original arguments passed to this script
  131. function drush_unit_return_argv() {
  132. return drush_get_context('argv');
  133. }