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');
9 'break_on_errors'=>false,
17 var $target_executables;
18 var $target_environments;
20 static function main($args=array())
27 $self = new CI_Tests($args);
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)
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;
65 $this->options
['break_on_errors'] = true;
68 $this->options
['test_mode'] = true;
75 $timesToRepeat = array_shift($args);
76 $this->options
['repeat'] = $timesToRepeat;
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;
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';
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) {
132 if ($this->options
['break_on_errors']) break 4;
146 $this->errors
= true;
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]);
181 return copy($this->config_file(),$this->config_backup_file());
186 if (copy($this->config_backup_file(),$this->config_file())){
187 return unlink($this->config_backup_file());
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";
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";
211 passthru($command,$return_value);
213 return $return_value;
217 function drawBox($message)
219 $this->drawNewline();
221 $this->drawNewline();
222 echo " TARGET: ".join(', ',$message)."\n\r";
224 $this->drawNewline();
227 function drawHeader()
229 #$this->drawLine('+');
232 function drawFooter()
234 $this->drawNewline();
235 $this->drawLine('+');
236 $this->drawNewline();
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);
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
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.
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).
292 'Myself_will_be_thrown_away',
301 #CI_Tests::main($test_args);