/[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.4, Tue Aug 19 22:19:47 2008 UTC revision 1.5, Thu Aug 21 02:47:57 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2    // $Id$
3    
4    /**
5     * Menu callback: export a SimpleTest automator instance to the code form.
6     */
7  function simpletest_automator_export_test($simpletest_automator) {  function simpletest_automator_export_test($simpletest_automator) {
8      // Send out the proper headers.
9    drupal_set_header('Content-Type: application/x-httpd-php');    drupal_set_header('Content-Type: application/x-httpd-php');
10    drupal_set_header('Content-Disposition: attachment; filename="'. $simpletest_automator->file .'"');    drupal_set_header('Content-Disposition: attachment; filename="'. $simpletest_automator->file .'"');
11    $code[] = "<?php\n\n";    $code[] = '<?php';
12    $code[] = "/**";    $code[] = '// $I' . 'd$';
13    $code[] = " * ". $simpletest_automator->description;    $code[] = '';
14    $code[] = " */";    $code[] = '/**';
15    $code[] = "class ". $simpletest_automator->class ." extends DrupalWebTestCase {\n";    $code[] = ' * ' . $simpletest_automator->description;
16      $code[] = ' */';
17      $code[] = 'class ' . $simpletest_automator->class . ' extends DrupalWebTestCase {';
18    $code = array_merge($code, _simpletest_automator_export_initial($simpletest_automator));    $code = array_merge($code, _simpletest_automator_export_initial($simpletest_automator));
19    $code = array_merge($code, _simpletest_automator_export_test($simpletest_automator));    $code = array_merge($code, _simpletest_automator_export_test($simpletest_automator));
20    $code[] = "}";    $code[] = '}';
21      drupal_alter('simpletest_automator', $code);
22      $code = _simpletest_automator_to_code($code);
23    exit(implode("\n", $code));    exit(implode("\n", $code));
24  }  }
25    
26    /**
27     * Generate the initial SimpleTest automator code: the setUp() and getInfo()
28     * implementations. Helper function for simpletest_automator_export_test().
29     *
30     * @param $simpletest_automator
31     *   The SimpleTest automator to export.
32     * @return
33     *   An array of lines of code corresponding to the SimpleTest's getInfo() and
34     *   setUp() functions.
35     */
36  function _simpletest_automator_export_initial($simpletest_automator) {  function _simpletest_automator_export_initial($simpletest_automator) {
37    $code[] = "  function getInfo() {";    $code[] = '  function getInfo() {';
38    $code[] = "    return array(";    $code[] = '    return array(';
39    $code[] = "      'name' => ". var_export($simpletest_automator->name, TRUE) .",";    $code[] = array(
40    $code[] = "      'description' => ". var_export($simpletest_automator->description, TRUE) .",";      'value' => "      'name' => %s,",
41    $code[] = "      'group' => '". var_export($simpletest_automator->test_group, TRUE) ."',";      'arguments' => array($simpletest_automator->name),
42    $code[] = "    );";    );
43    $code[] = "  }";    $code[] = array(
44    $code[] = "";      'value' => "      'description' => %s,",
45    $code[] = "  function setUp() {";      'arguments' => array($simpletest_automator->description),
46    $code[] = "    return parent::setUp(array('". implode("', '", $simpletest_automator->modules) ."'));";    );
47    $code[] = "  }";    $code[] = array(
48    $code[] = "";      'value' => "      'group' => %s,",
49        'arguments' => array($simpletest_automator->test_group),
50      );
51      $code[] = '    );';
52      $code[] = '  }';
53      $code[] = '';
54      $code[] = '  function setUp() {';
55      $code[] = array(
56        'value' => '    return parent::setUp(' . implode(', ', array_fill(0, count($simpletest_automator->modules), '%s')) . ');',
57        'arguments' => $simpletest_automator->modules,
58      );
59      $code[] = '  }';
60      $code[] = '';
61    return $code;    return $code;
62  }  }
63    
64    /**
65     * Export the test-specific actions into the SimpleTest test method.
66     *
67     * @param $simpletest_automator
68     *   The SimpleTest automator to export.
69     * @return
70     *   An array of lines of code corresponding to the SimpleTest's test*()
71     *   method.
72     * @todo - More than one test method?
73     */
74  function _simpletest_automator_export_test($simpletest_automator) {  function _simpletest_automator_export_test($simpletest_automator) {
75    $code[] = "  function ". $simpletest_automator->method ."() {";    $code[] = '  function '. $simpletest_automator->method .'() {';
76    $code[] = "    \$this->test_user = \$this->drupalCreateUser(array('". implode("', '", $simpletest_automator->permissions) ."'));";    $code[] = array(
77    $code[] = "    \$this->drupalLogin(\$this->test_user);";      'value' => '    $this->test_user = $this->drupalCreateUser(array(' . implode(', ', array_fill(0, count($simpletest_automator->permissions), '%s')) .'));',
78        'arguments' => $simpletest_automator->permissions,
79      );
80      $code[] = '    $this->drupalLogin($this->test_user);';
81    foreach ($simpletest_automator->actions as $action) {    foreach ($simpletest_automator->actions as $action) {
82      $export = module_invoke_all('simpletest_automator_actions', $action, 'export');      $action_code = module_invoke_all('simpletest_automator_actions', $action, 'export');
83      foreach ($export as $line) {      foreach ($action_code as $key => $line) {
84        $line_of_code = "    ";        if (is_array($line)) {
85        if ($line['type'] == 'comment') {          $action_code[$key]['value'] = '    ' . $line['value'];
         $line_of_code .= "// ";  
       }  
       if (isset($line['arguments'])) {  
         $args = array($line['value']);  
         foreach ($line['arguments'] as $arg) {  
           $args[] = var_export($arg, TRUE);  
         }  
         $code[] = $line_of_code . call_user_func_array('sprintf', $args);  
86        }        }
87        else {        else {
88          $code[] = $line_of_code . $line['value'];          $action_code[$key] = '    ' . $line;
89          }
90        }
91        $code = array_merge($code, $action_code);
92      }
93      $code[] = '  }';
94      return $code;
95    }
96    
97    /**
98     * Convert an export to it's simple code array format.
99     *
100     * @param $export
101     *   The complex export array, to be translated into raw code.
102     * @return
103     *   A linear array of raw lines of code.
104     */
105    function _simpletest_automator_to_code($export) {
106      foreach ($export as $line) {
107        if (is_string($line)) {
108          $line = array('value' => $line);
109        }
110        if (isset($line['arguments'])) {
111          $args = array($line['value']);
112          foreach ($line['arguments'] as $arg) {
113            $args[] = var_export($arg, TRUE);
114        }        }
115          $code[] = call_user_func_array('sprintf', $args);
116        }
117        else {
118          $code[] = $line['value'];
119      }      }
120    }    }
   $code[] = "  }";  
121    return $code;    return $code;
122  }  }
123    

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

  ViewVC Help
Powered by ViewVC 1.1.2