Skip to content

Commit

Permalink
fix: Undefined array key "result" parse-community#518
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmeese7 committed Jun 8, 2024
1 parent 353a85b commit cf9cd29
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/Parse/ParseCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,37 @@
*/
class ParseCloud
{
protected static $requestCallable;

/**
* Sets a callable to be used for making requests.
*
* This method allows injection of a mockable callable for testing purposes.
*
* @param callable $callable The callable to use for requests.
*/
public static function setRequestCallable(callable $callable)
{
self::$requestCallable = $callable;
}

/**
* Gets the callable used for making requests.
*
* If no callable has been set, it returns the default callable that calls ParseClient::_request.
*
* @return callable The callable used for requests.
*/
protected static function getRequestCallable()
{
if (!self::$requestCallable) {
self::$requestCallable = function($method, $path, $sessionToken = null, $data = null, $useMasterKey = false, $contentType = 'application/json', $returnHeaders = false) {
return ParseClient::_request($method, $path, $sessionToken, $data, $useMasterKey, $contentType, $returnHeaders);
};
}
return self::$requestCallable;
}

/**
* Makes a call to a Cloud function.
*
Expand All @@ -28,15 +59,18 @@ public static function run($name, $data = [], $useMasterKey = false)
if (ParseUser::getCurrentUser()) {
$sessionToken = ParseUser::getCurrentUser()->getSessionToken();
}
$response = ParseClient::_request(

$response = call_user_func(
self::getRequestCallable(),
'POST',
'functions/'.$name,
$sessionToken,
json_encode(ParseClient::_encode($data, false)),
$useMasterKey
);

return ParseClient::_decode($response['result']);
$returnVal = isset($response['result']) ? $response['result'] : [];
return ParseClient::_decode($returnVal);
}

/**
Expand Down
56 changes: 56 additions & 0 deletions tests/Parse/ParseCloudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public function tearDown() : void
ParseUser::logOut();
$user->destroy(true);
}

// Reset the callable after each test
ParseCloud::setRequestCallable(function($method, $path, $sessionToken = null, $data = null, $useMasterKey = false, $contentType = 'application/json', $returnHeaders = false) {
return \Parse\ParseClient::_request($method, $path, $sessionToken, $data, $useMasterKey, $contentType, $returnHeaders);
});

parent::tearDown();
}

/**
Expand Down Expand Up @@ -72,6 +79,55 @@ public function testFunctionCallException()
]);
}

/**
* @group cloud-code
*/
public function testFunctionCallWithNullParams()
{
$this->expectException(
'Parse\ParseException',
'bad stuff happened'
);
$response = ParseCloud::run('bar', null);
}

/**
* @group cloud-code
*/
public function testFunctionCallWithEmptyParams() {
$this->expectException(
'Parse\ParseException',
'bad stuff happened'
);
$response = ParseCloud::run('bar', []);
}

/**
* @group cloud-code
*/
public function testFunctionCallWithoutResultKey()
{
// Mock the _request method to return a response without the 'result' key
$mockResponse = [];
$mockCallable = $this->getMockBuilder(\stdClass::class)
->addMethods(['__invoke'])
->getMock();
$mockCallable->expects($this->once())
->method('__invoke')
->willReturn($mockResponse);

// Set the mock callable in ParseCloud
ParseCloud::setRequestCallable($mockCallable);

$response = ParseCloud::run('bar', [
'key1' => 'value2',
'key2' => 'value1'
]);

// Since 'result' key is missing, the default value is returned
$this->assertEquals([], $response);
}

/**
* @group cloud-code
*/
Expand Down

0 comments on commit cf9cd29

Please sign in to comment.