Fixed tests which require a webserver

This commit is contained in:
Francis Besset 2013-07-24 23:18:41 +02:00
parent 491d5f1ab1
commit 9487e3a9e7
10 changed files with 352 additions and 362 deletions

View File

@ -36,7 +36,9 @@
"besimple/soap-server": "self.version" "besimple/soap-server": "self.version"
}, },
"require-dev": { "require-dev": {
"mikey179/vfsStream": "dev-master" "ext-mcrypt": "*",
"mikey179/vfsStream": "dev-master",
"symfony/process": ">=2.3,<2.4-dev"
}, },
"autoload": { "autoload": {
"psr-0": { "BeSimple\\": "src/" } "psr-0": { "BeSimple\\": "src/" }

View File

@ -12,6 +12,10 @@
bootstrap="vendor/autoload.php" bootstrap="vendor/autoload.php"
> >
<php>
<const name="WEBSERVER_PORT" value="8000" />
</php>
<testsuites> <testsuites>
<testsuite name="BeSimpleSoap Test Suite"> <testsuite name="BeSimpleSoap Test Suite">
<directory>./src/BeSimple/*/Tests/</directory> <directory>./src/BeSimple/*/Tests/</directory>

View File

@ -24,9 +24,9 @@
"ext-soap": "*", "ext-soap": "*",
"besimple/soap-common": "self.version", "besimple/soap-common": "self.version",
"ass/xmlsecurity": "dev-master", "ass/xmlsecurity": "dev-master",
"zendframework/zend-soap": "2.1.*",
"zendframework/zend-mime": "2.1.*",
"zendframework/zend-mail": "2.1.*", "zendframework/zend-mail": "2.1.*",
"zendframework/zend-mime": "2.1.*",
"zendframework/zend-soap": "2.1.*",
"symfony/http-foundation": ">=2.0,<2.4-dev" "symfony/http-foundation": ">=2.0,<2.4-dev"
}, },
"suggest": { "suggest": {

View File

@ -0,0 +1,55 @@
<?php
/*
* This file is part of the BeSimpleSoapClient.
*
* (c) Christian Kerl <christian-kerl@web.de>
* (c) Francis Besset <francis.besset@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace BeSimple\SoapClient\Tests;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\ProcessBuilder;
/**
* @author francis.besset@gmail.com <francis.besset@gmail.com>
*/
abstract class AbstractWebServerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ProcessBuilder
*/
static protected $webserver;
static protected $websererPortLength;
public static function setUpBeforeClass()
{
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
self::markTestSkipped('PHP Webserver is available from PHP 5.4');
}
self::$webserver = ProcessBuilder::create(array(
'exec', // used exec binary (https://github.com/symfony/symfony/issues/5759)
(new PhpExecutableFinder())->find(),
'-S',
sprintf('localhost:%d', WEBSERVER_PORT),
'-t',
__DIR__.DIRECTORY_SEPARATOR.'Fixtures',
))->getProcess();
self::$webserver->start();
usleep(100000);
self::$websererPortLength = strlen(WEBSERVER_PORT);
}
public static function tearDownAfterClass()
{
self::$webserver->stop(0);
usleep(100000);
}
}

View File

