Skip to content

Commit e9f8924

Browse files
committed
fixed tests... I think.
1 parent 0ad1de7 commit e9f8924

File tree

8 files changed

+139
-41
lines changed

8 files changed

+139
-41
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
},
3434
"autoload-dev": {
3535
"psr-4": {
36-
"Mvc\\": "tests/Mvc"
36+
"Mvc\\": "tests/Mvc",
37+
"Tests\\": "tests/Tests"
3738
}
3839
},
3940
"extra": {

examples/views/Base/index.php

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
## Header 2
44

5-
This is a test.
5+
This is a test.

examples/views/base/index.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h2><?=$var_one?></h2>
2+
<p><?=$two?> + <?=$three?> = <?= ($two + $three) ?></p>

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[![CI](https://github.com/Neuron-PHP/mvc/actions/workflows/ci.yml/badge.svg)](https://github.com/Neuron-PHP/mvc/actions)
12
# Neuron-PHP MVC
23

34
A lightweight MVC (Model-View-Controller) framework component for PHP 8.4+ that provides core MVC functionality including controllers, views, routing integration, request handling, and a powerful view caching system.

tests/Mvc/Helpers/UrlHelperTest.php

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Neuron\Routing\Router;
88
use Neuron\Routing\RouteMap;
99
use PHPUnit\Framework\TestCase;
10+
use Tests\Mock\MockRouter;
1011

1112
/**
1213
* Unit tests for the UrlHelper class.
@@ -16,26 +17,20 @@
1617
*/
1718
class 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] );

tests/Tests/Mock/MockRouter.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace Tests\Mock;
4+
5+
use Neuron\Routing\Router;
6+
use Neuron\Routing\RouteMap;
7+
8+
/**
9+
* Mock Router for testing URL helpers.
10+
*
11+
* This router implements the URL helper methods needed for testing,
12+
* even when the vendor routing package doesn't have them.
13+
*/
14+
class MockRouter extends Router
15+
{
16+
private array $namedRoutes = [];
17+
18+
/**
19+
* Override to store routes with names for testing
20+
*/
21+
public function get( string $Route, $function, ?string $Filter = null ): RouteMap
22+
{
23+
$route = parent::get( $Route, $function, $Filter );
24+
return $route;
25+
}
26+
27+
/**
28+
* Override to store routes with names for testing
29+
*/
30+
public function post( string $Route, $function, ?string $Filter = null ): RouteMap
31+
{
32+
$route = parent::post( $Route, $function, $Filter );
33+
return $route;
34+
}
35+
36+
/**
37+
* Store a named route for testing
38+
*/
39+
public function addNamedRoute( string $name, string $path, string $method = 'GET' ): MockRouter
40+
{
41+
$this->namedRoutes[$name] = [
42+
'name' => $name,
43+
'path' => $path,
44+
'method' => $method
45+
];
46+
return $this;
47+
}
48+
49+
/**
50+
* Find a route by name (for URL helper testing)
51+
*/
52+
public function getRouteByName( string $name ): ?RouteMap
53+
{
54+
if( !isset( $this->namedRoutes[$name] ) )
55+
{
56+
return null;
57+
}
58+
59+
$routeData = $this->namedRoutes[$name];
60+
$route = new RouteMap( $routeData['path'], function() { return 'test'; } );
61+
$route->setName( $name );
62+
63+
return $route;
64+
}
65+
66+
/**
67+
* Generate URL from named route (for URL helper testing)
68+
*/
69+
public function generateUrl( string $name, array $parameters = [], bool $absolute = false ): ?string
70+
{
71+
$route = $this->getRouteByName( $name );
72+
73+
if( !$route )
74+
{
75+
return null;
76+
}
77+
78+
$path = $route->getPath();
79+
80+
// Replace route parameters with actual values
81+
foreach( $parameters as $key => $value )
82+
{
83+
$path = str_replace( ':' . $key, $value, $path );
84+
}
85+
86+
// If absolute URL requested, prepend base URL
87+
if( $absolute )
88+
{
89+
$baseUrl = \Neuron\Patterns\Registry::getInstance()->get( 'Base.Url' );
90+
if( $baseUrl )
91+
{
92+
return rtrim( $baseUrl, '/' ) . $path;
93+
}
94+
}
95+
96+
return $path;
97+
}
98+
99+
/**
100+
* Get all named routes (for URL helper testing)
101+
*/
102+
public function getAllNamedRoutes(): array
103+
{
104+
$routes = [];
105+
106+
foreach( $this->namedRoutes as $name => $routeData )
107+
{
108+
$routes[] = [
109+
'name' => $name,
110+
'method' => $routeData['method'],
111+
'path' => $routeData['path']
112+
];
113+
}
114+
115+
return $routes;
116+
}
117+
}

tests/phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</coverage>
88
<testsuites>
99
<testsuite name="unit">
10-
<directory>mvc</directory>
10+
<directory>Mvc</directory>
1111
</testsuite>
1212
</testsuites>
1313
</phpunit>

0 commit comments

Comments
 (0)