Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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\router;
18
 
19
use core\tests\router\route_testcase;
20
use Slim\Routing\RoutingResults;
21
 
22
/**
23
 * Tests for the standard route loader.
24
 *
25
 * @package    core
26
 * @category   test
27
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @covers     \core\router\route_loader
30
 */
31
final class route_loader_test extends route_testcase {
32
    /**
33
     * Ensure that the abstract loader implements the interface.
34
     */
35
    public function test_class_implements(): void {
36
        $reflection = new \ReflectionClass(route_loader::class);
37
        $this->assertTrue($reflection->implementsInterface(route_loader_interface::class));
38
    }
39
 
40
    /**
41
     * Test that we are able to fetch all routes.
42
     */
43
    public function test_configure_api_routes(): void {
44
        $loader = new route_loader();
45
 
46
        $app = $this->get_simple_app();
47
        $routegroups = $loader->configure_routes($app);
48
 
49
        $this->assertIsArray($routegroups);
50
        $this->assertGreaterThanOrEqual(1, count($routegroups));
51
        foreach ($routegroups as $group) {
52
            // Each of the returned groups shoudl be a RouteGroupInterface.
53
            if (is_array($group)) {
54
                foreach ($group as $thisgroup) {
55
                    $this->assertInstanceOf(\Slim\Interfaces\RouteInterface::class, $thisgroup);
56
                }
57
            } else {
58
                $this->assertInstanceOf(\Slim\Interfaces\RouteGroupInterface::class, $group);
59
            }
60
        }
61
 
62
        // Note: It is not possible to test the actual routes that are added to
63
        // the group as they are added to the App which we cannot inspect.
64
        // We can, however, test that a known route is resolved.
65
 
66
        $collector = $app->getRouteCollector();
67
        $allroutes = $collector->getRoutes();
68
        foreach ($allroutes as $route) {
69
            $thisroutegroups = $route->getGroups();
70
            $this->assertGreaterThanOrEqual(1, $thisroutegroups);
71
            foreach ($thisroutegroups as $thisroutegroup) {
72
                $this->assertContains($thisroutegroup, $routegroups);
73
            }
74
        }
75
 
76
        // Resolve the OpenAPI route.
77
        $path = route_loader_interface::ROUTE_GROUP_API . '/openapi.json';
78
        $result = $app->getRouteResolver()->computeRoutingResults($path, 'GET');
79
        $this->assertNotNull($result);
80
        $this->assertInstanceOf(\Slim\Routing\RoutingResults::class, $result);
81
 
82
        // The result should be found.
83
        $this->assertEquals(RoutingResults::FOUND, $result->getRouteStatus());
84
 
85
        // It should have an identifier which resolves to a Route.
86
        $identifier = $result->getRouteIdentifier();
87
        $this->assertIsString($identifier);
88
        $route = $app->getRouteResolver()->resolveRoute($identifier);
89
        $this->assertInstanceOf(\Slim\Interfaces\RouteInterface::class, $route);
90
    }
91
}