Skip to content

Commit 8836efb

Browse files
committed
Split routing methods
1 parent 051173c commit 8836efb

File tree

2 files changed

+187
-174
lines changed

2 files changed

+187
-174
lines changed

src/Codeception/Module/Laravel.php

Lines changed: 2 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,19 @@
1717
use Codeception\Module\Laravel\InteractsWithEloquent;
1818
use Codeception\Module\Laravel\InteractsWithEvents;
1919
use Codeception\Module\Laravel\InteractsWithExceptionHandling;
20+
use Codeception\Module\Laravel\InteractsWithRouting;
2021
use Codeception\Subscriber\ErrorHandler;
2122
use Codeception\TestInterface;
2223
use Codeception\Util\ReflectionHelper;
2324
use Exception;
24-
use Illuminate\Contracts\Routing\UrlGenerator;
2525
use Illuminate\Contracts\Session\Session;
2626
use Illuminate\Contracts\View\Factory as ViewContract;
2727
use Illuminate\Database\Connection;
2828
use Illuminate\Database\DatabaseManager;
2929
use Illuminate\Database\Eloquent\Factory;
3030
use Illuminate\Foundation\Application;
31-
use Illuminate\Http\Request;
3231
use Illuminate\Routing\Route;
33-
use Illuminate\Routing\Router;
3432
use Illuminate\Support\ViewErrorBag;
35-
use ReflectionClass;
3633
use ReflectionException;
3734
use function is_array;
3835

@@ -134,6 +131,7 @@ class Laravel extends Framework implements ActiveRecord, PartedModule
134131
use InteractsWithEloquent;
135132
use InteractsWithEvents;
136133
use InteractsWithExceptionHandling;
134+
use InteractsWithRouting;
137135

138136
/**
139137
* @var Application
@@ -308,176 +306,6 @@ public function disableMiddleware()
308306
$this->client->disableMiddleware();
309307
}
310308

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-
481309
/**
482310
* Assert that a session variable exists.
483311
*
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Codeception\Module\Laravel;
6+
7+
use Illuminate\Contracts\Routing\UrlGenerator;
8+
use Illuminate\Http\Request;
9+
use Illuminate\Routing\Route;
10+
use Illuminate\Routing\Router;
11+
use ReflectionClass;
12+
use ReflectionException;
13+
14+
trait InteractsWithRouting
15+
{
16+
/**
17+
* Opens web page by action name
18+
*
19+
* ``` php
20+
* <?php
21+
* // Laravel 6 or 7:
22+
* $I->amOnAction('PostsController@index');
23+
*
24+
* // Laravel 8+:
25+
* $I->amOnAction(PostsController::class . '@index');
26+
* ```
27+
*
28+
* @param string $action
29+
* @param mixed $parameters
30+
*/
31+
public function amOnAction(string $action, $parameters = []): void
32+
{
33+
$route = $this->getRouteByAction($action);
34+
$absolute = !is_null($route->domain());
35+
/** @var UrlGenerator $urlGenerator */
36+
$urlGenerator = $this->app['url'];
37+
$url = $urlGenerator->action($action, $parameters, $absolute);
38+
39+
$this->amOnPage($url);
40+
}
41+
42+
/**
43+
* Opens web page using route name and parameters.
44+
*
45+
* ```php
46+
* <?php
47+
* $I->amOnRoute('posts.create');
48+
* ```
49+
*
50+
* @param string $routeName
51+
* @param mixed $params
52+
*/
53+
public function amOnRoute(string $routeName, $params = []): void
54+
{
55+
$route = $this->getRouteByName($routeName);
56+
57+
$absolute = !is_null($route->domain());
58+
/** @var UrlGenerator $urlGenerator */
59+
$urlGenerator = $this->app['url'];
60+
$url = $urlGenerator->route($routeName, $params, $absolute);
61+
$this->amOnPage($url);
62+
}
63+
64+
/**
65+
* Checks that current url matches action
66+
*
67+
* ``` php
68+
* <?php
69+
* // Laravel 6 or 7:
70+
* $I->seeCurrentActionIs('PostsController@index');
71+
*
72+
* // Laravel 8+:
73+
* $I->seeCurrentActionIs(PostsController::class . '@index');
74+
* ```
75+
*
76+
* @param string $action
77+
*/
78+
public function seeCurrentActionIs(string $action): void
79+
{
80+
$this->getRouteByAction($action); // Fails if route does not exists
81+
/** @var Request $request */
82+
$request = $this->app->request;
83+
$currentRoute = $request->route();
84+
$currentAction = $currentRoute ? $currentRoute->getActionName() : '';
85+
$currentAction = ltrim(
86+
str_replace( (string)$this->getRootControllerNamespace(), '', $currentAction),
87+
'\\'
88+
);
89+
90+
if ($currentAction != $action) {
91+
$this->fail("Current action is \"$currentAction\"");
92+
}
93+
}
94+
95+
/**
96+
* Checks that current url matches route
97+
*
98+
* ``` php
99+
* <?php
100+
* $I->seeCurrentRouteIs('posts.index');
101+
* ```
102+
* @param string $routeName
103+
*/
104+
public function seeCurrentRouteIs(string $routeName): void
105+
{
106+
$this->getRouteByName($routeName); // Fails if route does not exists
107+
108+
/** @var Request $request */
109+
$request = $this->app->request;
110+
$currentRoute = $request->route();
111+
$currentRouteName = $currentRoute ? $currentRoute->getName() : '';
112+
113+
if ($currentRouteName != $routeName) {
114+
$message = empty($currentRouteName)
115+
? "Current route has no name"
116+
: "Current route is \"$currentRouteName\"";
117+
$this->fail($message);
118+
}
119+
}
120+
121+
/**
122+
* Get the root controller namespace for the application.
123+
*
124+
* @return string|null
125+
* @throws ReflectionException
126+
*/
127+
protected function getRootControllerNamespace(): ?string
128+
{
129+
$urlGenerator = $this->app['url'];
130+
$reflection = new ReflectionClass($urlGenerator);
131+
132+
$property = $reflection->getProperty('rootNamespace');
133+
$property->setAccessible(true);
134+
135+
return $property->getValue($urlGenerator);
136+
}
137+
138+
/**
139+
* @param string $action
140+
* @return Route
141+
*/
142+
protected function getRouteByAction(string $action): Route
143+
{
144+
$namespacedAction = $this->actionWithNamespace($action);
145+
146+
if (!$route = $this->app['routes']->getByAction($namespacedAction)) {
147+
$this->fail("Action '$action' does not exist");
148+
}
149+
150+
return $route;
151+
}
152+
153+
/**
154+
* @param string $routeName
155+
* @return mixed
156+
*/
157+
protected function getRouteByName(string $routeName)
158+
{
159+
/** @var Router $router */
160+
$router = $this->app['router'];
161+
$routes = $router->getRoutes();
162+
if (!$route = $routes->getByName($routeName)) {
163+
$this->fail("Route with name '$routeName' does not exist");
164+
}
165+
166+
return $route;
167+
}
168+
169+
/**
170+
* Normalize an action to full namespaced action.
171+
*
172+
* @param string $action
173+
* @return string
174+
*/
175+
protected function actionWithNamespace(string $action): string
176+
{
177+
$rootNamespace = $this->getRootControllerNamespace();
178+
179+
if ($rootNamespace && !(strpos($action, '\\') === 0)) {
180+
return $rootNamespace . '\\' . $action;
181+
}
182+
183+
return trim($action, '\\');
184+
}
185+
}

0 commit comments

Comments
 (0)