Skip to content

Commit 41ab525

Browse files
authored
feat: add class for testing (GoogleCloudPlatform#119)
1 parent 6d4c545 commit 41ab525

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/FunctionsFrameworkTesting.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2021 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace Google\CloudFunctions;
20+
21+
use ReflectionClass;
22+
23+
class FunctionsFrameworkTesting
24+
{
25+
private function __construct()
26+
{
27+
// Constructor disabled because this class should only be used statically.
28+
}
29+
30+
public static function getRegisteredFunction(string $functionName)
31+
{
32+
$registeredFunctionsReflection = (new ReflectionClass(Invoker::class))
33+
->getProperty('registeredFunctions');
34+
$registeredFunctionsReflection->setAccessible(true);
35+
36+
$fn = $registeredFunctionsReflection->getValue()[$functionName] ?? null;
37+
38+
if ($fn) {
39+
$functionReflection = (new ReflectionClass($fn))
40+
->getProperty('function');
41+
$functionReflection->setAccessible(true);
42+
43+
return $functionReflection->getValue($fn);
44+
}
45+
}
46+
}

tests/FunctionsFrameworkTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Copyright 2021 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\CloudFunctions\Tests;
19+
20+
use Google\CloudFunctions\FunctionsFramework;
21+
use Google\CloudFunctions\FunctionsFrameworkTesting;
22+
use PHPUnit\Framework\TestCase;
23+
use Psr\Http\Message\ServerRequestInterface;
24+
use CloudEvents\V1\CloudEventInterface;
25+
26+
/**
27+
* @group gcf-framework
28+
*/
29+
class FunctionsFrameworkTest extends TestCase
30+
{
31+
public function testRegisterAndRetrieveHttpFunction(): void
32+
{
33+
$fn = function (ServerRequestInterface $request) {
34+
return "this is a test function";
35+
};
36+
37+
FunctionsFramework::http('testFn', $fn);
38+
39+
$this->assertEquals(
40+
$fn,
41+
FunctionsFrameworkTesting::getRegisteredFunction('testFn')
42+
);
43+
}
44+
45+
public function testRegisterAndRetrieveCloudEventFunction(): void
46+
{
47+
$fn = function (CloudEventInterface $event) {
48+
return "this is a test function";
49+
};
50+
51+
FunctionsFramework::cloudEvent('testFn', $fn);
52+
53+
$this->assertEquals(
54+
$fn,
55+
FunctionsFrameworkTesting::getRegisteredFunction('testFn')
56+
);
57+
}
58+
59+
public function testRetrieveNonexistantFunction(): void
60+
{
61+
$this->assertNull(
62+
FunctionsFrameworkTesting::getRegisteredFunction('thisDoesntExist')
63+
);
64+
}
65+
}

0 commit comments

Comments
 (0)