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\schema\response;
|
|
|
18 |
|
|
|
19 |
use core\router\response_handler;
|
|
|
20 |
use core\tests\router\route_testcase;
|
|
|
21 |
use GuzzleHttp\Psr7\Response;
|
|
|
22 |
use GuzzleHttp\Psr7\ServerRequest;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Tests for the payload response.
|
|
|
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 |
* @covers \core\router\schema\response\payload_response
|
|
|
31 |
* @covers \core\router\schema\response\abstract_response
|
|
|
32 |
*/
|
|
|
33 |
final class payload_response_test extends route_testcase {
|
|
|
34 |
public function test_get_payload(): void {
|
|
|
35 |
$request = new ServerRequest('GET', 'http://example.com/example/endpoint');
|
|
|
36 |
|
|
|
37 |
$payload = [
|
|
|
38 |
'example' => 'data',
|
|
|
39 |
'goes' => 'here',
|
|
|
40 |
'count' => 123,
|
|
|
41 |
];
|
|
|
42 |
|
|
|
43 |
$response = new payload_response($payload, $request);
|
|
|
44 |
$this->assertEquals($payload, $response->payload);
|
|
|
45 |
$this->assertEquals($request, $response->get_request());
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public function test_basics(): void {
|
|
|
49 |
$request = new ServerRequest('GET', 'http://example.com/example/endpoint');
|
|
|
50 |
$response = new Response();
|
|
|
51 |
|
|
|
52 |
$payloaddata = [
|
|
|
53 |
'example' => 'data',
|
|
|
54 |
'goes' => 'here',
|
|
|
55 |
'count' => 123,
|
|
|
56 |
];
|
|
|
57 |
|
|
|
58 |
$payload = new payload_response($payloaddata, $request, $response);
|
|
|
59 |
$this->assertSame($request, $payload->request);
|
|
|
60 |
$this->assertSame($response, $payload->response);
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
public function test_response_standardisation(): void {
|
|
|
64 |
$request = new ServerRequest('GET', 'http://example.com/example/endpoint');
|
|
|
65 |
$response = new Response();
|
|
|
66 |
|
|
|
67 |
$payloaddata = [
|
|
|
68 |
'example' => 'data',
|
|
|
69 |
'goes' => 'here',
|
|
|
70 |
'count' => 123,
|
|
|
71 |
];
|
|
|
72 |
|
|
|
73 |
$payload = new payload_response($payloaddata, $request, $response);
|
|
|
74 |
|
|
|
75 |
$handler = new response_handler(\core\di::get_container());
|
|
|
76 |
|
|
|
77 |
// Note: The standardisation itself is tested elsewhere.
|
|
|
78 |
$response = $handler->standardise_response($payload);
|
|
|
79 |
$this->assertInstanceOf(Response::class, $response);
|
|
|
80 |
}
|
|
|
81 |
}
|