Просмотр исходного кода

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

Jonny Wenmoth 9 лет назад
Родитель
Сommit
23c9092e06

+ 1 - 1
composer.json

@@ -22,7 +22,7 @@
         "jakoch/phantomjs-installer": "^2.1"
         "jakoch/phantomjs-installer": "^2.1"
     },
     },
     "require-dev": {
     "require-dev": {
-        "phpunit/phpunit": "~4.0",
+        "phpunit/phpunit": "~5.0",
         "zendframework/zendpdf": "~2.0",
         "zendframework/zendpdf": "~2.0",
         "smalot/pdfparser": "~0.9"
         "smalot/pdfparser": "~0.9"
     },
     },

+ 29 - 0
src/JonnyW/PhantomJs/Engine.php

@@ -33,6 +33,14 @@ class Engine
      */
      */
     protected $debug;
     protected $debug;
 
 
+    /**
+     * Cache flag.
+     *
+     * @var boolean
+     * @access protected
+     */
+    protected $cache;
+
     /**
     /**
      * PhantomJs run options.
      * PhantomJs run options.
      *
      *
@@ -59,6 +67,9 @@ class Engine
     {
     {
         $this->path    = 'bin/phantomjs';
         $this->path    = 'bin/phantomjs';
         $this->options = array();
         $this->options = array();
+
+        $this->debug = false;
+        $this->cache = true;
     }
     }
 
 
     /**
     /**
@@ -75,6 +86,10 @@ class Engine
 
 
         $this->validateExecutable($path);
         $this->validateExecutable($path);
 
 
+        if ($this->cache) {
+            array_push($options, '--disk-cache=true');
+        }
+
         if ($this->debug) {
         if ($this->debug) {
             array_push($options, '--debug=true');
             array_push($options, '--debug=true');
         }
         }
@@ -164,6 +179,20 @@ class Engine
         return $this;
         return $this;
     }
     }
 
 
+    /**
+     * Cache.
+     *
+     * @access public
+     * @param  boolean                  $doCache
+     * @return \JonnyW\PhantomJs\Client
+     */
+    public function cache($doCache)
+    {
+        $this->cache = $doCache;
+
+        return $this;
+    }
+
     /**
     /**
      * Log info.
      * Log info.
      *
      *

+ 1 - 7
src/JonnyW/PhantomJs/Http/AbstractRequest.php

@@ -9,7 +9,6 @@
 
 
 namespace JonnyW\PhantomJs\Http;
 namespace JonnyW\PhantomJs\Http;
 
 
-use JonnyW\PhantomJs\Exception\InvalidUrlException;
 use JonnyW\PhantomJs\Exception\InvalidMethodException;
 use JonnyW\PhantomJs\Exception\InvalidMethodException;
 use JonnyW\PhantomJs\Procedure\InputInterface;
 use JonnyW\PhantomJs\Procedure\InputInterface;
 
 
@@ -243,16 +242,11 @@ abstract class AbstractRequest
      * Set request URL
      * Set request URL
      *
      *
      * @access public
      * @access public
-     * @param  string                                          $url
+     * @param  string                                 $url
      * @return \JonnyW\PhantomJs\Http\AbstractRequest
      * @return \JonnyW\PhantomJs\Http\AbstractRequest
-     * @throws \JonnyW\PhantomJs\Exception\InvalidUrlException
      */
      */
     public function setUrl($url)
     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;
         $this->url = $url;
 
 
         return $this;
         return $this;

+ 3 - 3
src/JonnyW/PhantomJs/Tests/Integration/Procedure/ProcedureCompilerTest.php

@@ -60,7 +60,7 @@ class ProcedureCompilerTest extends \PHPUnit_Framework_TestCase
         $request = $this->getRequest();
         $request = $this->getRequest();
         $request->setUrl('http://test.com');
         $request->setUrl('http://test.com');
 
 
-        $renderer = $this->getMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
+        $renderer = $this->createMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
         $renderer->expects($this->exactly(1))
         $renderer->expects($this->exactly(1))
             ->method('render')
             ->method('render')
             ->will($this->returnValue('var test=1; phantom.exit(1);'));
             ->will($this->returnValue('var test=1; phantom.exit(1);'));
@@ -94,7 +94,7 @@ class ProcedureCompilerTest extends \PHPUnit_Framework_TestCase
         $request = $this->getRequest();
         $request = $this->getRequest();
         $request->setUrl('http://test.com');
         $request->setUrl('http://test.com');
 
 
-        $renderer = $this->getMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
+        $renderer = $this->createMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
         $renderer->expects($this->exactly(2))
         $renderer->expects($this->exactly(2))
             ->method('render')
             ->method('render')
             ->will($this->returnValue('var test=1; phantom.exit(1);'));
             ->will($this->returnValue('var test=1; phantom.exit(1);'));
@@ -127,7 +127,7 @@ class ProcedureCompilerTest extends \PHPUnit_Framework_TestCase
         $request = $this->getRequest();
         $request = $this->getRequest();
         $request->setUrl('http://test.com');
         $request->setUrl('http://test.com');
 
 
