Fixing content type ordering when content_type is not defined.
[akelos.git] / script / extras / ci_tests.php
blob90afe25fd586d4b3ed4bbe4a7d3a5f85bb158fbe
1 <?php
2 defined('DS') ? null : define('DS',DIRECTORY_SEPARATOR);
3 defined('AK_BASE_DIR') ? null : define('AK_BASE_DIR',preg_replace('@\\'.DS.'(test|script)($|\\'.DS.'.*)@','',getcwd()));
4 define('AK_CI_CONFIG_FILE',AK_BASE_DIR.DS.'config'.DS.'ci-config.yaml');
6 class CI_Tests
8 var $options = array(
9 'break_on_errors'=>false,
10 'test_mode' =>false,
11 'repeat' =>1
14 var $settings;
16 var $target_files;
17 var $target_executables;
18 var $target_environments;
20 static function main($args=array())
22 if (empty($args)){
23 global $argv;
24 $args = $argv;
27 $self = new CI_Tests($args);
28 $self->run();
29 $self->hadError() ? exit(1) : exit(0);
32 function __construct($args)
34 if (!is_file($this->config_file())) die('Not sure where I am and where config/config.php is. Run from inside the test/* folders.');
36 $this->loadSettings();
37 $this->parseArgs($args);
40 function loadSettings($filename=AK_CI_CONFIG_FILE)
42 require AK_BASE_DIR.DS.'vendor'.DS.'TextParsers'.DS.'spyc.php';
44 if (!is_file($filename)){
45 die ('Could not find ci configuration file in '.AK_CI_CONFIG_FILE.'.');
47 $yaml = file_get_contents($filename);
48 $this->settings = Spyc::YAMLLoad($yaml);
51 function parseArgs($args)
53 array_shift($args);
54 while (count($args) > 0){
55 $arg = array_shift($args);
56 if (array_key_exists(strtolower($arg),$this->settings['executables'])){
57 $this->target_executables[] = $arg;
58 }elseif (array_key_exists(strtolower($arg),$this->settings['environments'])){
59 $this->target_environments[] = $arg;
60 }elseif ($filename = $this->constructTestFilename($arg)){
61 $this->target_files[] = $filename;
62 }else{
63 switch ($arg){
64 case '-b':
65 $this->options['break_on_errors'] = true;
66 break;
67 case '-t':
68 $this->options['test_mode'] = true;
69 break;
70 case '-?':
71 case '?':
72 $this->drawHelp();
73 break;
74 case '-n':
75 $timesToRepeat = array_shift($args);
76 $this->options['repeat'] = $timesToRepeat;
77 break;
82 $this->setDefaults();
85 function setDefaults()
87 if (!$this->target_executables) $this->target_executables = $this->settings['default_executables'];
88 if (!$this->target_files) $this->target_files[] = AK_BASE_DIR.DS.'test'.DS.'unit.php';
89 if (!$this->target_environments) $this->target_environments = array_keys($this->settings['environments']);
92 function constructTestFilename($filename)
94 if (is_file($filename)) return $filename;
96 $target_file = getcwd().DIRECTORY_SEPARATOR.$filename;
97 if (is_file($target_file)) return $target_file;
99 return false;
103 function config_file()
105 return AK_BASE_DIR.DS.'config'.DS.'config.php';
108 function config_backup_file()
110 return AK_BASE_DIR.DS.'config'.DS.'config-backup.php';
113 function config_file_for($environment)
115 return AK_BASE_DIR.DS.'config'.DS.$this->settings['environments'][$environment].'.php';
118 function run()
120 $this->drawHeader();
122 $this->beforeRun();
123 for ($i=1; $i <= $this->timesToRun(); $i++){
124 $this->drawRepeatIndicator($i);
125 foreach ($this->filesToRun() as $file){
126 foreach ($this->executablesToRun() as $php_version){
127 foreach ($this->environmentsToRun() as $environment){
128 if ($this->isValidCombination($environment,$php_version)){
129 $return_value = $this->runCommand($php_version,$file,$environment);
130 if ($return_value !== 0) {
131 $this->markError();
132 if ($this->options['break_on_errors']) break 4;
139 $this->afterRun();
141 $this->drawFooter();
144 function markError()
146 $this->errors = true;
149 function hadError()
151 return isset($this->errors);
154 function filesToRun()
156 return $this->target_files;
159 function executablesToRun()
161 return $this->target_executables;
164 function environmentsToRun()
166 return $this->target_environments;
169 function timesToRun()
171 return $this->options['repeat'];
174 function isValidCombination($environment,$php_version)
176 return in_array($environment,$this->settings['valid_combinations'][$php_version]);
179 function beforeRun()
181 return copy($this->config_file(),$this->config_backup_file());
184 function afterRun()
186 if (copy($this->config_backup_file(),$this->config_file())){
187 return unlink($this->config_backup_file());
189 return false;
192 function prepareEnvironment($environment)
194 if (!is_file($this->config_file_for($environment))){
195 echo "Can't find environment settings for $environment. Skipping...\n\r";
196 return false;
198 return copy($this->config_file_for($environment),$this->config_file());
201 function runCommand($php,$filename,$environment)
203 $this->drawBox(array($filename,strtoupper($environment),$php));
205 if ($this->prepareEnvironment($environment)){
206 $command = $this->settings['executables'][$php].' '.$filename;
207 if ($this->options['test_mode']){
208 echo "Executing: ".$command."\n\r";
209 $return_value = 0;
210 }else{
211 passthru($command,$return_value);
213 return $return_value;
217 function drawBox($message)
219 $this->drawNewline();
220 $this->drawLine();
221 $this->drawNewline();
222 echo " TARGET: ".join(', ',$message)."\n\r";
223 $this->drawLine();
224 $this->drawNewline();
227 function drawHeader()
229 #$this->drawLine('+');
232 function drawFooter()
234 $this->drawNewline();
235 $this->drawLine('+');
236 $this->drawNewline();
237 echo "FINISHED. ";
238 $this->drawNewline();
239 if (!$this->hadError()) echo " All fine.";
242 function drawRepeatIndicator($actual)
244 if ($this->timesToRun() == 1) return;
246 $this->drawNewline(2);
247 echo str_pad('# '.$actual.'. ',80,'#');
250 function drawLine($char='-',$num=80)
252 echo str_pad('',$num,$char);
255 function drawNewline($multiplier=1)
257 echo str_repeat("\n\r",$multiplier);
260 function drawHelp()
262 echo <<<BANNER
263 Usage:
265 ci_tests [php4|php5] [mysql|postgres|sqlite] [-b] [test-files]
266 -b break on first error
267 -t test-mode, don't run the commands actually
268 -n x repeat tests x times
269 -? this help
271 Examples:
272 > ci_tests
273 run all unit tests in any combination.
275 > ci_tests php5 postgres mysql AkHasMany AkBelongsTo
276 run AkHasMany and AkBelongsTo on PHP5 using the postgres and mysql-db.
278 Setup:
279 1. Copy DEFAULT-ci-config.yaml to config/ci-config.yaml and set it up
281 2. Copy config/config.php to config/mysql-testing.php, config/postgres-testing.php [...] and modify the database settings at least for the testing environment. You can configure the filename for these config-files in the script directly if you must.
283 3. Expects to be run from inside the test folder structure. So to speak your current directory must be */test or a subdir. The script itself can be placed whereever you want. You can define a (shell-)macro and quickly swap between different installations and test again. ;-)
285 This script backups config/config.php to config-backup.php (and restores it after run).
287 BANNER;
288 exit;
291 $test_args = array(
292 'Myself_will_be_thrown_away',
293 "all",
294 #"-b",
295 #"-?",
296 "-t",
297 #"-n","2",
298 #'AkHasMany.php',
299 #'postgres'
301 #CI_Tests::main($test_args);
302 CI_Tests::main();