GroupTest.php 2.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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
<?php
class GroupTest extends UnitTestCase
{
    protected $_testCases = array();
    protected $_name;
    protected $_title;

    public function __construct($title, $name)
    {
        $this->_title = $title;
        $this->_name =  $name;
    }

    public function getName(){
        return $this->_name;
    }

    public function addTestCase(UnitTestCase $testCase)
    {
        if($testCase instanceOf GroupTest) {
            $this->_testCases = array_merge($this->_testCases, $testCase->getTestCases());
         } else {
            $this->_testCases[get_class($testCase)] = $testCase;
         }
    }

    public function shouldBeRun($testCase, $filter){
        if ( ! is_array($filter)) {
            return true;
        }
        foreach($filter as $subFilter) {
            $name = strtolower(get_class($testCase));
            $pos = strpos($name, strtolower($subFilter));
            //it can be 0 so we have to use === to see if false
            if ($pos === false) {
                return false;
            }
        }
        return true;
    }
    public function run(DoctrineTest_Reporter $reporter = null, $filter = null)
    {
        $reporter->paintHeader($this->_title);
        foreach ($this->_testCases as $k => $testCase) {
            if ( ! $this->shouldBeRun($testCase, $filter)) {
                continue;
            }
            try{
                $testCase->run();
            } catch(Exception $e) {
                $this->_failed += 1;
                $this->_messages[] = 'Unexpected exception thrown with message [' . $e->getMessage() . '] in ' . $e->getFile() . ' on line ' . $e->getLine();
            }
            $this->_passed += $testCase->getPassCount();
            $this->_failed += $testCase->getFailCount();
            $this->_messages = array_merge($this->_messages, $testCase->getMessages());

            $this->_testCases[$k] = null;
59
            echo $reporter->getProgressIndicator();
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
            set_time_limit(900);
        }
        $reporter->setTestCase($this);
        
        $reporter->paintFooter();
    }


    public function getTestCaseCount()
    {
        return count($this->_testCases);
    }

    public function getTestCases(){
        return $this->_testCases;
    }
}