This repository was archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathRouterTrait.php
More file actions
87 lines (75 loc) · 2.55 KB
/
Copy pathRouterTrait.php
File metadata and controls
87 lines (75 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace Cake\Codeception\Helper;
use Cake\Routing\Router;
use InvalidArgumentException;
trait RouterTrait
{
/**
* Opens web page using route array (or `_name`) and parameters.
*
* @param array|string $route Route's array or name.
* @param array $params Extra route parameters (i.e. prefix, _method, etc.)
*/
public function amOnRoute($route, $params = [])
{
if (!is_array($route)) {
$route = ['_name' => $route];
}
$this->amOnPage(Router::url($route + $params + ['_method' => 'GET']));
}
/**
* Opens web page using action name and parameters.
*
* @param string $action String notation of the route (i.e. PostsController.add, Posts.add, posts.add).
* @param array $params Extra route parameters (i.e. prefix, _method, etc.).
*/
public function amOnAction($action, $params = [])
{
$this->amOnRoute($this->routeFromAction($action), $params);
}
/**
* Asserts that current url matches route.
*
* @param array|string $route Route's array or name.
* @param array $params Extra route parameters (i.e. prefix, _method, etc.)
*/
public function seeCurrentRouteIs($route, $params = [])
{
if (!is_array($route)) {
$route = ['_name' => $route];
}
$this->seeCurrentUrlEquals(Router::url($route + $params));
}
/**
* Asserts that current url matches action.
*
* @param string $action String notation of the route (i.e. PostsController.add, Posts.add, posts.add).
* @param array $params Extra route parameters (i.e. prefix, _method, etc.)
*/
public function seeCurrentActionIs($action, $params = [])
{
$this->seeCurrentRouteIs($this->routeFromAction($action), $params);
}
/**
* Returns a route array from an action string notation.
*
* @param string $action String notation of the route (i.e. PostsController.add, Posts.add, posts.add).
* @return array
*/
protected function routeFromAction($action)
{
$parts = [];
foreach (['@', '.'] as $delimiter) {
if (mb_strpos($action, $delimiter)) {
$parts = explode($delimiter, $action);
break;
}
}
if (count($parts) < 2) {
throw new InvalidArgumentException(sprintf('Invalid action name [%s]', $action));
}
list($controller, $action) = $parts;
$controller = str_replace('Controller', '', $controller);
return compact('controller', 'action');
}
}