Jump to content

Struggling to see the value of PHPUnit?


timtamboy63

Recommended Posts

Hey people,

 

I recently decided to try out PHPUnit, and I'm trying to write a test for my Config class, but I have no idea what to test. Here's the code for my class:

 

<?php
/**
* Config class - manages reading the configuration from the set config files (for both the global config file and components)
* The global config file must be an ini (unless the implementation of Config::get() is changed), and the component config files are all XML files
* 
* @version 1.0
* @author Chintan Parikh
*
* Changelist: 
*/
class Config
{
/**
 * The location of the config directory
 */
const CONFIG_DIRECTORY = 'config/';

/**
 * The location of the component directory (must be under the config directory)
 */
const COMPONENT_DIRECTORY = 'components/';

/**
 * The filename of the global config file (located inside the config directry)
 */
const GLOBAL_CONFIG = 'config.ini';

/**
 * Gets a config item from the global config file and returns it
 */
public function get($item)
{
	$config = parse_ini_file(self::CONFIG_DIRECTORY . self::GLOBAL_CONFIG);
	return $config[$item];
}

/**
 * Gets all config items from a component xml file
 */
public function getComponent($component)
{
	$config = array();
	$xml = simplexml_load_file(self::CONFIG_DIRECTORY . self::COMPONENT_DIRECTORY . $component . '.config.shel');

	$vars = get_object_vars($xml);
	foreach ($vars as $key=>$value)
	{
		$name = $key;
	}

	if (!empty($name)) {
		// Remove the comments field (uncessary, and we don't need it))
		unset($xml->$name->comment);
		$config[$name] = get_object_vars($xml->$name);
	}

	return $vars;
}
}

 

Can anyone give me examples of what I should be testing, and if possible, some example PHPUnit code?

 

Cheers!

Link to comment
Share on other sites

You should be testing that your methods do what they are meant to do.

 

This class in particular is going to be hard to test because it has hardcoded dependencies on specific files and directories. This is one (amongst many) key benefits to unit testing, you need to write code that is easily testable which actually improves your design.

 

You should remove the hardcode constants from your class and instead make setter methods for these values. This way can can easily test your code by using mock config files. It will also meen that you will be able to use this class more easily in various projects that don't have this same hardcoded directory structure.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.