Skip to content

Commit

Permalink
Add tests for the JS serverless plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Aug 14, 2023
1 parent a61011f commit 38d2e3a
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"guzzlehttp/guzzle": "^7.5",
"phpstan/phpstan": "^1.10.26",
"phpunit/phpunit": "^9.6.10",
"symfony/console": "^4.4|^5.0|^6.0|^7.0"
"symfony/console": "^4.4|^5.0|^6.0|^7.0",
"symfony/yaml": "^6.3"
},
"scripts": {
"test": [
Expand Down
14 changes: 14 additions & 0 deletions tests/Plugin/serverless-runtime-root.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
service: bref
provider:
name: aws
runtime: php-83

plugins:
- ../../index.js

functions:
function:
handler: function.php
function-arm:
handler: function.php
architecture: arm64
30 changes: 30 additions & 0 deletions tests/Plugin/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
service: bref
provider:
name: aws

plugins:
- ../../index.js

functions:
function:
handler: function.php
runtime: php-83
fpm:
handler: fpm.php
runtime: php-83-fpm
console:
handler: console.php
runtime: php-83-console

function-arm:
handler: function.php
architecture: arm64
runtime: php-83
fpm-arm:
handler: fpm.php
architecture: arm64
runtime: php-83-fpm
console-arm:
handler: console.php
architecture: arm64
runtime: php-83-console
70 changes: 70 additions & 0 deletions tests/PluginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php declare(strict_types=1);

namespace Bref\Test;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;
use Symfony\Component\Yaml\Yaml;

class PluginTest extends TestCase
{
public function test the plugin adds the layers(): void
{
$output = $this->slsPrint('serverless.yml');

self::assertFunction($output['functions']['function'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:php-83:',
]);
self::assertFunction($output['functions']['fpm'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:php-83-fpm:',
]);
self::assertFunction($output['functions']['console'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:php-83:',
'arn:aws:lambda:us-east-1:534081306603:layer:console:',
]);

self::assertFunction($output['functions']['function-arm'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:arm-php-83:',
]);
self::assertFunction($output['functions']['fpm-arm'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:arm-php-83-fpm:',
]);
self::assertFunction($output['functions']['console-arm'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:arm-php-83:',
'arn:aws:lambda:us-east-1:534081306603:layer:console:',
]);
}

public function test the plugin adds the layers when the runtime is set in the provider(): void
{
$output = $this->slsPrint('serverless-runtime-root.yml');

self::assertFunction($output['functions']['function'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:php-83:',
]);
self::assertFunction($output['functions']['function-arm'], 'provided.al2', [
'arn:aws:lambda:us-east-1:534081306603:layer:arm-php-83:',
]);
}

private function slsPrint(string $configFile): array
{
$process = (new Process(
['serverless', 'print', '-c', $configFile],
cwd: __DIR__ . '/Plugin',
env: [
'SLS_TELEMETRY_DISABLED' => '1', // else we sometimes get HTTP errors (and its faster)
],
))->mustRun();
return Yaml::parse($process->getOutput());
}

private static function assertFunction(array $config, string $runtime, array $layers): void
{
self::assertEquals($runtime, $config['runtime']);
self::assertCount(count($layers), $config['layers']);
foreach ($layers as $index => $layer) {
self::assertStringStartsWith($layer, $config['layers'][$index]);
}
}
}

0 comments on commit 38d2e3a

Please sign in to comment.