Skip to content

Commit f3667a5

Browse files
authored
Merge pull request #222 from leancloud/cloud-run-remote
feat: runRemote for invokning cloud function remotely
2 parents 24d926b + fd7e501 commit f3667a5

File tree

4 files changed

+87
-7
lines changed

4 files changed

+87
-7
lines changed

.github/workflows/php.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ name: PHP Composer
33
on:
44
push:
55
branches: [ master ]
6+
paths-ignore:
7+
- '**.md'
8+
- 'doc/**'
69
pull_request:
710
branches: [ master ]
11+
paths-ignore:
12+
- '**.md'
13+
- 'doc/**'
814

915
jobs:
1016
build:
@@ -50,6 +56,7 @@ jobs:
5056
LEANCLOUD_APP_HOST: 127.0.0.1
5157
LEANCLOUD_APP_PORT: 8081
5258
LEANCLOUD_WILDCARD_DOMAIN: lncldglobal.com
59+
LEANCLOUD_APP_ENV: production
5360
run: |
5461
make test_engine &
5562
php -r 'exit(PHP_VERSION_ID >= 70200 ? 0 : 1);' || vendor/bin/phpunit test/Php72ObjectDeprecated.php

src/LeanCloud/Engine/Cloud.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,8 @@ public static function onInsight($func) {
242242
* ```
243243
*
244244
* @param string $funcName Name of defined function
245-
* @param array $data Array of parameters passed to function
246-
* @param User $user Request user
245+
* @param array $params Array of parameters passed to function
246+
* @param \LeanCloud\User $user Request user
247247
* @param array $meta Optional parameters that will be passed to
248248
* user function
249249
* @return mixed
@@ -258,6 +258,26 @@ public static function run($funcName, $params, $user=null, $meta=array()) {
258258
return call_user_func($func, $params, $user, $meta);
259259
}
260260

261+
/**
262+
* Invokes a remote cloud function
263+
*
264+
* Example:
265+
*
266+
* ```php
267+
* LeanEngine::runRemote("sayHello", array("name" => "alice"));
268+
* ```
269+
*
270+
* @param string $funcName Name of defined function
271+
* @param array $params Array of parameters passed to function
272+
* @param string $sessionToken run this function as the user corresponding to this session token
273+
*
274+
* @return array JSON decoded associated array
275+
* @see self::run
276+
*/
277+
public static function runRemote($funcName, $params, $sessionToken=null) {
278+
return Client::post("/functions/{$funcName}", $params, $sessionToken);
279+
}
280+
261281
/**
262282
* Start cloud function Stand-alone mode, start to process request.
263283
*/
@@ -288,8 +308,8 @@ public static function stop() {
288308
*
289309
* @param string $className Classname
290310
* @param string $hookName Hook name, e.g. beforeUpdate
291-
* @param LeanObject $object The object of attached hook
292-
* @param User $user Request user
311+
* @param \LeanCloud\LeanObject $object The object of attached hook
312+
* @param \LeanCloud\User $user Request user
293313
* @param array $meta Optional parameters that will be passed to
294314
* user function
295315
* @return mixed
@@ -310,9 +330,10 @@ public static function runHook($className, $hookName, $object,
310330
/**
311331
* Run hook when a user logs in
312332
*
313-
* @param User $user The user object that tries to login
333+
* @param \LeanCloud\User $user The user object that tries to login
314334
* @param array $meta Optional parameters that will be passed to
315335
* user function
336+
* @return mixed
316337
* @throws FunctionError
317338
* @see self::onLogin
318339
*/
@@ -324,9 +345,10 @@ public static function runOnLogin($user, $meta=array()) {
324345
* Run hook when user verified by Email or SMS
325346
*
326347
* @param string $type Either "sms" or "email", case-sensitive
327-
* @param User $user The verifying user
348+
* @param \LeanCloud\User $user The verifying user
328349
* @param array $meta Optional parameters that will be passed to
329350
* user function
351+
* @return mixed
330352
* @throws FunctionError
331353
* @see self::onVerified
332354
*/

test/CloudTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
<?php
22

3+
use LeanCloud\Client;
34
use LeanCloud\Engine\Cloud;
5+
use LeanCloud\User;
46
use PHPUnit\Framework\TestCase;
57

68
class CloudTest extends TestCase {
9+
public static function setUpBeforeClass() {
10+
Client::initialize(
11+
getenv("LEANCLOUD_APP_ID"),
12+
getenv("LEANCLOUD_APP_KEY"),
13+
getenv("LEANCLOUD_APP_MASTER_KEY"));
14+
15+
$user = new User();
16+
$user->setUsername("alice");
17+
$user->setPassword("blabla");
18+
$user->setEmail("[email protected]");
19+
try {
20+
$user->signUp();
21+
} catch (CloudException $ex) {
22+
// skip
23+
}
24+
}
25+
26+
public static function tearDownAfterClass() {
27+
// destroy default user if present
28+
try {
29+
$user = User::logIn("alice", "blabla");
30+
$user->destroy();
31+
} catch (CloudException $ex) {
32+
// skip
33+
}
34+
}
35+
36+
37+
738
public function testGetKeys() {
839
$name = uniqid();
940
Cloud::define($name, function($params, $user) {
@@ -53,6 +84,27 @@ public function testFunctionAcceptMeta() {
5384
$this->assertEquals("10.0.0.1", $result);
5485
}
5586

87+
public function testRemoteFunction() {
88+
// Assumes [LeanFunction] is deployed at this application's LeanEngine.
89+
// [LeanFunction]: https://github.com/leancloud/LeanFunction
90+
$response = Cloud::runRemote("hello", []);
91+
$result = $response["result"];
92+
$this->assertEquals("Hello world!", $result);
93+
}
94+
95+
public function testRemoteFunctionWithSession() {
96+
// See testRemoteFunction for dependencies.
97+
try {
98+
User::logIn("alice", "blabla");
99+
} catch (\LeanCloud\CloudException $e) {
100+
// skip
101+
}
102+
$token = User::getCurrentSessionToken();
103+
$response = Cloud::runRemote("echo-session-token", [], $token);
104+
$result = $response["result"];
105+
$this->assertEquals($token, $result);
106+
}
107+
56108
public function testClassHook() {
57109
forEach(array("beforeSave", "afterSave",
58110
"beforeUpdate", "afterUpdate",

test/FileTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public static function setUpBeforeClass() {
1111
getenv("LEANCLOUD_APP_ID"),
1212
getenv("LEANCLOUD_APP_KEY"),
1313
getenv("LEANCLOUD_APP_MASTER_KEY"));
14-
1514
}
1615

1716
public function testInitializeEmptyFileName() {

0 commit comments

Comments
 (0)