Skip to content

Commit

Permalink
Merge pull request #542 from flightphp/fix-dispatcher-error-message
Browse files Browse the repository at this point in the history
Fix dispatcher error message
  • Loading branch information
fadrian06 authored Feb 9, 2024
2 parents b06b8da + 1d810b6 commit 5992fab
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
5 changes: 0 additions & 5 deletions flight/core/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ protected function runPreFilters(string $eventName, array &$params): self

/**
* @param array<int, mixed> &$params
* @param mixed &$output
*
* @return void|mixed
* @throws Exception
Expand Down Expand Up @@ -116,10 +115,6 @@ protected function runPostFilters(string $eventName, &$output)
*/
public function set(string $name, callable $callback): self
{
if ($this->get($name) !== null) {
trigger_error("Event '$name' has been overriden!", E_USER_NOTICE);
}

$this->events[$name] = $callback;

return $this;
Expand Down
58 changes: 58 additions & 0 deletions tests/DocExamplesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace tests;

use Exception;
use Flight;
use flight\Engine;
use PHPUnit\Framework\TestCase;
use Throwable;

class DocExamplesTest extends TestCase
{
protected function setUp(): void
{
$_SERVER = [];
$_REQUEST = [];
Flight::init();
Flight::setEngine(new Engine());
}

protected function tearDown(): void
{
unset($_REQUEST);
unset($_SERVER);
Flight::clear();
}

public function testMapNotFoundMethod()
{
Flight::map('notFound', function () {
Flight::json([], 404);
});

Flight::request()->url = '/not-found';

Flight::route('/', function () {
echo 'hello world!';
});

Flight::start();
ob_get_clean();
$this->assertEquals(404, Flight::response()->status());
$this->assertEquals('[]', Flight::response()->getBody());
}

public function testMapErrorMethod()
{
Flight::map('error', function (Throwable $error) {
// Handle error
echo 'Custom: ' . $error->getMessage();
});

Flight::app()->handleException(new Exception('Error'));
$this->expectOutputString('Custom: Error');
}
}

0 comments on commit 5992fab

Please sign in to comment.