| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 10 require_once 'limb/wact/src/compiler/templatecompiler.inc.php'; 11 12 Mock::generate('WactCompileTreeNode','MockWactCompileTreeNode'); 13 Mock::generate('WactCompiler','MockWactCompiler'); 14 15 class WactNodeBuilderTestTag extends WactCompilerTag{} 16 17 class WactNodeBuilderTestProperty extends WactCompilerProperty 18 { 19 function isConstant() 20 { 21 return true; 22 } 23 24 function getValue() 25 { 26 return 'my_property_value'; 27 } 28 } 29 30 class WactNodeBuilderTestFilter extends WactCompilerFilter{} 31 32 33 class WactTreeBuilderTest extends UnitTestCase 34 { 35 protected $compiler; 36 protected $tree_builder; 37 protected $component; 38 protected $tag_dictionary; 39 protected $property_dictionary; 40 protected $filter_dictionary; 41 42 function setUp() 43 { 44 $this->compiler = new MockWactCompiler(); 45 $this->tag_dictionary = new WactTagDictionary(); 46 $this->property_dictionary = new WactPropertyDictionary(); 47 $this->filter_dictionary = new WactFilterDictionary(); 48 49 $this->component = new WactCompilerTag(new WactSourceLocation('my_file', 1), 50 $tag_name = 'my_tag', 51 new WactTagInfo($tag_name, 'MyTagClass')); 52 $this->tree_builder = new WactTreeBuilder($this->compiler, $this->tag_dictionary, $this->property_dictionary, $this->filter_dictionary); 53 $this->tree_builder->setCursor($this->component); 54 } 55 56 function testPushNodeMakedPushedNodeCurrentCursor() 57 { 58 $this->assertEqual($this->component->getChildren(), array()); 59 $this->assertReference($this->component, $this->tree_builder->getCursor()); 60 61 $child_component = new MockWactCompileTreeNode(); 62 $child_component->expectOnce('preParse', array($this->compiler)); 63 64 $this->tree_builder->pushNode($child_component); 65 66 $this->assertReference($child_component, $this->tree_builder->getCursor()); 67 $children = $this->component->getChildren(); 68 $this->assertReference($children[0], $child_component); 69 } 70 71 function testAddNodeDontChangeCursor() 72 { 73 $this->assertEqual($this->component->getChildren(), array()); 74 $this->assertReference($this->component, $this->tree_builder->getCursor()); 75 76 $child_component = new MockWactCompileTreeNode(); 77 $child_component->expectOnce('preParse'); 78 79 $this->tree_builder->addNode($child_component); 80 81 $this->assertReference($this->component, $this->tree_builder->getCursor()); 82 $children = $this->component->getChildren(); 83 $this->assertReference($children[0], $child_component); 84 } 85 86 function testAddWactTextNode() 87 { 88 $this->assertReference($this->component, $this->tree_builder->getCursor()); 89 90 $this->tree_builder->addWactTextNode('text'); 91 92 $this->assertReference($this->component, $this->tree_builder->getCursor()); 93 $children = $this->component->getChildren(); 94 $this->assertEqual(sizeof($children), 1); 95 $this->assertIsA($children[0], 'WactTextNode'); 96 $this->assertEqual($children[0]->getText(), 'text'); 97 } 98 99 function testPopNodeChangeCursorToParent() 100 { 101 $this->assertReference($this->component, $this->tree_builder->getCursor()); 102 103 $parent_component = new WactCompileTreeNode(); 104 $this->component->parent = $parent_component; 105 106 $this->tree_builder->popNode(); 107 $TreeBuilderCursor = $this->tree_builder->getCursor(); 108 109 $this->assertTrue($this->component->hasClosingTag); 110 $this->assertReference($parent_component, $this->tree_builder->getCursor()); 111 } 112 113 function testPopExpectedTagWithoutAnyExpected() 114 { 115 $location = new WactSourceLocation('my_file', 10); 116 117 try 118 { 119 $this->tree_builder->popExpectedWactTag('tag2', $location); 120 } 121 catch(WactException $e) 122 { 123 $this->assertWantedPattern('/Lonely closing tag/', $e->getMessage()); 124 $params = $e->getParams(); 125 $this->assertEqual($params['file'], 'my_file'); 126 $this->assertEqual($params['line'], 10); 127 $this->assertEqual($params['tag'], 'tag2'); 128 } 129 } 130 131 function testPairPushAndPopTheSameTagWorksOk() 132 { 133 $whatever_location = new WactSourceLocation('my_file', 1); 134 135 $open_location = new WactSourceLocation('my_file', 10); 136 $close_location = new WactSourceLocation('my_file', 12); 137 138 $this->tree_builder->pushExpectedPlainTag('other_tag', $whatever_location); 139 $this->tree_builder->pushExpectedPlainTag('tag', $open_location); 140 $this->tree_builder->popExpectedPlainTag('tag', $close_location); 141 142 $this->assertEqual($this->tree_builder->getExpectedTagCount(), 1); 143 $this->assertEqual($this->tree_builder->getExpectedTag(), 'other_tag'); 144 } 145 146 function testPopPlainTagSearchesNearestOpeningTag() 147 { 148 $first_location = new WactSourceLocation('my_file', 1); 149 $open_location = new WactSourceLocation('my_file', 10); 150 $second_location = new WactSourceLocation('my_file', 11); 151 $close_location = new WactSourceLocation('my_file', 12); 152 153 $this->tree_builder->pushExpectedPlainTag('first_tag', $first_location); 154 $this->tree_builder->pushExpectedPlainTag('our_tag', $open_location); 155 $this->tree_builder->pushExpectedPlainTag('plain_tag', $second_location); 156 157 $this->tree_builder->popExpectedPlainTag('our_tag', $close_location); 158 159 $this->assertEqual($this->tree_builder->getExpectedTagCount(), 1); 160 $this->assertEqual($this->tree_builder->getExpectedTag(), 'first_tag'); 161 } 162 163 function testPopPlainTagStopsSearchesNearestOpeningTagIfFoundComponentTag() 164 { 165 $first_location = new WactSourceLocation('my_file', 1); 166 $open_location = new WactSourceLocation('my_file', 9); 167 $second_location = new WactSourceLocation('my_file', 10); 168 $third_location = new WactSourceLocation('my_file', 11); 169 $close_location = new WactSourceLocation('my_file', 12); 170 171 $this->tree_builder->pushExpectedPlainTag('first_tag', $first_location); 172 $this->tree_builder->pushExpectedPlainTag('our_tag', $open_location); 173 $this->tree_builder->pushExpectedWactTag('component_tag', $second_location); 174 $this->tree_builder->pushExpectedPlainTag('plain_tag', $third_location); 175 176 $this->assertEqual($this->tree_builder->popExpectedPlainTag('our_tag', $close_location), true); 177 178 $this->assertEqual($this->tree_builder->getExpectedTagCount(), 3); 179 $this->assertEqual($this->tree_builder->getExpectedTag(), 'component_tag'); 180 $this->assertReference($this->tree_builder->getExpectedTagLocation(), $second_location); 181 } 182 183 function testPopComponentTagStopsSearchesNearestOpeningTagIfFoundComponentTagAndThrowsException() 184 { 185 $first_location = new WactSourceLocation('my_file', 1); 186 $open_location = new WactSourceLocation('my_file', 9); 187 $second_location = new WactSourceLocation('my_file', 10); 188 $third_location = new WactSourceLocation('my_file', 11); 189 $close_location = new WactSourceLocation('my_file', 12); 190 191 $this->tree_builder->pushExpectedPlainTag('first_tag', $first_location); 192 $this->tree_builder->pushExpectedWactTag('our_tag', $open_location); 193 $this->tree_builder->pushExpectedWactTag('component_tag', $second_location); 194 $this->tree_builder->pushExpectedPlainTag('plain_tag', $third_location); 195 196 try 197 { 198 $this->tree_builder->popExpectedWactTag('our_tag', $close_location); 199 $this->assertTrue(false); 200 } 201 catch(WactException $e) 202 { 203 $this->assertWantedPattern('/Unexpected closing tag/', $e->getMessage()); 204 $params = $e->getParams(); 205 $this->assertEqual($params['file'], 'my_file'); 206 $this->assertEqual($params['tag'], 'our_tag'); 207 $this->assertEqual($params['line'], 12); 208 $this->assertEqual($params['ExpectTag'], 'component_tag'); 209 $this->assertEqual($params['ExpectTagFile'], 'my_file'); 210 $this->assertEqual($params['ExpectedTagLine'], 10); 211 } 212 } 213 214 function testPushCursor() 215 { 216 // This test is essentially a test of the functionality that enables the 217 // core:wrap implementation. 218 // Briefly: 219 // (1) A tree is set up 220 // (2) A new cursor is pushed 221 // (3) New components added should appear under the tree 222 // (4) When the parser pops the tag at which the cursor was pushed 223 // the cursor returns where it was before step (2) 224 // (5) New components added should appear under this orig point. 225 226 $root = new WactCompileTreeNode(); 227 $InsertionPoint = new WactCompileTreeNode(); 228 $child1 = new WactCompileTreeNode(); 229 $child2 = new WactCompileTreeNode(); 230 231 // set up an open tag at root 232 $this->tree_builder->setCursor($root); 233 $this->tree_builder->pushExpectedWactTag('tag', new WactSourceLocation('my_file', 10)); 234 235 // add some content to the tree 236 $this->tree_builder->pushNode($InsertionPoint); 237 $this->tree_builder->popNode(); 238 239 // make sure the tree is: Root --child--> InsertionPoint with cursor 240 // at Root and open 'tag' 241 $this->assertReference($this->tree_builder->getCursor(), $root); 242 $this->assertReference($InsertionPoint->parent, $root); 243 $this->assertEqual($this->tree_builder->getExpectedTag(), 'tag'); 244 245 // push InsertionPoint as cursor, and add another node to the tree 246 $this->tree_builder->pushCursor($InsertionPoint, new WactSourceLocation('my_file', 15)); 247 $this->tree_builder->pushNode($child1); 248 $this->tree_builder->popNode(); 249 250 // make sure cursor is at InsertionPoint, and new node is child of InsertionPoint 251 $this->assertReference($this->tree_builder->getCursor(), $InsertionPoint); 252 $this->assertReference($child1->parent, $InsertionPoint); 253 254 // now the parser gets '</tag>', and then more content 255 // so we pop 'tag' (should restore orig cursor), and add a new node 256 $this->tree_builder->popExpectedWactTag('tag', new WactSourceLocation('my_file', 16), $is_wact_tag = true); 257 $this->tree_builder->pushNode($child2); 258 $this->tree_builder->popNode(); 259 260 // the new node should be a child of Root, not InsertionPoint 261 $this->assertReference($this->tree_builder->getCursor(), $root); 262 $this->assertReference($child2->parent, $root); 263 } 264 265 function testPushAndPopExpectedTagsWithPushCursor() 266 { 267 $new_cursor = new WactCompileTreeNode(); 268 269 $this->tree_builder->pushExpectedWactTag('tag1', new WactSourceLocation('my_file', 10)); 270 271 // push a new cursor 272 $this->tree_builder->pushCursor($new_cursor, new WactSourceLocation('my_file', 12)); 273 $this->assertReference($this->tree_builder->getCursor(), $new_cursor); 274 275 $this->tree_builder->pushExpectedWactTag('tag2', new WactSourceLocation('my_file', 13)); 276 277 $this->assertEqual($this->tree_builder->getExpectedTagCount(), 3); 278 279 $this->assertEqual($this->tree_builder->getExpectedTag(), 'tag2'); 280 $this->assertEqual($this->tree_builder->popExpectedWactTag('tag2', new WactSourceLocation('my_file', 15)), WACT_EXPECTED_WACT_TAG); 281 $this->assertEqual($this->tree_builder->getExpectedTagCount(), 2); 282 283 // getting expected tag should skip the cursor 284 $this->assertEqual($this->tree_builder->getExpectedTag(), 'tag1'); 285 286 // popping the next tag should restore the cursor to the original 287 $this->assertEqual($this->tree_builder->popExpectedWactTag('tag1', new WactSourceLocation('my_file', 17)), WACT_EXPECTED_WACT_TAG); 288 $this->assertReference($this->tree_builder->getCursor(), $this->component); 289 $this->assertEqual($this->tree_builder->getExpectedTagCount(), 0); 290 } 291 292 function testAddProcessingInstructionPHP() 293 { 294 $this->tree_builder->addProcessingInstruction('php', $code = 'echo $var'); 295 $children = $this->component->getChildren(); 296 $this->assertIsA($children[0], 'WactPHPNode'); 297 $this->assertEqual($children[0]->getCode(), $code); 298 } 299 300 function testAddProcessingInstructionNotPHP() 301 { 302 $this->tree_builder->addProcessingInstruction('other', 'some content'); 303 $children = $this->component->getChildren(); 304 $this->assertIsA($children[0], 'WactPHPNode'); 305 $this->assertEqual($children[0]->getCode(), 'echo "<?other some content?>\n";'); 306 } 307 308 function testBuildTagNodeWithPropertyAndAttributes() 309 { 310 $tag_info = new WactTagInfo('my_tag', 'WactNodeBuilderTestTag'); 311 $location = new WactSourceLocation('my_file', 10); 312 313 $property_info = new WactPropertyInfo('my_property', 'WactNodeBuilderTestTag', 'WactNodeBuilderTestProperty'); 314 $this->property_dictionary->registerPropertyInfo($property_info, __FILE__); 315 316 $attrs = array('attr1' => 'value1', 317 'attr2' => 'value2{$my_property}', // will be compount attribute 318 'attr3' => '{$my_property}', // will be regular attribute expression 319 ); 320 321 $node = $this->