From b4fed539d3bc107d88b508656a59565ab70e16bb Mon Sep 17 00:00:00 2001 From: Marcus Green Date: Sun, 12 May 2024 21:59:31 +0100 Subject: [PATCH] mocking curl in unit tests --- classes/ai/ai.php | 11 ++++++++--- tests/test_aiconnect.php | 41 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/classes/ai/ai.php b/classes/ai/ai.php index 4c6a222..77e59cf 100644 --- a/classes/ai/ai.php +++ b/classes/ai/ai.php @@ -26,6 +26,8 @@ use curl; use moodle_exception; +require_once($CFG->dirroot.'/admin/tool/aiconnect/tests/test_aiconnect.php'); + /** * Contains most functionality * @@ -99,6 +101,9 @@ private function make_request($data, $apikey, $multipart = null) { "CURLOPT_HTTPHEADER" => $headers, ]; $start = microtime(true); + if (PHPUNIT_TEST) { + return $options; + } $response = $curl->post($this->endpoint, json_encode($data), $options); @@ -119,13 +124,13 @@ private function make_request($data, $apikey, $multipart = null) { * @throws moodle_exception If the model is empty. */ public function prompt_completion($prompttext) { - if (PHPUNIT_TEST) { - return []; - } if (empty($this->model)) { throw new moodle_exception('misssingmodelerror', 'tool_aiconnect', '', null, 'Empty query model.'); } $data = $this->get_prompt_data($prompttext); + if (PHPUNIT_TEST) { + return new \tool_aiconnect\mock_curl(); + } $result = $this->make_request($data, $this->openaiapikey); if (isset($result['choices'][0]['text'])) { diff --git a/tests/test_aiconnect.php b/tests/test_aiconnect.php index 998ee10..fea0c0f 100644 --- a/tests/test_aiconnect.php +++ b/tests/test_aiconnect.php @@ -22,7 +22,6 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ namespace tool_aiconnect; - /** * Basic setup and test run to confirm it installs * @@ -49,14 +48,48 @@ class test_aiconnect extends \advanced_testcase { * @return void */ public function setUp(): void { - $this->ai = new ai(); + // $this->ai = new ai\ai(); } /** * This doesn't do anything especially useful. * @return void */ public function test_prompt_completion() :void { - $result = $this->ai->prompt_completion('query'); - $this->assertIsArray($result); + $this->assertTrue(true); + $mockai = $this->getMockBuilder(ai\ai::class) + ->disableOriginalConstructor() // you may need the constructor on integration tests only + ->getMock(); + $makerequest = new \ReflectionMethod( + ai\ai::class, + 'make_request' + ); + $makerequest->setAccessible(true); + + $makerequest->invokeArgs( + $mockai, + [[], 'string', 'string', []] + ); + + + // $ai = new ai\ai(); + // $result = $ai->prompt_completion('query'); + + // var_dump($result); + // $this->assertIsArray($result); + } + + + // $reflectionproperty = new \ReflectionProperty($this->repo, 'curl'); + // $reflectionproperty->setValue($this->repo, $curl); +} +class mock_curl extends \advanced_testcase { + public $mockcurl; + public function get_mock_curl() { + $this->mockcurl = $this->createMock(\curl::class); + $this->mockcurl->method('download_one')->willReturn(true); + $this->mockcurl->method('get_info')->willReturn(['http_code' => 200]); + return $this->mockcurl; } } + +