17 July 2009

PHPUnit Custom Extension - Class ... could not be found in ...

(For the impatient, include your class early, that is the solution.)

So I have been using PHPUnit for quite some time now and find that it is a great library for testing. I also find that testing really helps in development. I also find that PHPUnit can be infuriating to use at times. For instance, I wanted to create a new base class that extendeds PHPUnit_Framework_TestCase so I can add some custom logging or something like that to all of my tests. So I simply create,
class lib_MyBaseUnitTest extends PHPUnit_Framework_TestCase {
    ....
}
Then I create my test cases,
class tests_MyFirstTest extends lib_MyBaseUnitTest {
    ....
}
Now whenever I run
phpunit tests/MyFirstTest.php
 I get the error:
Class lib_MyBaseUnitTest could not be found in tests/MyFirstTest.php
So clearly from the error the base class in not being loaded...? Actually, the problem is that the base class is being loaded by the autoloader when phpunit tries to load the test and the base class gets in the way. The phpunit code that causes the problem is located in StandardTestSuiteLoader.php.
PHPUnit_Util_Class::collectStart();
PHPUnit_Util_Fileloader::checkAndLoad($suiteClassFile, $syntaxCheck);
$loadedClasses = PHPUnit_Util_Class::collectEnd();
The collectStart call gathers the list of loaded classes and stores them in memory. The checkAndLoad call then includes each of the files listed in your suite, or the file specified on the commandline, thus loading more classes into memory. The collectEnd call now gathers the new list of loaded classes and diffs the result with the list collected in the collectStart call. Each of the resulting classes should be a test case that needs to be run. The problem is that the autoloader loaded both the specified test case and its base class. When PHPUnit tests to see if it can run the base class as a test, it can't and therefore throws and exception, "Class ... can not be found in ...".

So the solution in the end that I have taken is to place require_once calls in my bootstrap file for all custom base classes. This way the class will be in the initial list of classes created by the collectStart call. It will not be in the $loadedClasses variable and therefore will not cause an exception to be thrown.

22 June 2009

Bearpaw Lake

I spent the last week in the Grand Teton Nation Park. It has been said that the Park is the Disneyland of the National Park system. This seemed to be accurate as there was fun to be had by myself, my wife and my young children. Everyone had a good time. While I did a lot of different things from riding in a covered wagon and watching an old west shoot out to white water rafting I will focus this post only on my hike to Bearpaw Lake.

Bearpaw Lake is the oblong blue spot on the map just north of Leigh Lake. I rode the ferry across Jenny Lake from the South Visitors Center to the Hidden Falls trail head. From there I followed the trail on the west side of Jenny Lake to the trail on the east side of String Lake and Leigh Lake and then up to Bearpaw Lake. I completed my hike at Jenny Lake Lodge.

The overall trip was about 10 miles. The trail was fairly flat and well maintained allowing for fast and easy hiking. The landscapes were beautiful and the lakes pristine. Wildlife was abundant, especially the mosquitoes. I almost ran over a bull moose eating the vegetation on the edge of the trail. He didn't seem to mind me being there at all, which I appreciated. I saw eagles soaring above me. I also saw a lot of humans.

I found it interesting to see how well maintained everything was. As I hiked I heard a chain saw cutting through some fallen trees. Evidence of this could be seen quite often along the trail. Campsites were organized and bear safe lockers were available at each site. Flags and markers were frequent to give direction and show detours. I was surprised at how unnaturally this part of nature was being preserved.

Overall this hike was very beautiful and enjoyable. Next time I am in the area though I will hike deeper into the mountains hoping for a more natural experience.