class Documentation
{
private $_text;
public function __construct($text)
{
$this->_text = $text;
}
public function generate()
{
return new \DOMElement('documentation', $this->_text);
}
}
The obvious solution I can think of is to create new DOMDocument, append the result of generate() function call and to compare with expected element using $this->assertEqualXMLStructure, but for some reason I don’t like it and am sure there are alternatives.
Any ideas?
UPD: seems like I’ve missed something important: what I want to make sure of is that the element of a particular type with a particular content is returned. How to do that?
UPD 2:
this is what I currently could create, but it’s ugly, isn’t it?
public function testGenerate()
{
$expected = new \DOMDocument();
$expected->loadXML('<?xml version="1.0" encoding="utf-8"?><documentation>foo</documentation>');
$documentation = new Documentation('foo');
$actual = new \DOMDocument('1.0', 'utf-8');
$actual->appendChild($documentation->generate());
$this->assertEqualXMLStructure($expected, $actual);
}
It’s such a simple class that there’s hardly anything that could possibly go wrong with it. There’s no branching in the code at all, and all the methods have a cyclomatic complexity of 1. There’s really no need to write a test suite for such a simple class.
However, you could use PHPUnit to assert that the generate() method returns a DOMElement object and that the child node of that element is a text object and that the text object matches the input text.
There’s just not a lot of point, really.
EDIT TO ADD: This is an example method for doing the test (assuming PHPUnit as the test runner). It’s not tested so the syntax may be wrong but it should give you an idea of the test procedure.
As you can see, this method is longer than the class being tested! I’m a big fan of unit testing, but in this particular case it seems to be overkill. Unless you have a code-coverage quota you must hit, or unless you’re especially cautious and want some assurance regarding your class, I just wouldn’t bother in this particular case.