test_4.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. require_once('../phpQuery/phpQuery.php');
  3. phpQuery::$debug = true;
  4. // SLICE1
  5. $testResult = array(
  6. 'li#testID',
  7. );
  8. $result = phpQuery::newDocumentFile('test.html')
  9. ->find('li')
  10. ->slice(1, 2);
  11. if ( $result->whois() == $testResult )
  12. print "Test 'Slice1' PASSED :)";
  13. else {
  14. print "Test 'Slice1' <strong>FAILED</strong> !!! ";
  15. print "<pre>";
  16. print_r($result->whois());
  17. print "</pre>\n";
  18. }
  19. print "\n";
  20. // SLICE2
  21. $testResult = array(
  22. 'li#testID',
  23. 'li',
  24. 'li#i_have_nested_list',
  25. 'li.nested',
  26. );
  27. $result = phpQuery::newDocumentFile('test.html')
  28. ->find('li')
  29. ->slice(1, -1);
  30. if ( $result->whois() == $testResult )
  31. print "Test 'Slice2' PASSED :)";
  32. else {
  33. print "Test 'Slice2' <strong>FAILED</strong> !!! ";
  34. print "<pre>";
  35. print_r($result->whois());
  36. print "</pre>\n";
  37. }
  38. print "\n";
  39. // Multi-insert
  40. $result = phpQuery::newDocument('<li><span class="field1"></span><span class="field1"></span></li>')
  41. ->find('.field1')
  42. ->php('longlongtest');
  43. $validResult = '<li><span class="field1"><php>longlongtest</php></span><span class="field1"><php>longlongtest</php></span></li>';
  44. similar_text($result->htmlOuter(), $validResult, $similarity);
  45. if ( $similarity > 80 )
  46. print "Test 'Multi-insert' PASSED :)";
  47. else {
  48. print "Test 'Multi-insert' <strong>FAILED</strong> !!! ";
  49. print "<pre>";
  50. var_dump($result->htmlOuter());
  51. print "</pre>\n";
  52. }
  53. print "\n";
  54. // INDEX
  55. $testResult = 1;
  56. $result = phpQuery::newDocumentFile('test.html')
  57. ->find('p')
  58. ->index(pq('p.title:first'));
  59. if ( $result == $testResult )
  60. print "Test 'Index' PASSED :)";
  61. else {
  62. print "Test 'Index' <strong>FAILED</strong> !!! ";
  63. }
  64. print "\n";
  65. // CLONE
  66. $testName = 'Clone';
  67. $testResult = 3;
  68. $document;
  69. $p = phpQuery::newDocumentFile('test.html')
  70. ->toReference($document)
  71. ->find('p:first');
  72. foreach(array(0,1,2) as $i) {
  73. $p->clone()
  74. ->addClass("clone-test")
  75. ->addClass("class-$i")
  76. ->insertBefore($p);
  77. }
  78. if (pq('.clone-test')->size() == $testResult)
  79. print "Test '$testName' PASSED :)";
  80. else {
  81. print "Test '$testName' <strong>FAILED</strong> !!! ";
  82. }
  83. print "\n";
  84. // SIBLINGS
  85. $testName = 'Next';
  86. $testResult = 3;
  87. $document;
  88. $result = phpQuery::newDocumentFile('test.html')
  89. ->find('li:first')
  90. ->next()
  91. ->next()
  92. ->prev()
  93. ->is('#testID');
  94. if ($result)
  95. print "Test '$testName' PASSED :)";
  96. else {
  97. print "Test '$testName' <strong>FAILED</strong> !!! ";
  98. }
  99. print "\n";
  100. ?>
  101. <?php die();