|
| 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 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Google\Cloud\Samples\Functions\ConceptsBuildExtension\Test; |
| 20 | + |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | +use Google\Cloud\TestUtils\CloudFunctionLocalTestTrait; |
| 23 | +use Google\Cloud\TestUtils\GcloudWrapper\CloudFunction; |
| 24 | +use Google\Cloud\Utils\ExponentialBackoff; |
| 25 | +use Symfony\Component\Process\Process; |
| 26 | +use Symfony\Component\Process\PhpExecutableFinder; |
| 27 | + |
| 28 | +/** |
| 29 | + * Class SystemTest. |
| 30 | + */ |
| 31 | +class SystemTest extends TestCase |
| 32 | +{ |
| 33 | + use CloudFunctionLocalTestTrait; |
| 34 | + |
| 35 | + private static $entryPoint = 'helloBuildExtension'; |
| 36 | + |
| 37 | + public function testFunction(): void |
| 38 | + { |
| 39 | + // Send a request to the function. |
| 40 | + $resp = $this->client->get('/'); |
| 41 | + |
| 42 | + // Assert status code. |
| 43 | + $this->assertEquals('200', $resp->getStatusCode()); |
| 44 | + |
| 45 | + // Assert function output. |
| 46 | + $output = trim((string) $resp->getBody()); |
| 47 | + $this->assertEquals( |
| 48 | + 'Hello World! (from my_custom_extension.so)', |
| 49 | + $output |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Start the development server based on the prepared function. |
| 55 | + * |
| 56 | + * We override this to run the "gcp-build" step and to enable the built |
| 57 | + * extension when running the PHP build-in-web server. |
| 58 | + */ |
| 59 | + private static function doRun() |
| 60 | + { |
| 61 | + // Build the extension |
| 62 | + $process = new Process(['composer', 'run-script', 'gcp-build']); |
| 63 | + $process->start(); |
| 64 | + |
| 65 | + $backoff = new ExponentialBackoff($retries = 10); |
| 66 | + $backoff->execute(function () use ($process) { |
| 67 | + if ($process->isRunning()) { |
| 68 | + throw new \Exception('waiting for "gcp-build" step to complete'); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + $phpBin = (new PhpExecutableFinder())->find(); |
| 73 | + $phpBin .= ' -d extension=\'./my_custom_extension.so\''; |
| 74 | + return self::$fn->run([], CloudFunction::DEFAULT_PORT, $phpBin); |
| 75 | + } |
| 76 | +} |
0 commit comments