Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Undefined array key result #519

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading