Skip to content

Commit 162b138

Browse files
committed
add UndefinedRouteException exception
1 parent 0a830dd commit 162b138

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ PhpRouter also throws the following exceptions:
537537
* `RouteNotFoundException` if PhpRouter cannot find any route that matches the user request.
538538
* `InvalidControllerException` if PhpRouter cannot invoke the controller.
539539
* `InvalidMiddlewareException` if PhpRouter cannot invoke the middleware.
540+
* `UndefinedRouteException` if `Router::url()` cannot find any route with the given name.
540541

541542
The `RouteNotFoundException` should be considered `404 Not found` error.
542543

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace MiladRahimi\PhpRouter\Exceptions;
4+
5+
use Exception;
6+
7+
/**
8+
* Class UndefinedRouteException
9+
*
10+
* @package MiladRahimi\PhpRouter\Exceptions
11+
*/
12+
class UndefinedRouteException extends Exception
13+
{
14+
//
15+
}

src/Router.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use MiladRahimi\PhpRouter\Exceptions\InvalidControllerException;
99
use MiladRahimi\PhpRouter\Exceptions\InvalidMiddlewareException;
1010
use MiladRahimi\PhpRouter\Exceptions\RouteNotFoundException;
11+
use MiladRahimi\PhpRouter\Exceptions\UndefinedRouteException;
1112
use MiladRahimi\PhpRouter\Services\HttpPublisher;
1213
use MiladRahimi\PhpRouter\Services\Publisher;
1314
use MiladRahimi\PhpRouter\Values\Route;
@@ -670,16 +671,16 @@ public function define(string $name, string $pattern): self
670671

671672
/**
672673
* Generate URL for given route name
673-
* It returns null if route with given name is not found.
674674
*
675675
* @param string $routeName
676676
* @param string[] $parameters
677-
* @return string|null
677+
* @return string
678+
* @throws UndefinedRouteException
678679
*/
679-
public function url(string $routeName, array $parameters = []): ?string
680+
public function url(string $routeName, array $parameters = []): string
680681
{
681682
if (isset($this->names[$routeName]) == false) {
682-
return null;
683+
throw new UndefinedRouteException("There is no route with name `$routeName`.");
683684
}
684685

685686
$uri = $this->names[$routeName]->getUri();

tests/UrlTest.php

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MiladRahimi\PhpRouter\Tests;
44

55
use MiladRahimi\PhpRouter\Enums\HttpMethods;
6+
use MiladRahimi\PhpRouter\Exceptions\UndefinedRouteException;
67
use MiladRahimi\PhpRouter\Router;
78
use Throwable;
89

@@ -11,7 +12,7 @@ class UrlTest extends TestCase
1112
/**
1213
* @throws Throwable
1314
*/
14-
public function test_generating_url_for_home()
15+
public function test_generating_url_for_the_homepage()
1516
{
1617
$router = $this->router()
1718
->name('home')
@@ -47,41 +48,41 @@ public function test_generating_url_for_a_page()
4748
*/
4849
public function test_generating_url_for_a_page_with_required_parameter()
4950
{
50-
$this->mockRequest(HttpMethods::GET, 'http://web.com/666');
51+
$this->mockRequest(HttpMethods::GET, 'http://web.com/contact');
5152

5253
$router = $this->router()
5354
->name('page')
5455
->get('/{name}', function (Router $r) {
55-
return $r->url('page', ['name' => '13']);
56+
return $r->url('page', ['name' => 'about']);
5657
})
5758
->dispatch();
5859

59-
$this->assertEquals('/13', $this->output($router));
60+
$this->assertEquals('/about', $this->output($router));
6061
}
6162

6263
/**
6364
* @throws Throwable
6465
*/
6566
public function test_generating_url_for_a_page_with_optional_parameter()
6667
{
67-
$this->mockRequest(HttpMethods::GET, 'http://web.com/666');
68+
$this->mockRequest(HttpMethods::GET, 'http://web.com/contact');
6869

6970
$router = $this->router()
7071
->name('page')
7172
->get('/{name?}', function (Router $r) {
72-
return $r->url('page', ['name' => '13']);
73+
return $r->url('page', ['name' => 'about']);
7374
})
7475
->dispatch();
7576

76-
$this->assertEquals('/13', $this->output($router));
77+
$this->assertEquals('/about', $this->output($router));
7778
}
7879

7980
/**
8081
* @throws Throwable
8182
*/
8283
public function test_generating_url_for_a_page_with_optional_parameter_2()
8384
{
84-
$this->mockRequest(HttpMethods::GET, 'http://web.com/666');
85+
$this->mockRequest(HttpMethods::GET, 'http://web.com/contact');
8586

8687
$router = $this->router()
8788
->name('page')
@@ -98,24 +99,7 @@ public function test_generating_url_for_a_page_with_optional_parameter_2()
9899
*/
99100
public function test_generating_url_for_a_page_with_optional_parameter_3()
100101
{
101-
$this->mockRequest(HttpMethods::GET, 'http://web.com/page/666');
102-
103-
$router = $this->router()
104-
->name('page')
105-
->get('/page/?{name?}', function (Router $r) {
106-
return $r->url('page');
107-
})
108-
->dispatch();
109-
110-
$this->assertEquals('/page', $this->output($router));
111-
}
112-
113-
/**
114-
* @throws Throwable
115-
*/
116-
public function test_generating_url_for_a_page_with_optional_parameter_4()
117-
{
118-
$this->mockRequest(HttpMethods::GET, 'http://web.com/page/666');
102+
$this->mockRequest(HttpMethods::GET, 'http://web.com/page/contact');
119103

120104
$router = $this->router()
121105
->name('page')
@@ -132,14 +116,13 @@ public function test_generating_url_for_a_page_with_optional_parameter_4()
132116
*/
133117
public function test_generating_url_for_undefined_route()
134118
{
135-
$router = $this->router()
119+
$this->expectException(UndefinedRouteException::class);
120+
$this->expectExceptionMessage("There is no route with name `home`.");
121+
122+
$this->router()
136123
->get('/', function (Router $r) {
137-
// There is no route with this name
138-
// It must return NULL (empty response)
139124
return $r->url('home');
140125
})
141126
->dispatch();
142-
143-
$this->assertEquals('', $this->output($router));
144127
}
145128
}

0 commit comments

Comments
 (0)