Page 1 of 1

Multiple Regular Expressions

Posted: Sat Jun 09, 2007 3:07 am
by jamiel
Hi there,

I have a different XML Document's which each require different regular expression's performed on them. These regular expressions are going to change over time. At the moment I have a base Parser class which does things which are common to all documents, then an additional class for each document to do the regular expression's which are unique to it. Regular Expressions are not the only manipulation we are doing on these documents.

At the moment my base class has a doRegexReplace() method and in my child classes for the documents I just have a couple $this->doRegexReplace() function calls. However I have some really long nasty replaces in here and because the matches and replace strings are in the code I have no way of writing Unit Tests to test these expressions. The source XML is changing all the time, so I would need to test that just a certain section changes to the new value. Anyone have any ideas where I can put my find -> replace terms to have easy access to them in my code and unit tests? I don't want to use an XML file because the matches and replace's contain XML aswell.

Thanks Jamie

Posted: Sat Jun 09, 2007 4:21 pm
by Ambush Commander
Is using DOM out of the question? This sounds like exactly the job for something like this.

Posted: Sat Jun 09, 2007 7:00 pm
by Ollie Saunders
Use the extract class refactor to remove the behaviour relevent to a find replace out into a separate class and unit test that class.

Posted: Sun Jun 10, 2007 4:39 am
by jamiel
Hey guys,

Thanks for the responses.

I will be using DOM to do alot of the actual parsing yes. My real question which I don't think I conveyed very well is that I am not sure where to put my actual regular expressions so that my Unit tests can use them. I don't really like the idea of having them directly in the code because some are really long. eg. This is how I am testing them at the moment

Code: Select all

// Example method to tet
public function alterHeader()
{
   $find = '/<div id="header">.*</div>/';
   $replace = '/<div id="alternate_header">My alternate header</div>';
   $this->body = preg_replace($find, $replace, $this->body);
}

//Unit test
public function testAlterHeader()
{
$this->object->alterHeader();
$this->assertTrue(strstr('alterate_header'), $this->object->getBody());
}
Just example code so imagine some big changes, nasty huh? I need some guidance :)

Thanks

Jamie

Posted: Sun Jun 10, 2007 5:13 am
by Ollie Saunders
The assertion here is very unclear and you have the wrong number of parameters for strstr

Code: Select all

$this->assertTrue(strstr('alterate_header'), $this->object->getBody());
You should also call setBody() in the test method and use a random value. Other than that I think it's all right.