1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
From: Aleksander Machniak <alec@alec.pl>
Date: Sun, 17 Dec 2023 14:02:51 +0100
Subject: Tests: Use mocked Guzzle client in Modcss action test
Origin: https://github.com/roundcube/roundcubemail/commit/2546d2e2e728eb187167a356243138da7a39d796
Forwarded: not-needed
---
tests/Actions/Utils/Modcss.php | 5 +++++
tests/bootstrap.php | 22 ++++++++++++++++++++++
2 files changed, 27 insertions(+)
diff --git a/tests/Actions/Utils/Modcss.php b/tests/Actions/Utils/Modcss.php
index 232e912..1081886 100644
--- a/tests/Actions/Utils/Modcss.php
+++ b/tests/Actions/Utils/Modcss.php
@@ -47,6 +47,11 @@ class Actions_Utils_Modcss extends ActionTestCase
// 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'));
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
index 6cfd610..e49cda4 100644
--- a/tests/bootstrap.php
+++ b/tests/bootstrap.php
@@ -128,3 +128,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]);
+}
|