DispatcherTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\Attributes\DataProvider;
  4. final class FreshRSS_dispatcherTest_Controller extends Minz_ActionController {
  5. /** @var list<string> */
  6. public static array $executed = [];
  7. public function fooAction(): void {
  8. self::$executed[] = Minz_Request::controllerName() . '/' . Minz_Request::actionName();
  9. $this->view->_layout(null);
  10. $this->view->_path('index/index.phtml'); // Empty view
  11. }
  12. }
  13. final class DispatcherTest extends \PHPUnit\Framework\TestCase {
  14. private string $controllerName = '';
  15. private string $actionName = '';
  16. #[\Override]
  17. protected function setUp(): void {
  18. FreshRSS_Context::initSystem();
  19. $this->controllerName = Minz_Request::controllerName();
  20. $this->actionName = Minz_Request::actionName();
  21. FreshRSS_dispatcherTest_Controller::$executed = [];
  22. }
  23. #[\Override]
  24. protected function tearDown(): void {
  25. Minz_Request::_controllerName($this->controllerName);
  26. Minz_Request::_actionName($this->actionName);
  27. }
  28. public function testRunExecutesExactNames(): void {
  29. Minz_Request::_controllerName('dispatcherTest');
  30. Minz_Request::_actionName('foo');
  31. ob_start();
  32. try {
  33. Minz_Dispatcher::getInstance()->run();
  34. } finally {
  35. ob_end_clean();
  36. }
  37. self::assertSame(['dispatcherTest/foo'], FreshRSS_dispatcherTest_Controller::$executed);
  38. }
  39. /** @return list<array{string,string}> */
  40. public static function provideNamesInAnotherLetterCase(): array {
  41. return [
  42. ['dispatcherTest', 'Foo'],
  43. ['dispatcherTest', 'FOO'],
  44. ['DispatcherTest', 'foo'],
  45. ['DISPATCHERTEST', 'foo'],
  46. ];
  47. }
  48. /**
  49. * PHP resolves class and method names case-insensitively, but e.g. `Minz_Request::is()`,
  50. * which extensions use in `Minz_HookType::ActionExecute` to decide about an action, does not.
  51. */
  52. #[DataProvider('provideNamesInAnotherLetterCase')]
  53. public function testRunRefusesNamesInAnotherLetterCase(string $controllerName, string $actionName): void {
  54. Minz_Request::_controllerName($controllerName);
  55. Minz_Request::_actionName($actionName);
  56. $exception = null;
  57. ob_start();
  58. try {
  59. Minz_Dispatcher::getInstance()->run();
  60. } catch (Minz_ControllerNotExistException | Minz_ActionException $e) {
  61. $exception = $e;
  62. } finally {
  63. ob_end_clean();
  64. }
  65. self::assertNotNull($exception);
  66. self::assertSame([], FreshRSS_dispatcherTest_Controller::$executed);
  67. }
  68. }