|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by rozbo at 2017/4/17 下午6:37 |
| 4 | + */ |
| 5 | + |
| 6 | +namespace tests\puck\helpers; |
| 7 | + |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class DomTest extends TestCase { |
| 11 | + /** |
| 12 | + * @var \puck\helpers\Dom; |
| 13 | + */ |
| 14 | + private $dom; |
| 15 | + |
| 16 | + public function setUp() { |
| 17 | + $this->dom = app('dom'); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @expectedException InvalidArgumentException |
| 22 | + */ |
| 23 | + public function testLoadWithInvalidArgument() { |
| 24 | + $this->dom->load([]); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + public function loadHtmlCharsetTests() { |
| 29 | + return array( |
| 30 | + array('<html><div class="foo">English language</html>', 'English language'), |
| 31 | + array('<html><div class="foo">Русский язык</html>', 'Русский язык'), |
| 32 | + array('<html><div class="foo">اللغة العربية</html>', 'اللغة العربية'), |
| 33 | + array('<html><div class="foo">漢語</html>', '漢語'), |
| 34 | + array('<html><div class="foo">Tiếng Việt</html>', 'Tiếng Việt'), |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @dataProvider loadHtmlCharsetTests |
| 40 | + */ |
| 41 | + public function testLoadHtmlCharset($html, $text) { |
| 42 | + $document = $this->dom->load($html, false); |
| 43 | + $this->assertEquals($text, $document->first('div')->text()); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public function testCreateElementBySelector() { |
| 48 | + $document = $this->dom; |
| 49 | + $element = $document->createElementBySelector('a.external-link[href=http://example.com]'); |
| 50 | + $this->assertEquals('a', $element->tag); |
| 51 | + $this->assertEquals('', $element->text()); |
| 52 | + $this->assertEquals(['href' => 'http://example.com', 'class' => 'external-link'], $element->attributes()); |
| 53 | + $element = $document->createElementBySelector('#block', 'Foo'); |
| 54 | + $this->assertEquals('div', $element->tag); |
| 55 | + $this->assertEquals('Foo', $element->text()); |
| 56 | + $this->assertEquals(['id' => 'block'], $element->attributes()); |
| 57 | + $element = $document->createElementBySelector('input', null, ['name' => 'name', 'placeholder' => 'Enter your name']); |
| 58 | + $this->assertEquals('input', $element->tag); |
| 59 | + $this->assertEquals('', $element->text()); |
| 60 | + $this->assertEquals(['name' => 'name', 'placeholder' => 'Enter your name'], $element->attributes()); |
| 61 | + } |
| 62 | + |
| 63 | + public function testAppendChild() { |
| 64 | + $html = '<!DOCTYPE html> |
| 65 | + <html lang="en"> |
| 66 | + <head> |
| 67 | + <meta charset="UTF-8"> |
| 68 | + <title>Document</title> |
| 69 | + </head> |
| 70 | + <body> |
| 71 | + </body> |
| 72 | + </html>'; |
| 73 | + $document = $this->dom; |
| 74 | + $this->assertCount(0, $document->find('span')); |
| 75 | + $node = $document->createElement('span'); |
| 76 | + $appendedChild = $document->appendChild($node); |
| 77 | + $this->assertCount(1, $document->find('span')); |
| 78 | + $this->assertTrue($appendedChild->is($document->first('span'))); |
| 79 | + $appendedChild->remove(); |
| 80 | + $this->assertCount(0, $document->find('span')); |
| 81 | + $nodes = []; |
| 82 | + $nodes[] = $document->createElement('span'); |
| 83 | + $nodes[] = $document->createElement('span'); |
| 84 | + $appendedChildren = $document->appendChild($nodes); |
| 85 | + $nodes = $document->find('span'); |
| 86 | + $this->assertCount(2, $appendedChildren); |
| 87 | + $this->assertCount(2, $nodes); |
| 88 | + foreach ($appendedChildren as $index => $child) { |
| 89 | + $this->assertTrue($child->is($nodes[$index])); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @expectedException RuntimeException |
| 95 | + */ |
| 96 | + public function testLoadWithNotExistingFile() { |
| 97 | + $this->dom->load('path/to/file', true); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + public function testFirst() { |
| 102 | + $html = '<ul><li>One</li><li>Two</li><li>Three</li></ul>'; |
| 103 | + $document = $this->dom; |
| 104 | + $this->dom->loadHtml($html); |
| 105 | + $items = $document->find('ul > li'); |
| 106 | + $this->assertEquals($items[0]->getNode(), $document->first('ul > li')->getNode()); |
| 107 | + $this->assertEquals('One', $document->first('ul > li::text')); |
| 108 | + $document = $this->dom->release()->init(); |
| 109 | + $this->assertNull($document->first('ul > li')); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + public function testCount() { |
| 114 | + $html = '<ul><li>One</li><li>Two</li><li>Three</li></ul>'; |
| 115 | + $this->assertEquals(3, $this->dom->loadHtml($html)->count('li')); |
| 116 | + $this->assertEquals(0, $this->dom->release()->init()->count('li')); |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + public function testHtmlWithOptions() { |
| 121 | + $html = '<html><body><span></span></body></html>'; |
| 122 | + $document = $this->dom->loadHtml($html); |
| 123 | + $this->assertEquals('<html><body><span></span></body></html>', $document->html()); |
| 124 | + $this->assertEquals('<html><body><span/></body></html>', $document->html(0)); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + public function testText() { |
| 129 | + $html = '<html>foo</html>'; |
| 130 | + $document = $this->dom->loadHtml($html); |
| 131 | + $this->assertEquals('foo', $document->text()); |
| 132 | + } |
| 133 | +} |
0 commit comments