Silverbox:Enigma wechsler$ svn co http://services.phase.org/svn/tdd-enigma/trunk/ tdd-enigma A tdd-enigma/tests A tdd-enigma/tests/Enigma A tdd-enigma/library A tdd-enigma/library/Enigma A tdd-enigma/cli A tdd-enigma/docs Checked out revision 1.and dive right into the tests. As noted before, in pure TDD, we test first and
So, what on earth's going on here? Well, we've written a PHPUnit test case that will confirm now, and at any point in the future, that creating a rotor gives us a class that implements the Enigma_Encryptor_Interface. If this code works, we keep it, and in two years when we need to see if our simulator still works, we can run it to make sure.
<?php class Enigma_RotorTest extends PHPUnit_Framework_TestCase { public function testRotorIsAnEncrytor () { $rotor = new Enigma_Rotor(); $this->assertTrue($rotor instanceof Enigma_Encryptor_Interface); } }
PHPUnit_Framework_TestCase: To create a test case, simply create a class that extends this class - all the rest of the work will be done for you.public function test...: Any functions starting with the word "test" in a test case are automatically run when you trigger PHPUnit.$this->assertTrue(): Extending PHPUnit_Framework_TestCase gives your class a number of assert* functions, which you use to confirm that your code is doing what's expected.phpunit RotorTest.phpDo not run
php RotorTest.php Only twits who haven't had enough coffee do that, and then wonder why the output's blank. I do it about twice a week.Silverbox:Enigma wechsler$ phpunit RotorTest.php PHPUnit 3.4.5 by Sebastian Bergmann. Fatal error: Class 'Enigma_Rotor' not found in /Library/WebServer/phaseTwo/enigmaProject/tests/Enigma/RotorTest.php on line 7Wait a sec - that's a Fatal Error. How is that everything going to plan? Well, as we haven't written the class Enigma_Rotor, that's the Expected Behaviour. If it worked, we'd have reason to be scared.
Encryptor/Interface.php:
<?php class Enigma_Rotor implements Enigma_Encryptor_Interface {}
And that'll do. No functions yet, for now we're deliberately stretching the KISS (Keep It Simple, Stupid) principle to breaking point. Right now, we just want our tests to run and pass. Detail comes later.
<?php interface Enigma_Encryptor_Interface {}
phpunit RotorTest.php. And we'll get exactly the same result as last time, because our test code has no idea whatsoever how to find the class and interface files.and run
require_once('../../library/Enigma/Encryptor/Interface.php'); require_once('../../library/Enigma/Rotor.php');
phpunit RotorTest.php again:Silverbox:Enigma wechsler$ phpunit RotorTest.php PHPUnit 3.4.5 by Sebastian Bergmann. . Time: 0 seconds, Memory: 5.75Mb OK (1 test, 1 assertion)Wow! Success! Our first ever unit test passed!
And in library/initialise.php, add:
<?php class Tools_Autoloader { public function loadClass($class) { { throw new Exception('LIBRARY_ROOT must be defined'); } $classFile=LIBRARY_ROOT.DIRECTORY_SEPARATOR.$classPath.'.php'; require($classFile); } } }
And finally, we'll use this in our test script instead of those requires. In tests/Enigma/RotorTest.php, replace the two require()s with:
<?php require_once(LIBRARY_ROOT. DIRECTORY_SEPARATOR . 'Tools' . DIRECTORY_SEPARATOR . 'Autoloader.php');
and re-run the tests. They should pass, and we've finished our first complete Red-Green-Refactor cycle and so we're ready to check the code in. Our autoloader's not perfect, but it'll certainly do us for now.
require_once('../../library/initialise.php');
wechsler$ svn commit -m 'First red-green cycle & autoloader' Sending . Adding library/Enigma/Encryptor Adding library/Enigma/Encryptor/Interface.php Adding library/Enigma/Rotor.php Adding library/Tools Adding library/Tools/Autoloader.php Adding library/initialise.php Adding tests/Enigma/RotorTest.php Transmitting file data ..... Committed revision 2.In part 5, we'll take a closer look at this ethos of modular testing, and explain why it's not quite as artificial as our first example might make it look!
All content copyright Richard George (richard@phase.org), 2009-2010
Sponsored links to recommended books: