/ */ class P4Cms_Filter_MacroTest extends TestCase { /** * Test the constructor's ability to accept options. */ public function testConstructor() { // try with no argument $filter = new P4Cms_Filter_Macro; $this->assertTrue($filter instanceof P4Cms_Filter_Macro); $this->assertSame(null, $filter->getContext(), 'Expected null context'); // try with an argument $expected = array( 'foo' => 'bar', 'object' => new stdClass ); $filter = new P4Cms_Filter_Macro($expected); $this->assertTrue($filter instanceof P4Cms_Filter_Macro); $this->assertSame($expected, $filter->getContext(), 'Expected array context'); } /** * Test setContext. */ public function testSetContext() { $tests = array( array( 'label' => __LINE__ .': null', 'context' => null, ), array( 'label' => __LINE__ .': empty array', 'context' => array(), ), array( 'label' => __LINE__ .': good array', 'context' => array( 'foo' => 'bar', 'object' => new stdClass, ), ), ); foreach ($tests as $test) { $label = $test['label']; $filter = new P4Cms_Filter_Macro; $filter->setContext($test['context']); $this->assertSame($test['context'], $filter->getContext(), "$label - Expected context"); } } /** * Test the use of macros in content. */ public function testFilter() { $testText = << array( array ( 'params' => array(), 'body' => NULL, 'content' => 'P4Cms_Content', 'element' => 'Zend_Form_Element', ), ), 'macro2' => array( array ( 'params' => array('arg'), 'body' => NULL, 'content' => 'P4Cms_Content', 'element' => 'Zend_Form_Element', ), ), 'macro3' => array( array ( 'params' => array('arg1', 'arg2'), 'body' => NULL, 'content' => 'P4Cms_Content', 'element' => 'Zend_Form_Element', ), ), 'if' => array( array( 'params' => array('test'), 'body' => NULL, 'content' => 'P4Cms_Content', 'element' => 'Zend_Form_Element', ), array( 'params' => array (), 'body' => 'test body', 'content' => 'P4Cms_Content', 'element' => 'Zend_Form_Element', ), ), ); // outside place to hold result of all calls $macroCalls = array(); // for each of the possible macros (other than 'unhandled') sign up a // pub/sub subscriber that will log the call and return a hashed output array_map( function($macro) use (&$macroCalls) { P4Cms_PubSub::subscribe( P4Cms_Filter_Macro::TOPIC . $macro, function($params, $body, $context) use (&$macroCalls, $macro) { $content = get_class($context['content']); $element = get_class($context['element']); $input = compact('params', 'body', 'content', 'element'); $macroCalls[$macro][] = $input; return md5(var_export($input, true)); } ); }, array( 'macros', 'macro2', 'macro3', 'if', '/if', 'literal', '/literal', 'unpaired', '/unpaired' ) ); $filter = new P4Cms_Filter_Macro; $filter->setContext( array( 'content' => new P4Cms_Content, 'element' => new Zend_Form_Element('test') ) ); $output = $filter->filter($testText); $this->assertSame( $expectedOutput, $output, 'Expected expanded output to match' ); $this->assertSame( $expectedMacroCalls, $macroCalls, 'Expected matching macro calls' ); } }