Skip to content

Commit 1779e83

Browse files
committed
initial commit.
1 parent 75d89e1 commit 1779e83

File tree

8 files changed

+259
-0
lines changed

8 files changed

+259
-0
lines changed

.github/FUNDING.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github: ["syslogic"]
2+
custom: ["https://www.paypal.me/syslogic"]

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5+
6+
version: 2
7+
updates:
8+
- package-ecosystem: "composer"
9+
directory: "/"
10+
schedule:
11+
interval: "weekly"

.github/workflows/ci-php.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: PHP Composer
2+
3+
on:
4+
push:
5+
branches: [ master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
environment:
13+
name: CI
14+
steps:
15+
- uses: actions/checkout@v2
16+
17+
- name: 🔢 Validate composer.json and composer.lock
18+
id: composer-validation
19+
run: composer validate --strict
20+
21+
- name: 🔢 Cache Composer packages
22+
id: composer-cache
23+
uses: actions/cache@v2
24+
with:
25+
path: vendor
26+
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
27+
restore-keys: |
28+
${{ runner.os }}-php-
29+
30+
- name: 🔢 Install dependencies
31+
id: composer-dependencies
32+
run: composer install --prefer-dist --no-progress
33+
34+
- name: 🗄️ Run test suite
35+
id: phpunit-testsuite
36+
run: |
37+
echo "GitHub Action Run # $GITHUB_RUN_NUMBER"
38+
composer run-script test
39+
env:
40+
TEST_NAME: main
41+
XDEBUG_MODE: debug
42+

composer.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"name": "syslogic/php-cloudfunctions-quickstart",
3+
"description": "PHP Cloud Functions Quickstart",
4+
"minimum-stability": "stable",
5+
"type": "project",
6+
"license": "MIT",
7+
"version": "1.0.0",
8+
"authors": [{
9+
"name": "Martin Zeitler",
10+
"homepage": "https://github.com/syslogic"
11+
}],
12+
"support": {
13+
"docs": "https://github.com/syslogic/php-cloudfunctions-quickstart/README.md",
14+
"issues": "https://github.com/syslogic/php-cloudfunctions-quickstart/issues",
15+
"source": "https://github.com/syslogic/php-cloudfunctions-quickstart"
16+
},
17+
"funding": [{
18+
"type": "github",
19+
"url": "https://github.com/sponsors/syslogic"
20+
}, {
21+
"type": "paypal",
22+
"url": "https://www.paypal.me/syslogic"
23+
}],
24+
"scripts": {
25+
"test": "vendor/bin/phpunit --bootstrap vendor/autoload.php --configuration phpunit.xml --testsuite main",
26+
"start": [ "Composer\\Config::disableProcessTimeout", "FUNCTION_TARGET=on_https php -S localhost:${PORT:-8081} vendor/bin/router.php"
27+
]
28+
},
29+
"scripts-descriptions": {
30+
"test": "Run PHPunit tests",
31+
"start": "Call function on_https"
32+
},
33+
"repositories": [],
34+
"require": {
35+
"php": ">= 7.4",
36+
"ext-json": "*",
37+
"google/cloud-logging": "^1.23.0",
38+
"google/cloud-functions-framework": "^v1.1.0",
39+
"google/cloud-storage": "^v1.26.1",
40+
"guzzlehttp/psr7": "^2.1.0"
41+
42+
},
43+
"require-dev": {
44+
"phpunit/phpunit": "^9.5.13",
45+
"google/cloud-tools": "^0.12.0"
46+
},
47+
"autoload": {
48+
"psr-4": {
49+
"Quickstart\\": "src"
50+
}
51+
},
52+
"autoload-dev": {
53+
"psr-4": {
54+
"Quickstart\\Test\\": "tests"
55+
}
56+
}
57+
}

index.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
require_once __DIR__.'/vendor/autoload.php';
3+
use Google\CloudFunctions\FunctionsFramework;
4+
5+
// Register the function with Functions Framework.
6+
// https://console.cloud.google.com/functions/list
7+
FunctionsFramework::http( "on_https", "Quickstart\\CloudFunctions::on_https");
8+
FunctionsFramework::cloudEvent( "on_pubsub", "Quickstart\\CloudFunctions::on_pubsub");
9+
FunctionsFramework::cloudEvent( "on_gcs", "Quickstart\\CloudFunctions::on_gcs");

phpunit.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors="true">
3+
<php>
4+
</php>
5+
<coverage>
6+
<include>
7+
<directory suffix=".php">./src</directory>
8+
</include>
9+
<report>
10+
<html outputDirectory="./build/coverage" lowUpperBound="35" highLowerBound="70"/>
11+
<text outputFile="php://stdout" showUncoveredFiles="true"/>
12+
</report>
13+
</coverage>
14+
<testsuites>
15+
<testsuite name="main">
16+
<directory>src</directory>
17+
</testsuite>
18+
</testsuites>
19+
<logging/>
20+
</phpunit>

