Skip to content

Commit

Permalink
Merge pull request #133 from aik099/simplify-driver-install
Browse files Browse the repository at this point in the history
Simplify driver installation process
  • Loading branch information
aik099 committed Mar 17, 2024
2 parents 64070d5 + 86ce20f commit a3be254
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 54 deletions.
39 changes: 39 additions & 0 deletions library/aik099/PHPUnit/MinkDriver/AbstractDriverFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* This file is part of the phpunit-mink library.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
* @link https://github.com/aik099/phpunit-mink
*/


namespace aik099\PHPUnit\MinkDriver;


abstract class AbstractDriverFactory implements IMinkDriverFactory
{

/**
* Throws an exception with driver installation instructions.
*
* @param string $class_name Driver class name.
*
* @return void
* @throws \RuntimeException When driver isn't installed.
*/
protected function assertInstalled($class_name)
{
if ( !class_exists($class_name) ) {
throw new \RuntimeException(
sprintf(
'The "%s" driver package is not installed. Please follow installation instructions at %s.',
$this->getDriverName(),
$this->getDriverPackageUrl()
)
);
}
}

}
24 changes: 11 additions & 13 deletions library/aik099/PHPUnit/MinkDriver/GoutteDriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@


use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
use Behat\Mink\Driver\DriverInterface;