-        $renderer = $this->getMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
+        $renderer = $this->createMock('\JonnyW\PhantomJs\Template\TemplateRendererInterface');
         $renderer->expects($this->exactly(2))
         $renderer->expects($this->exactly(2))
             ->method('render')
             ->method('render')
             ->will($this->returnValue('var test=1; phantom.exit(1);'));
             ->will($this->returnValue('var test=1; phantom.exit(1);'));

+ 4 - 4
src/JonnyW/PhantomJs/Tests/Unit/ClientTest.php

@@ -121,7 +121,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
      */
      */
     protected function getEngine()
     protected function getEngine()
     {
     {
-        $engine = $this->getMock('\JonnyW\PhantomJs\Engine');
+        $engine = $this->createMock('\JonnyW\PhantomJs\Engine');
 
 
         return $engine;
         return $engine;
     }
     }
@@ -134,7 +134,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
      */
      */
     protected function getMessageFactory()
     protected function getMessageFactory()
     {
     {
-        $messageFactory = $this->getMock('\JonnyW\PhantomJs\Http\MessageFactoryInterface');
+        $messageFactory = $this->createMock('\JonnyW\PhantomJs\Http\MessageFactoryInterface');
 
 
         return $messageFactory;
         return $messageFactory;
     }
     }
@@ -147,7 +147,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
      */
      */
     protected function getProcedureLoader()
     protected function getProcedureLoader()
     {
     {
-        $procedureLoader = $this->getMock('\JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface');
+        $procedureLoader = $this->createMock('\JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface');
 
 
         return $procedureLoader;
         return $procedureLoader;
     }
     }
@@ -160,7 +160,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
      */
      */
     protected function getProcedureCompiler()
     protected function getProcedureCompiler()
     {
     {
-        $procedureCompiler = $this->getMock('\JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface');
+        $procedureCompiler = $this->createMock('\JonnyW\PhantomJs\Procedure\ProcedureCompilerInterface');
 
 
         return $procedureCompiler;
         return $procedureCompiler;
     }
     }

+ 29 - 0
src/JonnyW/PhantomJs/Tests/Unit/EngineTest.php

@@ -168,6 +168,35 @@ class EngineTest extends \PHPUnit_Framework_TestCase
         $this->assertNotContains('--debug=true', $engine->getCommand());
         $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.
      * Test command contains run options.
      *
      *

+ 0 - 15
src/JonnyW/PhantomJs/Tests/Unit/Http/CaptureRequestTest.php

@@ -180,21 +180,6 @@ class CaptureRequestTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($left, $captureRequest->getRectLeft());
         $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
      * Test URL does not contain query params if
      * mehtod is not HEAD or GET.
      * mehtod is not HEAD or GET.

+ 0 - 15
src/JonnyW/PhantomJs/Tests/Unit/Http/PdfRequestTest.php

@@ -110,21 +110,6 @@ class PdfRequestTest extends \PHPUnit_Framework_TestCase
         $pdfRequest->setMethod('INVALID_METHOD');
         $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
      * Test URL does not contain query params if
      * mehtod is not HEAD or GET.
      * mehtod is not HEAD or GET.

+ 0 - 15
src/JonnyW/PhantomJs/Tests/Unit/Http/RequestTest.php

@@ -110,21 +110,6 @@ class RequestTest extends \PHPUnit_Framework_TestCase
         $request->setMethod('INVALID_METHOD');
         $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
      * Test URL does not contain query params if
      * mehtod is not HEAD or GET.
      * mehtod is not HEAD or GET.

+ 2 - 2
src/JonnyW/PhantomJs/Tests/Unit/Procedure/ChainProcedureLoaderTest.php

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

+ 1 - 1
src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureLoaderFactoryTest.php

@@ -93,7 +93,7 @@ class ProcedureLoaderFactoryTest extends \PHPUnit_Framework_TestCase
      */
      */
     protected function getProcedureFactory()
     protected function getProcedureFactory()
     {
     {
-        $procedureFactory = $this->getMock('\JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface');
+        $procedureFactory = $this->createMock('\JonnyW\PhantomJs\Procedure\ProcedureFactoryInterface');
 
 
         return $procedureFactory;
         return $procedureFactory;
     }
     }

+ 1 - 1
src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureLoaderTest.php

@@ -268,7 +268,7 @@ class ProcedureLoaderTest extends \PHPUnit_Framework_TestCase
      */
      */
     protected function getFileLocator()
     protected function getFileLocator()
     {
     {
-        $fileLocator = $this->getMock('\Symfony\Component\Config\FileLocatorInterface');
+        $fileLocator = $this->createMock('\Symfony\Component\Config\FileLocatorInterface');
 
 
         return $fileLocator;
         return $fileLocator;
     }
     }

+ 1 - 1
src/JonnyW/PhantomJs/Tests/Unit/Procedure/ProcedureTest.php

@@ -232,7 +232,7 @@ class ProcedureTest extends \PHPUnit_Framework_TestCase
      */
      */
     protected function getEngine()
     protected function getEngine()
     {
     {
-        $engine = $this->getMock('\JonnyW\PhantomJs\Engine');
+        $engine = $this->createMock('\JonnyW\PhantomJs\Engine');
 
 
         return $engine;
         return $engine;
     }
     }