Skip to content
This repository was archived by the owner on Mar 26, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"Yangqi\\Htmldom": "src/"
}
},
"minimum-stability": "dev"
}
"minimum-stability": "dev",
"require-dev": {
"phpunit/phpunit": "^6.5"
}
}
8 changes: 8 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" syntaxCheck="false">
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
</phpunit>
9 changes: 9 additions & 0 deletions src/Yangqi/Htmldom/Htmldom.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Htmldom {
public $_target_charset = '';
protected $default_br_text = "";
public $default_span_text = "";
public $response_code = null;

// use isset instead of in_array, performance boost about 30%...
protected $self_closing_tags = array('img'=>1, 'br'=>1, 'input'=>1, 'meta'=>1, 'link'=>1, 'hr'=>1, 'base'=>1, 'embed'=>1, 'spacer'=>1);
Expand Down Expand Up @@ -176,6 +177,7 @@ public function load_file()
{
$args = func_get_args();
$this->load(call_user_func_array('file_get_contents', $args), true);
$this->set_response_code($http_response_header[0]);
// Throw an error if we can't properly load the dom.
if (($error=error_get_last())!==null) {
$this->clear();
Expand Down Expand Up @@ -418,6 +420,13 @@ protected function parse_charset()
return $this->_charset = $charset;
}

// search response code in $http_response_header variable.
protected function set_response_code($response_string)
{
$response_parts = explode(' ', $response_string);
$this->response_code = $response_parts[1];
}

// read tag info
protected function read_tag()
{
Expand Down
44 changes: 44 additions & 0 deletions tests/ResponseCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use PHPUnit\Framework\TestCase;
use Yangqi\Htmldom\Htmldom;

class ReseponseCodeTest extends TestCase
{

public function testContinueCode()
{
$htmldom = new Htmldom('https://httpstat.us/100');

$this->assertEquals(100, $htmldom->response_code);
}

public function testOKCode()
{
$htmldom = new Htmldom('https://httpstat.us/200');

$this->assertEquals(200, $htmldom->response_code);
}

public function testMultipleChoicesCode()
{
$htmldom = new Htmldom('https://httpstat.us/300');

$this->assertEquals(300, $htmldom->response_code);
}

public function testBadRequestCode()
{
$htmldom = new Htmldom('https://httpstat.us/400');

$this->assertEquals(400, $htmldom->response_code);
}

public function testInternalServerErrorCode()
{
$htmldom = new Htmldom('https://httpstat.us/500');

$this->assertEquals(500, $htmldom->response_code);
}

}