DoctrineTest.php 9.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.phpdoctrine.org>.
20 21 22 23 24 25 26 27 28 29
 */

/**
 * Doctrine_UnitTestCase
 *
 * @package     Doctrine
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @author      Bjarte S. Karlsen <bjartka@pvv.ntnu.no>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @category    Object Relational Mapping
30
 * @link        www.phpdoctrine.org
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
 * @since       1.0
 * @version     $Revision$
 */

require_once dirname(__FILE__) . '/DoctrineTest/UnitTestCase.php';
require_once dirname(__FILE__) . '/DoctrineTest/GroupTest.php';
require_once dirname(__FILE__) . '/DoctrineTest/Doctrine_UnitTestCase.php';
require_once dirname(__FILE__) . '/DoctrineTest/Reporter.php';

class DoctrineTest
{

    protected $testGroup; // the default test group
    protected $groups;

    public function __construct()
    {
        $this->requireModels();
        $this->testGroup = new GroupTest('Doctrine Framework Unit Tests', 'main');
    }

    /**
     * Add a test to be run. 
     *
     * This is a thin wrapper around GroupTest that also store the testcase in 
     * this class so that it is easier to create custom groups
     *
     * @param UnitTestCase A test case
     */
    public function addTestCase($testCase){
        $this->groups[$testCase->getName()] = $testCase;
        $this->testGroup->addTestCase($testCase);
    }

    /**
     * Run the tests
     *
     * This method will run the tests with the correct Reporter. It will run 
     * grouped tests if asked to and filter results. It also has support for 
     * running coverage report. 
     *
     */
    public function run(){
        $testGroup = $this->testGroup;
        if (PHP_SAPI === 'cli') {
            require_once(dirname(__FILE__) . '/DoctrineTest/Reporter/Cli.php');
            $reporter = new DoctrineTest_Reporter_Cli();
            $argv = $_SERVER['argv'];
            array_shift($argv);
            $options = $this->parseOptions($argv);
        } else {
            require_once(dirname(__FILE__) . '/DoctrineTest/Reporter/Html.php');
            $options = $_GET;
84 85 86 87 88 89
            if(isset($options["filter"])){
                $options["filter"] = explode(",", $options["filter"]);
            }
            if(isset($options["group"])){
                $options["group"] = explode(",", $options["group"]);
            }
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
            $reporter = new DoctrineTest_Reporter_Html();
        }

        //replace global group with custom group if we have group option set
        if (isset($options['group'])) {
            $testGroup = new GroupTest('Doctrine Framework Custom test', 'custom');
            foreach($options['group'] as $group) {
                if (isset($this->groups[$group])) {
                    $testGroup->addTestCase($this->groups[$group]);
                } else if (class_exists($group)) {
                    $testGroup->addTestCase(new $group);
                } else {
                    die($group . " is not a valid group or doctrine test class\n ");
                }
            }
        } 

        $filter = '';
        if (isset($options['filter'])) {
            $filter = $options['filter'];
        }

        //show help text
        if (isset($options['help'])) {
            echo "Doctrine test runner help\n";
            echo "===========================\n";
            echo " To run all tests simply run this script without arguments. \n";
            echo "\n Flags:\n";
            echo " -coverage will generate coverage report data that can be viewed with the cc.php script in this folder. NB! This takes time. You need xdebug to run this\n";
            echo " -group <groupName1> <groupName2> <className1> Use this option to run just a group of tests or tests with a given classname. Groups are currently defined as the variable name they are called in this script.\n";
            echo " -filter <string1> <string2> case insensitive strings that will be applied to the className of the tests. A test_classname must contain all of these strings to be run\n"; 
            echo "\nAvailable groups:\n tickets, transaction, driver, data_dict, sequence, export, import, expression, core, relation, data_types, utility, db, event_listener, query_tests, record, cache\n";
            die();
        }

        //generate coverage report
        if (isset($options['coverage'])) {
127 128 129 130 131 132

            /*
             * The below code will not work for me (meus). It would be nice if 
             * somebody could give it a try. Just replace this block of code 
             * with the one below
             *
133
             define('PHPCOVERAGE_HOME', dirname(dirname(__FILE__)) . '/vendor/spikephpcoverage');
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
            require_once PHPCOVERAGE_HOME . '/CoverageRecorder.php';
            require_once PHPCOVERAGE_HOME . '/reporter/HtmlCoverageReporter.php';

            $covReporter = new HtmlCoverageReporter('Doctrine Code Coverage Report', '', 'coverage2');

            $includePaths = array('../lib');
            $excludePaths = array();
            $cov = new CoverageRecorder($includePaths, $excludePaths, $covReporter);

            $cov->startInstrumentation();
            $testGroup->run($reporter, $filter);
            $cov->stopInstrumentation();

            $cov->generateReport();
            $covReporter->printTextSummary();
             */
150 151 152 153 154 155 156 157
            xdebug_start_code_coverage(XDEBUG_CC_UNUSED | XDEBUG_CC_DEAD_CODE);
            $testGroup->run($reporter, $filter);
            $result['coverage'] = xdebug_get_code_coverage();
            xdebug_stop_code_coverage();
            file_put_contents(dirname(__FILE__) . '/coverage/coverage.txt', serialize($result));
            require_once dirname(__FILE__) . '/DoctrineTest/Coverage.php';
            $coverageGeneration = new DoctrineTest_Coverage();
            $coverageGeneration->generateReport();
158
            return;
159
            // */
160

161
        }
162
        $testGroup->run($reporter, $filter);
163 164 165 166 167 168 169 170 171
    }


    /**
     * Require all the models needed in the tests
     *
     */
    public function requireModels()
    {
172
        $models = new DirectoryIterator(dirname(__FILE__) . '/models/');
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
        foreach($models as $key => $file) {
            if ($file->isFile() && ! $file->isDot()) {
                $e = explode('.', $file->getFileName());
                if (end($e) === 'php') {
                    require_once $file->getPathname();
                }
            }
        }
    }

    /**
     * Parse Options from cli into an associative array
     *
     * @param array $array An argv array from cli
     * @return array An array with options
     */
    public function parseOptions($array) {
        $currentName='';
        $options=array();
        foreach($array as $name) {
            if (strpos($name,'-')===0) {
                $name=str_replace('-','',$name);      
                $currentName=$name;
                if ( ! isset($options[$currentName])) {
                    $options[$currentName]=array();         
                }
            } else {
                $values=$options[$currentName];
                array_push($values,$name);    
                $options[$currentName]=$values;
            }
        }
        return $options;
    }

    /**
     * Autoload test cases
     *
     * Will create test case if it does not exist
     *
     * @param string $class The name of the class to autoload 
     * @return boolean True 
     */
    public static function autoload($class) {
        if (strpos($class, 'TestCase') === false) {
            return false;
        }

        $e      = explode('_', $class);
        $count  = count($e);

        $prefix = array_shift($e);

        if ($prefix !== 'Doctrine') {
            return false;
        }

        $dir    = array_shift($e);

        $file   = $dir . '_' . substr(implode('_', $e), 0, -(strlen('_TestCase'))) . 'TestCase.php';

        if ( $count > 3) {
            $file   = str_replace('_', DIRECTORY_SEPARATOR, $file);
        } else {
            $file   = str_replace('_', '', $file);
        }

        // create a test case file if it doesn't exist

        if ( ! file_exists($file)) {
            $contents = file_get_contents('template.tpl');
            $contents = sprintf($contents, $class, $class);

            if ( ! file_exists($dir)) {
                mkdir($dir, 0777);
            }

            file_put_contents($file, $contents);
        }
        require_once($file);

        return true;
    }
}