Skip to content

Commit a3be254

Browse files
authored
Merge pull request #133 from aik099/simplify-driver-install
Simplify driver installation process
2 parents 64070d5 + 86ce20f commit a3be254

7 files changed

Lines changed: 96 additions & 54 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* This file is part of the phpunit-mink library.
4+
* For the full copyright and license information, please view
5+
* the LICENSE file that was distributed with this source code.
6+
*
7+
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
8+
* @link https://github.com/aik099/phpunit-mink
9+
*/
10+
11+
12+
namespace aik099\PHPUnit\MinkDriver;
13+
14+
15+
abstract class AbstractDriverFactory implements IMinkDriverFactory
16+
{
17+
18+
/**
19+
* Throws an exception with driver installation instructions.
20+
*
21+
* @param string $class_name Driver class name.
22+
*
23+
* @return void
24+
* @throws \RuntimeException When driver isn't installed.
25+
*/
26+
protected function assertInstalled($class_name)
27+
{
28+
if ( !class_exists($class_name) ) {
29+
throw new \RuntimeException(
30+
sprintf(
31+
'The "%s" driver package is not installed. Please follow installation instructions at %s.',
32+
$this->getDriverName(),
33+
$this->getDriverPackageUrl()
34+
)
35+
);
36+
}
37+
}
38+
39+
}

library/aik099/PHPUnit/MinkDriver/GoutteDriverFactory.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313

1414

1515
use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
16-
use Behat\Mink\Driver\DriverInterface;
1716

