From 069f2ed2f732bb4848be7a212cd26ad6253a86c2 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 24 Nov 2021 15:18:57 -0800 Subject: [PATCH 1/3] feat: add class for testing --- src/FunctionsFrameworkTesting.php | 46 ++++++++++++++++++++++ tests/FunctionsFrameworkTest.php | 65 +++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/FunctionsFrameworkTesting.php create mode 100644 tests/FunctionsFrameworkTest.php diff --git a/src/FunctionsFrameworkTesting.php b/src/FunctionsFrameworkTesting.php new file mode 100644 index 00000000..08ee7fae --- /dev/null +++ b/src/FunctionsFrameworkTesting.php @@ -0,0 +1,46 @@ +getProperty('registeredFunctions'); + $registeredFunctionsReflection->setAccessible(true); + + $fn = $registeredFunctionsReflection->getValue()[$functionName] ?? null; + + if ($fn) { + $functionReflection = (new ReflectionClass($fn)) + ->getProperty('function'); + $functionReflection->setAccessible(true); + + return $functionReflection->getValue($fn); + } + } +} diff --git a/tests/FunctionsFrameworkTest.php b/tests/FunctionsFrameworkTest.php new file mode 100644 index 00000000..940c1b24 --- /dev/null +++ b/tests/FunctionsFrameworkTest.php @@ -0,0 +1,65 @@ +assertEquals( + $fn, + FunctionsFrameworkTesting::getRegisteredFunction('testFn') + ); + } + + public function testRegisterAndRetrieveCloudEventFunction(): void + { + $fn = function(CloudEventInterface $event) { + return "this is a test function"; + }; + + FunctionsFramework::cloudEvent('testFn', $fn); + + $this->assertEquals( + $fn, + FunctionsFrameworkTesting::getRegisteredFunction('testFn') + ); + } + + public function testRetrieveNonexistantFunction(): void + { + $this->assertNull( + FunctionsFrameworkTesting::getRegisteredFunction('thisDoesntExist') + ); + } +} From bac97a7cfdea19c3080c82787c131787155935a2 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 24 Nov 2021 15:19:50 -0800 Subject: [PATCH 2/3] fix copyright --- tests/FunctionsFrameworkTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/FunctionsFrameworkTest.php b/tests/FunctionsFrameworkTest.php index 940c1b24..0c3fade5 100644 --- a/tests/FunctionsFrameworkTest.php +++ b/tests/FunctionsFrameworkTest.php @@ -1,6 +1,6 @@ Date: Mon, 29 Nov 2021 13:44:50 -0800 Subject: [PATCH 3/3] lint happy --- tests/FunctionsFrameworkTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/FunctionsFrameworkTest.php b/tests/FunctionsFrameworkTest.php index 0c3fade5..63907543 100644 --- a/tests/FunctionsFrameworkTest.php +++ b/tests/FunctionsFrameworkTest.php @@ -30,7 +30,7 @@ class FunctionsFrameworkTest extends TestCase { public function testRegisterAndRetrieveHttpFunction(): void { - $fn = function(ServerRequestInterface $request) { + $fn = function (ServerRequestInterface $request) { return "this is a test function"; }; @@ -44,7 +44,7 @@ public function testRegisterAndRetrieveHttpFunction(): void public function testRegisterAndRetrieveCloudEventFunction(): void { - $fn = function(CloudEventInterface $event) { + $fn = function (CloudEventInterface $event) { return "this is a test function"; };