summaryrefslogtreecommitdiff
path: root/lib/querypath/test/Tests/QueryPath/CSS/ParserTest.php
blob: 981ddd1a22815023833158a02860b1e064cc21e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<?php
/**
 * @file
 * CSS Event handling tests
 */
namespace QueryPath\Tests;

require_once __DIR__ . '/../TestCase.php';

use \QueryPath\CSS\Token;
use \QueryPath\CSS\QueryPathEventHandler;
use \QueryPath\CSS\Parser;
use \QueryPath\CSS\EventHandler;

/**
 * @ingroup querypath_tests
 * @group CSS
 */
class ParserTest extends TestCase {

  private function getMockHandler($method) {
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array($method));
    $mock->expects($this->once())
      ->method($method)
      ->with($this->equalTo('mytest'));
    return $mock;
  }

  public function testElementID() {
    $mock = $this->getMockHandler('elementID');
    $parser = new Parser('#mytest', $mock);
    $parser->parse();

  }

  public function testElement() {

    // Without namespace
    $mock = $this->getMockHandler('element');
    $parser = new Parser('mytest', $mock);
    $parser->parse();

    // With empty namespace
    $mock = $this->getMockHandler('element');
    $parser = new Parser('|mytest', $mock);
    $parser->parse();
  }

  public function testElementNS() {
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('elementNS'));
    $mock->expects($this->once())
      ->method('elementNS')
      ->with($this->equalTo('mytest'), $this->equalTo('myns'));

    $parser = new Parser('myns|mytest', $mock);
    $parser->parse();

    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('elementNS'));
    $mock->expects($this->once())
      ->method('elementNS')
      ->with($this->equalTo('mytest'), $this->equalTo('*'));

    $parser = new Parser('*|mytest', $mock);
    $parser->parse();

    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('anyElementInNS'));
    $mock->expects($this->once())
      ->method('anyElementInNS')
      ->with($this->equalTo('*'));

    $parser = new Parser('*|*', $mock);
    $parser->parse();
  }

  public function testAnyElement() {
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('anyElement', 'element'));
    $mock->expects($this->once())
      ->method('anyElement');

    $parser = new Parser('*', $mock);
    $parser->parse();
  }

  public function testAnyElementInNS() {
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('anyElementInNS', 'element'));
    $mock->expects($this->once())
      ->method('anyElementInNS')
      ->with($this->equalTo('myns'));

    $parser = new Parser('myns|*', $mock);
    $parser->parse();
  }

  public function testElementClass() {
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('elementClass'));
    $mock->expects($this->once())
      ->method('elementClass')
      ->with($this->equalTo('myclass'));

    $parser = new Parser('.myclass', $mock);
    $parser->parse();
  }

  public function testPseudoClass() {

    // Test empty pseudoclass
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('pseudoClass'));
    $mock->expects($this->once())
      ->method('pseudoClass')
      ->with($this->equalTo('mypclass'));

    $parser = new Parser('myele:mypclass', $mock);
    $parser->parse();

    // Test pseudoclass with value
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('pseudoClass'));
    $mock->expects($this->once())
      ->method('pseudoClass')
      ->with($this->equalTo('mypclass'), $this->equalTo('myval'));

    $parser = new Parser('myele:mypclass(myval)', $mock);
    $parser->parse();

    // Test pseudclass with pseudoclass:
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('pseudoClass'));
    $mock->expects($this->once())
      ->method('pseudoClass')
      ->with($this->equalTo('mypclass'), $this->equalTo(':anotherPseudo'));

    $parser = new Parser('myele:mypclass(:anotherPseudo)', $mock);
    $parser->parse();

  }

  public function testPseudoElement() {
    // Test pseudo-element
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('pseudoElement'));
    $mock->expects($this->once())
      ->method('pseudoElement')
      ->with($this->equalTo('mypele'));

    $parser = new Parser('myele::mypele', $mock);
    $parser->parse();
  }

  public function testDirectDescendant() {
    // Test direct Descendant
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('directDescendant'));
    $mock->expects($this->once())
      ->method('directDescendant');

    $parser = new Parser('ele1 > ele2', $mock);
    $parser->parse();

  }

  public function testAnyDescendant() {
    // Test direct Descendant
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('anyDescendant'));
    $mock->expects($this->once())
      ->method('anyDescendant');

    $parser = new Parser('ele1  .class', $mock);
    $parser->parse();

  }

  public function testAdjacent() {
    // Test sibling
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('adjacent'));
    $mock->expects($this->once())
      ->method('adjacent');

    $parser = new Parser('ele1 + ele2', $mock);
    $parser->parse();
  }

  public function testSibling() {
    // Test adjacent
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('sibling'));
    $mock->expects($this->once())
      ->method('sibling');

    $parser = new Parser('ele1 ~ ele2', $mock);
    $parser->parse();
  }

  public function testAnotherSelector() {
    // Test adjacent
    $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('anotherSelector'));
    $mock->expects($this->once())
      ->method('anotherSelector');

    $parser = new Parser('ele1 , ele2', $mock);
    $parser->parse();
  }

  /**
   * @expectedException \QueryPath\CSS\ParseException
   */
  public function testIllegalAttribute() {

    // Note that this is designed to test throwError() as well as
    // bad selector handling.

    $parser = new Parser('[test=~far]', new TestEventHandler());
    try {
      $parser->parse();
    }
    catch (Exception $e) {
      //print $e->getMessage();
      throw $e;
    }
  }

  public function testAttribute() {
    $selectors = array(
      'element[attr]' => 'attr',
      '*[attr]' => 'attr',
      'element[attr]:class' => 'attr',
      'element[attr2]' => 'attr2', // Issue #
    );
    foreach ($selectors as $filter => $expected) {
      $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('attribute'));
      $mock->expects($this->once())
        ->method('attribute')
        ->with($this->equalTo($expected));

      $parser = new Parser($filter, $mock);
      $parser->parse();
    }

    $selectors = array(
      '*[attr="value"]' => array('attr','value',EventHandler::isExactly),
      '*[attr^="value"]' => array('attr','value',EventHandler::beginsWith),
      '*[attr$="value"]' => array('attr','value',EventHandler::endsWith),
      '*[attr*="value"]' => array('attr','value',EventHandler::containsInString),
      '*[attr~="value"]' => array('attr','value',EventHandler::containsWithSpace),
      '*[attr|="value"]' => array('attr','value',EventHandler::containsWithHyphen),

      // This should act like [attr="value"]
      '*[|attr="value"]' => array('attr', 'value', EventHandler::isExactly),

      // This behavior is displayed in the spec, but not accounted for in the
      // grammar:
      '*[attr=value]' => array('attr','value',EventHandler::isExactly),

      // Should be able to escape chars using backslash.
      '*[attr="\.value"]' => array('attr','.value',EventHandler::isExactly),
      '*[attr="\.\]\]\]"]' => array('attr','.]]]',EventHandler::isExactly),

      // Backslash-backslash should resolve to single backslash.
      '*[attr="\\\c"]' => array('attr','\\c',EventHandler::isExactly),

      // Should return an empty value. It seems, though, that a value should be
      // passed here.
      '*[attr=""]' => array('attr','',EventHandler::isExactly),
    );
    foreach ($selectors as $filter => $expected) {
      $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('attribute'));
      $mock->expects($this->once())
        ->method('attribute')
        ->with($this->equalTo($expected[0]), $this->equalTo($expected[1]), $this->equalTo($expected[2]));

      $parser = new Parser($filter, $mock);
      $parser->parse();
    }
  }

  public function testAttributeNS() {
    $selectors = array(
      '*[ns|attr="value"]' => array('attr', 'ns', 'value',EventHandler::isExactly),
      '*[*|attr^="value"]' => array('attr', '*', 'value',EventHandler::beginsWith),
      '*[*|attr|="value"]' => array('attr', '*', 'value',EventHandler::containsWithHyphen),
    );

    foreach ($selectors as $filter => $expected) {
      $mock = $this->getMock('\QueryPath\Tests\TestEventHandler', array('attributeNS'));
      $mock->expects($this->once())
        ->method('attributeNS')
        ->with($this->equalTo($expected[0]), $this->equalTo($expected[1]), $this->equalTo($expected[2]), $this->equalTo($expected[3]));

      $parser = new Parser($filter, $mock);
      $parser->parse();
    }
  }

  // Test things that should break...

  /**
   * @expectedException \QueryPath\CSS\ParseException
   */
  public function testIllegalCombinators1() {
    $handler = new TestEventHandler();
    $parser = new Parser('ele1 > > ele2', $handler);
    $parser->parse();
  }

  /**
   * @expectedException \QueryPath\CSS\ParseException
   */
  public function testIllegalCombinators2() {
    $handler = new TestEventHandler();
    $parser = new Parser('ele1+ ,ele2', $handler);
    $parser->parse();
  }

  /**
   * @expectedException \QueryPath\CSS\ParseException
   */
  public function testIllegalID() {
    $handler = new TestEventHandler();
    $parser = new Parser('##ID', $handler);
    $parser->parse();
  }

  // Test combinations

  public function testElementNSClassAndAttribute() {

    $expect = array(
      new TestEvent(TestEvent::elementNS, 'element', 'ns'),
      new TestEvent(TestEvent::elementClass, 'class'),
      new TestEvent(TestEvent::attribute, 'name', 'value', EventHandler::isExactly),
    );
    $selector = 'ns|element.class[name="value"]';

    $handler = new TestEventHandler();
    $handler->expects($expect);
    $parser = new Parser($selector, $handler);
    $parser->parse();
    $this->assertTrue($handler->success());

    // Again, with spaces this time:
    $selector = ' ns|element. class[  name = "value" ]';

    $handler = new TestEventHandler();
    $handler->expects($expect);
    $parser = new Parser($selector, $handler);
    $parser->parse();

    //$handler->dumpStack();
    $this->assertTrue($handler->success());
  }

  public function testAllCombo() {

    $selector = '*|ele1 > ele2.class1 + ns1|ele3.class2[attr=simple] ~
     .class2[attr2~="longer string of text."]:pseudoClass(value)
     .class3::pseudoElement';
    $expect = array(
      new TestEvent(TestEvent::elementNS, 'ele1', '*'),
      new TestEvent(TestEvent::directDescendant),
      new TestEvent(TestEvent::element, 'ele2'),
      new TestEvent(TestEvent::elementClass, 'class1'),
      new TestEvent(TestEvent::adjacent),
      new TestEvent(TestEvent::elementNS, 'ele3', 'ns1'),
      new TestEvent(TestEvent::elementClass, 'class2'),
      new TestEvent(TestEvent::attribute, 'attr', 'simple', EventHandler::isExactly),
      new TestEvent(TestEvent::sibling),
      new TestEvent(TestEvent::elementClass, 'class2'),
      new TestEvent(TestEvent::attribute, 'attr2', 'longer string of text.', EventHandler::containsWithSpace),
      new TestEvent(TestEvent::pseudoClass, 'pseudoClass', 'value'),
      new TestEvent(TestEvent::anyDescendant),
      new TestEvent(TestEvent::elementClass, 'class3'),
      new TestEvent(TestEvent::pseudoElement, 'pseudoElement'),
    );


    $handler = new TestEventHandler();
    $handler->expects($expect);
    $parser = new Parser($selector, $handler);
    $parser->parse();

    //$handler->dumpStack();

    $this->assertTrue($handler->success());

    /*
    // Again, with spaces this time:
    $selector = ' *|ele1 > ele2. class1 + ns1|ele3. class2[ attr=simple] ~ .class2[attr2 ~= "longer string of text."]:pseudoClass(value) .class3::pseudoElement';

    $handler = new TestEventHandler();
    $handler->expects($expect);
    $parser = new Parser($selector, $handler);
    $parser->parse();

    $handler->dumpStack();
    $this->assertTrue($handler->success());
    */
  }
}