src/CloudFunctions.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
namespace Quickstart;
3+
4+
use CloudEvents\V1\CloudEventInterface;
5+
use GuzzleHttp\Psr7\Response;
6+
use GuzzleHttp\Psr7\Utils;
7+
use Psr\Http\Message\ResponseInterface;
8+
use Psr\Http\Message\ServerRequestInterface;
9+
use stdClass;
10+
11+
/**
12+
* Class CloudFunctions.
13+
*
14+
* @author Martin Zeitler
15+
* @version 1.0.0
16+
*/
17+
class CloudFunctions {
18+
19+
private static stdClass $response;
20+
21+
/** */
22+
public static function on_https( ServerRequestInterface $request ): ResponseInterface {
23+
$params = $request->getQueryParams();
24+
$body = sprintf('Hello, %s!', $params['name'] ?? 'World');
25+
return (new Response())
26+
->withBody(Utils::streamFor($body))
27+
->withStatus(200);
28+
}
29+
30+
/** */
31+
public static function on_pubsub( CloudEventInterface $event ): void {
32+
$log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stdout', 'wb');
33+
$cloudEventData = $event->getData();
34+
$pubSubData = base64_decode($cloudEventData['data']);
35+
$name = $pubSubData ? htmlspecialchars($pubSubData) : 'World';
36+
fwrite($log, "Hello, $name!" . PHP_EOL);
37+
}
38+
39+
/** */
40+
public static function on_gcs( CloudEventInterface $event ): void {
41+
$log = fopen(getenv('LOGGER_OUTPUT') ?: 'php://stdout', 'wb');
42+
$data = $event->getData();
43+
fwrite($log, 'Event: ' . $event->getId() . PHP_EOL);
44+
fwrite($log, 'Event Type: ' . $event->getType() . PHP_EOL);
45+
fwrite($log, 'Bucket: ' . $data['bucket'] . PHP_EOL);
46+
fwrite($log, 'File: ' . $data['name'] . PHP_EOL);
47+
fwrite($log, 'Metageneration: ' . $data['metageneration'] . PHP_EOL);
48+
fwrite($log, 'Created: ' . $data['timeCreated'] . PHP_EOL);
49+
fwrite($log, 'Updated: ' . $data['updated'] . PHP_EOL);
50+
}
51+
}

tests/SimpleTestCase.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
namespace Quickstart\Test;
3+
4+
use CloudEvents\V1\CloudEventImmutable;
5+
use GuzzleHttp\Psr7\Response;
6+
use GuzzleHttp\Psr7\ServerRequest;
7+
use Quickstart\CloudFunctions;
8+
use Google\CloudFunctions\CloudEvent;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* Class SimpleTestCase.
13+
*
14+
* @author Martin Zeitler
15+
* @version 1.0.0
16+
*/
17+
class SimpleTestCase extends TestCase {
18+
19+
private static string $project_id;
20+
private static string $test_topic;
21+
private static string $test_bucket;
22+
23+
public static function setUpBeforeClass(): void {
24+
require_once __DIR__.'/../index.php';
25+
self::$project_id = self::get_project_id();
26+
self::$test_topic = 'play-notifications';
27+
self::$test_bucket = 'gs://php-cloudfunctions';
28+
}
29+
30+
private static function get_project_id() {
31+
return shell_exec('gcloud config get-value project');
32+
}
33+
34+
public function test_on_https(): void {
35+
$payload = json_encode(['data' => uniqid()]);
36+
$request = new ServerRequest('POST', '/', [], $payload);
37+
$response = CloudFunctions::on_https( $request );
38+
$this->assertTrue($response instanceof Response);
39+
}
40+
41+
public function test_on_pubsub(): void {
42+
CloudFunctions::on_pubsub(new CloudEventImmutable(
43+
uniqId(), // id
44+
'//pubsub.googleapis.com/projects/'.self::$project_id.'/topics/'.self::$test_topic, // source
45+
'google.cloud.pubsub.topic.v1.messagePublished', // type
46+
['data' => base64_encode('Test')], // data
47+
'application/json' // data content-type
48+
));
49+
self::assertTrue( true );
50+
}
51+
52+
public function test_on_gcs(): void {
53+
CloudFunctions::on_gcs(new CloudEventImmutable(
54+
uniqId(), // id
55+
'pubsub.googleapis.com', // source
56+
'google.storage.object.finalize', [ // type
57+
'bucket' => self::$test_bucket,
58+
'name' => 'test.json',
59+
'metageneration' => '',
60+
'timeCreated' => time(),
61+
'updated' => time()
62+
], // data
63+
'application/json' // data content-type
64+
));
65+
self::assertTrue( true );
66+
}
67+
}

0 commit comments

Comments
 (0)