77use Neuron \Routing \Router ;
88use Neuron \Routing \RouteMap ;
99use PHPUnit \Framework \TestCase ;
10+ use Tests \Mock \MockRouter ;
1011
1112/**
1213 * Unit tests for the UrlHelper class.
1617 */
1718class UrlHelperTest extends TestCase
1819{
19- private Router $ router ;
20+ private MockRouter $ router ;
2021 private UrlHelper $ urlHelper ;
2122
2223 protected function setUp (): void
2324 {
2425 parent ::setUp ();
2526
26- // Create a fresh router instance for testing
27- $ this ->router = new Router ();
27+ // Create a mock router instance for testing
28+ $ this ->router = new MockRouter ();
2829 $ this ->urlHelper = new UrlHelper ( $ this ->router );
2930
3031 // Set up Registry for URL generation
3132 $ registry = Registry::getInstance ();
3233 $ registry ->set ( 'Base.Url ' , 'https://example.com ' );
33-
34- // Skip tests if the Router doesn't have the required URL helper methods
35- if ( !method_exists ( $ this ->router , 'generateUrl ' ) )
36- {
37- $ this ->markTestSkipped ( 'Router URL helper methods not available. Install neuron-php/routing dev version. ' );
38- }
3934 }
4035
4136 protected function tearDown (): void
@@ -56,9 +51,7 @@ private function createMockRoute( string $name, string $path ): RouteMap
5651 public function testRoutePathGeneratesRelativeUrl (): void
5752 {
5853 // Arrange
59- $ route = $ this ->createMockRoute ( 'user_profile ' , '/users/:id ' );
60- $ this ->router ->get ( '/users/:id ' , function () { return 'test ' ; } )
61- ->setName ( 'user_profile ' );
54+ $ this ->router ->addNamedRoute ( 'user_profile ' , '/users/:id ' );
6255
6356 // Act
6457 $ url = $ this ->urlHelper ->routePath ( 'user_profile ' , ['id ' => 123 ] );
@@ -70,9 +63,7 @@ public function testRoutePathGeneratesRelativeUrl(): void
7063 public function testRouteUrlGeneratesAbsoluteUrl (): void
7164 {
7265 // Arrange
73- $ route = $ this ->createMockRoute ( 'user_profile ' , '/users/:id ' );
74- $ this ->router ->get ( '/users/:id ' , function () { return 'test ' ; } )
75- ->setName ( 'user_profile ' );
66+ $ this ->router ->addNamedRoute ( 'user_profile ' , '/users/:id ' );
7667
7768 // Act
7869 $ url = $ this ->urlHelper ->routeUrl ( 'user_profile ' , ['id ' => 123 ] );
@@ -102,8 +93,7 @@ public function testRouteUrlReturnsNullForMissingRoute(): void
10293 public function testRouteExistsReturnsTrueForExistingRoute (): void
10394 {
10495 // Arrange
105- $ this ->router ->get ( '/users/:id ' , function () { return 'test ' ; } )
106- ->setName ( 'user_profile ' );
96+ $ this ->router ->addNamedRoute ( 'user_profile ' , '/users/:id ' );
10797
10898 // Act & Assert
10999 $ this ->assertTrue ( $ this ->urlHelper ->routeExists ( 'user_profile ' ) );
@@ -118,8 +108,7 @@ public function testRouteExistsReturnsFalseForMissingRoute(): void
118108 public function testMagicMethodPathGeneration (): void
119109 {
120110 // Arrange
121- $ this ->router ->get ( '/users/:id ' , function () { return 'test ' ; } )
122- ->setName ( 'user_profile ' );
111+ $ this ->router ->addNamedRoute ( 'user_profile ' , '/users/:id ' );
123112
124113 // Act
125114 $ url = $ this ->urlHelper ->userProfilePath ( ['id ' => 456 ] );
@@ -131,8 +120,7 @@ public function testMagicMethodPathGeneration(): void
131120 public function testMagicMethodUrlGeneration (): void
132121 {
133122 // Arrange
134- $ this ->router ->get ( '/users/:id ' , function () { return 'test ' ; } )
135- ->setName ( 'user_profile ' );
123+ $ this ->router ->addNamedRoute ( 'user_profile ' , '/users/:id ' );
136124
137125 // Act
138126 $ url = $ this ->urlHelper ->userProfileUrl ( ['id ' => 456 ] );
@@ -144,8 +132,7 @@ public function testMagicMethodUrlGeneration(): void
144132 public function testMagicMethodWithComplexRouteName (): void
145133 {
146134 // Arrange
147- $ this ->router ->get ( '/admin/users/:id/posts/:post_id ' , function () { return 'test ' ; } )
148- ->setName ( 'admin_user_posts ' );
135+ $ this ->router ->addNamedRoute ( 'admin_user_posts ' , '/admin/users/:id/posts/:post_id ' );
149136
150137 // Act
151138 $ url = $ this ->urlHelper ->adminUserPostsPath ( ['id ' => 1 , 'post_id ' => 2 ] );
@@ -167,10 +154,8 @@ public function testMagicMethodThrowsExceptionForInvalidMethod(): void
167154 public function testGetAvailableRoutesReturnsNamedRoutes (): void
168155 {
169156 // Arrange
170- $ this ->router ->get ( '/users ' , function () { return 'index ' ; } )
171- ->setName ( 'users_index ' );
172- $ this ->router ->post ( '/users ' , function () { return 'create ' ; } )
173- ->setName ( 'users_create ' );
157+ $ this ->router ->addNamedRoute ( 'users_index ' , '/users ' , 'GET ' );
158+ $ this ->router ->addNamedRoute ( 'users_create ' , '/users ' , 'POST ' );
174159
175160 // Act
176161 $ routes = $ this ->urlHelper ->getAvailableRoutes ();
@@ -202,8 +187,7 @@ public function testGetSetRouter(): void
202187 public function testRouteWithoutParametersGeneratesCorrectUrl (): void
203188 {
204189 // Arrange
205- $ this ->router ->get ( '/about ' , function () { return 'about ' ; } )
206- ->setName ( 'about ' );
190+ $ this ->router ->addNamedRoute ( 'about ' , '/about ' );
207191
208192 // Act
209193 $ url = $ this ->urlHelper ->routePath ( 'about ' );
@@ -215,8 +199,7 @@ public function testRouteWithoutParametersGeneratesCorrectUrl(): void
215199 public function testRouteWithMultipleParameters (): void
216200 {
217201 // Arrange
218- $ this ->router ->get ( '/users/:id/posts/:post_id/comments/:comment_id ' , function () { return 'comment ' ; } )
219- ->setName ( 'user_post_comment ' );
202+ $ this ->router ->addNamedRoute ( 'user_post_comment ' , '/users/:id/posts/:post_id/comments/:comment_id ' );
220203
221204 // Act
222205 $ url = $ this ->urlHelper ->routePath ( 'user_post_comment ' , [
@@ -236,8 +219,7 @@ public function testAbsoluteUrlWithoutBaseUrlInRegistry(): void
236219 $ originalBaseUrl = $ registry ->get ( 'Base.Url ' );
237220 $ registry ->set ( 'Base.Url ' , null );
238221
239- $ this ->router ->get ( '/users/:id ' , function () { return 'test ' ; } )
240- ->setName ( 'user_profile ' );
222+ $ this ->router ->addNamedRoute ( 'user_profile ' , '/users/:id ' );
241223
242224 // Act
243225 $ url = $ this ->urlHelper ->routeUrl ( 'user_profile ' , ['id ' => 123 ] );
0 commit comments