From 23c9092e06f16c987fb8dfc37e05b20c90998e9c Mon Sep 17 00:00:00 2001 From: Jonny Wenmoth Date: Tue, 15 Nov 2016 12:36:27 +0000 Subject: [PATCH] URL validation has been removed as it cannot cover all use cases. A request with an invalid URL fails with a status code of 0 - https://github.com/jonnnnyw/php-phantomjs/issues/119 --- composer.json | 2 +- src/JonnyW/PhantomJs/Engine.php | 29 +++++++++++++++++++ src/JonnyW/PhantomJs/Http/AbstractRequest.php | 8 +---- .../Procedure/ProcedureCompilerTest.php | 6 ++-- .../PhantomJs/Tests/Unit/ClientTest.php | 8 ++--- .../PhantomJs/Tests/Unit/EngineTest.php | 29 +++++++++++++++++++ .../Tests/Unit/Http/CaptureRequestTest.php | 15 ---------- .../Tests/Unit/Http/PdfRequestTest.php | 15 ---------- .../PhantomJs/Tests/Unit/Http/RequestTest.php | 15 ---------- .../Procedure/ChainProcedureLoaderTest.php | 4 +-- .../Procedure/ProcedureLoaderFactoryTest.php | 2 +- .../Unit/Procedure/ProcedureLoaderTest.php | 2 +- .../Tests/Unit/Procedure/ProcedureTest.php | 2 +- 13 files changed, 72 insertions(+), 65 deletions(-) diff --git a/composer.json b/composer.json index cefd8cd..1469f7d 100644 --- a/composer.json +++ b/composer.json @@ -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" }, diff --git a/src/JonnyW/PhantomJs/Engine.php b/src/JonnyW/PhantomJs/Engine.php index 10a5a0f..ef46fb1 100644 --- a/src/JonnyW/PhantomJs/Engine.php +++ b/src/JonnyW/PhantomJs/Engine.php @@ -33,6 +33,14 @@ class Engine */ protected $debug; + /** + * Cache flag. + * + * @var boolean + * @access protected + */ + protected $cache; + /** * PhantomJs run options. * @@ -59,6 +67,9 @@ public function __construct() { $this->path = 'bin/phantomjs'; $this->options = array(); + + $this->debug = false; + $this->cache = true; } /** @@ -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'); } @@ -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. * diff --git a/src/JonnyW/PhantomJs/Http/AbstractRequest.php b/src/JonnyW/PhantomJs/Http/AbstractRequest.php index 5db9d2a..2efa042 100644 --- a/src/JonnyW/PhantomJs/Http/AbstractRequest.php +++ b/src/JonnyW/PhantomJs/Http/AbstractRequest.php @@ -9,7 +9,6 @@ namespace JonnyW\PhantomJs\Http; -use JonnyW\PhantomJs\Exception\InvalidUrlException; use JonnyW\PhantomJs\Exception\InvalidMethodException; use JonnyW\PhantomJs\Procedure\InputInterface; @@ -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; diff --git a/src/JonnyW/PhantomJs/Tests/Integration/Procedure/ProcedureCompilerTest.php b/src/JonnyW/PhantomJs/Tests/Integration/Procedure/ProcedureCompilerTest.php index 07c8f67..91e0cca 100644 --- a/src/JonnyW/PhantomJs/Tests/Integration/Procedure/ProcedureCompilerTest.php +++ b/src/JonnyW/PhantomJs/Tests/Integration/Procedure/ProcedureCompilerTest.php @@ -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);')); @@ -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);')); @@ -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);')); diff --git a/src/JonnyW/PhantomJs/Tests/Unit/ClientTest.php b/src/JonnyW/PhantomJs/Tests/Unit/ClientTest.php index 65f30b0..cb4b95f 100644 --- a/src/JonnyW/PhantomJs/Tests/Unit/ClientTest.php +++ b/src/JonnyW/PhantomJs/Tests/Unit/ClientTest.php @@ -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; } @@ -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; } @@ -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; } @@ -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; } diff --git a/src/JonnyW/PhantomJs/Tests/Unit/EngineTest.php b/src/JonnyW/PhantomJs/Tests/Unit/EngineTest.php index ef2b75d..c924b78 100644 --- a/src/JonnyW/PhantomJs/Tests/Unit/EngineTest.php +++ b/src/JonnyW/PhantomJs/Tests/Unit/EngineTest.php @@ -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. * diff --git a/src/JonnyW/PhantomJs/Tests/Unit/Http/CaptureRequestTest.php b/src/JonnyW/PhantomJs/Tests/Unit/Http/CaptureRequestTest.php index 3dd43e2..e0ab357 100644 --- a/src/JonnyW/PhantomJs/Tests/Unit/Http/CaptureRequestTest.php +++ b/src/JonnyW/PhantomJs/Tests/Unit/Http/CaptureRequestTest.php @@ -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. diff --git a/src/JonnyW/PhantomJs/Tests/Unit/Http/PdfRequestTest.php b/src/JonnyW/PhantomJs/Tests/Unit/Http/PdfRequestTest.php index f873bda..32b1dbe 100644 --- a/src/JonnyW/PhantomJs/Tests/Unit/Http/PdfRequestTest.php +++ b/src/JonnyW/PhantomJs/Tests/Unit/Http/PdfRequestTest.php @@ -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. diff --git a/src/JonnyW/PhantomJs/Tests/Unit/Http/RequestTest.php b/src/JonnyW/PhantomJs/Tests/Unit/Http/RequestTest.php index 58210b5..b0d2c46 100644 --- a/src/JonnyW/PhantomJs/Tests/Unit/Http/RequestTest.php +++ b/src/JonnyW/PhantomJs/Tests/Unit/Http/RequestTest.php @@ -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. diff --git a/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ChainProcedureLoaderTest.php b/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ChainProcedureLoaderTest.php index ecf42d2..d40d4fb 100644 --- a/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ChainProcedureLoaderTest.php +++ b/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ChainProcedureLoaderTest.php @@ -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; } @@ -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; } diff --git a/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureLoaderFactoryTest.php b/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureLoaderFactoryTest.php index 00f3429..0366ca8 100644 --- a/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureLoaderFactoryTest.php +++ b/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureLoaderFactoryTest.php @@ -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; } diff --git a/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureLoaderTest.php b/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureLoaderTest.php index 11d1693..4c9f74b 100644 --- a/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureLoaderTest.php +++ b/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureLoaderTest.php @@ -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; } diff --git a/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureTest.php b/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureTest.php index af706c2..4baa4d5 100644 --- a/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureTest.php +++ b/src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureTest.php @@ -232,7 +232,7 @@ protected function getOutput() */ protected function getEngine() { - $engine = $this->getMock('\JonnyW\PhantomJs\Engine'); + $engine = $this->createMock('\JonnyW\PhantomJs\Engine'); return $engine; }