Skip to content

Commit

Permalink
URL validation has been removed as it cannot cover all use cases. A r…
Browse files Browse the repository at this point in the history
…equest with an invalid URL fails with a status code of 0 - #119
  • Loading branch information
jonnnnyw committed Nov 15, 2016
1 parent bd17d5e commit 23c9092
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 65 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -22,7 +22,7 @@
"jakoch/phantomjs-installer": "^2.1"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"phpunit/phpunit": "~5.0",
"zendframework/zendpdf": "~2.0",
"smalot/pdfparser": "~0.9"
},
Expand Down
29 changes: 29 additions & 0 deletions src/JonnyW/PhantomJs/Engine.php
Expand Up @@ -33,6 +33,14 @@ class Engine
*/
protected $debug;

/**
* Cache flag.
*
* @var boolean
* @access protected
*/
protected $cache;

/**
* PhantomJs run options.
*
Expand All @@ -59,6 +67,9 @@ public function __construct()
{
$this->path = 'bin/phantomjs';
$this->options = array();

$this->debug = false;
$this->cache = true;
}

/**
Expand All @@ -75,6 +86,10 @@ public function getCommand()

$this->validateExecutable($path);

if ($this->cache) {
array_push($options, '--disk-cache=true');
}

if ($this->debug) {
array_push($options, '--debug=true');
}
Expand Down Expand Up @@ -164,6 +179,20 @@ public function debug($doDebug)
return $this;
}

/**
* Cache.
*
* @access public
* @param boolean $doCache
* @return \JonnyW\PhantomJs\Client
*/
public function cache($doCache)
{
$this->cache = $doCache;

return $this;
}

/**
* Log info.
*
Expand Down
8 changes: 1 addition & 7 deletions src/JonnyW/PhantomJs/Http/AbstractRequest.php
Expand Up @@ -9,7 +9,6 @@

namespace JonnyW\PhantomJs\Http;

use JonnyW\PhantomJs\Exception\InvalidUrlException;
use JonnyW\PhantomJs\Exception\InvalidMethodException;
use JonnyW\PhantomJs\Procedure\InputInterface;

Expand Down Expand Up @@ -243,16 +242,11 @@ public function getViewportHeight()
* Set request URL
*
* @access public
* @param string $url
* @param string $url
* @return \JonnyW\PhantomJs\Http\AbstractRequest
* @throws \JonnyW\PhantomJs\Exception\InvalidUrlException
*/
public function setUrl($url)
{
if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
throw new InvalidUrlException(sprintf('Invalid URL provided: %s', $url));
}

$this->url = $url;

return $this;
Expand Down
Expand Up @@ -60,7 +60,7 @@ public function testProcedureIsLoadedFromCacheIfCacheIsEnabled()
$request = $this->getRequest();
$request->setUrl('http://test.com');

$renderer = $this->getMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
$renderer = $this->createMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
$renderer->expects($this->exactly(1))
->method('render')
->will($this->returnValue('var test=1; phantom.exit(1);'));
Expand Down Expand Up @@ -94,7 +94,7 @@ public function testProcedureIsNotLoadedFromCacheIfCacheIsDisabled()
$request = $this->getRequest();
$request->setUrl('http://test.com');

$renderer = $this->getMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
$renderer = $this->createMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
$renderer->expects($this->exactly(2))
->method('render')
->will($this->returnValue('var test=1; phantom.exit(1);'));
Expand Down Expand Up @@ -127,7 +127,7 @@ public function testProcedureCacheCanBeCleared()
$request = $this->getRequest();
$request->setUrl('http://test.com');

$renderer = $this->getMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
$renderer = $this->createMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
$renderer->expects($this->exactly(2))
->method('render')
->will($this->returnValue('var test=1; phantom.exit(1);'));
Expand Down
8 changes: 4 additions & 4 deletions src/JonnyW/PhantomJs/Tests/Unit/ClientTest.php
Expand Up @@ -121,7 +121,7 @@ protected function getClient(Engine $engine, ProcedureLoaderInterface $procedure
*/
protected function getEngine()
{
$engine = $this->getMock('\JonnyW\PhantomJs\Engine');
$engine = $this->createMock('\JonnyW\PhantomJs\Engine');

return $engine;
}
Expand All @@ -134,7 +134,7 @@ protected function getEngine()
*/
protected function getMessageFactory()
{
$messageFactory = $this->getMock('\JonnyW\PhantomJs\Http\MessageFactoryInterface');
$messageFactory = $this->createMock('\JonnyW\PhantomJs\Http\MessageFactoryInterface');

return $messageFactory;
}
Expand All @@ -147,7 +147,7 @@ protected function getMessageFactory()
*/
protected function getProcedureLoader()
{
$procedureLoader = $this->getMock('\JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface');
$procedureLoader = $this->createMock('\JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface');

return $procedureLoader;
}
Expand All @@ -160,7 +160,7 @@ protected function getProcedureLoader()
*/
protected function getProcedureCompiler()
{
$procedureCompiler = $this->getMock('\JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface');
$procedureCompiler = $this->createMock('\JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface');

return $procedureCompiler;
}
Expand Down
29 changes: 29 additions & 0 deletions src/JonnyW/PhantomJs/Tests/Unit/EngineTest.php
Expand Up @@ -168,6 +168,35 @@ public function testDebugFlagIsNotSetIfDebuggingIsNotEnabled()
$this->assertNotContains('--debug=true', $engine->getCommand());
}