@ -17,167 +17,91 @@ use BeSimple\SoapClient\Curl;
/** /**
* @author Andreas Schamberger <mail@andreass.net> * @author Andreas Schamberger <mail@andreass.net>
*/ */
class CurlTest extends \PHPUnit_Framework_TestCase class CurlTest extends AbstractWebserverTest
{ {
protected $webserverProcessId;
protected function startPhpWebserver()
{
$this->markTestSkipped('Start a local webserver is not the best practice');
$dir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures';
if ('Windows' == substr(php_uname('s'), 0, 7)) {
$powershellCommand = "\$app = start-process php.exe -ArgumentList '-S localhost:8000 -t ".$dir."' -WindowStyle 'Hidden' -passthru; Echo \$app.Id;";
$shellCommand = 'powershell -command "& {'.$powershellCommand.'}"';
} else {
$shellCommand = "nohup php -S localhost:8000 -t ".$dir." &";
}
$output = array();
exec($shellCommand, $output);
$this->webserverProcessId = $output[0]; // pid is in first element
}
protected function stopPhpWebserver()
{
if (!is_null($this->webserverProcessId)) {
if ('Windows' == substr(php_uname('s'), 0, 7 )) {
exec('TASKKILL /F /PID ' . $this->webserverProcessId);
} else {
exec('kill ' . $this->webserverProcessId);
}
$this->webserverProcessId = null;
}
}
public function testExec() public function testExec()
{ {
$this->startPhpWebserver();
$curl = new Curl(); $curl = new Curl();
$this->assertTrue($curl->exec('http://localhost:8000/curl.txt')); $this->assertTrue($curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT)));
$this->assertTrue($curl->exec('http://localhost:8000/404.txt')); $this->assertTrue($curl->exec(sprintf('http://localhost:%d/404.txt', WEBSERVER_PORT)));
$this->stopPhpWebserver();
} }
public function testGetErrorMessage() public function testGetErrorMessage()
{ {
$this->startPhpWebserver();
$curl = new Curl(); $curl = new Curl();
$curl->exec('http://unknown/curl.txt'); $curl->exec('http://unknown/curl.txt');
$this->assertEquals('Could not connect to host', $curl->getErrorMessage()); $this->assertEquals('Could not connect to host', $curl->getErrorMessage());
$curl->exec('xyz://localhost:8000/@404.txt'); $curl->exec(sprintf('xyz://localhost:%d/@404.txt', WEBSERVER_PORT));
$this->assertEquals('Unknown protocol. Only http and https are allowed.', $curl->getErrorMessage()); $this->assertEquals('Unknown protocol. Only http and https are allowed.', $curl->getErrorMessage());
$curl->exec(''); $curl->exec('');
$this->assertEquals('Unable to parse URL', $curl->getErrorMessage()); $this->assertEquals('Unable to parse URL', $curl->getErrorMessage());
$this->stopPhpWebserver();
} }
public function testGetRequestHeaders() public function testGetRequestHeaders()
{ {
$this->startPhpWebserver();
$curl = new Curl(); $curl = new Curl();
$curl->exec('http://localhost:8000/curl.txt'); $curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT));
$this->assertEquals(136, strlen($curl->getRequestHeaders())); $this->assertEquals(132 + self::$websererPortLength, strlen($curl->getRequestHeaders()));
$curl->exec('http://localhost:8000/404.txt'); $curl->exec(sprintf('http://localhost:%s/404.txt', WEBSERVER_PORT));
$this->assertEquals(135, strlen($curl->getRequestHeaders())); $this->assertEquals(131 + self::$websererPortLength, strlen($curl->getRequestHeaders()));
$this->stopPhpWebserver();
} }
public function testGetResponse() public function testGetResponse()
{ {
$this->startPhpWebserver();
$curl = new Curl(); $curl = new Curl();
$curl->exec('http://localhost:8000/curl.txt'); $curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT));
$this->assertEquals(150, strlen($curl->getResponse())); $this->assertSame('OK', $curl->getResponseStatusMessage());
$this->assertEquals(145 + self::$websererPortLength, strlen($curl->getResponse()));
$curl->exec('http://localhost:8000/404.txt'); $curl->exec(sprintf('http://localhost:%d/404.txt', WEBSERVER_PORT));
$this->assertEquals(1282, strlen($curl->getResponse())); $this->assertSame('Not Found', $curl->getResponseStatusMessage());
$this->stopPhpWebserver();
} }
public function testGetResponseBody() public function testGetResponseBody()
{ {
$this->startPhpWebserver();
$curl = new Curl(); $curl = new Curl();
$curl->exec('http://localhost:8000/curl.txt'); $curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT));
$this->assertEquals('This is a testfile for cURL.', $curl->getResponseBody()); $this->assertEquals('This is a testfile for cURL.', $curl->getResponseBody());
$this->stopPhpWebserver();
} }
public function testGetResponseContentType() public function testGetResponseContentType()
{ {
$this->startPhpWebserver();
$curl = new Curl(); $curl = new Curl();
$curl->exec('http://localhost:8000/curl.txt'); $curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT));
$this->assertEquals('text/plain; charset=UTF-8', $curl->getResponseContentType()); $this->assertEquals('text/plain; charset=UTF-8', $curl->getResponseContentType());
$curl->exec('http://localhost:8000/404.txt'); $curl->exec(sprintf('http://localhost:%d/404.txt', WEBSERVER_PORT));
$this->assertEquals('text/html; charset=UTF-8', $curl->getResponseContentType()); $this->assertEquals('text/html; charset=UTF-8', $curl->getResponseContentType());
$this->stopPhpWebserver();
} }
public function testGetResponseHeaders() public function testGetResponseHeaders()
{ {
$this->startPhpWebserver();
$curl = new Curl(); $curl = new Curl();
$curl->exec('http://localhost:8000/curl.txt'); $curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT));
$this->assertEquals(122, strlen($curl->getResponseHeaders())); $this->assertEquals(117 + self::$websererPortLength, strlen($curl->getResponseHeaders()));
$curl->exec('http://localhost:8000/404.txt'); $curl->exec(sprintf('http://localhost:%d/404.txt', WEBSERVER_PORT));
$this->assertEquals(130, strlen($curl->getResponseHeaders())); $this->assertEquals(124 + self::$websererPortLength, strlen($curl->getResponseHeaders()));
$this->stopPhpWebserver();
} }
public function testGetResponseStatusCode() public function testGetResponseStatusCode()
{ {
$this->startPhpWebserver();
$curl = new Curl(); $curl = new Curl();
$curl->exec('http://localhost:8000/curl.txt'); $curl->exec(sprintf('http://localhost:%d/curl.txt', WEBSERVER_PORT));
$this->assertEquals(200, $curl->getResponseStatusCode()); $this->assertEquals(200, $curl->getResponseStatusCode());
$curl->exec('http://localhost:8000/404.txt'); $curl->exec(sprintf('http://localhost:%d/404.txt', WEBSERVER_PORT));
$this->assertEquals(404, $curl->getResponseStatusCode()); $this->assertEquals(404, $curl->getResponseStatusCode());
$this->stopPhpWebserver();
}
public function testGetResponseStatusMessage()
{
$this->startPhpWebserver();
$curl = new Curl();
$curl->exec('http://localhost:8000/curl.txt');
$this->assertEquals('OK', $curl->getResponseStatusMessage());
$curl->exec('http://localhost:8000/404.txt');
$this->assertEquals('Not Found', $curl->getResponseStatusMessage());
$this->stopPhpWebserver();
} }
} }

View File

