From cf9cd291c4aeea9ac5be1a20461e6950600420db Mon Sep 17 00:00:00 2001 From: Aaron Meese Date: Sat, 8 Jun 2024 01:51:02 +0000 Subject: [PATCH] fix: Undefined array key "result" #518 --- src/Parse/ParseCloud.php | 38 +++++++++++++++++++++-- tests/Parse/ParseCloudTest.php | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/Parse/ParseCloud.php b/src/Parse/ParseCloud.php index 7984fedd..6684f547 100644 --- a/src/Parse/ParseCloud.php +++ b/src/Parse/ParseCloud.php @@ -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. * @@ -28,7 +59,9 @@ 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, @@ -36,7 +69,8 @@ public static function run($name, $data = [], $useMasterKey = false) $useMasterKey ); - return ParseClient::_decode($response['result']); + $returnVal = isset($response['result']) ? $response['result'] : []; + return ParseClient::_decode($returnVal); } /** diff --git a/tests/Parse/ParseCloudTest.php b/tests/Parse/ParseCloudTest.php index 22649c90..4b449b0a 100644 --- a/tests/Parse/ParseCloudTest.php +++ b/tests/Parse/ParseCloudTest.php @@ -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(); } /** @@ -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 */