Test.php 5 KB
Newer Older
zYne's avatar
zYne committed
1
<?php
zYne's avatar
zYne committed
2
class GroupTest extends UnitTestCase
zYne's avatar
zYne committed
3 4 5
{
    protected $_testCases = array();

zYne's avatar
zYne committed
6 7 8 9 10 11 12
    public function __construct()
    {
        if (extension_loaded('xdebug')) {
            //xdebug_start_code_coverage(XDEBUG_CC_DEAD_CODE | XDEBUG_CC_UNUSED);
        }
    }

zYne's avatar
zYne committed
13 14
    public function addTestCase(UnitTestCase $testCase)
    {
15 16 17
        if($testCase instanceOf GroupTest) {
            $this->_testCases = array_merge($this->_testCases, $testCase->getTestCases());
         } else {
18
            $this->_testCases[get_class($testCase)] = $testCase;
19
         }
zYne's avatar
zYne committed
20
    }
21

22
    public function shouldBeRun($testCase, $filter){
23
        if( ! is_array($filter)) {
24 25
            return true;
         }
26
        foreach($filter as $subFilter) {
27 28 29
            $name = strtolower(get_class($testCase));
            $pos = strpos($name, strtolower($subFilter));
            //it can be 0 so we have to use === to see if false
30
            if ($pos === false) {
31 32 33 34 35
                return false;
             }
        }
        return true;
    }
36
    public function run(HtmlReporter $reporter = null, $filter = null)
zYne's avatar
zYne committed
37
    {
38
        $reporter->paintHeader();
39
        foreach ($this->_testCases as $k => $testCase) {
40 41 42
            if ( ! $this->shouldBeRun($testCase, $filter)) {
                continue;
            }
43 44
            try{
                $testCase->run();
meus's avatar
meus committed
45
            } catch(Exception $e) {
46 47 48
                $this->_failed += 1;
                $this->_messages[] = "Unexpected exception thrown with message [" . $e->getMessage() . "] in " . get_class($testCase) . " on line " . $e->getLine();
            }
49 50
            $this->_passed += $testCase->getPassCount();
            $this->_failed += $testCase->getFailCount();
51
            $this->_messages = array_merge($this->_messages, $testCase->getMessages());
zYne's avatar
zYne committed
52

53
            $this->_testCases[$k] = null;
meus's avatar
meus committed
54 55 56 57
            if(PHP_SAPI === "cli"){
                echo ".";
            }
            set_time_limit(900);
58
        }
zYne's avatar
zYne committed
59 60 61 62
        $reporter->setTestCase($this);
        
        $reporter->paintFooter();
    }
zYne's avatar
zYne committed
63 64


zYne's avatar
zYne committed
65 66 67 68
    public function getTestCaseCount()
    {
        return count($this->_testCases);
    }
zYne's avatar
zYne committed
69

70 71 72
    public function getTestCases(){
        return $this->_testCases;
    }
zYne's avatar
zYne committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
}
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
88 89
    
    protected $_messages = array();
zYne's avatar
zYne committed
90 91 92 93 94 95

    public function assertEqual($value, $value2)
    {
        if ($value == $value2) {
            $this->_passed++;
        } else {
96 97 98 99 100 101
            $seperator = "<br>";
            if(PHP_SAPI === "cli"){
                $seperator = "\n";
             }
            $message = "$seperator Value1: $value $seperator != $seperator Value2: $value2 $seperator";
            $this->_fail($message);
zYne's avatar
zYne committed
102 103
        }
    }
104 105 106 107 108 109 110 111 112 113

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

zYne's avatar
zYne committed
114 115 116 117 118
    public function assertNotEqual($value, $value2)
    {
        if ($value != $value2) {
            $this->_passed++;
        } else {
119
            $this->_fail();
zYne's avatar
zYne committed
120 121 122 123 124 125 126
        }
    }
    public function assertTrue($expr)
    {
        if ($expr) {
            $this->_passed++;
        } else {
127
            $this->_fail();
zYne's avatar
zYne committed
128 129
        }
    }
zYne's avatar
zYne committed
130 131 132 133 134
    public function assertFalse($expr)
    {
        if ( ! $expr) {
            $this->_passed++;
        } else {
135
            $this->_fail();
zYne's avatar
zYne committed
136 137 138 139 140 141
        }
    }
    public function pass() 
    {
        $this->_passed++;
    }
142
    public function fail($message = "")
143
    {
144
        $this->_fail($message);    
145
    }
146
    public function _fail($message = "")
zYne's avatar
zYne committed
147
    {
148 149
        $trace = debug_backtrace();
        array_shift($trace);
zYne's avatar
zYne committed
150 151 152 153 154 155


        foreach ($trace as $stack) {
            if (substr($stack['function'], 0, 4) === 'test') {
                $class = new ReflectionClass($stack['class']);

zYne's avatar
zYne committed
156
                if ( ! isset($line)) {
157
                    $line = $stack['line'];
zYne's avatar
zYne committed
158 159
                }

160
                $errorMessage = $class->getName() . ' : method ' . $stack['function'] . ' failed on line ' . $line;
161
                $this->_messages[] =  $errorMessage . " " . $message;
zYne's avatar
zYne committed
162 163 164
                break;
            }
            $line = $stack['line'];
165
        }
zYne's avatar
zYne committed
166 167
        $this->_failed++;
    }
168
    public function run(HtmlReporter $reporter = null, $filter = null) 
zYne's avatar
zYne committed
169 170 171 172 173 174 175 176 177
    {
        foreach (get_class_methods($this) as $method) {
            if (substr($method, 0, 4) === 'test') {
                $this->setUp();

                $this->$method();
            }
        }
    }
zYne's avatar
zYne committed
178 179 180 181
    public function getMessages() 
    {
        return $this->_messages;
    }
zYne's avatar
zYne committed
182 183 184 185 186 187 188 189 190
    public function getFailCount()
    {
        return $this->_failed;
    }
    public function getPassCount()
    {
        return $this->_passed;
    }
}