You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.5 KiB

2 years ago
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: x
  5. * Date: 2018/12/10
  6. * Time: 12:46 AM
  7. */
  8. namespace Tests\Dom;
  9. use QL\QueryList;
  10. use Tests\TestCaseBase;
  11. class FindTest extends TestCaseBase
  12. {
  13. protected $html;
  14. protected $ql;
  15. protected function setUp(): void
  16. {
  17. $this->html = $this->getSnippet('snippet-1');
  18. $this->ql = QueryList::html($this->html);
  19. }
  20. /**
  21. * @test
  22. */
  23. public function find_first_dom_attr()
  24. {
  25. $img = [];
  26. $img[] = $this->ql->find('img')->attr('src');
  27. $img[] = $this->ql->find('img')->src;
  28. $img[] = $this->ql->find('img:eq(0)')->src;
  29. $img[] = $this->ql->find('img')->eq(0)->src;
  30. $alt = $this->ql->find('img')->alt;
  31. $abc = $this->ql->find('img')->abc;
  32. $this->assertCount(1,array_unique($img));
  33. $this->assertEquals($alt,'这是图片');
  34. $this->assertEquals($abc,'这是一个自定义属性');
  35. }
  36. /**
  37. * @test
  38. */
  39. public function find_second_dom_attr()
  40. {
  41. $img2 = [];
  42. $img2[] = $this->ql->find('img')->eq(1)->alt;
  43. $img2[] = $this->ql->find('img:eq(1)')->alt;
  44. $img2[] = $this->ql->find('.second_pic')->alt;
  45. $this->assertCount(1,array_unique($img2));
  46. }
  47. /**
  48. * @test
  49. */
  50. public function find_dom_all_attr()
  51. {
  52. $imgAttr = $this->ql->find('img:eq(0)')->attr('*');
  53. $linkAttr = $this->ql->find('a:eq(1)')->attr('*');
  54. $this->assertCount(3,$imgAttr);
  55. $this->assertCount(1,$linkAttr);
  56. }
  57. }