File: DocumentTest.php

package info (click to toggle)
php-embed 4.4.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,728 kB
  • sloc: php: 40,309; makefile: 23
file content (60 lines) | stat: -rw-r--r-- 2,232 bytes parent folder | download
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
<?php
declare(strict_types = 1);

namespace Embed\Tests;

use Embed\Embed;
use Embed\Http\Crawler;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

class DocumentTest extends TestCase
{
    private static Embed $embed;

    private static function getEmbed(): Embed
    {
        if (isset(self::$embed)) {
            return self::$embed;
        }

        $dispatcher = new FileClient(__DIR__.'/cache');
        $dispatcher->setMode(0);

        return self::$embed = new Embed(new Crawler($dispatcher));
    }

    #[Group('network')]
    public function testSelectors()
    {
        $extractor = self::getEmbed()->get('http://www.wired.com/?p=2064839');
        $document = $extractor->getDocument();

        $expected = 23;

        $this->assertCount($expected, $document->select('.//p')->nodes());
        $this->assertCount($expected, $document->selectCss('p')->nodes());

        $document->remove('.//p');

        $this->assertCount(0, $document->select('.//p')->nodes());
        $this->assertCount(0, $document->selectCss('p')->nodes());
    }

    #[Group('network')]
    public function testMultipleSelectors()
    {
        $extractor = self::getEmbed()->get('https://css-tricks.com/css-scrollbar-with-progress-meter/');
        $document = $extractor->getDocument();

        $expected = 113;

        $this->assertCount($expected, $document->selectCss('[aria-hidden],[hidden],meta,style,canvas,svg,form,script,template,link,.hidden')->nodes());
        $this->assertCount($expected, $document->select('.//*[@aria-hidden]|.//*[@hidden]|.//meta|.//style|.//canvas|.//svg|.//form|.//script|.//template|.//link|.//*[contains(concat(" ",normalize-space(@class)," ")," hidden ")]')->nodes());

        $document->removeCss('[aria-hidden],[hidden],meta,style,canvas,svg,form,script,template,link,.hidden');

        $this->assertCount(0, $document->selectCss('[aria-hidden],[hidden],meta,style,canvas,svg,form,script,template,link,.hidden')->nodes());
        $this->assertCount(0, $document->select('.//*[@aria-hidden]|.//*[@hidden]|.//meta|.//style|.//canvas|.//svg|.//form|.//script|.//template|.//link|.//*[contains(concat(" ",normalize-space(@class)," ")," hidden ")]')->nodes());
    }
}