|
172 | 172 | false,
|
173 | 173 | ]);
|
174 | 174 |
|
175 |
| -test('the clone action prefixes already prefixed routes correctly', function () { |
| 175 | +test('the clone action prefixes already prefixed routes correctly', function (bool $tenantParameterBeforePrefix) { |
176 | 176 | $routes = [
|
177 | 177 | RouteFacade::get('/home', fn () => true)
|
178 | 178 | ->middleware(['clone'])
|
|
195 | 195 | ->prefix('prefix/'),
|
196 | 196 | ];
|
197 | 197 |
|
198 |
| - app(CloneRoutesAsTenant::class)->handle(); |
| 198 | + $cloneAction = app(CloneRoutesAsTenant::class); |
| 199 | + $cloneAction |
| 200 | + ->tenantParameterBeforePrefix($tenantParameterBeforePrefix) |
| 201 | + ->handle(); |
| 202 | + |
| 203 | + $expectedPrefix = $tenantParameterBeforePrefix ? '{tenant}/prefix' : 'prefix/{tenant}'; |
199 | 204 |
|
200 | 205 | $clonedRoutes = [
|
201 | 206 | RouteFacade::getRoutes()->getByName('tenant.home'),
|
|
206 | 211 |
|
207 | 212 | // The cloned route is prefixed correctly
|
208 | 213 | foreach ($clonedRoutes as $key => $route) {
|
209 |
| - expect($route->getPrefix())->toBe("prefix/{tenant}"); |
| 214 | + expect($route->getPrefix())->toBe($expectedPrefix); |
210 | 215 |
|
211 | 216 | $clonedRouteUrl = route($route->getName(), ['tenant' => $tenant = Tenant::create()]);
|
| 217 | + $expectedPrefixInUrl = $tenantParameterBeforePrefix ? "{$tenant->id}/prefix" : "prefix/{$tenant->id}"; |
212 | 218 |
|
213 | 219 | expect($clonedRouteUrl)
|
214 | 220 | // Original prefix does not occur in the cloned route's URL
|
215 | 221 | ->not()->toContain("prefix/{$tenant->id}/prefix")
|
216 | 222 | ->not()->toContain("//prefix")
|
217 | 223 | ->not()->toContain("prefix//")
|
218 | 224 | // Instead, the route is prefixed correctly
|
219 |
| - ->toBe("http://localhost/prefix/{$tenant->id}/{$routes[$key]->getName()}"); |
| 225 | + ->toBe("http://localhost/{$expectedPrefixInUrl}/{$routes[$key]->getName()}"); |
220 | 226 |
|
221 | 227 | // The cloned route is accessible
|
222 | 228 | pest()->get($clonedRouteUrl)->assertOk();
|
223 | 229 | }
|
224 |
| -}); |
| 230 | +})->with([true, false]); |
225 | 231 |
|
226 |
| -test('clone action trims trailing slashes from prefixes given to nested route groups', function () { |
| 232 | +test('clone action trims trailing slashes from prefixes given to nested route groups', function (bool $tenantParameterBeforePrefix) { |
227 | 233 | RouteFacade::prefix('prefix')->group(function () {
|
228 | 234 | RouteFacade::prefix('')->group(function () {
|
229 | 235 | // This issue seems to only happen when there's a group with a prefix, then a group with an empty prefix, and then a / route
|
|
237 | 243 | });
|
238 | 244 | });
|
239 | 245 |
|
240 |
| - app(CloneRoutesAsTenant::class)->handle(); |
| 246 | + $cloneAction = app(CloneRoutesAsTenant::class); |
| 247 | + $cloneAction |
| 248 | + ->tenantParameterBeforePrefix($tenantParameterBeforePrefix) |
| 249 | + ->handle(); |
241 | 250 |
|
242 | 251 | $clonedLandingUrl = route('tenant.landing', ['tenant' => $tenant = Tenant::create()]);
|
243 | 252 | $clonedHomeRouteUrl = route('tenant.home', ['tenant' => $tenant]);
|
244 | 253 |
|
245 | 254 | $landingRoute = RouteFacade::getRoutes()->getByName('tenant.landing');
|
246 | 255 | $homeRoute = RouteFacade::getRoutes()->getByName('tenant.home');
|
247 | 256 |
|
248 |
| - expect($landingRoute->uri())->toBe('prefix/{tenant}'); |
249 |
| - expect($homeRoute->uri())->toBe('prefix/{tenant}/home'); |
| 257 | + $expectedPrefix = $tenantParameterBeforePrefix ? '{tenant}/prefix' : 'prefix/{tenant}'; |
| 258 | + $expectedPrefixInUrl = $tenantParameterBeforePrefix ? "{$tenant->id}/prefix" : "prefix/{$tenant->id}"; |
| 259 | + |
| 260 | + expect($landingRoute->uri())->toBe($expectedPrefix); |
| 261 | + expect($homeRoute->uri())->toBe("{$expectedPrefix}/home"); |
250 | 262 |
|
251 | 263 | expect($clonedLandingUrl)
|
252 | 264 | ->not()->toContain("prefix//")
|
253 |
| - ->toBe("http://localhost/prefix/{$tenant->id}"); |
| 265 | + ->toBe("http://localhost/{$expectedPrefixInUrl}"); |
254 | 266 |
|
255 | 267 | expect($clonedHomeRouteUrl)
|
256 | 268 | ->not()->toContain("prefix//")
|
257 |
| - ->toBe("http://localhost/prefix/{$tenant->id}/home"); |
258 |
| -}); |
| 269 | + ->toBe("http://localhost/{$expectedPrefixInUrl}/home"); |
| 270 | +})->with([true, false]); |
259 | 271 |
|
260 | 272 | test('tenant routes are ignored from cloning and clone middleware in groups causes no issues', function () {
|
261 | 273 | // Should NOT be cloned, already has tenant parameter
|
|
0 commit comments