timtamboy63 Posted March 31, 2012 Share Posted March 31, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/260052-struggling-to-see-the-value-of-phpunit/ Share on other sites More sharing options...
trq Posted March 31, 2012 Share Posted March 31, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/260052-struggling-to-see-the-value-of-phpunit/#findComment-1332932 Share on other sites More sharing options...
timtamboy63 Posted March 31, 2012 Author Share Posted March 31, 2012 Ah right, that makes sense. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/260052-struggling-to-see-the-value-of-phpunit/#findComment-1332948 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.