| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace core\tests\router;
|
|
|
18 |
|
|
|
19 |
use core\router\abstract_route_loader;
|
|
|
20 |
use core\router\route_loader_interface;
|
|
|
21 |
use Slim\App;
|
|
|
22 |
use Slim\Routing\RouteCollectorProxy;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* A route loader containing mocked routes.
|
|
|
26 |
*
|
|
|
27 |
* @package core
|
|
|
28 |
* @copyright Andrew Lyons <andrew@nicols.co.uk>
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
*/
|
|
|
31 |
class mocking_route_loader extends abstract_route_loader implements route_loader_interface {
|
|
|
32 |
/** @var array[] The mocked routes to configure in the loader */
|
|
|
33 |
private array $groupdata = [];
|
|
|
34 |
|
|
|
35 |
#[\Override]
|
|
|
36 |
public function configure_routes(App $app): array {
|
|
|
37 |
$routegroups = [];
|
|
|
38 |
|
|
|
39 |
foreach ($this->groupdata as $path => $groupdata) {
|
|
|
40 |
$routegroups[$path] = $app->group($path, function (
|
|
|
41 |
RouteCollectorProxy $group,
|
|
|
42 |
) use (
|
|
|
43 |
$groupdata,
|
|
|
44 |
): void {
|
|
|
45 |
foreach ($groupdata as $data) {
|
|
|
46 |
$group
|
|
|
47 |
->map(...$data['mapdata'])
|
|
|
48 |
->setName($data['name']);
|
|
|
49 |
}
|
|
|
50 |
});
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
return $routegroups;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Add a mocked route to the loader.
|
|
|
58 |
*
|
|
|
59 |
* @param string $grouppath The path of the RouteGroup to add the route to
|
|
|
60 |
* @param array $methods The HTTP methods to add the route for
|
|
|
61 |
* @param string $pattern The path to add the route for
|
|
|
62 |
* @param callable $callable The callable to add the route for
|
|
|
63 |
* @param string $name The name of the route
|
|
|
64 |
*/
|
|
|
65 |
public function mock_route_from_callable(
|
|
|
66 |
string $grouppath,
|
|
|
67 |
array $methods,
|
|
|
68 |
string $pattern,
|
|
|
69 |
callable $callable,
|
|
|
70 |
string $name,
|
|
|
71 |
): void {
|
|
|
72 |
$this->add_groupdata(
|
|
|
73 |
$grouppath,
|
|
|
74 |
[
|
|
|
75 |
'methods' => $methods,
|
|
|
76 |
'pattern' => $pattern,
|
|
|
77 |
'callable' => $callable,
|
|
|
78 |
],
|
|
|
79 |
$name,
|
|
|
80 |
);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
/**
|
|
|
84 |
* Add all routes in a class to the loader.
|
|
|
85 |
*
|
|
|
86 |
* @param string $grouppath Thegroup to add the route to
|
|
|
87 |
* @param string|\ReflectionMethod $class The class to add to the loader
|
|
|
88 |
*/
|
|
|
89 |
public function add_all_routes_in_class(
|
|
|
90 |
string $grouppath,
|
|
|
91 |
\ReflectionMethod|string $class,
|
|
|
92 |
) {
|
|
|
93 |
$classinfo = $class instanceof \ReflectionClass ? $class : new \ReflectionClass($class);
|
|
|
94 |
|
|
|
95 |
$routes = $this->get_all_routes_in_class(
|
|
|
96 |
componentpath: '',
|
|
|
97 |
classinfo: $classinfo,
|
|
|
98 |
);
|
|
|
99 |
|
|
|
100 |
foreach ($routes as $mapdata) {
|
|
|
101 |
$this->add_groupdata(
|
|
|
102 |
$grouppath,
|
|
|
103 |
$mapdata,
|
|
|
104 |
implode('::', $mapdata['callable']),
|
|
|
105 |
);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Mock a route from a class method.
|
|
|
111 |
*
|
|
|
112 |
* @param string $grouppath The path to add the route to
|
|
|
113 |
* @param \ReflectionMethod $method The method to mock the route from
|
|
|
114 |
*/
|
|
|
115 |
public function mock_route_from_class_method(
|
|
|
116 |
string $grouppath,
|
|
|
117 |
\ReflectionMethod $method,
|
|
|
118 |
) {
|
|
|
119 |
$mapdata = $this->get_route_data_for_method(
|
|
|
120 |
componentpath: '',
|
|
|
121 |
classinfo: $method->getDeclaringClass(),
|
|
|
122 |
methodinfo: $method,
|
|
|
123 |
);
|
|
|
124 |
|
|
|
125 |
$this->add_groupdata(
|
|
|
126 |
$grouppath,
|
|
|
127 |
$mapdata,
|
|
|
128 |
implode('::', $mapdata['callable']),
|
|
|
129 |
);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Add group data to the loader.
|
|
|
134 |
*
|
|
|
135 |
* @param string $grouppath The path of the RouteGroup to add the data to
|
|
|
136 |
* @param array $data The data to add to the group
|
|
|
137 |
* @param string $name The name of the group
|
|
|
138 |
*/
|
|
|
139 |
protected function add_groupdata(
|
|
|
140 |
string $grouppath,
|
|
|
141 |
array $data,
|
|
|
142 |
string $name,
|
|
|
143 |
): void {
|
|
|
144 |
if (!array_key_exists($grouppath, $this->groupdata)) {
|
|
|
145 |
$this->groupdata[$grouppath] = [];
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
$this->groupdata[$grouppath][] = [
|
|
|
149 |
'mapdata' => $data,
|
|
|
150 |
'name' => $name,
|
|
|
151 |
];
|
|
|
152 |
}
|
|
|
153 |
}
|