@ -13,256 +13,245 @@
namespace BeSimple\SoapClient\Tests; namespace BeSimple\SoapClient\Tests;
use BeSimple\SoapClient\WsdlDownloader; use BeSimple\SoapClient\WsdlDownloader;
use BeSimple\SoapCommon\Cache;
use BeSimple\SoapClient\Curl; use BeSimple\SoapClient\Curl;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamWrapper;
/** /**
* @author Andreas Schamberger <mail@andreass.net> * @author Andreas Schamberger <mail@andreass.net>
* @author Francis Besset <francis.bessset@gmail.com>
*/ */
class WsdlDownloaderTest extends \PHPUnit_Framework_TestCase class WsdlDownloaderTest extends AbstractWebserverTest
{ {
protected $webserverProcessId; /**
* @dataProvider provideDownload
protected function startPhpWebserver() */
public function testDownload($source, $regexp, $nbDownloads)
{ {
$this->markTestSkipped('Start a local webserver is not the best practice'); $wsdlCacheDir = vfsStream::setup('wsdl');
$wsdlCacheUrl = $wsdlCacheDir->url('wsdl');
$dir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'; Cache::setEnabled(Cache::ENABLED);
if ('Windows' == substr(php_uname('s'), 0, 7)) { Cache::setDirectory($wsdlCacheUrl);
$powershellCommand = "\$app = start-process php.exe -ArgumentList '-S localhost:8000 -t ".$dir."' -WindowStyle 'Hidden' -passthru; Echo \$app.Id;"; $cacheDirForRegExp = preg_quote($wsdlCacheUrl, '#');
$shellCommand = 'powershell -command "& {'.$powershellCommand.'}"';
} else { $wsdlDownloader = new WsdlDownloader(new Curl());
$shellCommand = "nohup php -S localhost:8000 -t ".$dir." &"; $this->assertCount(0, $wsdlCacheDir->getChildren());
}
$output = array(); $cacheFileName = $wsdlDownloader->download($source);
exec($shellCommand, $output); $this->assertCount($nbDownloads, $wsdlCacheDir->getChildren());
$this->webserverProcessId = $output[0]; // pid is in first element
$this->assertRegExp('#'.sprintf($regexp, $cacheDirForRegExp).'#', file_get_contents($cacheFileName));
} }
protected function stopPhpWebserver() public function provideDownload()
{ {
if (!is_null($this->webserverProcessId)) { return array(
if ('Windows' == substr(php_uname('s'), 0, 7)) { array(
exec('TASKKILL /F /PID ' . $this->webserverProcessId); __DIR__.DIRECTORY_SEPARATOR.'Fixtures/xsdinclude/xsdinctest_absolute.xml',
} else { '%s/wsdl_[a-f0-9]{32}\.cache',
exec('kill ' . $this->webserverProcessId); 2,
}
$this->webserverProcessId = null;
}
}
public function testDownload()
{
$this->startPhpWebserver();
$curl = new Curl();
$wd = new WsdlDownloader($curl);
$cacheDir = ini_get('soap.wsdl_cache_dir');
if (!is_dir($cacheDir)) {
$cacheDir = sys_get_temp_dir();
$cacheDirForRegExp = preg_quote($cacheDir);
}
$tests = array(
'localWithAbsolutePath' => array(
'source' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/xsdinclude/xsdinctest_absolute.xml',
'assertRegExp' => '~.*'.$cacheDirForRegExp.'\\\wsdl_.*\.cache.*~',
), ),
'localWithRelativePath' => array( array(
'source' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/xsdinclude/xsdinctest_relative.xml', __DIR__.DIRECTORY_SEPARATOR.'Fixtures/xsdinclude/xsdinctest_relative.xml',
'assertRegExp' => '~.*\.\./type_include\.xsd.*~', '\.\./type_include\.xsd',
1,
), ),
'remoteWithAbsolutePath' => array( array(
'source' => 'http://localhost:8000/xsdinclude/xsdinctest_absolute.xml', sprintf('http://localhost:%d/xsdinclude/xsdinctest_absolute.xml', WEBSERVER_PORT),
'assertRegExp' => '~.*'.$cacheDirForRegExp.'\\\wsdl_.*\.cache.*~', '%s/wsdl_[a-f0-9]{32}\.cache',
2,
), ),
'remoteWithAbsolutePath' => array( array(
'source' => 'http://localhost:8000/xsdinclude/xsdinctest_relative.xml', sprintf('http://localhost:%d/xsdinclude/xsdinctest_relative.xml', WEBSERVER_PORT),
'assertRegExp' => '~.*'.$cacheDirForRegExp.'\\\wsdl_.*\.cache.*~', '%s/wsdl_[a-f0-9]{32}\.cache',
2,
), ),
); );
foreach ($tests as $name => $values) {
$cacheFileName = $wd->download($values['source']);
$result = file_get_contents($cacheFileName);
$this->assertRegExp($values['assertRegExp'], $result, $name);
unlink($cacheFileName);
}
$this->stopPhpWebserver();
} }
public function testIsRemoteFile() public function testIsRemoteFile()
{ {
$curl = new Curl(); $wsdlDownloader = new WsdlDownloader(new Curl());
$wd = new WsdlDownloader($curl);
$class = new \ReflectionClass($wd); $r = new \ReflectionClass($wsdlDownloader);
$method = $class->getMethod('isRemoteFile'); $m = $r->getMethod('isRemoteFile');
$method->setAccessible(true); $m->setAccessible(true);
$this->assertTrue($method->invoke($wd, 'http://www.php.net/')); $this->assertTrue($m->invoke($wsdlDownloader, 'http://www.php.net/'));
$this->assertTrue($method->invoke($wd, 'http://localhost/')); $this->assertTrue($m->invoke($wsdlDownloader, 'http://localhost/'));
$this->assertTrue($method->invoke($wd, 'http://mylocaldomain/')); $this->assertTrue($m->invoke($wsdlDownloader, 'http://mylocaldomain/'));
$this->assertTrue($method->invoke($wd, 'http://www.php.net/dir/test.html')); $this->assertTrue($m->invoke($wsdlDownloader, 'http://www.php.net/dir/test.html'));
$this->assertTrue($method->invoke($wd, 'http://localhost/dir/test.html')); $this->assertTrue($m->invoke($wsdlDownloader, 'http://localhost/dir/test.html'));
$this->assertTrue($method->invoke($wd, 'http://mylocaldomain/dir/test.html')); $this->assertTrue($m->invoke($wsdlDownloader, 'http://mylocaldomain/dir/test.html'));
$this->assertTrue($method->invoke($wd, 'https://www.php.net/')); $this->assertTrue($m->invoke($wsdlDownloader, 'https://www.php.net/'));
$this->assertTrue($method->invoke($wd, 'https://localhost/')); $this->assertTrue($m->invoke($wsdlDownloader, 'https://localhost/'));
$this->assertTrue($method->invoke($wd, 'https://mylocaldomain/')); $this->assertTrue($m->invoke($wsdlDownloader, 'https://mylocaldomain/'));
$this->assertTrue($method->invoke($wd, 'https://www.php.net/dir/test.html')); $this->assertTrue($m->invoke($wsdlDownloader, 'https://www.php.net/dir/test.html'));
$this->assertTrue($method->invoke($wd, 'https://localhost/dir/test.html')); $this->assertTrue($m->invoke($wsdlDownloader, 'https://localhost/dir/test.html'));
$this->assertTrue($method->invoke($wd, 'https://mylocaldomain/dir/test.html')); $this->assertTrue($m->invoke($wsdlDownloader, 'https://mylocaldomain/dir/test.html'));
$this->assertFalse($method->invoke($wd, 'c:/dir/test.html')); $this->assertFalse($m->invoke($wsdlDownloader, 'c:/dir/test.html'));
$this->assertFalse($method->invoke($wd, '/dir/test.html')); $this->assertFalse($m->invoke($wsdlDownloader, '/dir/test.html'));
$this->assertFalse($method->invoke($wd, '../dir/test.html')); $this->assertFalse($m->invoke($wsdlDownloader, '../dir/test.html'));
} }
public function testResolveWsdlIncludes() /**
* @dataProvider provideResolveWsdlIncludes
*/
public function testResolveWsdlIncludes($source, $cacheFile, $remoteParentUrl, $regexp, $nbDownloads)
{ {
$this->startPhpWebserver(); $wsdlCacheDir = vfsStream::setup('wsdl');
$wsdlCacheUrl = $wsdlCacheDir->url('wsdl');
$curl = new Curl(); Cache::setEnabled(Cache::ENABLED);
$wd = new WsdlDownloader($curl); Cache::setDirectory($wsdlCacheUrl);
$cacheDirForRegExp = preg_quote($wsdlCacheUrl, '#');
$class = new \ReflectionClass($wd); $wsdlDownloader = new WsdlDownloader(new Curl());
$method = $class->getMethod('resolveRemoteIncludes'); $r = new \ReflectionClass($wsdlDownloader);
$method->setAccessible(true); $m = $r->getMethod('resolveRemoteIncludes');
$m->setAccessible(true);
$cacheDir = ini_get('soap.wsdl_cache_dir'); $this->assertCount(0, $wsdlCacheDir->getChildren());
if (!is_dir($cacheDir)) {
$cacheDir = sys_get_temp_dir(); $cacheFile = sprintf($cacheFile, $wsdlCacheUrl);
$cacheDirForRegExp = preg_quote($cacheDir); $m->invoke($wsdlDownloader, file_get_contents($source), $cacheFile, $remoteParentUrl);
$this->assertCount($nbDownloads, $wsdlCacheDir->getChildren());
$this->assertRegExp('#'.sprintf($regexp, $cacheDirForRegExp).'#', file_get_contents($cacheFile));
} }
$remoteUrlAbsolute = 'http://localhost:8000/wsdlinclude/wsdlinctest_absolute.xml'; public function provideResolveWsdlIncludes()
$remoteUrlRelative = 'http://localhost:8000/wsdlinclude/wsdlinctest_relative.xml'; {
$tests = array( $remoteUrlAbsolute = sprintf('http://localhost:%d/wsdlinclude/wsdlinctest_absolute.xml', WEBSERVER_PORT);
'localWithAbsolutePath' => array( $remoteUrlRelative = sprintf('http://localhost:%d/wsdlinclude/wsdlinctest_relative.xml', WEBSERVER_PORT);
'source' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/wsdlinclude/wsdlinctest_absolute.xml',
'cacheFile' => $cacheDir.'/cache_local_absolute.xml', return array(
'remoteParentUrl' => null, array(
'assertRegExp' => '~.*'.$cacheDirForRegExp.'\\\wsdl_.*\.cache.*~', __DIR__.DIRECTORY_SEPARATOR.'Fixtures/wsdlinclude/wsdlinctest_absolute.xml',
'%s/cache_local_absolute.xml',
null,
'%s/wsdl_[a-f0-9]{32}.cache',
2,
), ),
'localWithRelativePath' => array( array(
'source' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/wsdlinclude/wsdlinctest_relative.xml', __DIR__.DIRECTORY_SEPARATOR.'Fixtures/wsdlinclude/wsdlinctest_relative.xml',
'cacheFile' => $cacheDir.'/cache_local_relative.xml', '%s/cache_local_relative.xml',
'remoteParentUrl' => null, null,
'assertRegExp' => '~.*\.\./wsdl_include\.wsdl.*~', '\.\./wsdl_include\.wsdl',
1,
), ),
'remoteWithAbsolutePath' => array( array(
'source' => $remoteUrlAbsolute, $remoteUrlAbsolute,
'cacheFile' => $cacheDir.'/cache_remote_absolute.xml', '%s/cache_remote_absolute.xml',
'remoteParentUrl' => $remoteUrlAbsolute, $remoteUrlAbsolute,
'assertRegExp' => '~.*'.$cacheDirForRegExp.'\\\wsdl_.*\.cache.*~', '%s/wsdl_[a-f0-9]{32}\.cache',
2,
), ),
'remoteWithAbsolutePath' => array( array(
'source' => $remoteUrlRelative, $remoteUrlRelative,
'cacheFile' => $cacheDir.'/cache_remote_relative.xml', '%s/cache_remote_relative.xml',
'remoteParentUrl' => $remoteUrlRelative, $remoteUrlRelative,
'assertRegExp' => '~.*'.$cacheDirForRegExp.'\\\wsdl_.*\.cache.*~', '%s/wsdl_[a-f0-9]{32}\.cache',
2
), ),
); );
foreach ($tests as $name => $values) {
$wsdl = file_get_contents($values['source']);
$method->invoke($wd, $wsdl, $values['cacheFile'], $values['remoteParentUrl']);
$result = file_get_contents($values['cacheFile']);
$this->assertRegExp($values['assertRegExp'], $result, $name);
unlink($values['cacheFile']);
} }
$this->stopPhpWebserver(); /**
} * @dataProvider provideResolveXsdIncludes
*/
public function testResolveXsdIncludes() public function testResolveXsdIncludes($source, $cacheFile, $remoteParentUrl, $regexp, $nbDownloads)
{ {
$this->startPhpWebserver(); $wsdlCacheDir = vfsStream::setup('wsdl');
$wsdlCacheUrl = $wsdlCacheDir->url('wsdl');
$curl = new Curl(); Cache::setEnabled(Cache::ENABLED);
$wd = new WsdlDownloader($curl); Cache::setDirectory($wsdlCacheUrl);
$cacheDirForRegExp = preg_quote($wsdlCacheUrl, '#');
$class = new \ReflectionClass($wd); $wsdlDownloader = new WsdlDownloader(new Curl());
$method = $class->getMethod('resolveRemoteIncludes'); $r = new \ReflectionClass($wsdlDownloader);
$method->setAccessible(true); $m = $r->getMethod('resolveRemoteIncludes');
$m->setAccessible(true);
$cacheDir = ini_get('soap.wsdl_cache_dir'); $this->assertCount(0, $wsdlCacheDir->getChildren());
if (!is_dir($cacheDir)) {
$cacheDir = sys_get_temp_dir(); $cacheFile = sprintf($cacheFile, $wsdlCacheUrl);
$cacheDirForRegExp = preg_quote($cacheDir); $m->invoke($wsdlDownloader, file_get_contents($source), $cacheFile, $remoteParentUrl);
$this->assertCount($nbDownloads, $wsdlCacheDir->getChildren());
$this->assertRegExp('#'.sprintf($regexp, $cacheDirForRegExp).'#', file_get_contents($cacheFile));
} }
$remoteUrlAbsolute = 'http://localhost:8000/xsdinclude/xsdinctest_absolute.xml'; public function provideResolveXsdIncludes()
$remoteUrlRelative = 'http://localhost:8000/xsdinclude/xsdinctest_relative.xml'; {
$tests = array( $remoteUrlAbsolute = sprintf('http://localhost:%d/xsdinclude/xsdinctest_absolute.xml', WEBSERVER_PORT);
'localWithAbsolutePath' => array( $remoteUrlRelative = sprintf('http://localhost:%d/xsdinclude/xsdinctest_relative.xml', WEBSERVER_PORT);
'source' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/xsdinclude/xsdinctest_absolute.xml',
'cacheFile' => $cacheDir.'/cache_local_absolute.xml', return array(
'remoteParentUrl' => null, array(
'assertRegExp' => '~.*'.$cacheDirForRegExp.'\\\wsdl_.*\.cache.*~', __DIR__.DIRECTORY_SEPARATOR.'Fixtures/xsdinclude/xsdinctest_absolute.xml',
'%s/cache_local_absolute.xml',
null,
'%s/wsdl_[a-f0-9]{32}\.cache',
2,
), ),
'localWithRelativePath' => array( array(
'source' => __DIR__.DIRECTORY_SEPARATOR.'Fixtures/xsdinclude/xsdinctest_relative.xml', __DIR__.DIRECTORY_SEPARATOR.'Fixtures/xsdinclude/xsdinctest_relative.xml',
'cacheFile' => $cacheDir.'/cache_local_relative.xml', '%s/cache_local_relative.xml',
'remoteParentUrl' => null, null,
'assertRegExp' => '~.*\.\./type_include\.xsd.*~', '\.\./type_include\.xsd',
1,
), ),
'remoteWithAbsolutePath' => array( array(
'source' => $remoteUrlAbsolute, $remoteUrlAbsolute,
'cacheFile' => $cacheDir.'/cache_remote_absolute.xml', '%s/cache_remote_absolute.xml',
'remoteParentUrl' => $remoteUrlAbsolute, $remoteUrlAbsolute,
'assertRegExp' => '~.*'.$cacheDirForRegExp.'\\\wsdl_.*\.cache.*~', '%s/wsdl_[a-f0-9]{32}\.cache',
2,
), ),
'remoteWithAbsolutePath' => array( array(
'source' => $remoteUrlRelative, $remoteUrlRelative,
'cacheFile' => $cacheDir.'/cache_remote_relative.xml', '%s/cache_remote_relative.xml',
'remoteParentUrl' => $remoteUrlRelative, $remoteUrlRelative,
'assertRegExp' => '~.*'.$cacheDirForRegExp.'\\\wsdl_.*\.cache.*~', '%s/wsdl_[a-f0-9]{32}\.cache',
2,
), ),
); );
foreach ($tests as $name => $values) {
$wsdl = file_get_contents($values['source']);
$method->invoke($wd, $wsdl, $values['cacheFile'], $values['remoteParentUrl']);
$result = file_get_contents($values['cacheFile']);
$this->assertRegExp($values['assertRegExp'], $result, $name);
unlink($values['cacheFile']);
}
$this->stopPhpWebserver();
} }
public function testResolveRelativePathInUrl() public function testResolveRelativePathInUrl()
{ {
$curl = new Curl(); $wsdlDownloader = new WsdlDownloader(new Curl());
$wd = new WsdlDownloader($curl);
$class = new \ReflectionClass($wd); $r = new \ReflectionClass($wsdlDownloader);
$method = $class->getMethod('resolveRelativePathInUrl'); $m = $r->getMethod('resolveRelativePathInUrl');
$method->setAccessible(true); $m->setAccessible(true);
$this->assertEquals('http://localhost:8080/test', $method->invoke($wd, 'http://localhost:8080/sub', '/test')); $this->assertEquals('http://localhost:8080/test', $m->invoke($wsdlDownloader, 'http://localhost:8080/sub', '/test'));
$this->assertEquals('http://localhost:8080/test', $method->invoke($wd, 'http://localhost:8080/sub/', '/test')); $this->assertEquals('http://localhost:8080/test', $m->invoke($wsdlDownloader, 'http://localhost:8080/sub/', '/test'));
$this->assertEquals('http://localhost/test', $method->invoke($wd, 'http://localhost/sub', '/test')); $this->assertEquals('http://localhost/test', $m->invoke($wsdlDownloader, 'http://localhost/sub', '/test'));
$this->assertEquals('http://localhost/test', $method->invoke($wd, 'http://localhost/sub/', '/test')); $this->assertEquals('http://localhost/test', $m->invoke($wsdlDownloader, 'http://localhost/sub/', '/test'));
$this->assertEquals('http://localhost/test', $method->invoke($wd, 'http://localhost', './test')); $this->assertEquals('http://localhost/test', $m->invoke($wsdlDownloader, 'http://localhost', './test'));
$this->assertEquals('http://localhost/test', $method->invoke($wd, 'http://localhost/', './test')); $this->assertEquals('http://localhost/test', $m->invoke($wsdlDownloader, 'http://localhost/', './test'));
$this->assertEquals('http://localhost/sub/test', $method->invoke($wd, 'http://localhost/sub/sub', './test')); $this->assertEquals('http://localhost/sub/test', $m->invoke($wsdlDownloader, 'http://localhost/sub/sub', './test'));
$this->assertEquals('http://localhost/sub/sub/test', $method->invoke($wd, 'http://localhost/sub/sub/', './test')); $this->assertEquals('http://localhost/sub/sub/test', $m->invoke($wsdlDownloader, 'http://localhost/sub/sub/', './test'));
$this->assertEquals('http://localhost/test', $method->invoke($wd, 'http://localhost/sub/sub', '../test')); $this->assertEquals('http://localhost/test', $m->invoke($wsdlDownloader, 'http://localhost/sub/sub', '../test'));
$this->assertEquals('http://localhost/sub/test', $method->invoke($wd, 'http://localhost/sub/sub/', '../test')); $this->assertEquals('http://localhost/sub/test', $m->invoke($wsdlDownloader, 'http://localhost/sub/sub/', '../test'));
$this->assertEquals('http://localhost/test', $method->invoke($wd, 'http://localhost/sub/sub/sub', '../../test')); $this->assertEquals('http://localhost/test', $m->invoke($wsdlDownloader, 'http://localhost/sub/sub/sub', '../../test'));
$this->assertEquals('http://localhost/sub/test', $method->invoke($wd, 'http://localhost/sub/sub/sub/', '../../test')); $this->assertEquals('http://localhost/sub/test', $m->invoke($wsdlDownloader, 'http://localhost/sub/sub/sub/', '../../test'));
$this->assertEquals('http://localhost/test', $method->invoke($wd, 'http://localhost/sub/sub/sub/sub', '../../../test')); $this->assertEquals('http://localhost/test', $m->invoke($wsdlDownloader, 'http://localhost/sub/sub/sub/sub', '../../../test'));
$this->assertEquals('http://localhost/sub/test', $method->invoke($wd, 'http://localhost/sub/sub/sub/sub/', '../../../test')); $this->assertEquals('http://localhost/sub/test', $m->invoke($wsdlDownloader, 'http://localhost/sub/sub/sub/sub/', '../../../test'));
$this->assertEquals('http://localhost/test', $method->invoke($wd, 'http://localhost/sub/sub/sub', '../../../test')); $this->assertEquals('http://localhost/test', $m->invoke($wsdlDownloader, 'http://localhost/sub/sub/sub', '../../../test'));
$this->assertEquals('http://localhost/test', $method->invoke($wd, 'http://localhost/sub/sub/sub/', '../../../test')); $this->assertEquals('http://localhost/test', $m->invoke($wsdlDownloader, 'http://localhost/sub/sub/sub/', '../../../test'));
} }
} }

View File

@ -12,6 +12,7 @@
namespace BeSimple\SoapClient; namespace BeSimple\SoapClient;
use BeSimple\SoapCommon\Cache;
use BeSimple\SoapCommon\Helper; use BeSimple\SoapCommon\Helper;
/** /**
@ -66,22 +67,18 @@ class WsdlDownloader
* @param boolean $resolveRemoteIncludes WSDL/XSD include enabled? * @param boolean $resolveRemoteIncludes WSDL/XSD include enabled?
* @param boolean $cacheWsdl Cache constant * @param boolean $cacheWsdl Cache constant
*/ */
public function __construct(Curl $curl, $resolveRemoteIncludes = true, $cacheWsdl = WSDL_CACHE_DISK) public function __construct(Curl $curl, $resolveRemoteIncludes = true, $cacheWsdl = Cache::TYPE_DISK)
{ {
$this->curl = $curl; $this->curl = $curl;
$this->resolveRemoteIncludes = $resolveRemoteIncludes; $this->resolveRemoteIncludes = (Boolean) $resolveRemoteIncludes;
// get current WSDL caching config // get current WSDL caching config
$this->cacheEnabled = (bool) ini_get('soap.wsdl_cache_enabled'); $this->cacheEnabled = $cacheWsdl === Cache::TYPE_NONE ? Cache::DISABLED : Cache::ENABLED == Cache::isEnabled();
if ($this->cacheEnabled === true
&& $cacheWsdl === WSDL_CACHE_NONE) { if ($this->cacheEnabled) {
$this->cacheEnabled = false; $this->cacheDir = Cache::getDirectory();
$this->cacheTtl = Cache::getLifetime();
} }
$this->cacheDir = ini_get('soap.wsdl_cache_dir');
if (!is_dir($this->cacheDir)) {
$this->cacheDir = sys_get_temp_dir();
}
$this->cacheDir = rtrim($this->cacheDir, '/\\');
$this->cacheTtl = ini_get('soap.wsdl_cache_ttl');
} }
/** /**
@ -96,40 +93,39 @@ class WsdlDownloader
// download and cache remote WSDL files or local ones where we want to // download and cache remote WSDL files or local ones where we want to
// resolve remote XSD includes // resolve remote XSD includes
$isRemoteFile = $this->isRemoteFile($wsdl); $isRemoteFile = $this->isRemoteFile($wsdl);
if ($isRemoteFile === true || $this->resolveRemoteIncludes === true) { if ($isRemoteFile || $this->resolveRemoteIncludes) {
$cacheFile = $this->cacheDir . DIRECTORY_SEPARATOR . 'wsdl_' . md5($wsdl) . '.cache'; $cacheFilePath = $this->cacheDir.DIRECTORY_SEPARATOR.'wsdl_'.md5($wsdl).'.cache';
if ($this->cacheEnabled === false
|| !file_exists($cacheFile) if (!$this->cacheEnabled || !file_exists($cacheFilePath) || (filemtime($cacheFilePath) + $this->cacheTtl) < time()) {
|| (filemtime($cacheFile) + $this->cacheTtl) < time()) { if ($isRemoteFile) {
if ($isRemoteFile === true) {
// execute request // execute request
$responseSuccessfull = $this->curl->exec($wsdl); $responseSuccessfull = $this->curl->exec($wsdl);
// get content // get content
if ($responseSuccessfull === true) { if ($responseSuccessfull) {
$response = $this->curl->getResponseBody(); $response = $this->curl->getResponseBody();
if ($this->resolveRemoteIncludes === true) {
$this->resolveRemoteIncludes($response, $cacheFile, $wsdl); if ($this->resolveRemoteIncludes) {
$this->resolveRemoteIncludes($response, $cacheFilePath, $wsdl);
} else { } else {
file_put_contents($cacheFile, $response); file_put_contents($cacheFilePath, $response);
} }
} else { } else {
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'"); throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
} }
} elseif (file_exists($wsdl)) { } elseif (file_exists($wsdl)) {
$response = file_get_contents($wsdl); $response = file_get_contents($wsdl);
$this->resolveRemoteIncludes($response, $cacheFile); $this->resolveRemoteIncludes($response, $cacheFilePath);
} else { } else {
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'"); throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
} }
} }
return $cacheFile; return $cacheFilePath;
} elseif (file_exists($wsdl)) { } elseif (file_exists($wsdl)) {
return realpath($wsdl); return realpath($wsdl);
} else {
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
} }
throw new \ErrorException("SOAP-ERROR: Parsing WSDL: Couldn't load from '" . $wsdl ."'");
} }
/** /**
@ -141,33 +137,34 @@ class WsdlDownloader
*/ */
private function isRemoteFile($file) private function isRemoteFile($file)
{ {
$isRemoteFile = false;
// @parse_url to suppress E_WARNING for invalid urls // @parse_url to suppress E_WARNING for invalid urls
if (($url = @parse_url($file)) !== false) { if (false !== $url = @parse_url($file)) {
if (isset($url['scheme']) && substr($url['scheme'], 0, 4) == 'http') { if (isset($url['scheme']) && 'http' === substr($url['scheme'], 0, 4)) {
$isRemoteFile = true; return true;
} }
} }
return $isRemoteFile; return false;
} }
/** /**
* Resolves remote WSDL/XSD includes within the WSDL files. * Resolves remote WSDL/XSD includes within the WSDL files.
* *
* @param string $xml XML file * @param string $xml XML file
* @param string $cacheFile Cache file name * @param string $cacheFilePath Cache file name
* @param boolean $parentFile Parent file name * @param boolean $parentFilePath Parent file name
* *
* @return void * @return void
*/ */
private function resolveRemoteIncludes($xml, $cacheFile, $parentFile = null) private function resolveRemoteIncludes($xml, $cacheFilePath, $parentFilePath = null)
{ {
$doc = new \DOMDocument(); $doc = new \DOMDocument();
$doc->loadXML($xml); $doc->loadXML($xml);
$xpath = new \DOMXPath($doc); $xpath = new \DOMXPath($doc);
$xpath->registerNamespace(Helper::PFX_XML_SCHEMA, Helper::NS_XML_SCHEMA); $xpath->registerNamespace(Helper::PFX_XML_SCHEMA, Helper::NS_XML_SCHEMA);
$xpath->registerNamespace(Helper::PFX_WSDL, Helper::NS_WSDL); $xpath->registerNamespace(Helper::PFX_WSDL, Helper::NS_WSDL);
// WSDL include/import // WSDL include/import
$query = './/'.Helper::PFX_WSDL.':include | .//'.Helper::PFX_WSDL.':import'; $query = './/'.Helper::PFX_WSDL.':include | .//'.Helper::PFX_WSDL.':import';
$nodes = $xpath->query($query); $nodes = $xpath->query($query);
@ -177,13 +174,14 @@ class WsdlDownloader
if ($this->isRemoteFile($location)) { if ($this->isRemoteFile($location)) {
$location = $this->download($location); $location = $this->download($location);
$node->setAttribute('location', $location); $node->setAttribute('location', $location);
} elseif (!is_null($parentFile)) { } elseif (null !== $parentFilePath) {
$location = $this->resolveRelativePathInUrl($parentFile, $location); $location = $this->resolveRelativePathInUrl($parentFilePath, $location);
$location = $this->download($location); $location = $this->download($location);
$node->setAttribute('location', $location); $node->setAttribute('location', $location);
} }
} }
} }
// XML schema include/import // XML schema include/import
$query = './/'.Helper::PFX_XML_SCHEMA.':include | .//'.Helper::PFX_XML_SCHEMA.':import'; $query = './/'.Helper::PFX_XML_SCHEMA.':include | .//'.Helper::PFX_XML_SCHEMA.':import';
$nodes = $xpath->query($query); $nodes = $xpath->query($query);
@ -194,15 +192,16 @@ class WsdlDownloader
if ($this->isRemoteFile($schemaLocation)) { if ($this->isRemoteFile($schemaLocation)) {
$schemaLocation = $this->download($schemaLocation); $schemaLocation = $this->download($schemaLocation);
$node->setAttribute('schemaLocation', $schemaLocation); $node->setAttribute('schemaLocation', $schemaLocation);
} elseif (null !== $parentFile) { } elseif (null !== $parentFilePath) {
$schemaLocation = $this->resolveRelativePathInUrl($parentFile, $schemaLocation); $schemaLocation = $this->resolveRelativePathInUrl($parentFilePath, $schemaLocation);
$schemaLocation = $this->download($schemaLocation); $schemaLocation = $this->download($schemaLocation);
$node->setAttribute('schemaLocation', $schemaLocation); $node->setAttribute('schemaLocation', $schemaLocation);
} }
} }
} }
} }
$doc->save($cacheFile);
$doc->save($cacheFilePath);
} }
/** /**
@ -216,8 +215,9 @@ class WsdlDownloader
private function resolveRelativePathInUrl($base, $relative) private function resolveRelativePathInUrl($base, $relative)
{ {
$urlParts = parse_url($base); $urlParts = parse_url($base);
// combine base path with relative path // combine base path with relative path
if (isset($urlParts['path']) && strpos($relative, '/') === 0) { if (isset($urlParts['path']) && '/' === $relative{0}) {
// $relative is absolute path from domain (starts with /) // $relative is absolute path from domain (starts with /)
$path = $relative; $path = $relative;
} elseif (isset($urlParts['path']) && strrpos($urlParts['path'], '/') === (strlen($urlParts['path']) )) { } elseif (isset($urlParts['path']) && strrpos($urlParts['path'], '/') === (strlen($urlParts['path']) )) {
@ -230,27 +230,33 @@ class WsdlDownloader
// no base path // no base path
$path = '/'.$relative; $path = '/'.$relative;
} }
// foo/./bar ==> foo/bar // foo/./bar ==> foo/bar
$path = preg_replace('~/\./~', '/', $path);
// remove double slashes // remove double slashes
$path = preg_replace('~/+~', '/', $path); $path = preg_replace(array('#/\./#', '#/+#'), '/', $path);
// split path by '/' // split path by '/'
$parts = explode('/', $path); $parts = explode('/', $path);
// resolve /../ // resolve /../
foreach ($parts as $key => $part) { foreach ($parts as $key => $part) {
if ($part == "..") { if ('..' === $part) {
$keyToDelete = $key - 1; $keyToDelete = $key - 1;
while ($keyToDelete > 0) { while ($keyToDelete > 0) {
if (isset($parts[$keyToDelete])) { if (isset($parts[$keyToDelete])) {
unset($parts[$keyToDelete]); unset($parts[$keyToDelete]);
break; break;
} else { }
$keyToDelete--; $keyToDelete--;
} }
}
unset($parts[$key]); unset($parts[$key]);
} }
} }
$hostname = $urlParts['scheme'].'://'.$urlParts['host']; $hostname = $urlParts['scheme'].'://'.$urlParts['host'];
if (isset($urlParts['port'])) { if (isset($urlParts['port'])) {
$hostname .= ':'.$urlParts['port']; $hostname .= ':'.$urlParts['port'];

View File

@ -26,6 +26,10 @@
"besimple/soap-common": "self.version", "besimple/soap-common": "self.version",
"ass/xmlsecurity": "dev-master" "ass/xmlsecurity": "dev-master"
}, },
"require-dev": {
"mikey179/vfsStream": "dev-master",
"symfony/process": ">=2.3,<2.4-dev"
},
"autoload": { "autoload": {
"psr-0": { "BeSimple\\SoapClient": "" } "psr-0": { "BeSimple\\SoapClient": "" }
}, },

View File

@ -11,6 +11,11 @@
syntaxCheck="false" syntaxCheck="false"
bootstrap="vendor/autoload.php" bootstrap="vendor/autoload.php"
> >
<php>
<const name="WEBSERVER_PORT" value="8000" />
</php>
<testsuites> <testsuites>
<testsuite name="BeSimple SoapClient Test Suite"> <testsuite name="BeSimple SoapClient Test Suite">
<directory>./Tests/</directory> <directory>./Tests/</directory>

View File

@ -25,6 +25,7 @@
"ass/xmlsecurity": "dev-master" "ass/xmlsecurity": "dev-master"
}, },
"require-dev": { "require-dev": {
"ext-mcrypt": "*",
"mikey179/vfsStream": "dev-master" "mikey179/vfsStream": "dev-master"
}, },
"autoload": { "autoload": {