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

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

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


Revision 1.8 - (show annotations) (download) (as text)
Sun May 24 01:33:14 2009 UTC (6 months ago) by cwgordon7
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +3 -3 lines
File MIME type: text/x-php
Some obvious updates necessary to keep up to date with API changes in SimpleTest.
1 <?php
2 // $Id: simpletest_automator.export.inc,v 1.7 2008/08/21 03:56:03 cwgordon7 Exp $
3
4 /**
5 * Menu callback: export a SimpleTest automator instance to the code form.
6 */
7 function simpletest_automator_export_test($simpletest_automator) {
8 // Send out the proper headers.
9 drupal_set_header('Content-Type: application/x-httpd-php');
10 drupal_set_header('Content-Disposition: attachment; filename="'. $simpletest_automator->file .'"');
11 $code[] = '<?php';
12 $code[] = '// $I' . 'd$';
13 $code[] = '';
14 $code[] = '/**';
15 $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));
19 $code = array_merge($code, _simpletest_automator_export_test($simpletest_automator));
20 $code[] = '}';
21 drupal_alter('simpletest_automator', $code);
22 $code = _simpletest_automator_to_code($code);
23 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) {
37 $code[] = ' public static function getInfo() {';
38 $code[] = ' return array(';
39 $code[] = array(
40 'value' => " 'name' => %s,",
41 'arguments' => array($simpletest_automator->name),
42 );
43 $code[] = array(
44 'value' => " 'description' => %s,",
45 'arguments' => array($simpletest_automator->description),
46 );
47 $code[] = array(
48 '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' => ' parent::setUp(' . implode(', ', array_fill(0, count($simpletest_automator->modules), '%s')) . ');',
57 'arguments' => $simpletest_automator->modules,
58 );
59 $code[] = ' }';
60 $code[] = '';
61 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) {
75 $code[] = ' function '. $simpletest_automator->method .'() {';
76 $code[] = array(
77 '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) {
82 $action_code = module_invoke_all('simpletest_automator_actions', $action, 'export');
83 foreach ($action_code as $key => $line) {
84 if (is_array($line)) {
85 $action_code[$key]['value'] = ' ' . $line['value'];
86 }
87 else {
88 $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 }
121 return $code;
122 }
123
124 function simpletest_automator_export_php(&$form_state, $simpletest_automator) {
125 unset($simpletest_automator->said);
126 unset($simpletest_automator->db_prefix);
127 foreach ($simpletest_automator->actions as $index => $action) {
128 unset($simpletest_automator->actions[$index]->aid);
129 unset($simpletest_automator->actions[$index]->said);
130 }
131 $actions = $simpletest_automator->actions;
132 unset($simpletest_automator->actions);
133 $output = array();
134 _simpletest_automator_export_php($simpletest_automator, '$simpletest_automator', $output);
135 $output[] = "";
136 foreach ($actions as $action) {
137 _simpletest_automator_export_php($action, '$action', $output);
138 $output[] = "\$simpletest_automator->actions[] = \$action;";
139 $output[] = "";
140 }
141 $form = array();
142 $form['code'] = array(
143 '#type' => 'textarea',
144 '#title' => t('Code'),
145 '#description' => t('Copy this code and paste it where you want.'),
146 '#cols' => 60,
147 '#rows' => 20,
148 '#default_value' => implode("\n", $output),
149 '#attributes' => array('readonly' => 'readonly'),
150 );
151
152 return $form;
153 }
154
155 function _simpletest_automator_export_php($element, $variable, &$output) {
156 $output[] = "$variable = new stdClass;";
157 foreach ($element as $key => $value) {
158 if (is_array($value)) {
159 $elements = array();
160 foreach ($value as $value_element) {
161 $elements[] = (count($value) > 5 ? " " : "") . var_export($value_element, TRUE) . (count($value) > 5 ? "," : "");
162 }
163 $temp = "$variable"."->$key = array(";
164 if (count($elements) < 6) {
165 $output[] = $temp . implode(', ', $elements) .");";
166 }
167 else {
168 $output[] = $temp;
169 $output = array_merge($output, $elements);
170 $output[] = ");";
171 }
172 }
173 else {
174 $output[] = "$variable"."->$key = ". var_export($value, TRUE) .";";
175 }
176 }
177 }
178

  ViewVC Help
Powered by ViewVC 1.1.2