Skip to content

Commit

Permalink
Tests: Use mocked Guzzle client in Modcss action test
Browse files Browse the repository at this point in the history
  • Loading branch information
alecpl committed Dec 17, 2023
1 parent 04671ba commit 2546d2e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/Actions/Utils/Modcss.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ function test_run()
// Valid url pointing to non-existing resource
$_SESSION['modcssurls'][$key] = $url;

setHttpClientMock([
['code' => 404],
['code' => 200, 'headers' => ['Content-Type' => 'text/css'], 'response' => 'div.pre { display: none; }'],
]);

$this->runAndAssert($action, OutputHtmlMock::E_EXIT);

$this->assertSame(404, $output->getProperty('errorCode'));
Expand Down
22 changes: 22 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,25 @@ function getHTMLNodes($html, $xpath_query)

return $xpath->query($xpath_query);
}

/**
* Mock Guzzle HTTP Client
*/
function setHttpClientMock(array $responses)
{
foreach ($responses as $idx => $response) {
if (is_array($response)) {
$responses[$idx] = new \GuzzleHttp\Psr7\Response(
$response['code'] ?? 200,
$response['headers'] ?? [],
$response['response'] ?? ''
);
}
}

$mock = new \GuzzleHttp\Handler\MockHandler($responses);
$handler = \GuzzleHttp\HandlerStack::create($mock);
$rcube = rcube::get_instance();

$rcube->config->set('http_client', ['handler' => $handler]);
}

0 comments on commit 2546d2e

Please sign in to comment.