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 GuzzleHttp\Psr7\Response;
21
use Psr\Http\Message\ServerRequestInterface;
22
 
23
/**
24
 * Tests for the controller invoker, and related bridge.
25
 *
26
 * @package    core
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\controller_invoker
30
 * @covers     \core\router\bridge
31
 * @covers     \core\router\response_handler
32
 */
33
final class controller_invoker_test extends route_testcase {
34
    /**
35
     * Configure an instance of Slim and fetch the Invoker.
36
     *
37
     * @return \Slim\Interfaces\InvocationStrategyInterface
38
     */
39
    protected function get_invocation_strategy(): \Slim\Interfaces\InvocationStrategyInterface {
40
        $container = \core\di::get_container();
41
        bridge::create($container);
42
        $app = $container->get(\Slim\App::class);
43
        return $app->getRouteCollector()->getDefaultInvocationStrategy();
44
    }
45
 
46
    /**
47
     * Test that setup of the invoker using the router\bridge sets the correct invoker strategy.
48
     * @covers \core\router\bridge
49
     * @covers \core\router\controller_invoker
50
     */
51
    public function test_setup_of_invoker(): void {
52
        $strategy = $this->get_invocation_strategy();
53
        $this->assertInstanceOf(controller_invoker::class, $strategy);
54
    }
55
 
56
    public function test_invocation_with_arguments(): void {
57
        $strategy = $this->get_invocation_strategy();
58
        $testcase = $this;
59
 
60
        // Providing a callable with no args will mean that none are provided.
61
        $callable = function () use ($testcase): Response {
62
            $testcase->assertCount(0, func_get_args());
63
            return new Response();
64
        };
65
 
66
        $request = $this->create_request('GET', '/example');
67
        $response = new Response();
68
        $strategy($callable, $request, $response, []);
69
 
70
        // Requesting one Response will get us the Response.
71
        $originalresponse = new Response();
72
        $callable = function (Response $response) use ($testcase, $originalresponse): Response {
73
            $testcase->assertNotNull($response);
74
            $testcase->assertEquals($originalresponse, $response);
75
            // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue
76
            $testcase->assertCount(1, func_get_args());
77
            return $response;
78
        };
79
 
80
        $request = $this->create_request('GET', '/example');
81
 
82
        $strategy($callable, $request, $originalresponse, []);
83
 
84
        // Requesting the Request will get us the Request.
85
        $serverrequest = $this->create_request('GET', '/example');
86
        $callable = function (ServerRequestInterface $request) use ($testcase, $serverrequest): Response {
87
            $this->assertEquals($serverrequest, $request);
88
            // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue
89
            $testcase->assertCount(1, func_get_args());
90
 
91
            return new Response();
92
        };
93
 
94
        $strategy($callable, $serverrequest, $response, []);
95
 
96
        // Attributes on the request will be passed through if requested.
97
        $serverrequest = $this->create_request('GET', '/example')
98
            ->withAttribute('example', 'This is an examplar attribute!');
99
        $callable = function (
100
            string $example,
101
        ) use ($testcase): Response {
102
            $testcase->assertCount(1, func_get_args());
103
            $testcase->assertEquals('This is an examplar attribute!', $example);
104
            return new Response();
105
        };
106
 
107
        $strategy($callable, $serverrequest, $response, []);
108
 
109
        // Route arguments to request will be passed through if requested.
110
        $serverrequest = $this->create_request('GET', '/example');
111
        $callable = function (
112
            string $example,
113
        ) use ($testcase): Response {
114
            $testcase->assertCount(1, func_get_args());
115
            $testcase->assertEquals('This is an examplar attribute!', $example);
116
            return new Response();
117
        };
118
 
119
        $strategy($callable, $serverrequest, $response, [
120
            'example' => 'This is an examplar attribute!',
121
        ]);
122
 
123
        // Attributes will be overridden by Route arguments.
124
        $serverrequest = $this->create_request('GET', '/example')
125
            ->withAttribute('example', 'This is an examplar attribute!');
126
        $callable = function (
127
            string $example,
128
        ) use ($testcase): Response {
129
            $testcase->assertCount(1, func_get_args());
130
            $testcase->assertEquals('This is a different examplar attribute!', $example);
131
            return new Response();
132
        };
133
 
134
        $strategy($callable, $serverrequest, $response, [
135
            'example' => 'This is a different examplar attribute!',
136
        ]);
137
    }
138
}