class GoutteDriverFactory implements IMinkDriverFactory
class GoutteDriverFactory extends AbstractDriverFactory
{

/**
Expand All @@ -28,6 +27,14 @@ public function getDriverName()
return 'goutte';
}

/**
* @inheritDoc
*/
public function getDriverPackageUrl()
{
return 'https://packagist.org/packages/behat/mink-goutte-driver';
}

/**
* Returns default values for browser configuration.
*
Expand All @@ -44,20 +51,11 @@ public function getDriverDefaults()
}

/**
* Returns a new driver instance according to the browser configuration.
*
* @param BrowserConfiguration $browser The browser configuration.
*
* @return DriverInterface
* @throws \RuntimeException When driver isn't installed.
* @inheritDoc
*/
public function createDriver(BrowserConfiguration $browser)
{
if ( !class_exists('Behat\Mink\Driver\GoutteDriver') ) {
throw new \RuntimeException(
'Install MinkGoutteDriver in order to use goutte driver.'
);
}
$this->assertInstalled('Behat\Mink\Driver\GoutteDriver');

$driver_options = $browser->getDriverOptions();

Expand Down
8 changes: 8 additions & 0 deletions library/aik099/PHPUnit/MinkDriver/IMinkDriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ interface IMinkDriverFactory
*/
public function getDriverName();

/**
* Returns driver package URL.
*
* @return string
*/
public function getDriverPackageUrl();

/**
* Returns default values for browser configuration.
*
Expand All @@ -38,6 +45,7 @@ public function getDriverDefaults();
* @param BrowserConfiguration $browser The browser configuration.
*
* @return DriverInterface
* @throws \RuntimeException When driver isn't installed.
*/
public function createDriver(BrowserConfiguration $browser);

Expand Down
24 changes: 11 additions & 13 deletions library/aik099/PHPUnit/MinkDriver/SahiDriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@


use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
use Behat\Mink\Driver\DriverInterface;

class SahiDriverFactory implements IMinkDriverFactory
class SahiDriverFactory extends AbstractDriverFactory
{

/**
Expand All @@ -28,6 +27,14 @@ public function getDriverName()
return 'sahi';
}

/**
* @inheritDoc
*/
public function getDriverPackageUrl()
{
return 'https://packagist.org/packages/behat/mink-sahi-driver';
}

/**
* Returns default values for browser configuration.
*
Expand All @@ -46,20 +53,11 @@ public function getDriverDefaults()
}

/**
* Returns a new driver instance according to the browser configuration.
*
* @param BrowserConfiguration $browser The browser configuration.
*
* @return DriverInterface
* @throws \RuntimeException When driver isn't installed.
* @inheritDoc
*/
public function createDriver(BrowserConfiguration $browser)
{
if ( !class_exists('Behat\Mink\Driver\SahiDriver') ) {
throw new \RuntimeException(
'Install MinkSahiDriver in order to use sahi driver.'
);
}
$this->assertInstalled('Behat\Mink\Driver\SahiDriver');

$driver_options = $browser->getDriverOptions();

Expand Down
24 changes: 11 additions & 13 deletions library/aik099/PHPUnit/MinkDriver/Selenium2DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@


use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
use Behat\Mink\Driver\DriverInterface;

class Selenium2DriverFactory implements IMinkDriverFactory
class Selenium2DriverFactory extends AbstractDriverFactory
{

/**
Expand All @@ -28,6 +27,14 @@ public function getDriverName()
return 'selenium2';
}

/**
* @inheritDoc
*/
public function getDriverPackageUrl()
{
return 'https://packagist.org/packages/behat/mink-selenium2-driver';
}

/**
* Returns default values for browser configuration.
*
Expand All @@ -42,20 +49,11 @@ public function getDriverDefaults()
}

/**
* Returns a new driver instance according to the browser configuration.
*
* @param BrowserConfiguration $browser The browser configuration.
*
* @return DriverInterface
* @throws \RuntimeException When driver isn't installed.
* @inheritDoc
*/
public function createDriver(BrowserConfiguration $browser)
{
if ( !class_exists('Behat\Mink\Driver\Selenium2Driver') ) {
throw new \RuntimeException(
'Install MinkSelenium2Driver in order to use selenium2 driver.'
);
}
$this->assertInstalled('Behat\Mink\Driver\Selenium2Driver');

$browser_name = $browser->getBrowserName();
$capabilities = $browser->getDesiredCapabilities();
Expand Down
24 changes: 11 additions & 13 deletions library/aik099/PHPUnit/MinkDriver/ZombieDriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@


use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
use Behat\Mink\Driver\DriverInterface;

class ZombieDriverFactory implements IMinkDriverFactory
class ZombieDriverFactory extends AbstractDriverFactory
{

/**
Expand All @@ -28,6 +27,14 @@ public function getDriverName()
return 'zombie';
}

/**
* @inheritDoc
*/
public function getDriverPackageUrl()
{
return 'https://packagist.org/packages/behat/mink-zombie-driver';
}

/**
* Returns default values for browser configuration.
*
Expand All @@ -47,20 +54,11 @@ public function getDriverDefaults()
}

/**
* Returns a new driver instance according to the browser configuration.
*
* @param BrowserConfiguration $browser The browser configuration.
*
* @return DriverInterface
* @throws \RuntimeException When driver isn't installed.
* @inheritDoc
*/
public function createDriver(BrowserConfiguration $browser)
{
if ( !class_exists('Behat\Mink\Driver\ZombieDriver') ) {
throw new \RuntimeException(
'Install MinkZombieDriver in order to use zombie driver.'
);
}
$this->assertInstalled('Behat\Mink\Driver\ZombieDriver');

$driver_options = $browser->getDriverOptions();

Expand Down
7 changes: 5 additions & 2 deletions tests/aik099/PHPUnit/MinkDriver/DriverFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,14 @@ public function testMinkDriverMissingError($driver_class, $factory_class)

/** @var IMinkDriverFactory $factory */
$factory = new $factory_class();
$driver_class_parts = explode('\\', $driver_class);

$this->expectException('RuntimeException');
$this->expectExceptionMessage(
'Install Mink' . end($driver_class_parts) . ' in order to use ' . $factory->getDriverName() . ' driver.'
sprintf(
'The "%s" driver package is not installed. Please follow installation instructions at %s.',
$factory->getDriverName(),
$factory->getDriverPackageUrl()
)
);
$factory->createDriver($this->createBrowserConfiguration($factory));
}
Expand Down

0 comments on commit a3be254

Please sign in to comment.