diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml new file mode 100644 index 00000000..37151ba5 --- /dev/null +++ b/.github/blunderbuss.yml @@ -0,0 +1,7 @@ +assign_prs: + - josephlewis42 + - KaylaNguyen + +assign_issues: + - josephlewis42 + - KaylaNguyen diff --git a/.github/release-please.yml b/.github/release-please.yml new file mode 100644 index 00000000..34e8f816 --- /dev/null +++ b/.github/release-please.yml @@ -0,0 +1,2 @@ +releaseType: php +handleGHRelease: true \ No newline at end of file diff --git a/.github/renovate.json b/.github/renovate.json new file mode 100644 index 00000000..32ac90d6 --- /dev/null +++ b/.github/renovate.json @@ -0,0 +1,15 @@ +{ + "$schema": "https://docs.renovatebot.com/renovate-schema.json", + "extends": ["group:allNonMajor", "schedule:monthly"], + "packageRules": [ + { + "description": "Create a PR whenever there is a new major version", + "matchUpdateTypes": [ + "major" + ] + } + ], + "ignorePaths": [ + "examples/**" + ] +} diff --git a/.github/workflows/buildpack-integration-test.yml b/.github/workflows/buildpack-integration-test.yml new file mode 100644 index 00000000..f61705fe --- /dev/null +++ b/.github/workflows/buildpack-integration-test.yml @@ -0,0 +1,42 @@ +# Validates Functions Framework with GCF buildpacks. +name: Buildpack Integration Test +on: + push: + branches: + - main + workflow_dispatch: + +# Declare default permissions as read only. +permissions: read-all + +jobs: + php74-buildpack-test: + uses: GoogleCloudPlatform/functions-framework-conformance/.github/workflows/buildpack-integration-test.yml@v1.8.0 + with: + http-builder-source: 'tests/conformance' + http-builder-target: 'declarativeHttpFunc' + cloudevent-builder-source: 'tests/conformance' + cloudevent-builder-target: 'declarativeCloudEventFunc' + prerun: 'tests/conformance/prerun.sh ${{ github.sha }}' + output-file: 'vendor/bin/function_output.json' + builder-runtime: 'php74' + php81-buildpack-test: + uses: GoogleCloudPlatform/functions-framework-conformance/.github/workflows/buildpack-integration-test.yml@v1.8.0 + with: + http-builder-source: 'tests/conformance' + http-builder-target: 'declarativeHttpFunc' + cloudevent-builder-source: 'tests/conformance' + cloudevent-builder-target: 'declarativeCloudEventFunc' + prerun: 'tests/conformance/prerun.sh ${{ github.sha }}' + output-file: 'vendor/bin/function_output.json' + builder-runtime: 'php81' + php82-buildpack-test: + uses: GoogleCloudPlatform/functions-framework-conformance/.github/workflows/buildpack-integration-test.yml@v1.8.0 + with: + http-builder-source: 'tests/conformance' + http-builder-target: 'declarativeHttpFunc' + cloudevent-builder-source: 'tests/conformance' + cloudevent-builder-target: 'declarativeCloudEventFunc' + prerun: 'tests/conformance/prerun.sh ${{ github.sha }}' + output-file: 'vendor/bin/function_output.json' + builder-runtime: 'php82' \ No newline at end of file diff --git a/.github/workflows/conformance.yml b/.github/workflows/conformance.yml index adfcbdd0..dbcbf513 100644 --- a/.github/workflows/conformance.yml +++ b/.github/workflows/conformance.yml @@ -1,42 +1,49 @@ name: PHP Conformance CI on: - pull_request: - branches: - - master push: branches: - - master + - main + pull_request: workflow_dispatch: + +# Declare default permissions as read only. +permissions: read-all + jobs: build: runs-on: ubuntu-latest strategy: matrix: - php-version: [ '7.4','8.0' ] + php-version: [ '7.4','8.0','8.1','8.2' ] name: PHP ${{ matrix.php-version }} Conformance Test steps: + - name: Harden Runner + uses: step-security/harden-runner@03bee3930647ebbf994244c21ddbc0d4933aab4f # v2.3.0 + with: + egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs + - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0 - name: Setup PHP ${{ matrix.php-version }} - uses: shivammathur/setup-php@v2 + uses: shivammathur/setup-php@d30ad8b1843ace22e6698ab99bbafaa747b6bd0d # v2 with: php-version: ${{ matrix.php-version }} - name: Install Dependencies - uses: nick-invision/retry@v1 + uses: nick-invision/retry@39da88d5f7d15a96aed861dbabbe8b7443e3182a # v1.0.4 with: timeout_minutes: 10 max_attempts: 3 command: composer install - name: Setup Go - uses: actions/setup-go@v2 + uses: actions/setup-go@bfdd3570ce990073878bf10f6b2d79082de49492 # v2.2.0 with: go-version: '1.15' - name: Run HTTP conformance tests - uses: GoogleCloudPlatform/functions-framework-conformance/action@v1.2.1 + uses: GoogleCloudPlatform/functions-framework-conformance/action@e42b1533877f1b639f508ec7fbcfb5be31aca663 # v1.2.1 env: FUNCTION_TARGET: 'httpFunc' FUNCTION_SIGNATURE_TYPE: 'http' @@ -48,7 +55,7 @@ jobs: cmd: "'php -S localhost:8080 router.php'" - name: Run Declarative HTTP conformance tests - uses: GoogleCloudPlatform/functions-framework-conformance/action@v1.2.1 + uses: GoogleCloudPlatform/functions-framework-conformance/action@e42b1533877f1b639f508ec7fbcfb5be31aca663 # v1.2.1 env: FUNCTION_TARGET: 'declarativeHttpFunc' FUNCTION_SOURCE: ${{ github.workspace }}/tests/conformance/index.php @@ -59,7 +66,7 @@ jobs: cmd: "'php -S localhost:8080 router.php'" - name: Run CloudEvent conformance tests - uses: GoogleCloudPlatform/functions-framework-conformance/action@v1.2.1 + uses: GoogleCloudPlatform/functions-framework-conformance/action@e42b1533877f1b639f508ec7fbcfb5be31aca663 # v1.2.1 env: FUNCTION_TARGET: 'cloudEventFunc' FUNCTION_SIGNATURE_TYPE: 'cloudevent' @@ -72,7 +79,7 @@ jobs: cmd: "'php -S localhost:8080 router.php'" - name: Run Declarative CloudEvent conformance tests - uses: GoogleCloudPlatform/functions-framework-conformance/action@v1.2.1 + uses: GoogleCloudPlatform/functions-framework-conformance/action@e42b1533877f1b639f508ec7fbcfb5be31aca663 # v1.2.1 env: FUNCTION_TARGET: 'declarativeCloudEventFunc' FUNCTION_SOURCE: ${{ github.workspace }}/tests/conformance/index.php @@ -81,4 +88,4 @@ jobs: functionType: 'cloudevent' useBuildpacks: false validateMapping: true - cmd: "'php -S localhost:8080 router.php'" \ No newline at end of file + cmd: "'php -S localhost:8080 router.php'" diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml new file mode 100644 index 00000000..033fd439 --- /dev/null +++ b/.github/workflows/dependency-review.yml @@ -0,0 +1,27 @@ +# Dependency Review Action +# +# This Action will scan dependency manifest files that change as part of a Pull Request, +# surfacing known-vulnerable versions of the packages declared or updated in the PR. +# Once installed, if the workflow run is marked as required, +# PRs introducing known-vulnerable packages will be blocked from merging. +# +# Source repository: https://github.com/actions/dependency-review-action +name: 'Dependency Review' +on: [pull_request] + +permissions: + contents: read + +jobs: + dependency-review: + runs-on: ubuntu-latest + steps: + - name: Harden Runner + uses: step-security/harden-runner@03bee3930647ebbf994244c21ddbc0d4933aab4f # v2.3.0 + with: + egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs + + - name: 'Checkout Repository' + uses: actions/checkout@8f4b7f84864484a7bf31766abe9204da3cbe65b3 # v3.5.0 + - name: 'Dependency Review' + uses: actions/dependency-review-action@0efb1d1d84fc9633afcdaad14c485cbbc90ef46c # v2.5.1 diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 4973c9aa..55492ac8 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -2,14 +2,22 @@ name: PHP Lint CI on: push: branches: - - master + - main pull_request: +permissions: + contents: read + jobs: php-cs-fixer: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - name: Harden Runner + uses: step-security/harden-runner@03bee3930647ebbf994244c21ddbc0d4933aab4f # v2.3.0 + with: + egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs + + - uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0 - name: PHP-CS-Fixer - uses: docker://oskarstark/php-cs-fixer-ga + uses: docker://oskarstark/php-cs-fixer-ga:latest@sha256:73ae29e0cf222d3e132651af5bfb84ac3f210d58cd98e8f863d0b47f8ee0346f with: args: . --diff --dry-run diff --git a/.github/workflows/scorecard.yml b/.github/workflows/scorecard.yml new file mode 100644 index 00000000..ab8aed99 --- /dev/null +++ b/.github/workflows/scorecard.yml @@ -0,0 +1,52 @@ +name: Scorecard supply-chain security +on: + # For Branch-Protection check. Only the default branch is supported. See + # https://github.com/ossf/scorecard/blob/main/docs/checks.md#branch-protection + branch_protection_rule: + # To guarantee Maintained check is occasionally updated. See + # https://github.com/ossf/scorecard/blob/main/docs/checks.md#maintained + schedule: + - cron: '0 */12 * * *' # runs every 12 hours + push: + branches: [ "main" ] + +# Declare default permissions as read only. +permissions: read-all + +jobs: + analysis: + name: Scorecard analysis + runs-on: ubuntu-latest + permissions: + # Needed to upload the results to code-scanning dashboard. + security-events: write + # Needed to publish results and get a badge (see publish_results below). + id-token: write + + steps: + - name: Harden Runner + uses: step-security/harden-runner@03bee3930647ebbf994244c21ddbc0d4933aab4f # v2.3.0 + with: + egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs + + - name: "Checkout code" + uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 + with: + persist-credentials: false + + - name: "Run analysis" + uses: ossf/scorecard-action@99c53751e09b9529366343771cc321ec74e9bd3d # v2.0.6 + with: + results_file: results.sarif + results_format: sarif + # Public repositories: + # - Publish results to OpenSSF REST API for easy access by consumers + # - Allows the repository to include the Scorecard badge. + # - See https://github.com/ossf/scorecard-action#publishing-results. + publish_results: true + + # Upload the results to GitHub's code scanning dashboard. + - name: "Upload to code-scanning" + uses: github/codeql-action/upload-sarif@807578363a7869ca324a79039e6db9c843e0e100 # v2.1.27 + with: + sarif_file: results.sarif diff --git a/.github/workflows/unit.yaml b/.github/workflows/unit.yaml deleted file mode 100644 index 56ba817f..00000000 --- a/.github/workflows/unit.yaml +++ /dev/null @@ -1,29 +0,0 @@ -name: PHP Unit CI -on: - push: - branches: - - master - pull_request: -jobs: - build: - runs-on: ${{ matrix.operating-system }} - strategy: - matrix: - operating-system: [ubuntu-latest] - php-versions: [ '7.4','8.0' ] - name: PHP ${{ matrix.php-versions }} Unit Test - steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Uses PHP ${{ matrix.php-versions }} - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-versions }} - - name: Install Dependencies - uses: nick-invision/retry@v1 - with: - timeout_minutes: 10 - max_attempts: 3 - command: composer install - - name: Run Script - run: vendor/bin/phpunit \ No newline at end of file diff --git a/.github/workflows/unit.yml b/.github/workflows/unit.yml new file mode 100644 index 00000000..674043ec --- /dev/null +++ b/.github/workflows/unit.yml @@ -0,0 +1,37 @@ +name: PHP Unit CI +on: + push: + branches: + - main + pull_request: +permissions: + contents: read + +jobs: + build: + runs-on: ${{ matrix.operating-system }} + strategy: + matrix: + operating-system: [ubuntu-latest] + php-versions: [ '7.4','8.0','8.1','8.2' ] + name: PHP ${{ matrix.php-versions }} Unit Test + steps: + - name: Harden Runner + uses: step-security/harden-runner@03bee3930647ebbf994244c21ddbc0d4933aab4f # v2.3.0 + with: + egress-policy: audit # TODO: change to 'egress-policy: block' after couple of runs + + - name: Checkout + uses: actions/checkout@ee0669bd1cc54295c223e0bb666b733df41de1c5 # v2.7.0 + - name: Uses PHP ${{ matrix.php-versions }} + uses: shivammathur/setup-php@d30ad8b1843ace22e6698ab99bbafaa747b6bd0d # v2 + with: + php-version: ${{ matrix.php-versions }} + - name: Install Dependencies + uses: nick-invision/retry@39da88d5f7d15a96aed861dbabbe8b7443e3182a # v1.0.4 + with: + timeout_minutes: 10 + max_attempts: 3 + command: composer install + - name: Run Script + run: vendor/bin/phpunit diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..38372e86 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,25 @@ +# Changelog + +## [1.2.0](https://github.com/GoogleCloudPlatform/functions-framework-php/compare/v1.1.0...v1.2.0) (2023-05-25) + + +### Features + +* add class for testing ([#119](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/119)) ([41ab525](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/41ab52514051df9976285fa788a7e7239dec7715)) + + +### Miscellaneous Chores + +* [StepSecurity] Apply security best practices ([#137](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/137)) ([ed6afaa](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/ed6afaad643862d9a0912ca2539fa4f1de0c4ecc)) +* [StepSecurity] Apply security best practices ([#142](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/142)) ([e55fef1](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/e55fef1caf138837a2ca004bdd7498d92d311701)) +* add GCF buildpack integration test Workflow ([2da1fa3](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/2da1fa3ef34f632aa4179adef830972a5223b572)) +* add GCF buildpack integration test Workflow ([#129](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/129)) ([8db2c42](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/8db2c426cd0c0e4ae32abfe62b2dbf9f73b0b013)) +* configure security scorecard action ([#134](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/134)) ([a482dda](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/a482dda7017043ca41eefa6d4966fff8e8b08cff)) +* Create blunderbuss.yml according to preference ([#140](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/140)) ([395181d](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/395181ded7bbd17c59112c52308848901a82f80f)) +* Create renovate config ([#138](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/138)) ([eba4ec0](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/eba4ec09578131bfad90318b23cad364d21ab931)) +* fix invoker docblock ([#117](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/117)) ([6d4c545](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/6d4c545183d4c2eb9d89d981904b488c377acd4a)) +* make workflows' default permission read only ([#139](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/139)) ([d54e85f](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/d54e85f0c211c77c6de6c13c5f5dc921578b7d65)) +* Rename unit.yaml to unit.yml ([#136](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/136)) ([360b7d6](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/360b7d6605077f2977539e9d8b3455df44847ad3)) +* switch master to main ([#121](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/121)) ([c693c8c](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/c693c8c77166fab1fa88a8d47d3fb60768dc3c11)) +* update buildpack integration test ([#132](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/132)) ([e66d6f2](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/e66d6f20b166f5ae9d216051aacdc7f39815568a)) +* use Release Please for releases ([#126](https://github.com/GoogleCloudPlatform/functions-framework-php/issues/126)) ([d5c3589](https://github.com/GoogleCloudPlatform/functions-framework-php/commit/d5c3589012e2daeba963576eaf3748a7b4622b0f)) diff --git a/README.md b/README.md index 6f097b4b..ce5b4a87 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# Functions Framework for PHP [![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2FGoogleCloudPlatform%2Ffunctions-framework-php%2Fbadge&style=flat)](https://actions-badge.atrox.dev/GoogleCloudPlatform/functions-framework-php/goto) [![Packagist](https://poser.pugx.org/google/cloud-functions-framework/v/stable)](https://packagist.org/packages/google/cloud-functions-framework) +# Functions Framework for PHP [![Packagist](https://poser.pugx.org/google/cloud-functions-framework/v/stable)](https://packagist.org/packages/google/cloud-functions-framework) + +[![PHP unit CI][ff_php_unit_img]][ff_php_unit_link] [![PHP lint CI][ff_php_lint_img]][ff_php_lint_link] [![PHP conformace CI][ff_php_conformance_img]][ff_php_conformance_link] An open source FaaS (Function as a service) framework for writing portable PHP functions. @@ -196,7 +198,7 @@ gcloud run deploy my-cloud-function \ After your instance deploys, you can access it at the URL provided, or view it in the [Cloud Console][cloud-run-console]. -[functions-samples]: https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/functions +[functions-samples]: https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/functions [functions-how-to]: https://cloud.google.com/functions/docs/how-to [gcloud]: https://cloud.google.com/sdk/gcloud/ [gcloud-auth]: https://cloud.google.com/sdk/docs/authorizing @@ -215,7 +217,7 @@ cloudevent function signature: ```php use Google\CloudFunctions\CloudEvent; -function helloCloudEvents(CloudEvent $cloudevent) +function helloCloudEvent(CloudEvent $cloudevent) { // Print the whole CloudEvent $stdout = fopen('php://stdout', 'wb'); @@ -368,3 +370,10 @@ You can configure the Functions Framework using the environment variables shown Contributions to this library are welcome and encouraged. See [CONTRIBUTING](CONTRIBUTING.md) for more information on how to get started. + +[ff_php_unit_img]: https://github.com/GoogleCloudPlatform/functions-framework-php/workflows/PHP%20Unit%20CI/badge.svg +[ff_php_unit_link]: https://github.com/GoogleCloudPlatform/functions-framework-php/actions?query=workflow%3A"PHP+Unit+CI" +[ff_php_lint_img]: https://github.com/GoogleCloudPlatform/functions-framework-php/workflows/PHP%20Lint%20CI/badge.svg +[ff_php_lint_link]: https://github.com/GoogleCloudPlatform/functions-framework-php/actions?query=workflow%3A"PHP+Lint+CI" +[ff_php_conformance_img]: https://github.com/GoogleCloudPlatform/functions-framework-php/workflows/PHP%20Conformance%20CI/badge.svg +[ff_php_conformance_link]: https://github.com/GoogleCloudPlatform/functions-framework-php/actions?query=workflow%3A"PHP+Conformance+CI" diff --git a/examples/hello/Dockerfile b/examples/hello/Dockerfile index 20b178ca..15671b98 100644 --- a/examples/hello/Dockerfile +++ b/examples/hello/Dockerfile @@ -1,4 +1,4 @@ -FROM gcr.io/gae-runtimes/php74:php74_20210216_7_4_15_RC00 +FROM gcr.io/gae-runtimes/php74:php74_20210216_7_4_15_RC00@sha256:b7d4aef9e57c17152c2ea9a3fbd04c23687fc93c7254608bdf6e19f14a7c10f3 WORKDIR /srv/ 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/src/Invoker.php b/src/Invoker.php index d434a86b..a986d37b 100644 --- a/src/Invoker.php +++ b/src/Invoker.php @@ -29,18 +29,19 @@ class Invoker { private $function; private $errorLogFunc; - private static $registeredFunction = []; + private static $registeredFunctions = []; /** * @param array|string $target The callable to be invoked. * @param string|null $signatureType The signature type of the target callable, - * either "cloudevent" or "http". If null, - * env var `FUNCTION_SIGNATURE_TYPE` is used. + * either "cloudevent" or "http". This can be + * null if $target is registered using + * Invoker::registerFunction. */ public function __construct($target, ?string $signatureType = null) { - if (is_string($target) && isset(self::$registeredFunction[$target])) { - $this->function = self::$registeredFunction[$target]; + if (is_string($target) && isset(self::$registeredFunctions[$target])) { + $this->function = self::$registeredFunctions[$target]; } else { if (!is_callable($target)) { throw new InvalidArgumentException(sprintf( @@ -104,6 +105,6 @@ public function handle( */ public static function registerFunction(string $name, FunctionWrapper $function) { - self::$registeredFunction[$name] = $function; + self::$registeredFunctions[$name] = $function; } } diff --git a/src/LegacyEventMapper.php b/src/LegacyEventMapper.php index 3dc225d9..77f57a2a 100644 --- a/src/LegacyEventMapper.php +++ b/src/LegacyEventMapper.php @@ -23,7 +23,7 @@ class LegacyEventMapper { // Maps background/legacy event types to their equivalent CloudEvent types. // For more info on event mappings see - // https://github.com/GoogleCloudPlatform/functions-framework-conformance/blob/master/docs/mapping.md + // https://github.com/GoogleCloudPlatform/functions-framework-conformance/blob/main/docs/mapping.md private static $ceTypeMap = [ 'google.pubsub.topic.publish' => 'google.cloud.pubsub.topic.v1.messagePublished', 'providers/cloud.pubsub/eventTypes/topic.publish' => 'google.cloud.pubsub.topic.v1.messagePublished', 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') + ); + } +} diff --git a/tests/conformance/prerun.sh b/tests/conformance/prerun.sh new file mode 100755 index 00000000..69186fc2 --- /dev/null +++ b/tests/conformance/prerun.sh @@ -0,0 +1,27 @@ +# prerun.sh sets up the test function to use the functions framework commit +# specified by generating a `composer.json`. This makes the function `pack` buildable +# with GCF buildpacks. +# +# `pack` build example command: +# pack build myfn --builder us.gcr.io/fn-img/buildpacks/php74/builder:php74_20220620_7_4_29_RC00 --env GOOGLE_RUNTIME=php74 --env GOOGLE_FUNCTION_TARGET=declarativeHttpFunc --env X_GOOGLE_TARGET_PLATFORM=gcf +FRAMEWORK_VERSION=$1 + +# exit when any command fails +set -e + +cd $(dirname $0) + +if [ -z "${FRAMEWORK_VERSION}" ] + then + echo "Functions Framework version required as first parameter" + exit 1 +fi + +echo '{ + "require": { + "google/cloud-functions-framework": "dev-main#'${FRAMEWORK_VERSION}'", + "cloudevents/sdk-php": "^1.0" + } +}' > composer.json + +cat composer.json \ No newline at end of file diff --git a/run_conformance_tests.sh b/tests/conformance/run_conformance_tests.sh similarity index 96% rename from run_conformance_tests.sh rename to tests/conformance/run_conformance_tests.sh index bb284371..9fd74f62 100755 --- a/run_conformance_tests.sh +++ b/tests/conformance/run_conformance_tests.sh @@ -14,6 +14,12 @@ # Defaults to the latest version of the repo, which may be ahead of the # latest release. +# exit when any command fails +set -e + +# Change into the repo root +cd $(dirname $0)/../.. + CLIENT_VERSION=$1 if [ $CLIENT_VERSION ]; then CLIENT_VERSION="@$CLIENT_VERSION"