Writing complex "helper" methods into testcases?
Posted: Mon Mar 05, 2007 12:20 am
I'm at a point where I think I need to seriously refactor a few of my testcases because they repeatedly use long, complicated regexes like this:
The regex are:
a) A vital annoyance to test what I need
b) COMPLICATED!!
I'd like to simplify, and make the tests more readable to "Average Joe" by allowing something like XML or a native multi-dimensional array to generate those regex, but that's complicated in itself so I'd then need to add a handful of methods which tests the helper functions of my test case before I ever use the helper functions. Does this seem reasonable or am I just adding to the risk of over-complicating things?
I haven't even begun to pull hairs out thinking about actually creating those regex in a flexible fashion
Code: Select all
public function testDetachingALLPartsFromAnEmailWithAnAttachmentResultsInCorrectStructure()
{
$msg = new Swift_Message();
$id1 = $msg->attach(new Swift_Message_Part("some part"));
$id2 = $msg->attach(new Swift_Message_Part("another part"));
$id3 = $msg->attach(new Swift_Message_Attachment("foobar"));
$id4 = $msg->attach(new Swift_Message_Attachment("zipbutton"));
$msg->detach($id2);
$structure = $msg->build()->readFull();
//$this->dump($structure);
$this->assertPattern(
"~.*?Content-Type: multipart/mixed;\\s* boundary=(\"?)(.*?)\\1\r\n" .
".*?\r\n\r\n" .
".*?\r\n--\\2\r\n" .
"Content-Type: multipart/alternative;\\s* boundary=(\"?)(.*?)\\3\r\n" .
".*?\r\n\r\n" .
".*?\r\n--\\4\r\n" .
".*?\r\n\r\n" .
".*?\r\n--\\4--" .
"\\s*\r\n--\\2\r\n" .
".*?\r\n\r\n" .
".*?\r\n--\\2\r\n" .
".*?\r\n\r\n" .
".*?\r\n--\\2--~s",
$structure);
$msg->detach($id1);
$structure = $msg->build()->readFull();
//$this->dump($structure);
$this->assertNoPattern("~Content-Type: multipart/alternative~", $structure);
$this->assertNoPattern(
"~.*?Content-Type: multipart/mixed;\\s* boundary=(\"?)(.*?)\\1\r\n" .
".*?\r\n\r\n" .
".*?\r\n--\\2\r\n" .
"Content-Type: multipart/alternative;.*?\r\n" .
".*?\r\n\r\n" .
".*?\r\n--\\2\r\n" .
".*?\r\n\r\n" .
".*?\r\n--\\2--~s",
$structure);
}a) A vital annoyance to test what I need
b) COMPLICATED!!
I'd like to simplify, and make the tests more readable to "Average Joe" by allowing something like XML or a native multi-dimensional array to generate those regex, but that's complicated in itself so I'd then need to add a handful of methods which tests the helper functions of my test case before I ever use the helper functions. Does this seem reasonable or am I just adding to the risk of over-complicating things?
I haven't even begun to pull hairs out thinking about actually creating those regex in a flexible fashion