| 1 | <?php |
| 2 | /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 3 | /* |
| 4 | # ***** BEGIN LICENSE BLOCK ***** |
| 5 | # This file is part of Plume Framework, a simple PHP Application Framework. |
| 6 | # Copyright (C) 2001-2007 Loic d'Anterroches and contributors. |
| 7 | # |
| 8 | # Plume Framework is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU Lesser General Public License as published by |
| 10 | # the Free Software Foundation; either version 2.1 of the License, or |
| 11 | # (at your option) any later version. |
| 12 | # |
| 13 | # Plume Framework is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU Lesser General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU Lesser General Public License |
| 19 | # along with this program; if not, write to the Free Software |
| 20 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | # |
| 22 | # ***** END LICENSE BLOCK ***** */ |
| 23 | |
| 24 | /** |
| 25 | * testrunner for your application. |
| 26 | */ |
| 27 | set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__)); |
| 28 | function usage($script) |
| 29 | { |
| 30 | echo($script.' YourApp [path/to/config/file.php]'."\n"); |
| 31 | echo(' If no config file given, will use the following:'."\n"); |
| 32 | echo(' YourApp/conf/yourapp.test.php'."\n"); |
| 33 | echo(' If you are using SQLite, we recommend the ":memory:" database.'."\n"); |
| 34 | echo(' [installed apps]/Migrations/Install.php will be used to setup/teardown'."\n"); |
| 35 | echo(' the database before/after the run.'."\n"); |
| 36 | } |
| 37 | function e($m) |
| 38 | { |
| 39 | echo($m."\n"); |
| 40 | } |
| 41 | function getTestDirs($dir='./') |
| 42 | { |
| 43 | $file = new DirectoryIterator($dir); |
| 44 | $res = array(); |
| 45 | while ($file->valid()) { |
| 46 | if ($file->isDir() && !$file->isDot()) { |
| 47 | $res[] = $file->getPathName(); |
| 48 | } |
| 49 | $file->next(); |
| 50 | } |
| 51 | return $res; |
| 52 | } |
| 53 | |
| 54 | function getTestFiles($dir='') |
| 55 | { |
| 56 | $file = new DirectoryIterator($dir); |
| 57 | $res = array(); |
| 58 | while ($file->valid()) { |
| 59 | if ($file->isFile() && substr($file->getPathName(), -4) == '.php') { |
| 60 | $class = str_replace(DIRECTORY_SEPARATOR, '_', substr($file->getPathName(), 0, -4)); |
| 61 | $res[] = array($file->getPathName(), $class); |
| 62 | } |
| 63 | $file->next(); |
| 64 | } |
| 65 | return $res; |
| 66 | } |
| 67 | if (PHP_SAPI == 'cli') { |
| 68 | // Get the application and then the configuration file |
| 69 | if ($argc < 2) { |
| 70 | usage($argv[0]); |
| 71 | exit(1); |
| 72 | } |
| 73 | $app = $argv[1]; |
| 74 | if ($argc >= 3) { |
| 75 | $config = $argv[2]; |
| 76 | } else { |
| 77 | $config = $app.'/conf/'.strtolower($app).'.test.php'; |
| 78 | } |
| 79 | } else { |
| 80 | echo('Error: This script can only be run from the command line.'."\n"); |
| 81 | exit(1); |
| 82 | } |
| 83 | echo(sprintf('Application: %s ', $app)); |
| 84 | if (!file_exists($config)) { |
| 85 | echo(sprintf("\n".'Error, the config file does not exists: %s'."\n", $config)); |
| 86 | exit(1); |
| 87 | } else { |
| 88 | echo(sprintf('(%s)'."\n", $config)); |
| 89 | } |
| 90 | define('IN_UNIT_TESTS', true); |
| 91 | require 'Pluf.php'; |
| 92 | Pluf::start($config); |
| 93 | |
| 94 | $simple_test = Pluf::f('simple_test_path', false); |
| 95 | if (false == $simple_test) { |
| 96 | e('Error, the path to the simple test framework is not defined.'); |
| 97 | e('Download simple test from:'); |
| 98 | e(' http://simpletest.sourceforge.net/'); |
| 99 | e('Extract the archive on your system and set the "simple_test_path"'); |
| 100 | e('configuration variable in your configuration file.'); |
| 101 | e('For example: $cfg[\'simple_test_path\'] = \'/home/you/simpletest\'; '); |
| 102 | exit(1); |
| 103 | } |
| 104 | $testfolder = $app.'/Tests/'; |
| 105 | if (!file_exists($testfolder)) { |
| 106 | e(sprintf('The test folder does not exists: %s.', $app.'/Tests/')); |
| 107 | exit(1); |
| 108 | } |
| 109 | |
| 110 | define('SIMPLE_TEST', $simple_test.'/'); |
| 111 | require_once(SIMPLE_TEST.'unit_tester.php'); |
| 112 | require_once(SIMPLE_TEST.'reporter.php'); |
| 113 | |
| 114 | $files = getTestFiles($testfolder); |
| 115 | $dirs = getTestDirs($testfolder); |
| 116 | foreach ($dirs as $dir) { |
| 117 | foreach (getTestFiles($dir) as $test) { |
| 118 | $files[] = $test; |
| 119 | } |
| 120 | } |
| 121 | $test = &new GroupTest(sprintf('All tests for application %s.', $app)); |
| 122 | foreach ($files as $t) { |
| 123 | $test->addTestCase(new $t[1]()); |
| 124 | } |
| 125 | $reporter = new TextReporter(); |
| 126 | $mig = new Pluf_Migration(null); |
| 127 | $mig->display = false; |
| 128 | $mig->install(); |
| 129 | // If available, load an initialisation file. |
| 130 | if (file_exists($app.'/Tests/init.json')) { |
| 131 | $created = Pluf_Test_Fixture::loadFile($app.'/Tests/init.json'); |
| 132 | } else { |
| 133 | $created = array(); |
| 134 | } |
| 135 | $test->run($reporter); |
| 136 | $mig->unInstall(); |