1 : <?php
2 : /**
3 : * Dictionaries of Old French and Latin
4 : *
5 : * PHP version 5
6 : *
7 : * @category DicFro
8 : * @package Tests
9 : * @author Michel Corne <mcorne@yahoo.com>
10 : * @copyright 2010 Michel Corne
11 : * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
12 : * @version SVN: $Id$
13 : */
14 :
15 : /**
16 : * Test run
17 : *
18 : * @category DicFro
19 : * @package Tests
20 : * @author Michel Corne <mcorne@yahoo.com>
21 : * @copyright 2010 Michel Corne
22 : * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
23 : */
24 :
25 : class Test {
26 :
27 : /**
28 : * The base directory where the test results are stored
29 : */
30 : const PHPUNIT_DIR = '../../../../www/documentation/dicfro/phpunit/';
31 :
32 : /**
33 : * The name of the sub directory of the code coverage report
34 : */
35 : const COVERAGE_DIR = 'coverage';
36 :
37 : /**
38 : * The name of the file containing the test results
39 : */
40 : const TESTDOX_FILE = 'testdox.html';
41 :
42 : /**
43 : * The name of temporary working directory
44 : */
45 : const TMP_DIR = 'tmp';
46 :
47 : /**
48 : * The help text
49 : */
50 : const HELP_TEXT =
51 : '
52 : Usage: runtest [option] [value]
53 :
54 : a -- runs all the tests
55 : c -- runs the test code coverage
56 : r -- reruns the last command
57 : s <test> -- runs a specific test
58 : u -- runs the recently updated tests
59 : v -- runs all the tests in verbose mode
60 :
61 : h -- list options (help)
62 : ';
63 :
64 : /**
65 : * Processes the command option
66 : *
67 : * @return string the result of the command
68 : */
69 : public function processOption()
70 : {
71 0 : global $argv;
72 0 : $option = isset($argv[1])? $argv[1] : null;
73 0 : $option = str_replace('-', '', $option);
74 0 : $param = isset($argv[2])? $argv[2] : null;
75 :
76 : switch($option) {
77 0 : case 'a':
78 : // runs all the tests
79 0 : putenv('RUN_ALL_TESTS=1');
80 0 : $command = 'phpunit --stop-on-failure AllTests';
81 : break;
82 :
83 0 : case 'c':
84 : // runs the test code coverage
85 0 : $phpunitDir = dirname(__FILE__) . '/' . self::PHPUNIT_DIR;
86 0 : $coverageDir = $phpunitDir . '/' . self::COVERAGE_DIR;
87 0 : $testdoxFile = $phpunitDir . '/' . self::TESTDOX_FILE;
88 :
89 0 : is_dir($phpunitDir) or die('Error: invalid phpunit directory');
90 0 : is_dir($coverageDir) or die('Error: invalid test code coverage directory');
91 :
92 0 : putenv('RUN_ALL_TESTS=1');
93 0 : $command = "phpunit --testdox-html $testdoxFile --coverage-html $coverageDir AllTests";
94 : break;
95 :
96 0 : case 'r':
97 : // reruns the last command
98 0 : $command = "phpunit --stop-on-failure AllTests";
99 : break;
100 :
101 0 : case 's':
102 : // runs a specific test
103 0 : $param or die('Error: enter the test to run !');
104 :
105 0 : putenv('RUN_ALL_TESTS=1');
106 0 : $command = "phpunit --stop-on-failure --filter $param .";
107 : break;
108 :
109 0 : case 'u':
110 : // runs the updated tests
111 0 : putenv('RUN_ALL_TESTS=0');
112 0 : $command = 'phpunit --stop-on-failure AllTests';
113 : break;
114 :
115 0 : case 'v':
116 : // runs all tests in verbose mode
117 0 : putenv('RUN_ALL_TESTS=1');
118 0 : $command = 'phpunit --verbose AllTests';
119 : break;
120 :
121 0 : case 'h':
122 : default:
123 0 : $command = null;
124 : }
125 :
126 0 : return $command;
127 : }
128 :
129 : public function removeTempDir($tmpDir)
130 : {
131 0 : array_map('unlink', glob("$tmpDir/*.*"));
132 0 : array_map('unlink', glob("$tmpDir/*/*.*"));
133 0 : array_map('unlink', glob("$tmpDir/*/*/*.*"));
134 0 : array_map('unlink', glob("$tmpDir/*/*/*/*.*"));
135 0 : array_map('unlink', glob("$tmpDir/*/*/*/*/*.*"));
136 0 : array_map('rmdir', glob("$tmpDir/*/*/*/*", GLOB_ONLYDIR));
137 0 : array_map('rmdir', glob("$tmpDir/*/*/*", GLOB_ONLYDIR));
138 0 : array_map('rmdir', glob("$tmpDir/*/*", GLOB_ONLYDIR));
139 0 : array_map('rmdir', glob("$tmpDir/*", GLOB_ONLYDIR));
140 0 : @rmdir($tmpDir);
141 0 : }
142 :
143 :
144 : public function createTempDir()
145 : {
146 0 : self::TMP_DIR or die('missing temp directory!!!');
147 :
148 0 : $tmpDir = dirname(__FILE__) . '/' . self::TMP_DIR;
149 :
150 0 : $this->removeTempDir($tmpDir);
151 :
152 0 : @mkdir($tmpDir) or die('cannot create temp directory');
153 :
154 0 : return $tmpDir;
155 : }
156 :
157 : /**
158 : * Runs the command
159 : *
160 : * @return void
161 : */
162 : public function run()
163 : {
164 0 : $tmpDir = $this->createTempDir();
165 :
166 0 : $command = $this->processOption();
167 0 : if ($command) {
168 0 : echo "$command\n";
169 0 : passthru($command);
170 :
171 0 : } else {
172 0 : echo self::HELP_TEXT;
173 : }
174 :
175 0 : $this->removeTempDir($tmpDir);
176 0 : }
|