18-
class GoutteDriverFactory implements IMinkDriverFactory
17+
class GoutteDriverFactory extends AbstractDriverFactory
1918
{
2019

2120
/**
@@ -28,6 +27,14 @@ public function getDriverName()
2827
return 'goutte';
2928
}
3029

30+
/**
31+
* @inheritDoc
32+
*/
33+
public function getDriverPackageUrl()
34+
{
35+
return 'https://packagist.org/packages/behat/mink-goutte-driver';
36+
}
37+
3138
/**
3239
* Returns default values for browser configuration.
3340
*
@@ -44,20 +51,11 @@ public function getDriverDefaults()
4451
}
4552

4653
/**
47-
* Returns a new driver instance according to the browser configuration.
48-
*
49-
* @param BrowserConfiguration $browser The browser configuration.
50-
*
51-
* @return DriverInterface
52-
* @throws \RuntimeException When driver isn't installed.
54+
* @inheritDoc
5355
*/
5456
public function createDriver(BrowserConfiguration $browser)
5557
{
56-
if ( !class_exists('Behat\Mink\Driver\GoutteDriver') ) {
57-
throw new \RuntimeException(
58-
'Install MinkGoutteDriver in order to use goutte driver.'
59-
);
60-
}
58+
$this->assertInstalled('Behat\Mink\Driver\GoutteDriver');
6159

6260
$driver_options = $browser->getDriverOptions();
6361

library/aik099/PHPUnit/MinkDriver/IMinkDriverFactory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ interface IMinkDriverFactory
2525
*/
2626
public function getDriverName();
2727

28+
/**
29+
* Returns driver package URL.
30+
*
31+
* @return string
32+
*/
33+
public function getDriverPackageUrl();
34+
2835
/**
2936
* Returns default values for browser configuration.
3037
*
@@ -38,6 +45,7 @@ public function getDriverDefaults();
3845
* @param BrowserConfiguration $browser The browser configuration.
3946
*
4047
* @return DriverInterface
48+
* @throws \RuntimeException When driver isn't installed.
4149
*/
4250
public function createDriver(BrowserConfiguration $browser);
4351

library/aik099/PHPUnit/MinkDriver/SahiDriverFactory.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313

1414

1515
use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
16-
use Behat\Mink\Driver\DriverInterface;
1716

18-
class SahiDriverFactory implements IMinkDriverFactory
17+
class SahiDriverFactory extends AbstractDriverFactory
1918
{
2019

2120
/**
@@ -28,6 +27,14 @@ public function getDriverName()
2827
return 'sahi';
2928
}
3029

30+
/**
31+
* @inheritDoc
32+
*/
33+
public function getDriverPackageUrl()
34+
{
35+
return 'https://packagist.org/packages/behat/mink-sahi-driver';
36+
}
37+
3138
/**
3239
* Returns default values for browser configuration.
3340
*
@@ -46,20 +53,11 @@ public function getDriverDefaults()
4653
}
4754

4855
/**
49-
* Returns a new driver instance according to the browser configuration.
50-
*
51-
* @param BrowserConfiguration $browser The browser configuration.
52-
*
53-
* @return DriverInterface
54-
* @throws \RuntimeException When driver isn't installed.
56+
* @inheritDoc
5557
*/
5658
public function createDriver(BrowserConfiguration $browser)
5759
{
58-
if ( !class_exists('Behat\Mink\Driver\SahiDriver') ) {
59-
throw new \RuntimeException(
60-
'Install MinkSahiDriver in order to use sahi driver.'
61-
);
62-
}
60+
$this->assertInstalled('Behat\Mink\Driver\SahiDriver');
6361

6462
$driver_options = $browser->getDriverOptions();
6563

library/aik099/PHPUnit/MinkDriver/Selenium2DriverFactory.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313

1414

1515
use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
16-
use Behat\Mink\Driver\DriverInterface;
1716

18-
class Selenium2DriverFactory implements IMinkDriverFactory
17+
class Selenium2DriverFactory extends AbstractDriverFactory
1918
{
2019

2120
/**
@@ -28,6 +27,14 @@ public function getDriverName()
2827
return 'selenium2';
2928
}
3029

30+
/**
31+
* @inheritDoc
32+
*/
33+
public function getDriverPackageUrl()
34+
{
35+
return 'https://packagist.org/packages/behat/mink-selenium2-driver';
36+
}
37+
3138
/**
3239
* Returns default values for browser configuration.
3340
*
@@ -42,20 +49,11 @@ public function getDriverDefaults()
4249
}
4350

4451
/**
45-
* Returns a new driver instance according to the browser configuration.
46-
*
47-
* @param BrowserConfiguration $browser The browser configuration.
48-
*
49-
* @return DriverInterface
50-
* @throws \RuntimeException When driver isn't installed.
52+
* @inheritDoc
5153
*/
5254
public function createDriver(BrowserConfiguration $browser)
5355
{
54-
if ( !class_exists('Behat\Mink\Driver\Selenium2Driver') ) {
55-
throw new \RuntimeException(
56-
'Install MinkSelenium2Driver in order to use selenium2 driver.'
57-
);
58-
}
56+
$this->assertInstalled('Behat\Mink\Driver\Selenium2Driver');
5957

6058
$browser_name = $browser->getBrowserName();
6159
$capabilities = $browser->getDesiredCapabilities();

library/aik099/PHPUnit/MinkDriver/ZombieDriverFactory.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313

1414

1515
use aik099\PHPUnit\BrowserConfiguration\BrowserConfiguration;
16-
use Behat\Mink\Driver\DriverInterface;
1716

18-
class ZombieDriverFactory implements IMinkDriverFactory
17+
class ZombieDriverFactory extends AbstractDriverFactory
1918
{
2019

2120
/**
@@ -28,6 +27,14 @@ public function getDriverName()
2827
return 'zombie';
2928
}
3029

30+
/**
31+
* @inheritDoc
32+
*/
33+
public function getDriverPackageUrl()
34+
{
35+
return 'https://packagist.org/packages/behat/mink-zombie-driver';
36+
}
37+
3138
/**
3239
* Returns default values for browser configuration.
3340
*
@@ -47,20 +54,11 @@ public function getDriverDefaults()
4754
}
4855

4956
/**
50-
* Returns a new driver instance according to the browser configuration.
51-
*
52-
* @param BrowserConfiguration $browser The browser configuration.
53-
*
54-
* @return DriverInterface
55-
* @throws \RuntimeException When driver isn't installed.
57+
* @inheritDoc
5658
*/
5759
public function createDriver(BrowserConfiguration $browser)
5860
{
59-
if ( !class_exists('Behat\Mink\Driver\ZombieDriver') ) {
60-
throw new \RuntimeException(
61-
'Install MinkZombieDriver in order to use zombie driver.'
62-
);
63-
}
61+
$this->assertInstalled('Behat\Mink\Driver\ZombieDriver');
6462

6563
$driver_options = $browser->getDriverOptions();
6664

tests/aik099/PHPUnit/MinkDriver/DriverFactoryTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ public function testMinkDriverMissingError($driver_class, $factory_class)
5353

5454
/** @var IMinkDriverFactory $factory */
5555
$factory = new $factory_class();
56-
$driver_class_parts = explode('\\', $driver_class);
5756

5857
$this->expectException('RuntimeException');
5958
$this->expectExceptionMessage(
60-
'Install Mink' . end($driver_class_parts) . ' in order to use ' . $factory->getDriverName() . ' driver.'
59+
sprintf(
60+
'The "%s" driver package is not installed. Please follow installation instructions at %s.',
61+
$factory->getDriverName(),
62+
$factory->getDriverPackageUrl()
63+
)
6164
);
6265
$factory->createDriver($this->createBrowserConfiguration($factory));
6366
}

0 commit comments

Comments
 (0)