Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify driver installation process #133

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
* @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 @@
return 'selenium2';
}

/**
* @inheritDoc
*/
public function getDriverPackageUrl()

Check warning on line 33 in library/aik099/PHPUnit/MinkDriver/Selenium2DriverFactory.php

View check run for this annotation

Codecov / codecov/patch

library/aik099/PHPUnit/MinkDriver/Selenium2DriverFactory.php#L33

Added line #L33 was not covered by tests
{
return 'https://packagist.org/packages/behat/mink-selenium2-driver';

Check warning on line 35 in library/aik099/PHPUnit/MinkDriver/Selenium2DriverFactory.php

View check run for this annotation

Codecov / codecov/patch

library/aik099/PHPUnit/MinkDriver/Selenium2DriverFactory.php#L35

Added line #L35 was not covered by tests
}

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

/**
* 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
Loading