Skip to content

Commit fb1f217

Browse files
committed
Improve error, when unknown driver is being used
1 parent a3be254 commit fb1f217

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

library/aik099/PHPUnit/MinkDriver/DriverFactoryRegistry.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ public function add(IMinkDriverFactory $driver_factory)
5252
public function get($driver_name)
5353
{
5454
if ( !isset($this->_registry[$driver_name]) ) {
55-
throw new \OutOfBoundsException(sprintf('No driver factory for "%s" driver.', $driver_name));
55+
$error_msg = 'The "' . $driver_name . '" driver is unknown.';
56+
57+
if ( $this->_registry ) {
58+
$drivers = '"' . implode('", "', array_keys($this->_registry)) . '"';
59+
$error_msg .= ' Please instead use any of these supported drivers: ' . $drivers . '.';
60+
}
61+
62+
throw new \OutOfBoundsException($error_msg);
5663
}
5764

5865
return $this->_registry[$driver_name];

tests/aik099/PHPUnit/MinkDriver/DriverFactoryRegistryTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,28 @@ public function testAddingExisting()
6060
$this->_driverFactoryRegistry->add($factory);
6161
}
6262

63-
public function testGettingNonExisting()
63+
public function testGettingNonExistingWithoutAlternatives()
6464
{
6565
$this->expectException('OutOfBoundsException');
66-
$this->expectExceptionMessage('No driver factory for "test" driver.');
66+
$this->expectExceptionMessage('The "test" driver is unknown.');
67+
68+
$this->_driverFactoryRegistry->get('test');
69+
}
70+
71+
public function testGettingNonExistingWithAlternatives()
72+
{
73+
$this->expectException('OutOfBoundsException');
74+
$this->expectExceptionMessage(
75+
'The "test" driver is unknown. Please instead use any of these supported drivers: "driver1", "driver2".'
76+
);
77+
78+
$factory1 = m::mock(IMinkDriverFactory::class);
79+
$factory1->shouldReceive('getDriverName')->once()->andReturn('driver1');
80+
$this->_driverFactoryRegistry->add($factory1);
81+
82+
$factory2 = m::mock(IMinkDriverFactory::class);
83+
$factory2->shouldReceive('getDriverName')->once()->andReturn('driver2');
84+
$this->_driverFactoryRegistry->add($factory2);
6785

6886
$this->_driverFactoryRegistry->get('test');
6987
}

0 commit comments

Comments
 (0)