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
/**
20
 * Tests for the abstract route loader.
21
 *
22
 * Note: This is an abstract class used as an optional helper for any other route loader.
23
 * All methods on it are protected and testing them requires a concrete implementation.
24
 *
25
 * @package    core
26
 * @category   test
27
 * @copyright  2024 Andrew Lyons <andrew@nicols.co.uk>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @covers     \core\router\abstract_route_loader
30
 */
31
final class abstract_route_loader_test extends \advanced_testcase {
32
    /**
33
     * Ensure that the abstract loader does not implement the interface. That would defeat the point.
34
     */
35
    public function test_abstract_route_loader_does_not_implement_interface(): void {
36
        $reflection = new \ReflectionClass(abstract_route_loader::class);
37
        $this->assertFalse($reflection->implementsInterface(route_loader_interface::class));
38
    }
39
 
40
    /**
41
     * Test that we can fetch routes in a namespace.
42
     */
43
    public function test_get_all_routes_in_namespace(): void {
44
        // phpcs:ignore
45
        $loader = new class() extends abstract_route_loader {
46
            // phpcs:ignore
47
            public function get_routes(): array {
48
                return $this->get_all_routes_in_namespace(
49
                    'route\api',
50
                    function (string $component): string {
51
                        return '/path/to/' . $component;
52
                    },
53
                );
54
            }
55
        };
56
 
57
        $routes = $loader->get_routes();
58
        $this->assertGreaterThan(1, count($routes));
59
        foreach ($routes as $route) {
60
            $this->assertArrayHasKey('methods', $route);
61
            $this->assertArrayHasKey('pattern', $route);
62
            $this->assertArrayHasKey('callable', $route);
63
            $this->assertStringStartsWith('/path/to/', $route['pattern']);
64
        }
65
    }
66
 
67
    /**
68
     * Test tha the normalise_component_path method works as expected.
69
     *
70
     * @dataProvider normalise_component_path_provider
71
     * @param string $input
72
     * @param string $expected
73
     */
74
    public function test_normalise_component_path(
75
        string $input,
76
        string $expected,
77
    ): void {
78
        // phpcs:ignore
79
        $loader = new class() extends abstract_route_loader {
80
            // phpcs:ignore
81
            public function method(...$args): string {
82
                return $this->normalise_component_path(...$args);
83
            }
84
        };
85
 
86
        $this->assertEquals(
87
            $expected,
88
            $loader->method($input),
89
        );
90
    }
91
 
92
    /**
93
     * Data provider for test_normalise_component_path.
94
     *
95
     * @return array
96
     */
97
    public static function normalise_component_path_provider(): array {
98
        return [
99
            ['core', 'core'],
100
            ['core_user', 'user'],
101
            ['mod_forum', 'mod_forum'],
102
            ['', ''],
103
        ];
104
    }
105
 
106
    /**
107
     * Tests for the set_route_name_for_callable method.
108
     */
109
    public function test_set_route_name_for_callable(): void {
110
        // phpcs:ignore
111
        $loader = new class() extends abstract_route_loader {
112
            // phpcs:ignore
113
            public function call(...$args): ?string {
114
                return $this->set_route_name_for_callable(...$args);
115
            }
116
        };
117
 
118
        $route = $this->createMock(\Slim\Routing\Route::class);
119
        $route->expects($this->once())
120
            ->method('setName')
121
            ->with('routename');
122
 
123
        $name = $loader->call($route, 'routename');
124
        $this->assertEquals('routename', $name);
125
 
126
        $route = $this->createMock(\Slim\Routing\Route::class);
127
        $route->expects($this->once())
128
            ->method('setName')
129
            ->with('class::method');
130
 
131
        $name = $loader->call($route, ['class', 'method']);
132
        $this->assertEquals('class::method', $name);
133
 
134
        $route = $this->createMock(\Slim\Routing\Route::class);
135
        $route->expects($this->never())
136
            ->method('setName');
137
 
138
        $name = $loader->call($route, fn () => '');
139
        $this->assertEquals(null, $name);
140
    }
141
}