/**
 * Testing harness for the EventHandler.
 *
 * @ingroup querypath_tests
 * @group CSS
 */
class TestEventHandler implements EventHandler {
  var $stack = NULL;
  var $expect = array();

  public function __construct() {
    $this->stack = array();
  }

  public function getStack() {
    return $this->stack;
  }

  public function dumpStack() {
    print "\nExpected:\n";
    $format = "Element %d: %s\n";
    foreach ($this->expect as $item) {
      printf($format, $item->eventType(), implode(',', $item->params()));
    }

    print "Got:\n";
    foreach($this->stack as $item){
      printf($format, $item->eventType(), implode(',', $item->params()));
    }
  }

  public function expects($stack) {
    $this->expect = $stack;
  }

  public function success() {
    return ($this->expect == $this->stack);
  }

  public function elementID($id) {
    $this->stack[] = new TestEvent(TestEvent::elementID, $id);
  }
  public function element($name) {
    $this->stack[] = new TestEvent(TestEvent::element, $name);
  }
  public function elementNS($name, $namespace = NULL){
    $this->stack[] = new TestEvent(TestEvent::elementNS, $name, $namespace);
  }
  public function anyElement(){
    $this->stack[] = new TestEvent(TestEvent::anyElement);
  }
  public function anyElementInNS($ns){
    $this->stack[] = new TestEvent(TestEvent::anyElementInNS, $ns);
  }
  public function elementClass($name){
    $this->stack[] = new TestEvent(TestEvent::elementClass, $name);
  }
  public function attribute($name, $value = NULL, $operation = EventHandler::isExactly){
    $this->stack[] = new TestEvent(TestEvent::attribute, $name, $value, $operation);
  }
  public function attributeNS($name, $ns, $value = NULL, $operation = EventHandler::isExactly){
    $this->stack[] = new TestEvent(TestEvent::attributeNS, $name, $ns, $value, $operation);
  }
  public function pseudoClass($name, $value = NULL){
    $this->stack[] = new TestEvent(TestEvent::pseudoClass, $name, $value);
  }
  public function pseudoElement($name){
    $this->stack[] = new TestEvent(TestEvent::pseudoElement, $name);
  }
  public function directDescendant(){
    $this->stack[] = new TestEvent(TestEvent::directDescendant);
  }
  public function anyDescendant() {
    $this->stack[] = new TestEvent(TestEvent::anyDescendant);
  }
  public function adjacent(){
    $this->stack[] = new TestEvent(TestEvent::adjacent);
  }
  public function anotherSelector(){
    $this->stack[] = new TestEvent(TestEvent::anotherSelector);
  }
  public function sibling(){
    $this->stack[] = new TestEvent(TestEvent::sibling);
  }
}

/**
 * Simple utility object for use with the TestEventHandler.
 *
 * @ingroup querypath_tests
 * @group CSS
 */
class TestEvent {
  const elementID = 0;
  const element = 1;
  const elementNS = 2;
  const anyElement = 3;
  const elementClass = 4;
  const attribute = 5;
  const attributeNS = 6;
  const pseudoClass = 7;
  const pseudoElement = 8;
  const directDescendant = 9;
  const adjacent = 10;
  const anotherSelector = 11;
  const sibling = 12;
  const anyElementInNS = 13;
  const anyDescendant = 14;

  var $type = NULL;
  var $params = NULL;

  public function __construct($event_type) {
    $this->type = $event_type;
    $args = func_get_args();
    array_shift($args);
    $this->params = $args;
  }

  public function eventType() {
    return $this->type;
  }

  public function params() {
    return $this->params;
  }
}