...or 'Blechhhh', as the case may be...
I've been using
PHP since the relative Pleistocene (I recently found a PHP3 script I wrote in '99). I've been using and evangelising test-driven development (TDD) for about the last five years, usually with most such work being done in C++, Java, Python or other traditionally non-Web languages (with PHP really only being amenable to that since PHP 5 in 2004).
So here I am, puttering away on a smallish PHP project that I've decided to TDD from the very beginning. For one of the classes, I throw together a couple of simple constructor tests in
PHPUnit, to start, such as:
require_once( 'PHPUnit/Framework.php' );
require_once( '../scripts/foo.php' );
class FooTest extends PHPUnit_Framework_TestCase
public function testCanConstructBasic();
{
$Foo = new Foo( 'index.php' );
}
public function testCanConstructBasicWildcard()
{
$Foo = new Foo( '*.php' );
}
};
And, as is right and proper, I code the minimal class necessary to make that pass:
class Foo
{
};
That's it. That's really
it. No declaration whatever for the constructor or any other methods in the class. Since it doesn't subclass something else, we can't just say "oh, there might be a constructor up the tree that matches the call semantics." PHPUnit will take these two files and happily pass the tests.
I understand what's really going on here - since the class is empty, you've just defined a name without defining any usage semantics (including construction). I would say fine; not a problem. But I would think that
PHPUnit should, if not give an error, then at least have some sort of diagnostic saying "Hey, you're constructing this object, but there are no ctor semantics defined for the class." I can see people new to PHP and/or TDD, who are maybe just working through and mentally adapting an xUnit tutorial from somewhere, getting really confused by this. I know I did a double-take when I opened the source file to add a new method (to pass a test not shown above) and saw
nothing between the curly braces. On one level, very cool stuff. On another, equally but not always obviously important level, more than
enough rope for you to shoot yourself in the foot.
Or, to put it another way, even though I've been writing in dynamic languages
off and on for ages, I still tend to think in incompletely dynamic ways. Sometimes this comes back and bites me. Beware: here be (reasonably friendly, under the circumstances) dragons.
No comments:
Post a Comment