MDL-82989 communication_matrix: Apply helper to mock http_client

This commit is contained in:
meirzamoodle 2024-10-27 15:56:22 +07:00
parent 149a477903
commit 3e0c2a45fb
2 changed files with 29 additions and 36 deletions

View File

@ -20,10 +20,7 @@ use communication_matrix\local\command;
use communication_matrix\local\spec\v1p7;
use communication_matrix\local\spec\features;
use communication_matrix\tests\fixtures\mocked_matrix_client;
use core\http_client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Response;
use moodle_exception;
@ -89,16 +86,13 @@ class matrix_client_test extends \advanced_testcase {
?array $versions,
string $expectedversion,
): void {
// Create a mock and queue two responses.
$mock = new MockHandler([
$this->get_mocked_version_response($versions),
]);
$handlerstack = HandlerStack::create($mock);
$container = [];
$history = Middleware::history($container);
$handlerstack->push($history);
$client = new http_client(['handler' => $handlerstack]);
['client' => $client, 'mock' => $mock] = $this->get_mocked_http_client(
history: $container,
);
$mock->append($this->get_mocked_version_response($versions));
mocked_matrix_client::set_client($client);
$instance = mocked_matrix_client::instance(
@ -121,15 +115,15 @@ class matrix_client_test extends \advanced_testcase {
* Test that the instance method returns a valid instance for the given versions.
*/
public function test_instance_cached(): void {
$mock = new MockHandler([
$this->get_mocked_version_response(),
$this->get_mocked_version_response(),
]);
$handlerstack = HandlerStack::create($mock);
$container = [];
$history = Middleware::history($container);
$handlerstack->push($history);
$client = new http_client(['handler' => $handlerstack]);
['client' => $client, 'mock' => $mock] = $this->get_mocked_http_client(
history: $container,
);
// Queue two responses.
$mock->append($this->get_mocked_version_response());
$mock->append($this->get_mocked_version_response());
mocked_matrix_client::set_client($client);
$instance = mocked_matrix_client::instance('https://example.com', 'testtoken');
@ -153,16 +147,10 @@ class matrix_client_test extends \advanced_testcase {
* Test that the instance method throws an appropriate exception if no support is found.
*/
public function test_instance_no_support(): void {
// Create a mock and queue two responses.
['client' => $client, 'mock' => $mock] = $this->get_mocked_http_client();
$mock->append($this->get_mocked_version_response(['v99.9']));
$mock = new MockHandler([
$this->get_mocked_version_response(['v99.9']),
]);
$handlerstack = HandlerStack::create($mock);
$container = [];
$history = Middleware::history($container);
$handlerstack->push($history);
$client = new http_client(['handler' => $handlerstack]);
mocked_matrix_client::set_client($client);
$this->expectException(moodle_exception::class);

View File

@ -67,19 +67,24 @@ trait matrix_client_test_trait {
array &$historycontainer = [],
?MockHandler $mock = null,
): matrix_client {
// If no mock is provided, use get_mocked_http_client to create the mock and client.
if ($mock === null) {
$mock = new MockHandler();
['mock' => $mock, 'client' => $client] = $this->get_mocked_http_client(
history: $historycontainer
);
} else {
// If mock is provided, create the handlerstack and history middleware.
$handlerstack = HandlerStack::create($mock);
$history = Middleware::history($historycontainer);
$handlerstack->push($history);
$client = new http_client(['handler' => $handlerstack]);
}
// Add the version response.
$mock->append($this->get_mocked_version_response([$version]));
$handlerstack = HandlerStack::create($mock);
$history = Middleware::history($historycontainer);
$handlerstack->push($history);
$client = new http_client(['handler' => $handlerstack]);
mocked_matrix_client::set_client($client);
$client = mocked_matrix_client::instance(
$instance = mocked_matrix_client::instance(
'https://example.com',
'testtoken',
);
@ -87,7 +92,7 @@ trait matrix_client_test_trait {
// Remove the request that is required to fetch the version from the history.
array_shift($historycontainer);
return $client;
return $instance;
}
/**