Skip to content

Commit

Permalink
Merge pull request #222 from leancloud/cloud-run-remote
Browse files Browse the repository at this point in the history
feat: runRemote for invokning cloud function remotely
  • Loading branch information
weakish committed Dec 15, 2020
2 parents 24d926b + fd7e501 commit f3667a5
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
34 changes: 28 additions & 6 deletions src/LeanCloud/Engine/Cloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down
52 changes: 52 additions & 0 deletions test/CloudTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
<?php

use LeanCloud\Client;
use LeanCloud\Engine\Cloud;
use LeanCloud\User;
use PHPUnit\Framework\TestCase;

class CloudTest extends TestCase {
public static function setUpBeforeClass() {
Client::initialize(
getenv("LEANCLOUD_APP_ID"),
getenv("LEANCLOUD_APP_KEY"),
getenv("LEANCLOUD_APP_MASTER_KEY"));

$user = new User();
$user->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) {
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion test/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public static function setUpBeforeClass() {
getenv("LEANCLOUD_APP_ID"),
getenv("LEANCLOUD_APP_KEY"),
getenv("LEANCLOUD_APP_MASTER_KEY"));

}

public function testInitializeEmptyFileName() {
Expand Down

0 comments on commit f3667a5

Please sign in to comment.