|
17 | 17 | use Codeception\Module\Laravel\InteractsWithEloquent; |
18 | 18 | use Codeception\Module\Laravel\InteractsWithEvents; |
19 | 19 | use Codeception\Module\Laravel\InteractsWithExceptionHandling; |
| 20 | +use Codeception\Module\Laravel\InteractsWithRouting; |
20 | 21 | use Codeception\Subscriber\ErrorHandler; |
21 | 22 | use Codeception\TestInterface; |
22 | 23 | use Codeception\Util\ReflectionHelper; |
23 | 24 | use Exception; |
24 | | -use Illuminate\Contracts\Routing\UrlGenerator; |
25 | 25 | use Illuminate\Contracts\Session\Session; |
26 | 26 | use Illuminate\Contracts\View\Factory as ViewContract; |
27 | 27 | use Illuminate\Database\Connection; |
28 | 28 | use Illuminate\Database\DatabaseManager; |
29 | 29 | use Illuminate\Database\Eloquent\Factory; |
30 | 30 | use Illuminate\Foundation\Application; |
31 | | -use Illuminate\Http\Request; |
32 | 31 | use Illuminate\Routing\Route; |
33 | | -use Illuminate\Routing\Router; |
34 | 32 | use Illuminate\Support\ViewErrorBag; |
35 | | -use ReflectionClass; |
36 | 33 | use ReflectionException; |
37 | 34 | use function is_array; |
38 | 35 |
|
@@ -134,6 +131,7 @@ class Laravel extends Framework implements ActiveRecord, PartedModule |
134 | 131 | use InteractsWithEloquent; |
135 | 132 | use InteractsWithEvents; |
136 | 133 | use InteractsWithExceptionHandling; |
| 134 | + use InteractsWithRouting; |
137 | 135 |
|
138 | 136 | /** |
139 | 137 | * @var Application |
@@ -308,176 +306,6 @@ public function disableMiddleware() |
308 | 306 | $this->client->disableMiddleware(); |
309 | 307 | } |
310 | 308 |
|
311 | | - /** |
312 | | - * Opens web page using route name and parameters. |
313 | | - * |
314 | | - * ```php |
315 | | - * <?php |
316 | | - * $I->amOnRoute('posts.create'); |
317 | | - * ``` |
318 | | - * |
319 | | - * @param string $routeName |
320 | | - * @param mixed $params |
321 | | - */ |
322 | | - public function amOnRoute(string $routeName, $params = []): void |
323 | | - { |
324 | | - $route = $this->getRouteByName($routeName); |
325 | | - |
326 | | - $absolute = !is_null($route->domain()); |
327 | | - /** @var UrlGenerator $urlGenerator */ |
328 | | - $urlGenerator = $this->app['url']; |
329 | | - $url = $urlGenerator->route($routeName, $params, $absolute); |
330 | | - $this->amOnPage($url); |
331 | | - } |
332 | | - |
333 | | - /** |
334 | | - * Checks that current url matches route |
335 | | - * |
336 | | - * ``` php |
337 | | - * <?php |
338 | | - * $I->seeCurrentRouteIs('posts.index'); |
339 | | - * ``` |
340 | | - * @param string $routeName |
341 | | - */ |
342 | | - public function seeCurrentRouteIs(string $routeName): void |
343 | | - { |
344 | | - $this->getRouteByName($routeName); // Fails if route does not exists |
345 | | - |
346 | | - /** @var Request $request */ |
347 | | - $request = $this->app->request; |
348 | | - $currentRoute = $request->route(); |
349 | | - $currentRouteName = $currentRoute ? $currentRoute->getName() : ''; |
350 | | - |
351 | | - if ($currentRouteName != $routeName) { |
352 | | - $message = empty($currentRouteName) |
353 | | - ? "Current route has no name" |
354 | | - : "Current route is \"$currentRouteName\""; |
355 | | - $this->fail($message); |
356 | | - } |
357 | | - } |
358 | | - |
359 | | - /** |
360 | | - * Opens web page by action name |
361 | | - * |
362 | | - * ``` php |
363 | | - * <?php |
364 | | - * // Laravel 6 or 7: |
365 | | - * $I->amOnAction('PostsController@index'); |
366 | | - * |
367 | | - * // Laravel 8+: |
368 | | - * $I->amOnAction(PostsController::class . '@index'); |
369 | | - * ``` |
370 | | - * |
371 | | - * @param string $action |
372 | | - * @param mixed $parameters |
373 | | - */ |
374 | | - public function amOnAction(string $action, $parameters = []): void |
375 | | - { |
376 | | - $route = $this->getRouteByAction($action); |
377 | | - $absolute = !is_null($route->domain()); |
378 | | - /** @var UrlGenerator $urlGenerator */ |
379 | | - $urlGenerator = $this->app['url']; |
380 | | - $url = $urlGenerator->action($action, $parameters, $absolute); |
381 | | - |
382 | | - $this->amOnPage($url); |
383 | | - } |
384 | | - |
385 | | - /** |
386 | | - * Checks that current url matches action |
387 | | - * |
388 | | - * ``` php |
389 | | - * <?php |
390 | | - * // Laravel 6 or 7: |
391 | | - * $I->seeCurrentActionIs('PostsController@index'); |
392 | | - * |
393 | | - * // Laravel 8+: |
394 | | - * $I->seeCurrentActionIs(PostsController::class . '@index'); |
395 | | - * ``` |
396 | | - * |
397 | | - * @param string $action |
398 | | - */ |
399 | | - public function seeCurrentActionIs(string $action): void |
400 | | - { |
401 | | - $this->getRouteByAction($action); // Fails if route does not exists |
402 | | - /** @var Request $request */ |
403 | | - $request = $this->app->request; |
404 | | - $currentRoute = $request->route(); |
405 | | - $currentAction = $currentRoute ? $currentRoute->getActionName() : ''; |
406 | | - $currentAction = ltrim( |
407 | | - str_replace( (string)$this->getRootControllerNamespace(), '', $currentAction), |
408 | | - '\\' |
409 | | - ); |
410 | | - |
411 | | - if ($currentAction != $action) { |
412 | | - $this->fail("Current action is \"$currentAction\""); |
413 | | - } |
414 | | - } |
415 | | - |
416 | | - /** |
417 | | - * @param string $routeName |
418 | | - * @return mixed |
419 | | - */ |
420 | | - protected function getRouteByName(string $routeName) |
421 | | - { |
422 | | - /** @var Router $router */ |
423 | | - $router = $this->app['router']; |
424 | | - $routes = $router->getRoutes(); |
425 | | - if (!$route = $routes->getByName($routeName)) { |
426 | | - $this->fail("Route with name '$routeName' does not exist"); |
427 | | - } |
428 | | - |
429 | | - return $route; |
430 | | - } |
431 | | - |
432 | | - /** |
433 | | - * @param string $action |
434 | | - * @return Route |
435 | | - */ |
436 | | - protected function getRouteByAction(string $action): Route |
437 | | - { |
438 | | - $namespacedAction = $this->actionWithNamespace($action); |
439 | | - |
440 | | - if (!$route = $this->app['routes']->getByAction($namespacedAction)) { |
441 | | - $this->fail("Action '$action' does not exist"); |
442 | | - } |
443 | | - |
444 | | - return $route; |
445 | | - } |
446 | | - |
447 | | - /** |
448 | | - * Normalize an action to full namespaced action. |
449 | | - * |
450 | | - * @param string $action |
451 | | - * @return string |
452 | | - */ |
453 | | - protected function actionWithNamespace(string $action): string |
454 | | - { |
455 | | - $rootNamespace = $this->getRootControllerNamespace(); |
456 | | - |
457 | | - if ($rootNamespace && !(strpos($action, '\\') === 0)) { |
458 | | - return $rootNamespace . '\\' . $action; |
459 | | - } |
460 | | - |
461 | | - return trim($action, '\\'); |
462 | | - } |
463 | | - |
464 | | - /** |
465 | | - * Get the root controller namespace for the application. |
466 | | - * |
467 | | - * @return string|null |
468 | | - * @throws ReflectionException |
469 | | - */ |
470 | | - protected function getRootControllerNamespace(): ?string |
471 | | - { |
472 | | - $urlGenerator = $this->app['url']; |
473 | | - $reflection = new ReflectionClass($urlGenerator); |
474 | | - |
475 | | - $property = $reflection->getProperty('rootNamespace'); |
476 | | - $property->setAccessible(true); |
477 | | - |
478 | | - return $property->getValue($urlGenerator); |
479 | | - } |
480 | | - |
481 | 309 | /** |
482 | 310 | * Assert that a session variable exists. |
483 | 311 | * |
|
0 commit comments