Skip to content

Commit

Permalink
mocking curl in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusgreen committed May 12, 2024
1 parent 8b74f51 commit b4fed53
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
11 changes: 8 additions & 3 deletions classes/ai/ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

use curl;
use moodle_exception;
require_once($CFG->dirroot.'/admin/tool/aiconnect/tests/test_aiconnect.php');

/**
* Contains most functionality
*
Expand Down Expand Up @@ -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);

Expand All @@ -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'])) {
Expand Down
41 changes: 37 additions & 4 deletions tests/test_aiconnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
}
}


0 comments on commit b4fed53

Please sign in to comment.