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,
16 var $target_executables;
17 var $target_environments;
19 static function main($args=array())
26 $self = new CI_Tests($args);
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)
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;
64 $this->options
['break_on_errors'] = true;
67 $this->options
['test_mode'] = true;
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;
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';
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) {
125 if ($this->options
['break_on_errors']) break 3;
138 $this->errors
= true;
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]);
168 return copy($this->config_file(),$this->config_backup_file());
173 if (copy($this->config_backup_file(),$this->config_file())){
174 return unlink($this->config_backup_file());
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";
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";
198 passthru($command,$return_value);
200 return $return_value;
204 function drawBox($message)
206 $this->drawNewline();
208 echo " TARGET: ".join(', ',$message)."\n\r";
210 $this->drawNewline();
213 function drawHeader()
215 #$this->drawLine('+');
218 function drawFooter()
220 $this->drawNewline();
221 $this->drawLine('+');
223 if (!$this->hadError()) echo " All fine.";
226 function drawLine($char='-',$num=80)
228 echo str_pad('',$num,$char);
231 function drawNewline()
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
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.
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).
267 'Myself_will_be_thrown_away',
275 #CI_Tests::main($test_args);