Initial version of a ci-test-runner. (= run tests with different php-versions and...
[akelos.git] / script / extras / ci_tests.php
blob3d03562a0c884aff8e185281667c0563fa79f857
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
13 var $settings;
15 var $target_files;
16 var $target_executables;
17 var $target_environments;
19 static function main($args=array())
21 if (empty($args)){
22 global $argv;
23 $args = $argv;
26 $self = new CI_Tests($args);
27 $self->run();
28 $self->hadError() ? exit(1) : exit(0);
31 function __construct($args)
33 if (!is_file($this->config_file())) die('Not sure where I am and where config/config.php is. Run from inside the test/* folders.');
35 $this->loadSettings();
36 $this->parseArgs($args);
39 function loadSettings($filename=AK_CI_CONFIG_FILE)
41 require AK_BASE_DIR.DS.'vendor'.DS.'TextParsers'.DS.'spyc.php';
43 if (!is_file($filename)){
44 die ('Could not find ci configuration file in '.AK_CI_CONFIG_FILE.'.');
46 $yaml = file_get_contents($filename);
47 $this->settings = Spyc::YAMLLoad($yaml);
50 function parseArgs($args)
52 array_shift($args);
53 while (count($args) > 0){
54 $arg = array_shift($args);
55 if (array_key_exists(strtolower($arg),$this->settings['executables'])){
56 $this->target_executables[] = $arg;
57 }elseif (array_key_exists(strtolower($arg),$this->settings['environments'])){
58 $this->target_environments[] = $arg;
59 }elseif ($filename = $this->constructTestFilename($arg)){
60 $this->target_files[] = $filename;
61 }else{
62 switch ($arg){
63 case '-b':
64 $this->options['break_on_errors'] = true;
65 break;
66 case '-t':
67 $this->options['test_mode'] = true;
68 break;
69 case '-?':
70 case '?':
71 $this->drawHelp();
72 break;
77 $this->setDefaults();
80 function setDefaults()
82 if (!$this->target_executables) $this->target_executables = $this->settings['default_executables'];
83 if (!$this->target_files) $this->target_files[] = AK_BASE_DIR.DS.'test'.DS.'unit.php';
84 if (!$this->target_environments) $this->target_environments = array_keys($this->settings['environments']);
87 function constructTestFilename($filename)
89 if (is_file($filename)) return $filename;
91 $target_file = getcwd().DIRECTORY_SEPARATOR.$filename;
92 if (is_file($target_file)) return $target_file;
94 return false;
98 function config_file()
100 return AK_BASE_DIR.DS.'config'.DS.'config.php';
103 function config_backup_file()
105 return AK_BASE_DIR.DS.'config'.DS.'config-backup.php';
108 function config_file_for($environment)
110 return AK_BASE_DIR.DS.config.DS.$this->settings['environments'][$environment].'.php';
113 function run()
115 $this->drawHeader();
117 $this->beforeRun();
118 foreach ($this->filesToRun() as $file){
119 foreach ($this->executablesToRun() as $php_version){
120 foreach ($this->environmentsToRun() as $environment){
121 if ($this->isValidCombination($environment,$php_version)){
122 $return_value = $this->runCommand($php_version,$file,$environment);
123 if ($return_value !== 0) {
124 $this->markError();
125 if ($this->options['break_on_errors']) break 3;
131 $this->afterRun();
133 $this->drawFooter();
136 function markError()
138 $this->errors = true;
141 function hadError()
143 return isset($this->errors);
146 function filesToRun()
148 return $this->target_files;
151 function executablesToRun()
153 return $this->target_executables;
156 function environmentsToRun()
158 return $this->target_environments;
161 function isValidCombination($environment,$php_version)
163 return in_array($environment,$this->settings['valid_combinations'][$php_version]);
166 function beforeRun()
168 return copy($this->config_file(),$this->config_backup_file());
171 function afterRun()
173 if (copy($this->config_backup_file(),$this->config_file())){
174 return unlink($this->config_backup_file());
176 return false;
179 function prepareEnvironment($environment)
181 if (!is_file($this->config_file_for($environment))){
182 echo "Can't find environment settings for $environment. Skipping...\n\r";
183 return false;
185 return copy($this->config_file_for($environment),$this->config_file());
188 function runCommand($php,$filename,$environment)
190 $this->drawBox(array($filename,strtoupper($environment),$php));
192 if ($this->prepareEnvironment($environment)){
193 $command = $this->settings['executables'][$php].' '.$filename;
194 if ($this->options['test_mode']){
195 echo "Executing: ".$command."\n\r";
196 $return_value = 0;
197 }else{
198 passthru($command,$return_value);
200 return $return_value;
204 function drawBox($message)
206 $this->drawNewline();
207 $this->drawLine();
208 echo " TARGET: ".join(', ',$message)."\n\r";
209 $this->drawLine();
210 $this->drawNewline();
213 function drawHeader()
215 #$this->drawLine('+');
218 function drawFooter()
220 $this->drawNewline();
221 $this->drawLine('+');
222 echo "FINISHED. ";
223 if (!$this->hadError()) echo " All fine.";
226 function drawLine($char='-',$num=80)
228 echo str_pad('',$num,$char);
231 function drawNewline()
233 echo "\n\r";
236 function drawHelp()
238 echo <<<BANNER
239 Usage:
241 ci_tests [php4|php5] [mysql|postgres|sqlite] [-b] [test-files]
242 -b break on first error
243 -t test-mode, don't run the commands actually
244 -? this help
246 Examples:
247 > ci_tests
248 run all unit tests in any combination.
250 > ci_tests php5 postgres mysql AkHasMany AkBelongsTo
251 run AkHasMany and AkBelongsTo on PHP5 using the postgres and mysql-db.
253 Setup:
254 1. Copy DEFAULT-ci-config.yaml to config/ci-config.yaml and set it up
256 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.
258 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. ;-)
260 This script backups config/config.php to config-backup.php (and restores it after run).
262 BANNER;
263 exit;
266 $test_args = array(
267 'Myself_will_be_thrown_away',
268 "all",
269 #"-b",
270 #"-?",
271 "-t",
272 #'AkHasMany.php',
273 #'postgres'
275 #CI_Tests::main($test_args);
276 CI_Tests::main();