Skip to content

Commit

Permalink
Improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jkphl committed May 24, 2017
1 parent 95fb2b4 commit c61d145
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 43 deletions.
30 changes: 15 additions & 15 deletions phpunit.php
@@ -1,21 +1,21 @@
<?php

// Start the built-in web server
//chdir(__DIR__);
//$server = defined('HHVM_VERSION') ?
// 'hhvm -m server -d hhvm.server.host=%s -d hhvm.server.type=fastcgi -d hhvm.server.port=%d -d hhvm.server.source_root=%s' :
// 'php -S %s:%d -t %s';
//$command = sprintf($server, WEB_SERVER_HOST, WEB_SERVER_PORT, WEB_SERVER_DOCROOT);
//$process = proc_open($command, [['pipe', 'r']], $pipes);
//$pstatus = proc_get_status($process);
//$pid = $pstatus['pid'];
//echo sprintf('%s - Web server started on %s:%d with PID %d', date('r'), WEB_SERVER_HOST, WEB_SERVER_PORT, $pid).PHP_EOL;
//
//// Register shutdown function to stop the built-in webserver
//register_shutdown_function(function () use ($pid) {
// echo sprintf('%s - Killing process with ID %d', date('r'), $pid).PHP_EOL;
// (stripos(php_uname('s'), 'win') > -1) ? exec("taskkill /F /T /PID $pid") : exec("kill -9 $pid");
//});
chdir(__DIR__);
$server = defined('HHVM_VERSION') ?
'hhvm -m server -d hhvm.server.host=%s -d hhvm.server.type=fastcgi -d hhvm.server.port=%d -d hhvm.server.source_root=%s' :
'php -S %s:%d -t %s';
$command = sprintf($server, WEB_SERVER_HOST, WEB_SERVER_PORT, WEB_SERVER_DOCROOT);
$process = proc_open($command, [['pipe', 'r']], $pipes);
$pstatus = proc_get_status($process);
$pid = $pstatus['pid'];
echo sprintf('%s - Web server started on %s:%d with PID %d', date('r'), WEB_SERVER_HOST, WEB_SERVER_PORT, $pid).PHP_EOL;

// Register shutdown function to stop the built-in webserver
register_shutdown_function(function () use ($pid) {
echo sprintf('%s - Killing process with ID %d', date('r'), $pid).PHP_EOL;
(stripos(php_uname('s'), 'win') > -1) ? exec("taskkill /F /T /PID $pid") : exec("kill -9 $pid");
});


error_reporting(E_ALL);
Expand Down
9 changes: 6 additions & 3 deletions phpunit.xml.dist
Expand Up @@ -5,11 +5,14 @@
</testsuite>
</testsuites>

<php>
<const name="WEB_SERVER_HOST" value="localhost"/>
<const name="WEB_SERVER_PORT" value="1349"/>
<const name="WEB_SERVER_DOCROOT" value="./src/Domfactory/Tests/Fixture"/>
</php>

<logging>
<log type="coverage-xml" target="build/coverage"/>
<const name="WEB_SERVER_HOST" value="localhost" />
<const name="WEB_SERVER_PORT" value="1349" />
<const name="WEB_SERVER_DOCROOT" value="./src/Domfactory/Tests/Fixture" />
</logging>

<filter>
Expand Down
32 changes: 15 additions & 17 deletions src/Domfactory/Infrastructure/Dom.php
Expand Up @@ -36,12 +36,10 @@

namespace Jkphl\Domfactory\Infrastructure;

use Guzzle\Common\Exception\InvalidArgumentException as GuzzleInvalidArgumentException;
use Guzzle\Common\Exception\RuntimeException as GuzzleRuntimeException;
use Guzzle\Http\Client;
use Guzzle\Http\Url;
use Jkphl\Domfactory\Domain\Dom as DomainDom;
use Jkphl\Domfactory\Ports\InvalidArgumentException;
use Jkphl\Domfactory\Ports\RuntimeException;

