phpunit - Problem generating skeleton test classes [SOLVED]

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
Apex77
Forum Newbie
Posts: 3
Joined: Tue Nov 01, 2011 3:16 am

phpunit - Problem generating skeleton test classes [SOLVED]

Post by Apex77 »

Hello,

I am running into a problem using phpunit to generate test classes for classes that inherit from another class.

For example, if I have the following class :

class cow extends animal{

}

[text]phpunit skeleton-test cow [/text]

will not work.
If the cow class does not extend Animal, then phpunit will generate the test class as expected.
I have spent hours searching the web and numerous people have had similar problems but the solutions posted are not clear.

From what I can gather, there seems to be some issue with autoloading. Some solutions seem to imply that a bootstrap.php file needs to be loaded using phpunit --bootsrap <bootstrap file>
But this file isn't explained properly. I was under the impression that this file needed to contain my __autoload() function and my include path setup using set_include_path(), however this is not working.

My bootstrap.php file ended up looking like this:

Code: Select all

<?php
  set_include_path( ... a bunch of directories added here ...);
  function __autoload($class)
  {
    require $class . '.php';
  }
?>
Then at the command line I type the following

phpunit --bootstrap bootstrap.php skeleton-test cow

Nothing happens. No file is generated :(

I'm at a loss.
Help
Last edited by Apex77 on Tue Nov 01, 2011 8:10 pm, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: phpunit - Problem generating skeleton test classes

Post by Weirdan »

You need to register your autoload function with spl_autoload_register: http://php.net/manual/en/function.spl-a ... gister.php
Apex77
Forum Newbie
Posts: 3
Joined: Tue Nov 01, 2011 3:16 am

Re: phpunit - Problem generating skeleton test classes

Post by Apex77 »

Thanks Weirdan.

The doc for spl_autoload_register says the following:

spl_autoload_register — Register given function as __autoload() implementation

So based on that, I assume my bootstrap.php would end up looking like the following:
(I'll test when I get home from work)

Code: Select all

<?php
  set_include_path( ... a bunch of directories added here ...);
  function my_autoloader($class)
  {
    require $class . '.php';
  }
  
  spl_autoload_register('my_autoloader');
?>
Like so?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: phpunit - Problem generating skeleton test classes

Post by Weirdan »

yes
Apex77
Forum Newbie
Posts: 3
Joined: Tue Nov 01, 2011 3:16 am

Re: phpunit - Problem generating skeleton test classes

Post by Apex77 »

It seems to work. :D
Thank you very much for your help.
Post Reply