arjan.top wrote:a.) but how am I supposed to get html to parse?
Write your own

It probably helps to read RFCs for stuff like this and test it the way it's documented in the RFC. But basically just set up your tests with some HTML in them:
Code: Select all
public function testHyperlinksAreParsed() {
$html = 'This is HTML with <a href="http://foo.bar/path/?q=v">link one</a> and ' .
'<a class="test" href="http://www.test.com/">link two</a> in it.';
$parser = new HtmlParser();
$parser->parse($html);
//make assertions here
$this->assertEqual(array('http://foo.bar/path/?q=v', 'http://www.test.com/'),
$parser->getHyperlinks());
}
I'd be tempted to offer a Document interface you could mock:
Code: Select all
interface Document {
public function getHtml();
}
Then you could make the worry about downloading content as small as possible.
Code: Select all
class RemoteDocument implements Document {
private $_url;
public function __construct($url) {
$this->_url = $url;
}
public function getHtml() {
return file_get_contents($this->_url);
}
}
The advantage with that is that you can mock the interface and focus your parser on parsing HTML, not on downloading content. It;s a parser, not downloader right?
Using SimpleTest to mock that interface:
Code: Select all
Mock::generate('Document', 'MockDocument');
class HtmlParserTest extends UnitTestCase {
public function testParsingHyperlinks() {
$html = 'This is HTML with <a href="http://foo.bar/path/?q=v">link one</a> and ' .
'<a class="test" href="http://www.test.com/">link two</a> in it.';
$document = new MockDocument();
$document->setReturnValue('getHtml', $html);
$parser = new HtmlParser($document);
//make assertions here
$this->assertEqual(array('http://foo.bar/path/?q=v', 'http://www.test.com/'),
$parser->getHyperlinks());
}
}
I also write a little mock object library called Yay! Mock but it's very much in its infancy. You'd do the same as above like this:
Code: Select all
class HtmlParserTest extends UnitTestCase {
public function testParsingHyperlinks() {
$context = new Yay_Mockery();
$html = 'This is HTML with <a href="http://foo.bar/path/?q=v">link one</a> and ' .
'<a class="test" href="http://www.test.com/">link two</a> in it.';
$document = $context->mock('Document');
$context->checking(Yay_Expectations::create()
-> atLeast(1)->of($document)->getHtml() -> returns($html)
);
$parser = new HtmlParser($document);
//make assertions here
$this->assertEqual(array('http://foo.bar/path/?q=v', 'http://www.test.com/'),
$parser->getHyperlinks());
$context->assertIsSatisfied();
}
}
b.) so acceptance test is just set of operations tested at once?
Roughly speaking yes. I still try to focus them as much as possible, but I don't worry so much about external dependencies.