From a6606184cd5512e3c7a5a16c9d925e8aa1160792 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 17 Mar 2024 16:14:00 +0200 Subject: [PATCH] Simplify driver installation process --- .../MinkDriver/AbstractDriverFactory.php | 39 +++++++++++++++++++ .../MinkDriver/GoutteDriverFactory.php | 24 ++++++------ .../PHPUnit/MinkDriver/IMinkDriverFactory.php | 8 ++++ .../PHPUnit/MinkDriver/SahiDriverFactory.php | 24 ++++++------ .../MinkDriver/Selenium2DriverFactory.php | 24 ++++++------ .../MinkDriver/ZombieDriverFactory.php | 24 ++++++------ .../PHPUnit/MinkDriver/DriverFactoryTest.php | 7 +++- 7 files changed, 96 insertions(+), 54 deletions(-) create mode 100644 library/aik099/PHPUnit/MinkDriver/AbstractDriverFactory.php diff --git a/library/aik099/PHPUnit/MinkDriver/AbstractDriverFactory.php b/library/aik099/PHPUnit/MinkDriver/AbstractDriverFactory.php new file mode 100644 index 0000000..b9b1e8e --- /dev/null +++ b/library/aik099/PHPUnit/MinkDriver/AbstractDriverFactory.php @@ -0,0 +1,39 @@ + + * @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 is not installed. Please follow installation instructions at %s.', + $this->getDriverName(), + $this->getDriverPackageUrl() + ) + ); + } + } + +} diff --git a/library/aik099/PHPUnit/MinkDriver/GoutteDriverFactory.php b/library/aik099/PHPUnit/MinkDriver/GoutteDriverFactory.php index 31b7075..c68438e 100644 --- a/library/aik099/PHPUnit/MinkDriver/GoutteDriverFactory.php +++ b/library/aik099/PHPUnit/MinkDriver/GoutteDriverFactory.php @@ -13,9 +13,8 @@ use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration; -use Behat\Mink\Driver\DriverInterface; -class GoutteDriverFactory implements IMinkDriverFactory +class GoutteDriverFactory extends AbstractDriverFactory { /** @@ -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. * @@ -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(); diff --git a/library/aik099/PHPUnit/MinkDriver/IMinkDriverFactory.php b/library/aik099/PHPUnit/MinkDriver/IMinkDriverFactory.php index 9ee6eb9..dadce1d 100644 --- a/library/aik099/PHPUnit/MinkDriver/IMinkDriverFactory.php +++ b/library/aik099/PHPUnit/MinkDriver/IMinkDriverFactory.php @@ -25,6 +25,13 @@ interface IMinkDriverFactory */ public function getDriverName(); + /** + * Returns driver package URL. + * + * @return string + */ + public function getDriverPackageUrl(); + /** * Returns default values for browser configuration. * @@ -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); diff --git a/library/aik099/PHPUnit/MinkDriver/SahiDriverFactory.php b/library/aik099/PHPUnit/MinkDriver/SahiDriverFactory.php index a0f2539..5fa3cd0 100644 --- a/library/aik099/PHPUnit/MinkDriver/SahiDriverFactory.php +++ b/library/aik099/PHPUnit/MinkDriver/SahiDriverFactory.php @@ -13,9 +13,8 @@ use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration; -use Behat\Mink\Driver\DriverInterface; -class SahiDriverFactory implements IMinkDriverFactory +class SahiDriverFactory extends AbstractDriverFactory { /** @@ -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. * @@ -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(); diff --git a/library/aik099/PHPUnit/MinkDriver/Selenium2DriverFactory.php b/library/aik099/PHPUnit/MinkDriver/Selenium2DriverFactory.php index 02a184e..7667fe8 100644 --- a/library/aik099/PHPUnit/MinkDriver/Selenium2DriverFactory.php +++ b/library/aik099/PHPUnit/MinkDriver/Selenium2DriverFactory.php @@ -13,9 +13,8 @@ use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration; -use Behat\Mink\Driver\DriverInterface; -class Selenium2DriverFactory implements IMinkDriverFactory +class Selenium2DriverFactory extends AbstractDriverFactory { /** @@ -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. * @@ -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(); diff --git a/library/aik099/PHPUnit/MinkDriver/ZombieDriverFactory.php b/library/aik099/PHPUnit/MinkDriver/ZombieDriverFactory.php index 10a00bf..59bef48 100644 --- a/library/aik099/PHPUnit/MinkDriver/ZombieDriverFactory.php +++ b/library/aik099/PHPUnit/MinkDriver/ZombieDriverFactory.php @@ -13,9 +13,8 @@ use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration; -use Behat\Mink\Driver\DriverInterface; -class ZombieDriverFactory implements IMinkDriverFactory +class ZombieDriverFactory extends AbstractDriverFactory { /** @@ -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. * @@ -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(); diff --git a/tests/aik099/PHPUnit/MinkDriver/DriverFactoryTest.php b/tests/aik099/PHPUnit/MinkDriver/DriverFactoryTest.php index 54b6360..c38f964 100644 --- a/tests/aik099/PHPUnit/MinkDriver/DriverFactoryTest.php +++ b/tests/aik099/PHPUnit/MinkDriver/DriverFactoryTest.php @@ -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 is not installed. Please follow installation instructions at %s.', + $factory->getDriverName(), + $factory->getDriverPackageUrl() + ) ); $factory->createDriver($this->createBrowserConfiguration($factory)); }