mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Fix & test Test\DOMParser
This commit is contained in:
parent
e3d9a037f6
commit
14e1451aa9
@ -47,7 +47,10 @@ class DOMParser
|
||||
{
|
||||
if (! extension_loaded('DOM'))
|
||||
{
|
||||
// always there in travis-ci
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \BadMethodCallException('DOM extension is required, but not currently loaded.');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
$this->dom = new \DOMDocument('1.0', 'utf-8');
|
||||
@ -78,8 +81,10 @@ class DOMParser
|
||||
//turning off some errors
|
||||
libxml_use_internal_errors(true);
|
||||
|
||||
if (! $this->dom->loadHTML($content))
|
||||
$this->dom->loadHTML($content);
|
||||
if (sizeof(libxml_get_errors()) > 0)
|
||||
{
|
||||
libxml_clear_errors();
|
||||
throw new \BadMethodCallException('Invalid HTML');
|
||||
}
|
||||
|
||||
@ -248,7 +253,7 @@ class DOMParser
|
||||
{
|
||||
foreach ($selector['attr'] as $key => $value)
|
||||
{
|
||||
$path .= "[{$key}={$value}]";
|
||||
$path .= "[@{$key}=\"{$value}\"]";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,9 @@
|
||||
<?php namespace CodeIgniter\Test;
|
||||
<?php
|
||||
namespace CodeIgniter\Test;
|
||||
|
||||
class DOMParserTest extends CIUnitTestCase
|
||||
{
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
@ -18,7 +20,7 @@ class DOMParserTest extends CIUnitTestCase
|
||||
|
||||
$html = '<div><h1>Hello</h1></div>';
|
||||
$expected = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">' . "\n"
|
||||
. '<html><body><div><h1>Hello</h1></div></body></html>';
|
||||
. '<html><body><div><h1>Hello</h1></div></body></html>';
|
||||
|
||||
$this->assertEquals($expected . "\n", $dom->withString($html)->getBody());
|
||||
}
|
||||
@ -74,6 +76,16 @@ class DOMParserTest extends CIUnitTestCase
|
||||
$this->assertTrue($dom->see('Hello World'));
|
||||
}
|
||||
|
||||
public function testBadHTML()
|
||||
{
|
||||
$dom = new DOMParser();
|
||||
|
||||
$html = '<peanut><banana><h1>Hello World</body></h2></butter>';
|
||||
|
||||
$this->expectException(\BadMethodCallException::class);
|
||||
$dom->withString($html);
|
||||
}
|
||||
|
||||
public function testSeeHTML()
|
||||
{
|
||||
$dom = new DOMParser();
|
||||
@ -333,4 +345,41 @@ class DOMParserTest extends CIUnitTestCase
|
||||
|
||||
$this->assertTrue($dom->seeCheckboxIsChecked('.btn'));
|
||||
}
|
||||
|
||||
public function testWithFile()
|
||||
{
|
||||
$dom = new DOMParser();
|
||||
|
||||
$filename = APPPATH . 'index.html';
|
||||
|
||||
$dom->withFile($filename);
|
||||
$this->assertTrue($dom->see('Directory access is forbidden.'));
|
||||
}
|
||||
|
||||
public function testWithNotFile()
|
||||
{
|
||||
$dom = new DOMParser();
|
||||
|
||||
$filename = APPPATH . 'bogus.html';
|
||||
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$dom->withFile($filename);
|
||||
}
|
||||
|
||||
public function testSeeAttribute()
|
||||
{
|
||||
$dom = new DOMParser();
|
||||
|
||||
$path = '[ name = user ]';
|
||||
$selector = $dom->parseSelector($path);
|
||||
|
||||
$this->assertEquals(['name' => 'user'], $selector['attr']);
|
||||
|
||||
$html = '<html><body><div name="user">George</div></body></html>';
|
||||
$dom->withString($html);
|
||||
|
||||
$this->assertTrue($dom->see(null, '*[ name = user ]'));
|
||||
$this->assertFalse($dom->see(null, '*[ name = notthere ]'));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user