Changeset 2295

Show
Ignore:
Timestamp:
03/12/2008 03:19:30 PM (7 months ago)
Author:
Geert
Message:

Validate setup() and teardown() methods before calling them.

Location:
trunk/modules/unit_test
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/unit_test/libraries/Unit_Test.php

    r2294 r2295  
    8383                                        throw new Kohana_Exception('unit_test.test_class_extends', $class); 
    8484 
     85                                // Initialize setup and teardown method triggers 
     86                                $setup = $teardown = FALSE; 
     87 
     88                                // Look for valid setup and teardown methods 
     89                                foreach (array('setup', 'teardown') as $method_name) 
     90                                { 
     91                                        if ($reflector->hasMethod($method_name)) 
     92                                        { 
     93                                                $method = new ReflectionMethod($class, $method_name); 
     94                                                $$method_name = ($method->isPublic() AND ! $method->isStatic() AND $method->getNumberOfRequiredParameters() === 0); 
     95                                        } 
     96                                } 
     97 
    8598                                // Initialize test class results and stats 
    8699                                $this->results[$class] = array(); 
     
    91104                                { 
    92105                                        // Skip invalid test methods 
    93                                         if ( ! $method->isPublic() OR $method->isStatic() OR $method->getNumberOfParameters() != 0) 
     106                                        if ( ! $method->isPublic() OR $method->isStatic() OR $method->getNumberOfRequiredParameters() !== 0) 
    94107                                                continue; 
    95108 
    96109                                        // Test methods should be suffixed with "_test" 
    97                                         if (substr($method_name = $method->getName(), -5) != '_test') 
     110                                        if (substr($method_name = $method->getName(), -5) !== '_test') 
    98111                                                continue; 
    99112 
     
    102115 
    103116                                        // Run setup method 
    104                                         if ($reflector->hasMethod('setup')) 
     117                                        if ($setup === TRUE) 
    105118                                        { 
    106119                                                $object->setup(); 
     
    130143 
    131144                                        // Run teardown method 
    132                                         if ($reflector->hasMethod('teardown')) 
     145                                        if ($teardown === TRUE) 
    133146                                        { 
    134147                                                $object->teardown(); 
  • trunk/modules/unit_test/tests/Example_Test.php

    r2293 r2295  
    1111 */ 
    1212class Example_Test extends Unit_Test_Case { 
     13 
     14        public $setup_has_run = FALSE; 
    1315 
    1416        public function setup()