/**
Expand All @@ -56,24 +54,20 @@ class Dom
* Create a DOM document using a HTTP client implementation
*
* @param string $url HTTP / HTTPS URL
* @param array $options Connection options
* @return \DOMDocument DOM document
* @throws RuntimeException If the request wasn't successful
* @throws InvalidArgumentException If an argument was invalid
* @throws RuntimeException If a runtime exception occurred
*/
protected static function createViaHttpClient($url)
protected static function createViaHttpClient($url, array $options = ['timeout' => 10.0])
{
try {
$guzzleUrl = Url::factory($url);
$client = new Client($guzzleUrl, ['timeout' => 10.0]);
$client = new Client($guzzleUrl, $options);
$request = $client->get($guzzleUrl);
$response = $client->send($request);
return self::createFromString(strval($response->getBody()));

// If an argument was invalid
} catch (GuzzleInvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode());

// If a runtime exception occurred
} catch (GuzzleRuntimeException $e) {
throw new RuntimeException($e->getMessage(), $e->getCode());
Expand All @@ -96,18 +90,22 @@ public static function createFromString($str)
* Create a DOM document via the PHP stream wrapper
*
* @param string $url URL
* @param array $options Connection options
* @return \DOMDocument DOM document
*/
protected static function createViaStreamWrapper($url)
protected static function createViaStreamWrapper($url, array $options = [])
{
$opts = array(
'http' => array(
'method' => 'GET',
'protocol_version' => 1.1,
'user_agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.466.4 Safari/534.3',
'max_redirects' => 10,
'timeout' => 120,
'header' => "Accept-language: en\r\n",
'http' => array_merge(
array(
'method' => 'GET',
'protocol_version' => 1.1,
'user_agent' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.466.4 Safari/534.3',
'max_redirects' => 10,
'timeout' => 10.0,
'header' => "Accept-language: en\r\n",
),
$options
)
);
$context = stream_context_create($opts);
Expand Down
6 changes: 4 additions & 2 deletions src/Domfactory/Ports/Dom.php
Expand Up @@ -50,12 +50,14 @@ class Dom extends InternalDom
* Create a DOM document from a URI
*
* @param string $url HTTP / HTTPS URL
* @param array $options Connection options
* @return \DOMDocument DOM document
* @api
*/
public static function createFromUri($url)
public static function createFromUri($url, array $options = ['timeout' => 10.0])
{
return extension_loaded('curl') ? self::createViaHttpClient($url) : self::createViaStreamWrapper($url);
return extension_loaded('curl') ?
self::createViaHttpClient($url, $options) : self::createViaStreamWrapper($url, $options);
}

/**
Expand Down
85 changes: 85 additions & 0 deletions src/Domfactory/Tests/Infrastructure/DomTest.php
@@ -0,0 +1,85 @@
<?php

/**
* dom-factory
*
* @category Jkphl
* @package Jkphl\Domfactory
* @subpackage Jkphl\Domfactory\Tests
* @author Joschi Kuphal <joschi@tollwerk.de> / @jkphl
* @copyright Copyright © 2017 Joschi Kuphal <joschi@tollwerk.de> / @jkphl
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
*/

/***********************************************************************************
* The MIT License (MIT)
*
* Copyright © 2017 Joschi Kuphal <joschi@kuphal.net> / @jkphl
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***********************************************************************************/

namespace Jkphl\Domfactory\Tests\Infrastructure {

use Jkphl\Domfactory\Ports\Dom;
use Jkphl\Domfactory\Tests\AbstractTestBase;

/**
* DOM factory tests
*
* @package Jkphl\Domfactory
* @subpackage Jkphl\Domfactory\Tests
*/
class DomTest extends AbstractTestBase
{
/**
* Test malformed URL
*
* @expectedException \Jkphl\Domfactory\Ports\RuntimeException
*/
public function testMalformedUri()
{
Dom::createFromUri('');
}

/**
* Test stream wrapper
*/
public function testStreamWrapper()
{
putenv('MOCK_EXTENSION_LOADED=1');
$dom = Dom::createFromUri('http://localhost:1349/books.xml');
$this->assertInstanceOf(\DOMDocument::class, $dom);
$this->assertEquals('catalog', $dom->documentElement->localName);
putenv('MOCK_EXTENSION_LOADED');
}
}
}

namespace Jkphl\Domfactory\Ports {
/**
* Find out whether an extension is loaded
*
* @param string $name The extension name
* @return boolean The extension is loaded
*/
function extension_loaded($name)
{
return (getenv('MOCK_EXTENSION_LOADED') != 1) ? \extension_loaded($name) : false;
}
}
12 changes: 6 additions & 6 deletions src/Domfactory/Tests/Ports/DomTest.php
Expand Up @@ -75,12 +75,12 @@ public function testXmlFile()
/**
* Test parsing an XML string via HTTP
*/
// public function testXmlUri()
// {
// $dom = Dom::createFromUri('http://localhost:1349/books.xml');
// $this->assertInstanceOf(\DOMDocument::class, $dom);
// $this->assertEquals('catalog', $dom->documentElement->localName);
// }
public function testXmlUri()
{
$dom = Dom::createFromUri('http://localhost:1349/books.xml');
$this->assertInstanceOf(\DOMDocument::class, $dom);
$this->assertEquals('catalog', $dom->documentElement->localName);
}

/**
* Test parsing an HTML5 string
Expand Down

0 comments on commit c61d145

Please sign in to comment.