Kaynağa Gözat

Adding ability to set custom bin dir in client

Jonny Wenmoth 11 yıl önce
ebeveyn
işleme
cf2c2a7305

BIN
bin/phantomjs


+ 38 - 4
src/JonnyW/PhantomJs/Client.php

@@ -46,6 +46,14 @@ class Client implements ClientInterface
      */
     protected $messageFactory;
 
+    /**
+     * Bin directory path.
+     *
+     * @var string
+     * @access protected
+     */
+    protected $binDir;
+
     /**
      * Path to PhantomJs executable
      *
@@ -98,8 +106,9 @@ class Client implements ClientInterface
     {
         $this->procedureLoader = $procedureLoader;
         $this->messageFactory  = $messageFactory;
-        $this->phantomJs       = 'bin/phantomjs';
-        $this->phantomLoader   = 'bin/phantomloader';
+        $this->binDir          = 'bin';
+        $this->phantomJs       = '%s/phantomjs';
+        $this->phantomLoader   = '%s/phantomloader';
         $this->options         = array();
     }
 
@@ -188,6 +197,31 @@ class Client implements ClientInterface
         return sprintf('%s %s %s', $phantomJs, implode(' ', $options), $phantomLoader);
     }
 
+    /**
+     * Set bin directory.
+     *
+     * @access public
+     * @param  string                   $path
+     * @return \JonnyW\PhantomJs\Client
+     */
+    public function setBinDir($path)
+    {
+        $this->binDir = rtrim($path, '/\\');
+
+        return $this;
+    }
+
+    /**
+     * Get bin directory.
+     *
+     * @access public
+     * @return string
+     */
+    public function getBinDir()
+    {
+        return $this->binDir;
+    }
+
     /**
      * Set new PhantomJs executable path.
      *
@@ -212,7 +246,7 @@ class Client implements ClientInterface
      */
     public function getPhantomJs()
     {
-        return $this->phantomJs;
+        return sprintf($this->phantomJs, $this->getBinDir());
     }
 
     /**
@@ -239,7 +273,7 @@ class Client implements ClientInterface
      */
     public function getPhantomLoader()
     {
-        return $this->phantomLoader;
+        return sprintf($this->phantomLoader, $this->getBinDir());
     }
 
     /**

+ 17 - 0
src/JonnyW/PhantomJs/ClientInterface.php

@@ -43,6 +43,23 @@ interface ClientInterface
      */
     public function send(RequestInterface $request, ResponseInterface $response);
 
+    /**
+     * Set bin directory.
+     *
+     * @access public
+     * @param  string                   $path
+     * @return \JonnyW\PhantomJs\Client
+     */
+    public function setBinDir($path);
+
+    /**
+     * Get bin directory.
+     *
+     * @access public
+     * @return string
+     */
+    public function getBinDir();
+
     /**
      * Set new PhantomJs executable path.
      *

+ 26 - 0
src/JonnyW/PhantomJs/Message/RequestInterface.php

@@ -83,6 +83,32 @@ interface RequestInterface
      */
     public function getDelay();
 
