SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 1 Java
2022-09-27 08:33:42 -07:00
<?php
2022-09-29 16:43:24 -07:00
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
namespace lambda\tests;
2022-09-27 08:33:42 -07:00
use Aws\Lambda\LambdaClient;
use Aws\S3\S3Client;
use Iam\IAMService;
use Lambda\LambdaService;
2022-09-27 08:33:42 -07:00
use PHPUnit\Framework\TestCase;
/**
* @group integ
*/
class LambdaTest extends TestCase
2022-09-27 08:33:42 -07:00
{
protected array $clientArgs;
protected LambdaClient $lambdaClient;
protected LambdaService $lambdaService;
protected IAMService $iamService;
2022-09-27 08:33:42 -07:00
protected S3Client $s3client;
2023-04-28 15:56:27 -07:00
public function setup(): void
2022-09-27 08:33:42 -07:00
{
echo "constructor";
$this->clientArgs = [
'region' => 'us-west-2',
'version' => 'latest',
'profile' => 'default',
];
$this->lambdaClient = new LambdaClient($this->clientArgs);
$this->lambdaService = new LambdaService();
$this->iamService = new IAMService();
2022-09-27 08:33:42 -07:00
$this->s3client = new S3Client($this->clientArgs);
}
public function testSingleActionCalls()
{
echo "start single action tests\n";
$uniqid = uniqid();
$code = __DIR__ . "/../lambda_handler_calculator.zip";
2022-09-27 08:33:42 -07:00
$functionName = "calculator-$uniqid";
$lambda_assume_role_policy = "{
\"Version\": \"2012-10-17\",
\"Statement\": [
{
\"Effect\": \"Allow\",
\"Principal\": {
\"Service\": \"lambda.amazonaws.com\"
},
\"Action\": \"sts:AssumeRole\"
}
]
}";
$bucketName = "test-example-bucket-$uniqid";
$this->s3client->createBucket([
'Bucket' => $bucketName,
]);
$file = file_get_contents($code);
$this->s3client->putObject([
'Bucket' => $bucketName,
'Key' => $functionName,
2022-09-27 08:33:42 -07:00
'Body' => $file,
]);
$handler = "lambda_handler_calculator";
$roleName = "test-lambda-role-$uniqid";
$role = $this->iamService->createRole($roleName, $lambda_assume_role_policy);
$this->iamService->attachRolePolicy(
$role['RoleName'],
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
);
$this->lambdaService->createFunction($functionName, $role, $bucketName, $handler);
$functionsList = $this->lambdaService->listFunctions();
foreach ($functionsList['Functions'] as $functionList) {
echo "{$functionList['FunctionName']}\n";
}
do {
$function = $this->lambdaService->getFunction($functionName);
} while ($function['Configuration']['State'] == 'Pending');
$params = [
'action' => 'plus',
'x' => 5,
'y' => 4,
];
$result = $this->lambdaService->invoke($functionName, $params);
//Clean up resources
$this->lambdaService->deleteFunction($functionName);
$this->iamService->deleteRole($roleName);
$deleteObjects = $this->s3client->listObjectsV2([
'Bucket' => $bucketName,
]);
$deleteObjects = $this->s3client->deleteObjects([
'Bucket' => $bucketName,
'Delete' => [
'Objects' => $deleteObjects['Contents'],
]
]);
$this->s3client->deleteBucket(['Bucket' => $bucketName]);
self::assertTrue(true);
}
}