Skip to content

Commit

Permalink
Merge pull request #134 from aik099/uknown-driver-usage-error
Browse files Browse the repository at this point in the history
Improve error, when unknown driver is being used
  • Loading branch information
aik099 committed Mar 17, 2024
2 parents a3be254 + fb1f217 commit 7142faa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 8 additions & 1 deletion library/aik099/PHPUnit/MinkDriver/DriverFactoryRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ public function add(IMinkDriverFactory $driver_factory)
public function get($driver_name)
{
if ( !isset($this->_registry[$driver_name]) ) {
throw new \OutOfBoundsException(sprintf('No driver factory for "%s" driver.', $driver_name));
$error_msg = 'The "' . $driver_name . '" driver is unknown.';

if ( $this->_registry ) {
$drivers = '"' . implode('", "', array_keys($this->_registry)) . '"';
$error_msg .= ' Please instead use any of these supported drivers: ' . $drivers . '.';
}

throw new \OutOfBoundsException($error_msg);
}

return $this->_registry[$driver_name];
Expand Down
22 changes: 20 additions & 2 deletions tests/aik099/PHPUnit/MinkDriver/DriverFactoryRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,28 @@ public function testAddingExisting()
$this->_driverFactoryRegistry->add($factory);
}

public function testGettingNonExisting()
public function testGettingNonExistingWithoutAlternatives()
{
$this->expectException('OutOfBoundsException');
$this->expectExceptionMessage('No driver factory for "test" driver.');
$this->expectExceptionMessage('The "test" driver is unknown.');

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

public function testGettingNonExistingWithAlternatives()
{
$this->expectException('OutOfBoundsException');
$this->expectExceptionMessage(
'The "test" driver is unknown. Please instead use any of these supported drivers: "driver1", "driver2".'
);

$factory1 = m::mock(IMinkDriverFactory::class);
$factory1->shouldReceive('getDriverName')->once()->andReturn('driver1');
$this->_driverFactoryRegistry->add($factory1);

$factory2 = m::mock(IMinkDriverFactory::class);
$factory2->shouldReceive('getDriverName')->once()->andReturn('driver2');
$this->_driverFactoryRegistry->add($factory2);

$this->_driverFactoryRegistry->get('test');
}
Expand Down

0 comments on commit 7142faa

Please sign in to comment.