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..63907543 --- /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') + ); + } +}