SimpleTest

Tests

The framework used for tests is the SimpleTest unit tester.
The documentation for tests is quite explained and available at http://www.simpletest.org/.

Tests Folder

Every test should be included inside Tests folder.
The menu for test is automatically generated getting the name of the files inserted on it.
If a file starts with _, it will not be listed. This way, you can include files without displaying them.

Example

Here is an example of test file:

<pre>
<?php
//	ini_set('display_errors', 1);
//	error_reporting(E_ALL);

	SimpleTest::prefer(new TextReporter());
	include("_MagratheaConfig.php");
?>
</pre>		
_MagratheaConfig.php file is:
<?php
class TestOfStaticConfig extends UnitTestCase{

	function setUp(){
	}
	function tearDown(){
	}

	// load a section in Static Config
	// I check if the section that it returns is an array:
	function testLoadSectionStaticConfig(){
		echo "testing magratheaConfig loading static config...";
		$thisSection = MagratheaConfig::Instance()->GetConfigSection("general");
		$this->assertIsA($thisSection, "array");
	}

	// config file must have a default environment option
	function testConfigShouldHaveADefaultEnvironment(){
		echo "testing magratheaConfig confirming we have a default...";
		$env = MagratheaConfig::Instance()->GetEnvironment();
		$this->assertNotNull($env);
	}

	// required fields
	function testConfigRequiredFields(){
		echo "testing magratheaConfig checking required fields...";
		$env = MagratheaConfig::Instance()->GetConfig("general/use_environment");
		$site_path = MagratheaConfig::Instance()->GetConfig($env."/site_path");
		$magrathea_path = MagratheaConfig::Instance()->GetConfig($env."/magrathea_path");
		$compress_js = MagratheaConfig::Instance()->GetConfig($env."/compress_js");
		$compress_css = MagratheaConfig::Instance()->GetConfig($env."/compress_css");
		$this->assertNotNull($site_path);
		$this->assertNotNull($magrathea_path);
		$this->assertNotNull($compress_js);
		$this->assertNotNull($compress_css);
	}

	function testReturnFalseWhenItemDoesNotExists(){
		echo "testing magratheaConfig should return false if item doesn't exists...";
		try {
			$thisIsFalse = MagratheaConfig::Instance()->GetFromDefault("idontexist");
			$this->fail("I should get an exception");
		} catch(Exception $e) {
			$this->assertEqual($e->getCode(), 704);
			$this->pass("exception ok!");
		}
	}

}
?>

Keep testing...

It's a good programming practice to test your code.
For more information about testing and TDD, you can study Mauricio Aniche work: