1 | <?php |
---|
2 | |
---|
3 | $template = '<?php |
---|
4 | require_once \'PHPUnit.php\'; |
---|
5 | require_once \'XML/Feed/Parser.php\'; |
---|
6 | class '; |
---|
7 | |
---|
8 | $endTemplate = ' |
---|
9 | $result = PHPUnit::run($suite, \'123\'); |
---|
10 | echo $result->toString(); |
---|
11 | |
---|
12 | ?> |
---|
13 | '; |
---|
14 | error_reporting(E_ERROR); |
---|
15 | |
---|
16 | function applyFilters(&$test) |
---|
17 | { |
---|
18 | if (! strstr($test, 'bozo') and ! preg_match('/^encoding/', $test) |
---|
19 | and ! preg_match('/^header/', $test)) |
---|
20 | { |
---|
21 | $testparts = explode(' == ', $test); |
---|
22 | $testparts[0] = preg_replace('/^not/', '!', $testparts[0]); |
---|
23 | $testparts[0] = preg_replace('/(^|\s)feed/', '$1\$feed', $testparts[0]); |
---|
24 | $testparts[0] = preg_replace('/entries\[(\d+)\]/', '\$feed->getEntryByOffset($1)', $testparts[0]); |
---|
25 | $testparts[0] = preg_replace('/\[\'(.*)\'\]/', '->$1', $testparts[0]); |
---|
26 | $testparts[0] = preg_replace('/\'.*?\'/', '->', $testparts[0]); |
---|
27 | $testparts[0] = preg_replace('/^version$/', '\$feed->version()', $testparts[0]); |
---|
28 | $testparts[0] = preg_replace('/_detail->value/', '', $testparts[0]); |
---|
29 | $testparts[0] = preg_replace('/_detail->(.*)/', '(0, \'$1\')', $testparts[0]); |
---|
30 | $testparts[0] = preg_replace('/getEntryByOffset\(0\)->(.*?)->(.*)/', |
---|
31 | 'getEntryByOffset(0)->$1(0, \'$2\')', $testparts[0]); |
---|
32 | |
---|
33 | if (! preg_match('/feed->getEntry/', $testparts[0])) { |
---|
34 | $testparts[0] = preg_replace('/feed->(.*?)->(.*)/', 'feed->$1(0, \'$2\')', $testparts[0]); |
---|
35 | } |
---|
36 | |
---|
37 | $testparts[1] = preg_replace('/u\'/', '\'', $testparts[1]); |
---|
38 | $testparts[1] = preg_replace('/\'(.*?)\': \'(.*?)\'/', '\'$1\' => \'$2\'', $testparts[1]); |
---|
39 | $testparts[1] = preg_replace('/{(.*?)}/', 'array($1)', $testparts[1]); |
---|
40 | $testparts[1] = preg_replace('/\[(.*?)\]/', 'array($1)', $testparts[1]); |
---|
41 | $testparts[1] = preg_replace('/^\((.*)\)$/', 'array($1)', $testparts[1]); |
---|
42 | $testparts[1] = preg_replace('/^<div>(.*)<\/div>$/', '$1', $testparts[1]); |
---|
43 | |
---|
44 | $test = implode(' == ', $testparts); |
---|
45 | return $test; |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | function testToPHP($test) |
---|
50 | { |
---|
51 | $tests = explode(' and ', $test); |
---|
52 | return $tests; |
---|
53 | } |
---|
54 | |
---|
55 | function extractTest($path) |
---|
56 | { |
---|
57 | $data = array(); |
---|
58 | $f = fopen('./feedparsertests/wellformed/' . $path, 'r'); |
---|
59 | while ($line = fgets($f)) |
---|
60 | { |
---|
61 | $line = trim($line); |
---|
62 | if (preg_match('/^Description:/', $line)) |
---|
63 | { |
---|
64 | $data['description'] = trim(preg_replace('/Description:/', '', $line)); |
---|
65 | } else if (preg_match('/^Expect:/', $line)) |
---|
66 | { |
---|
67 | $data['expect'] = trim(preg_replace('/Expect:/', '', $line)); |
---|
68 | } |
---|
69 | if (!empty($data['expect']) and !empty($data['description'])) |
---|
70 | { |
---|
71 | break; |
---|
72 | } |
---|
73 | } |
---|
74 | fclose($f); |
---|
75 | return $data; |
---|
76 | } |
---|
77 | |
---|
78 | $handle = opendir('./feedparsertests/wellformed/'); |
---|
79 | $tests_passed = array(); |
---|
80 | $tests_failed = array(); |
---|
81 | $all_tests = array(); |
---|
82 | while (false !== ($dir = readdir($handle))) |
---|
83 | { |
---|
84 | if (! preg_match('/^\./', $dir) and is_dir('./feedparsertests/wellformed/' . $dir)) |
---|
85 | { |
---|
86 | $tests_passed[$dir] = array(); |
---|
87 | $tests_failed[$dir] = array(); |
---|
88 | $allTests[$dir] = array(); |
---|
89 | $innerHandle = opendir('feedparsertests/wellformed/' . $dir); |
---|
90 | while (false !== ($file = readdir($innerHandle))) |
---|
91 | { |
---|
92 | if (preg_match('/.xml$/', $file)) |
---|
93 | { |
---|
94 | $parts = extractTest($dir . '/' . $file); |
---|
95 | $theseTests = testToPHP($parts['expect']); |
---|
96 | foreach ($theseTests as $thisKey => $thisTest) |
---|
97 | { |
---|
98 | $allTests[$dir][$file . '_' . $thisKey] = $thisTest; |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | $iterTests = array_filter($allTests[$dir], 'applyFilters'); |
---|
103 | $fw = fopen('./convertedtests/' . $dir . '.php', 'w'); |
---|
104 | fwrite($fw, $template . $dir . "_TestCase extends XML_Feed_Parser_TestCase {\n"); |
---|
105 | foreach($iterTests as $key => $test) |
---|
106 | { |
---|
107 | $funcname = str_replace('.xml', '', $key); |
---|
108 | $funcname = str_replace('.', '_', $funcname); |
---|
109 | $file = preg_replace('/(.*)_.*/', '$1', $key); |
---|
110 | fwrite($fw, ' |
---|
111 | function test_' . $funcname . ' () { |
---|
112 | $content = file_get_contents(\'../feedparsertests/wellformed/' . $dir . '/' . $file . '\'); |
---|
113 | try { |
---|
114 | $feed = new XML_Feed_Parser($content); |
---|
115 | } catch (XML_Feed_Parser_Exception $e) { |
---|
116 | $this->assertTrue(false); |
---|
117 | return; |
---|
118 | } |
---|
119 | $this->assertEquals(' . implode(', ', array_reverse(explode(' == ', $test))) .'); |
---|
120 | } |
---|
121 | '); |
---|
122 | } |
---|
123 | fwrite($fw, '} |
---|
124 | |
---|
125 | $suite = new PHPUnit_TestSuite(\'' . $dir . '_TestCase\');'); |
---|
126 | fwrite($fw, $endTemplate); |
---|
127 | fclose($fw); |
---|
128 | $all_tests = array_merge($all_tests, $allTests[$dir]); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | $total_tests = 0; |
---|
133 | $total_passed = 0; |
---|
134 | $total_failed = 0; |
---|
135 | |
---|
136 | foreach($tests_passed as $test) |
---|
137 | { |
---|
138 | $total_tests += count($test); |
---|
139 | $total_passed += count($test); |
---|
140 | } |
---|
141 | |
---|
142 | foreach ($tests_failed as $testf) |
---|
143 | { |
---|
144 | $total_tests += count($testf); |
---|
145 | $total_failed += count($testf); |
---|
146 | } |
---|
147 | |
---|
148 | ?> |
---|