Test.php 3.56 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php
class GroupTest
{
    protected $_testCases = array();

    public function addTestCase(UnitTestCase $testCase)
    {
        $this->_testCases[] = $testCase;
    }
    public function run(HtmlReporter $reporter)
    {
    	foreach ($this->_testCases as $testCase) {
    	    $testCase->run();
    	}
        $reporter->setTestCase($this);
        
        $reporter->paintHeader();
        $reporter->paintFooter();
    }
zYne's avatar
zYne committed
20 21 22 23
    public function getMessages()
    {
    	$messages = array();
        foreach($this->_testCases as $testCase) {
zYne's avatar
zYne committed
24
            $messages = array_merge($messages, $testCase->getMessages());
zYne's avatar
zYne committed
25 26 27
        }
        return $messages;
    }
zYne's avatar
zYne committed
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 59 60 61 62
    public function getFailCount()
    {
    	$fails = 0;
        foreach ($this->_testCases as $testCase) {
            $fails += $testCase->getFailCount();
        }
        return $fails;
    }
    public function getTestCaseCount()
    {
        return count($this->_testCases);
    }
    public function getPassCount()
    {
    	$passes = 0;
        foreach ($this->_testCases as $testCase) {
            $passes += $testCase->getPassCount();
        }
        return $passes;
    }
}
class HtmlReporter
{
    protected $_test;
    
    public function setTestCase(GroupTest $test) 
    {
        $this->_test = $test;
    }
}
class UnitTestCase
{
    protected $_passed = 0;
    
    protected $_failed = 0;
zYne's avatar
zYne committed
63 64
    
    protected $_messages = array();
zYne's avatar
zYne committed
65 66 67 68 69 70

    public function assertEqual($value, $value2)
    {
        if ($value == $value2) {
            $this->_passed++;
        } else {
71
            $this->_fail();
zYne's avatar
zYne committed
72 73
        }
    }
74 75 76 77 78 79 80 81 82 83

    public function assertIdentical($value, $value2)
    {
        if ($value === $value2) {
            $this->_passed++;
        } else {
            $this->_fail();
        }
    }

zYne's avatar
zYne committed
84 85 86 87 88
    public function assertNotEqual($value, $value2)
    {
        if ($value != $value2) {
            $this->_passed++;
        } else {
89
            $this->_fail();
zYne's avatar
zYne committed
90 91 92 93 94 95 96
        }
    }
    public function assertTrue($expr)
    {
        if ($expr) {
            $this->_passed++;
        } else {
97
            $this->_fail();
zYne's avatar
zYne committed
98 99
        }
    }
zYne's avatar
zYne committed
100 101 102 103 104
    public function assertFalse($expr)
    {
        if ( ! $expr) {
            $this->_passed++;
        } else {
105
            $this->_fail();
zYne's avatar
zYne committed
106 107 108 109 110 111 112
        }
    }
    public function pass() 
    {
        $this->_passed++;
    }
    public function fail()
113 114 115 116
    {
        $this->_fail();	
    }
    public function _fail()
zYne's avatar
zYne committed
117 118 119 120 121 122
    {
    	$trace = debug_backtrace();
    	array_shift($trace);


        foreach ($trace as $stack) {
zYne's avatar
zYne committed
123

zYne's avatar
zYne committed
124 125 126
            if (substr($stack['function'], 0, 4) === 'test') {
                $class = new ReflectionClass($stack['class']);

zYne's avatar
zYne committed
127
                if ( ! isset($line)) {
128
                    $line = $stack['line'];
zYne's avatar
zYne committed
129 130
                }

zYne's avatar
zYne committed
131 132 133 134
                $this->_messages[] = $class->getName() . ' : method ' . $stack['function'] . ' failed on line ' . $line;
                break;
            }
            $line = $stack['line'];
135

zYne's avatar
zYne committed
136 137 138 139
    	}

        $this->_failed++;
    }
zYne's avatar
zYne committed
140 141 142 143 144 145 146 147 148 149
    public function run() 
    {
        foreach (get_class_methods($this) as $method) {
            if (substr($method, 0, 4) === 'test') {
                $this->setUp();

                $this->$method();
            }
        }
    }
zYne's avatar
zYne committed
150 151 152 153
    public function getMessages() 
    {
        return $this->_messages;
    }
zYne's avatar
zYne committed
154 155 156 157 158 159 160 161 162
    public function getFailCount()
    {
        return $this->_failed;
    }
    public function getPassCount()
    {
        return $this->_passed;
    }
}