Guard code around Mock::generate?

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Guard code around Mock::generate?

Post 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");
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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;");
        }
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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?
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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).
Post Reply