diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 44a16f9..c67887a 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -3,8 +3,14 @@ name: PHP Composer on: push: branches: [ master ] + paths-ignore: + - '**.md' + - 'doc/**' pull_request: branches: [ master ] + paths-ignore: + - '**.md' + - 'doc/**' jobs: build: @@ -50,6 +56,7 @@ jobs: LEANCLOUD_APP_HOST: 127.0.0.1 LEANCLOUD_APP_PORT: 8081 LEANCLOUD_WILDCARD_DOMAIN: lncldglobal.com + LEANCLOUD_APP_ENV: production run: | make test_engine & php -r 'exit(PHP_VERSION_ID >= 70200 ? 0 : 1);' || vendor/bin/phpunit test/Php72ObjectDeprecated.php diff --git a/src/LeanCloud/Engine/Cloud.php b/src/LeanCloud/Engine/Cloud.php index 5205dda..1210866 100644 --- a/src/LeanCloud/Engine/Cloud.php +++ b/src/LeanCloud/Engine/Cloud.php @@ -242,8 +242,8 @@ public static function onInsight($func) { * ``` * * @param string $funcName Name of defined function - * @param array $data Array of parameters passed to function - * @param User $user Request user + * @param array $params Array of parameters passed to function + * @param \LeanCloud\User $user Request user * @param array $meta Optional parameters that will be passed to * user function * @return mixed @@ -258,6 +258,26 @@ public static function run($funcName, $params, $user=null, $meta=array()) { return call_user_func($func, $params, $user, $meta); } + /** + * Invokes a remote cloud function + * + * Example: + * + * ```php + * LeanEngine::runRemote("sayHello", array("name" => "alice")); + * ``` + * + * @param string $funcName Name of defined function + * @param array $params Array of parameters passed to function + * @param string $sessionToken run this function as the user corresponding to this session token + * + * @return array JSON decoded associated array + * @see self::run + */ + public static function runRemote($funcName, $params, $sessionToken=null) { + return Client::post("/functions/{$funcName}", $params, $sessionToken); + } + /** * Start cloud function Stand-alone mode, start to process request. */ @@ -288,8 +308,8 @@ public static function stop() { * * @param string $className Classname * @param string $hookName Hook name, e.g. beforeUpdate - * @param LeanObject $object The object of attached hook - * @param User $user Request user + * @param \LeanCloud\LeanObject $object The object of attached hook + * @param \LeanCloud\User $user Request user * @param array $meta Optional parameters that will be passed to * user function * @return mixed @@ -310,9 +330,10 @@ public static function runHook($className, $hookName, $object, /** * Run hook when a user logs in * - * @param User $user The user object that tries to login + * @param \LeanCloud\User $user The user object that tries to login * @param array $meta Optional parameters that will be passed to * user function + * @return mixed * @throws FunctionError * @see self::onLogin */ @@ -324,9 +345,10 @@ public static function runOnLogin($user, $meta=array()) { * Run hook when user verified by Email or SMS * * @param string $type Either "sms" or "email", case-sensitive - * @param User $user The verifying user + * @param \LeanCloud\User $user The verifying user * @param array $meta Optional parameters that will be passed to * user function + * @return mixed * @throws FunctionError * @see self::onVerified */ diff --git a/test/CloudTest.php b/test/CloudTest.php index 95b8614..48dfd8c 100644 --- a/test/CloudTest.php +++ b/test/CloudTest.php @@ -1,9 +1,40 @@ setUsername("alice"); + $user->setPassword("blabla"); + $user->setEmail("alice@example.com"); + try { + $user->signUp(); + } catch (CloudException $ex) { + // skip + } + } + + public static function tearDownAfterClass() { + // destroy default user if present + try { + $user = User::logIn("alice", "blabla"); + $user->destroy(); + } catch (CloudException $ex) { + // skip + } + } + + + public function testGetKeys() { $name = uniqid(); Cloud::define($name, function($params, $user) { @@ -53,6 +84,27 @@ public function testFunctionAcceptMeta() { $this->assertEquals("10.0.0.1", $result); } + public function testRemoteFunction() { + // Assumes [LeanFunction] is deployed at this application's LeanEngine. + // [LeanFunction]: https://github.com/leancloud/LeanFunction + $response = Cloud::runRemote("hello", []); + $result = $response["result"]; + $this->assertEquals("Hello world!", $result); + } + + public function testRemoteFunctionWithSession() { + // See testRemoteFunction for dependencies. + try { + User::logIn("alice", "blabla"); + } catch (\LeanCloud\CloudException $e) { + // skip + } + $token = User::getCurrentSessionToken(); + $response = Cloud::runRemote("echo-session-token", [], $token); + $result = $response["result"]; + $this->assertEquals($token, $result); + } + public function testClassHook() { forEach(array("beforeSave", "afterSave", "beforeUpdate", "afterUpdate", diff --git a/test/FileTest.php b/test/FileTest.php index ba12cbf..5206298 100644 --- a/test/FileTest.php +++ b/test/FileTest.php @@ -11,7 +11,6 @@ public static function setUpBeforeClass() { getenv("LEANCLOUD_APP_ID"), getenv("LEANCLOUD_APP_KEY"), getenv("LEANCLOUD_APP_MASTER_KEY")); - } public function testInitializeEmptyFileName() {