+    /**
+     * Set viewport size.
+     *
+     * @access public
+     * @param  int  $width
+     * @param  int  $height
+     * @return void
+     */
+    public function setViewportSize($width, $height);
+
+    /**
+     * Get viewport width.
+     *
+     * @access public
+     * @return int
+     */
+    public function getViewportWidth();
+
+    /**
+     * Get viewport height.
+     *
+     * @access public
+     * @return int
+     */
+    public function getViewportHeight();
+
     /**
      * Set request URL
      *

+ 176 - 0
src/JonnyW/PhantomJs/Tests/Unit/ClientTest.php

@@ -417,6 +417,182 @@ class ClientTest extends \PHPUnit_Framework_TestCase
         $this->assertContains('--debug=true', $command);
     }
 
+    /**
+     * Test set bin dir strips forward
+     * slash from end.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetBinDirStripsForwardSlashFromEnd()
+    {
+        $binDir = '/path/to/bin/dir/';
+
+        $procedureLoader = $this->getProcedureLoader();
+        $messageFactory  = $this->getMessageFactory();
+
+        $client = $this->getClient($procedureLoader, $messageFactory);
+        $client->setBinDir($binDir);
+
+        $this->assertSame('/path/to/bin/dir', $client->getBinDir());
+    }
+
+    /**
+     * Test set bin dir strips multiple forward
+     * slashes from end.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetBinDirStripsMultipleForwardSlashesFromEnd()
+    {
+        $binDir = '/path/to/bin/dir//';
+
+        $procedureLoader = $this->getProcedureLoader();
+        $messageFactory  = $this->getMessageFactory();
+
+        $client = $this->getClient($procedureLoader, $messageFactory);
+        $client->setBinDir($binDir);
+
+        $this->assertSame('/path/to/bin/dir', $client->getBinDir());
+    }
+
+    /**
+     * Test set bin dir strips backslash
+     * from end.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetBinDirStripsBackslashFromEnd()
+    {
+        $binDir = '\path\to\bin\dir\\';
+
+        $procedureLoader = $this->getProcedureLoader();
+        $messageFactory  = $this->getMessageFactory();
+
+        $client = $this->getClient($procedureLoader, $messageFactory);
+        $client->setBinDir($binDir);
+
+        $this->assertSame('\path\to\bin\dir', $client->getBinDir());
+    }
+
+    /**
+     * Test set bin dir strips multiple
+     * backslashes from end.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetBinDirStripsMultipleBackslashesFromEnd()
+    {
+        $binDir = '\path\to\bin\dir\\\\';
+
+        $procedureLoader = $this->getProcedureLoader();
+        $messageFactory  = $this->getMessageFactory();
+
+        $client = $this->getClient($procedureLoader, $messageFactory);
+        $client->setBinDir($binDir);
+
+        $this->assertSame('\path\to\bin\dir', $client->getBinDir());
+    }
+
+    /**
+     * Test set bin dir sets path of
+     * PhantomJs executable.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetBinDirSetsPathOfPhantomJsExecutable()
+    {
+        $binDir = '/path/to/bin/dir';
+
+        $procedureLoader = $this->getProcedureLoader();
+        $messageFactory  = $this->getMessageFactory();
+
+        $client = $this->getClient($procedureLoader, $messageFactory);
+        $client->setBinDir($binDir);
+
+        $this->assertSame('/path/to/bin/dir/phantomjs', $client->getPhantomJs());
+    }
+
+    /**
+     * Test set bin dir sets path of
+     * Phantom loader executable.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetBinDirSetsPathOfPhantomLoaderExecutable()
+    {
+        $binDir = '/path/to/bin/dir';
+
+        $procedureLoader = $this->getProcedureLoader();
+        $messageFactory  = $this->getMessageFactory();
+
+        $client = $this->getClient($procedureLoader, $messageFactory);
+        $client->setBinDir($binDir);
+
+        $this->assertSame('/path/to/bin/dir/phantomloader', $client->getPhantomLoader());
+    }
+
+    /**
+     * Test set bin dir does not set
+     * path of PhantomJs executable if
+     * custom PhantomJs executable path
+     * is set.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetBinDirDoesNotSetPathToPhantomJsExecutableIfCustomPhantomJsPathIsSet()
+    {
+        $binDir  = '/path/to/bin/dir';
+        $path    = '/path/to/phantomjs';
+
+        $procedureLoader = $this->getProcedureLoader();
+        $messageFactory  = $this->getMessageFactory();
+
+        $client = $this->getClient($procedureLoader, $messageFactory);
+
+        $phantomJs = new \ReflectionProperty(get_class($client), 'phantomJs');
+        $phantomJs->setAccessible(true);
+        $phantomJs->setValue($client, $path);
+
+        $client->setBinDir($binDir);
+
+        $this->assertSame($path, $client->getPhantomJs());
+    }
+
+    /**
+     * Test set bin dir does not set
+     * path of Phantom loader executable if
+     * custom Phantom loader executable path
+     * is set.
+     *
+     * @access public
+     * @return void
+     */
+    public function testSetBinDirDoesNotSetPathToPhantomLoaderExecutableIfCustomPhantomLoaderPathIsSet()
+    {
+        $binDir  = '/path/to/bin/dir';
+        $path    = '/path/to/phantomloader';
+
+        $procedureLoader = $this->getProcedureLoader();
+        $messageFactory  = $this->getMessageFactory();
+
+        $client = $this->getClient($procedureLoader, $messageFactory);
+
+        $phantomLoader = new \ReflectionProperty(get_class($client), 'phantomLoader');
+        $phantomLoader->setAccessible(true);
+        $phantomLoader->setValue($client, $path);
+
+        $client->setBinDir($binDir);
+
+        $this->assertSame($path, $client->getPhantomLoader());
+    }
+
 /** +++++++++++++++++++++++++++++++++++ **/
 /** ++++++++++ TEST ENTITIES ++++++++++ **/
 /** +++++++++++++++++++++++++++++++++++ **/