/**
* Test disk cache flag can be set.
*
* @access public
* @return void
*/
public function testDiskCacheFlagCanBeSet()
{
$engine = $this->getEngine();
$engine->cache(true);

$this->assertContains('--disk-cache=true', $engine->getCommand());
}

/**
* Test disk cache flag is not set if
* caching is not enabled.
*
* @access public
* @return void
*/
public function testDiskCacheFlagIsNotSetIfCachingIsNotEnabled()
{
$engine = $this->getEngine();
$engine->cache(false);

$this->assertNotContains('--disk-cache=true', $engine->getCommand());
}

/**
* Test command contains run options.
*
Expand Down
15 changes: 0 additions & 15 deletions src/JonnyW/PhantomJs/Tests/Unit/Http/CaptureRequestTest.php
Expand Up @@ -180,21 +180,6 @@ public function testRectLeftCanBeSet()
$this->assertSame($left, $captureRequest->getRectLeft());
}

/**
* Test invalid URL exception is thrown
* if URL is invalid format.
*
* @access public
* @return void
*/
public function testInvalidUrlExceptionIsThrownIfUrlIsInvalidFormat()
{
$this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidUrlException');

$captureRequest = $this->getCaptureRequest();
$captureRequest->setUrl('\\AnInvalidUrl');
}

/**
* Test URL does not contain query params if
* mehtod is not HEAD or GET.
Expand Down
15 changes: 0 additions & 15 deletions src/JonnyW/PhantomJs/Tests/Unit/Http/PdfRequestTest.php
Expand Up @@ -110,21 +110,6 @@ public function testInvalidMethodIsThrownIfMethodIsInvalid()
$pdfRequest->setMethod('INVALID_METHOD');
}

/**
* Test invalid URL exception is thrown
* if URL is invalid format.
*
* @access public
* @return void
*/
public function testInvalidUrlExceptionIsThrownIfUrlIsInvalidFormat()
{
$this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidUrlException');

$pdfRequest = $this->getPdfRequest();
$pdfRequest->setUrl('\\AnInvalidUrl');
}

/**
* Test URL does not contain query params if
* mehtod is not HEAD or GET.
Expand Down
15 changes: 0 additions & 15 deletions src/JonnyW/PhantomJs/Tests/Unit/Http/RequestTest.php
Expand Up @@ -110,21 +110,6 @@ public function testInvalidMethodIsThrownIfMethodIsInvalid()
$request->setMethod('INVALID_METHOD');
}

/**
* Test invalid URL exception is thrown
* if URL is invalid format.
*
* @access public
* @return void
*/
public function testInvalidUrlExceptionIsThrownIfUrlIsInvalidFormat()
{
$this->setExpectedException('\JonnyW\PhantomJs\Exception\InvalidUrlException');

$request = $this->getRequest();
$request->setUrl('\\AnInvalidUrl');
}

/**
* Test URL does not contain query params if
* mehtod is not HEAD or GET.
Expand Down
Expand Up @@ -153,7 +153,7 @@ protected function getChainProcedureLoader(array $procedureLoaders)
*/
protected function getProcedureLoader()
{
$procedureLoader = $this->getMock('\JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface');
$procedureLoader = $this->createMock('\JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface');

return $procedureLoader;
}
Expand All @@ -166,7 +166,7 @@ protected function getProcedureLoader()
*/
protected function getProcedure()
{
$procedure = $this->getMock('\JonnyW\PhantomJs\Procedure\ProcedureInterface');
$procedure = $this->createMock('\JonnyW\PhantomJs\Procedure\ProcedureInterface');

return $procedure;
}
Expand Down
Expand Up @@ -93,7 +93,7 @@ protected function getProcedureLoaderFactory(ProcedureFactoryInterface $procedur
*/
protected function getProcedureFactory()
{
$procedureFactory = $this->getMock('\JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface');
$procedureFactory = $this->createMock('\JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface');

return $procedureFactory;
}
Expand Down
Expand Up @@ -268,7 +268,7 @@ protected function getRenderer()
*/
protected function getFileLocator()
{
$fileLocator = $this->getMock('\Symfony\Component\Config\FileLocatorInterface');
$fileLocator = $this->createMock('\Symfony\Component\Config\FileLocatorInterface');

return $fileLocator;
}
Expand Down
Expand Up @@ -232,7 +232,7 @@ protected function getOutput()
*/
protected function getEngine()
{
$engine = $this->getMock('\JonnyW\PhantomJs\Engine');
$engine = $this->createMock('\JonnyW\PhantomJs\Engine');

return $engine;
}
Expand Down

0 comments on commit 23c9092

Please sign in to comment.