/[drupal]/contributions/modules/simpletest_automator/simpletest_automator.export.inc
ViewVC logotype

Diff of /contributions/modules/simpletest_automator/simpletest_automator.export.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.3, Tue Aug 19 20:10:45 2008 UTC revision 1.4, Tue Aug 19 22:19:47 2008 UTC
# Line 3  Line 3 
3  function simpletest_automator_export_test($simpletest_automator) {  function simpletest_automator_export_test($simpletest_automator) {
4    drupal_set_header('Content-Type: application/x-httpd-php');    drupal_set_header('Content-Type: application/x-httpd-php');
5    drupal_set_header('Content-Disposition: attachment; filename="'. $simpletest_automator->file .'"');    drupal_set_header('Content-Disposition: attachment; filename="'. $simpletest_automator->file .'"');
6    $export[] = array(    $code[] = "<?php\n\n";
7      'value' => '<?php',    $code[] = "/**";
8      'break' => TRUE,    $code[] = " * ". $simpletest_automator->description;
9    );    $code[] = " */";
10    $export[] = '/**';    $code[] = "class ". $simpletest_automator->class ." extends DrupalWebTestCase {\n";
11    $export[] = ' * ' . $simpletest_automator->description;    $code = array_merge($code, _simpletest_automator_export_initial($simpletest_automator));
12    $export[] = ' */';    $code = array_merge($code, _simpletest_automator_export_test($simpletest_automator));
13    $export[] = array(    $code[] = "}";
14      'value' => 'class ' . $simpletest_automator->class . ' extends DrupalWebTestCase {',    exit(implode("\n", $code));
     'indent' => 2,  
     'break' => TRUE,  
   );  
   $export = array_merge($export, _simpletest_automator_export_initial($simpletest_automator));  
   $export = array_merge($export, _simpletest_automator_export_test($simpletest_automator));  
   $export[] = array(  
     'value' => '}',  
     'indent' => -2,  
   );  
   drupal_alter('simpletest_automator', $export);  
   $code = _simpletest_automator_export_to_code($export);  
   exit($code);  
15  }  }
16    
17  function _simpletest_automator_export_initial($simpletest_automator) {  function _simpletest_automator_export_initial($simpletest_automator) {
18    $export = array();    $code[] = "  function getInfo() {";
19    $export[] = array(    $code[] = "    return array(";
20      'value' => 'function getInfo() {',    $code[] = "      'name' => ". var_export($simpletest_automator->name, TRUE) .",";
21      'indent' => 2,    $code[] = "      'description' => ". var_export($simpletest_automator->description, TRUE) .",";
22    );    $code[] = "      'group' => '". var_export($simpletest_automator->test_group, TRUE) ."',";
23    $export[] = array(    $code[] = "    );";
24      'value' => 'return array(',    $code[] = "  }";
25      'indent' => 2,    $code[] = "";
26    );    $code[] = "  function setUp() {";
27    $export[] = array(    $code[] = "    return parent::setUp(array('". implode("', '", $simpletest_automator->modules) ."'));";
28      'value' => "'name' => %s,",    $code[] = "  }";
29      'arguments' => array($simpletest_automator->name),    $code[] = "";
30    );    return $code;
   $export[] = array(  
     'value' => "'description' => %s,",  
     'arguments' => array($simpletest_automator->description),  
   );  
   $export[] = array(  
     'value' => "'group' => %s,",  
     'arguments' => array($simpletest_automator->test_group),  
   );  
   $export[] = array(  
     'value' => ');',  
     'indent' => -2,  
   );  
   $export[] = array(  
     'value' => '}',  
     'indent' => -2,  
     'break' => TRUE,  
   );  
   $export[] = array(  
     'value' => 'function setUp() {',  
     'indent' => 2,  
   );  
   $export[] = array(  
     'value' =>  'return parent::setUp(' . implode(', ', array_fill(0, count($simpletest_automator->modules), '%s')) . ');',  
     'arguments' => $simpletest_automator->modules,  
   );  
   $export[] = array(  
     'value' => '}',  
     'indent' => -2,  
     'break' => TRUE,  
   );  
   return $export;  
31  }  }
32    
33  function _simpletest_automator_export_test($simpletest_automator) {  function _simpletest_automator_export_test($simpletest_automator) {
34    $export = array();    $code[] = "  function ". $simpletest_automator->method ."() {";
35    $export[] = array(    $code[] = "    \$this->test_user = \$this->drupalCreateUser(array('". implode("', '", $simpletest_automator->permissions) ."'));";
36      'value' => 'function ' . $simpletest_automator->method . '() {',    $code[] = "    \$this->drupalLogin(\$this->test_user);";
     'indent' => 2,  
   );  
   $export[] = array(  
     'value' => '$user = $this->drupalCreateUser(array(' . implode(', ', array_fill(0, count($simpletest_automator->permissions), '%s')) . '));',  
     'arguments' => $simpletest_automator->permissions,  
   );  
   $export[] = '$this->drupalLogin($user)';  
37    foreach ($simpletest_automator->actions as $action) {    foreach ($simpletest_automator->actions as $action) {
38      $export = array_merge($export, module_invoke_all('simpletest_automator_actions', $action, 'export'));      $export = module_invoke_all('simpletest_automator_actions', $action, 'export');
39        foreach ($export as $line) {
40          $line_of_code = "    ";
41          if ($line['type'] == 'comment') {
42            $line_of_code .= "// ";
43          }
44          if (isset($line['arguments'])) {
45            $args = array($line['value']);
46            foreach ($line['arguments'] as $arg) {
47              $args[] = var_export($arg, TRUE);
48            }
49            $code[] = $line_of_code . call_user_func_array('sprintf', $args);
50          }
51          else {
52            $code[] = $line_of_code . $line['value'];
53          }
54        }
55    }    }
56    $export[] = array(    $code[] = "  }";
57      'value' => '}',    return $code;
     'indent' => -2,  
   );  
   return $export;  
58  }  }
59    
60  function _simpletest_automator_export_php($simpletest_automator) {  function simpletest_automator_export_php(&$form_state, $simpletest_automator) {
61    unset($simpletest_automator->said);    unset($simpletest_automator->said);
62    unset($simpletest_automator->db_prefix);    unset($simpletest_automator->db_prefix);
63    foreach ($simpletest_automator->actions as $index => $action) {    foreach ($simpletest_automator->actions as $index => $action) {
64      unset($simpletest_automator->actions[$index]->aid);      unset($simpletest_automator->actions[$index]->aid);
65      unset($simpletest_automator->actions[$index]->said);      unset($simpletest_automator->actions[$index]->said);
66    }    }
67    return serialize($simpletest_automator);    $actions = $simpletest_automator->actions;
68      unset($simpletest_automator->actions);
69      $output = array();
70      _simpletest_automator_export_php($simpletest_automator, '$simpletest_automator', $output);
71      $output[] = "";
72      foreach ($actions as $action) {
73        _simpletest_automator_export_php($action, '$action', $output);
74        $output[] = "\$simpletest_automator->actions[] = \$action;";
75        $output[] = "";
76      }
77      $form = array();
78      $form['code'] = array(
79        '#type' => 'textarea',
80        '#title' => t('Code'),
81        '#description' => t('Copy this code and paste it where you want.'),
82        '#cols' => 60,
83        '#rows' => 20,
84        '#default_value' => implode("\n", $output),
85        '#attributes' => array('readonly' => 'readonly'),
86      );
87    
88      return $form;
89  }  }
90    
91  /**  function _simpletest_automator_export_php($element, $variable, &$output) {
92   * Helper function for simpletest_automator_export_test(). Turns a simpletest    $output[] = "$variable = new stdClass;";
93   * automator export array into actual code.    foreach ($element as $key => $value) {
94   *      if (is_array($value)) {
95   * @param $export        $elements = array();
96   *   The export array to convert.        foreach ($value as $value_element) {
97   * @return          $elements[] = (count($value) > 5 ? "  " : "") . var_export($value_element, TRUE) . (count($value) > 5 ? "," : "");
  *   The actual code.  
  */  
 function _simpletest_automator_export_to_code($export) {  
   $code = '';  
   $indent = 0;  
   foreach ($export as $line) {  
     if (is_string($line)) {  
       $line = array('value' => $line);  
     }  
     if (isset($line['indent']) && $line['indent'] < 0) {  
       $indent += $line['indent'];  
     }  
     for ($i = 0; $i < $indent; $i++) {  
       $code .= ' ';  
     }  
     if (isset($line['indent']) && $line['indent'] > 0) {  
       $indent += $line['indent'];  
     }  
     if (isset($line['type'])) {  
       switch ($line['type']) {  
         case 'comment':  
           $code .= '// ';  
           break;  
           // @TODO - comment block?  
98        }        }
99      }        $temp = "$variable"."->$key = array(";
100      if (isset($line['arguments'])) {        if (count($elements) < 6) {
101        foreach ($line['arguments'] as $key => $arg) {          $output[] = $temp . implode(', ', $elements) .");";
102          $line['arguments'][$key] = var_export($arg, TRUE);        }
103          else {
104            $output[] = $temp;
105            $output = array_merge($output, $elements);
106            $output[] = ");";
107        }        }
       $code .= call_user_func_array('sprintf', array_merge(array($line['value']), $line['arguments']));  
108      }      }
109      else {      else {
110        $code .= $line['value'];        $output[] = "$variable"."->$key = ". var_export($value, TRUE) .";";
     }  
     $code .= "\n";  
     if (isset($line['break']) && $line['break']) {  
       $code .= "\n";  
111      }      }
112    }    }
   return rtrim($code);  
113  }  }
114    

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

  ViewVC Help
Powered by ViewVC 1.1.2