Page 1 of 1

Guard code around Mock::generate?

Posted: Wed Aug 31, 2005 8:59 pm
by nielsene
I've just realized that there are several classes that are getting Mock'd a lot of places, which is probably cause the tests to take a little longer to run then they should. Do you wrap any sort of guard clause around your Mock::generates statements, somethling like

Code: Select all

$mockClasses = array("Request","Handler");
foreach ($mockClasses as $className) 
  if (!class_exists("Mock$className",false))  Mock::generate("$className");

Posted: Wed Aug 31, 2005 9:33 pm
by sweatje
No need, this is being checked internal to the Mock::generate() method anyway:

Code: Select all

/**
         *    Clones a class' interface and creates a mock version
         *    that can have return values and expectations set.
         *    @param string $class         Class to clone.
         *    @param string $mock_class    New class name. Default is
         *                                 the old name with "Mock"
         *                                 prepended.
         *    @param array $methods        Additional methods to add beyond
         *                                 those in th cloned class. Use this
         *                                 to emulate the dynamic addition of
         *                                 methods in the cloned class or when
         *                                 the class hasn't been written yet.
         *    @static
         *    @access public
         */
        function generate($class, $mock_class = false, $methods = false) {
            if (! class_exists($class)) {
                return false;
            }
            if (! $mock_class) {
                $mock_class = "Mock" . $class;
            }
            if (class_exists($mock_class)) {
                return false;
            }
            return eval(Mock::_createClassCode(
                    $class,
                    $mock_class,
                    $methods ? $methods : array()) . " return true;");
        }

Posted: Wed Aug 31, 2005 9:37 pm
by nielsene
Hmm, cool. Shouldn't the call to class_exist($mock_class) include the second parameter to stop an autoload attempt for the mock class?

Posted: Wed Aug 31, 2005 11:27 pm
by sweatje
Might be worth posting to the development list. Marcus is underway with a pretty significant rewrite of the MockObject interface, particularly targeting PHP5. Not sure how much he pays attention to the autoload functionality though (I know I do not use it at all, and therefore it is a bit of a blind spot for me).