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

Commands which are useful for unit tests.

Functions

File

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