33namespace MiladRahimi \PhpRouter \Tests ;
44
55use MiladRahimi \PhpRouter \Enums \HttpMethods ;
6+ use MiladRahimi \PhpRouter \Exceptions \UndefinedRouteException ;
67use MiladRahimi \PhpRouter \Router ;
78use 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