Skip to content

Commit

Permalink
Corona: Throw exception if we found multiple matching controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Jan 15, 2024
1 parent 1f560d2 commit 7bee9e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/Lunr/Corona/FrontController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use RegexIterator;
use RuntimeException;

/**
* Controller class
Expand Down Expand Up @@ -143,6 +144,11 @@ public function get_controller(string $src, array $list = [], bool $blacklist =
return '';
}

if (count($matches) > 1)
{
throw new RuntimeException('Found multiple matching controllers!');
}

$search = [ '.php', $src, '/' ];
$replace = [ '', '', '\\' ];

Expand Down
14 changes: 8 additions & 6 deletions src/Lunr/Corona/Tests/FrontControllerGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Lunr\Corona\Tests;

use RuntimeException;

/**
* This class contains tests for getting controllers from the FrontController class.
*
Expand Down Expand Up @@ -77,23 +79,23 @@ public function testGetControllerReturnsEmptyStringIfNoControllerInfoAvailable()
}

/**
* Test that get_controller() returns the first result if more than one exists.
* Test that get_controller() throws an exception if more than one exists.
*
* @covers Lunr\Corona\FrontController::get_controller
*/
public function testGetControllerReturnsFirstMatchIfMultipleFound(): void
public function testGetControllerThrowsExceptionIfMultipleFound(): void
{
$dir = TEST_STATICS . '/Corona/';
$fqcn = 'Project\\Package2\\FooController';
$dir = TEST_STATICS . '/Corona/';

$this->request->expects($this->exactly(2))
->method('__get')
->with('controller')
->willReturn('foo');

$value = $this->class->get_controller($dir);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Found multiple matching controllers!');

$this->assertEquals($fqcn, $value);
$this->class->get_controller($dir);
}

/**
Expand Down

0 comments on commit 7bee9e2

Please sign in to comment.