diff --git a/phpunit.php b/phpunit.php index 0f85890..a39c5a5 100755 --- a/phpunit.php +++ b/phpunit.php @@ -1,21 +1,21 @@ -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); diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d4ff581..ba8b4a6 100755 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -5,11 +5,14 @@ + + + + + + - - - diff --git a/src/Domfactory/Infrastructure/Dom.php b/src/Domfactory/Infrastructure/Dom.php index 3c43154..07eac90 100755 --- a/src/Domfactory/Infrastructure/Dom.php +++ b/src/Domfactory/Infrastructure/Dom.php @@ -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; /** @@ -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()); @@ -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); diff --git a/src/Domfactory/Ports/Dom.php b/src/Domfactory/Ports/Dom.php index 6adf01f..0749764 100755 --- a/src/Domfactory/Ports/Dom.php +++ b/src/Domfactory/Ports/Dom.php @@ -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); } /** diff --git a/src/Domfactory/Tests/Infrastructure/DomTest.php b/src/Domfactory/Tests/Infrastructure/DomTest.php new file mode 100755 index 0000000..6689595 --- /dev/null +++ b/src/Domfactory/Tests/Infrastructure/DomTest.php @@ -0,0 +1,85 @@ + / @jkphl + * @copyright Copyright © 2017 Joschi Kuphal / @jkphl + * @license http://opensource.org/licenses/MIT The MIT License (MIT) + */ + +/*********************************************************************************** + * The MIT License (MIT) + * + * Copyright © 2017 Joschi Kuphal / @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; + } +} diff --git a/src/Domfactory/Tests/Ports/DomTest.php b/src/Domfactory/Tests/Ports/DomTest.php index 00a8dec..476b40c 100755 --- a/src/Domfactory/Tests/Ports/DomTest.php +++ b/src/Domfactory/Tests/Ports/DomTest.php @@ -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