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
2023-05-12 05:28:46 -07:00
namespace lambda\tests ;
2022-09-27 08:33:42 -07:00
use Aws\Lambda\LambdaClient ;
use Aws\S3\S3Client ;
2023-02-14 16:31:03 -07:00
use Iam\IAMService ;
2023-05-12 05:28:46 -07:00
use Lambda\LambdaService ;
2022-09-27 08:33:42 -07:00
use PHPUnit\Framework\TestCase ;
2023-05-12 05:28:46 -07:00
/**
* @group integ
*/
class LambdaTest extends TestCase
2022-09-27 08:33:42 -07:00
{
protected array $clientArgs ;
protected LambdaClient $lambdaClient ;
protected LambdaService $lambdaService ;
2023-02-14 16:31:03 -07:00
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 ();
2023-02-14 16:31:03 -07:00
$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 ();
2023-05-12 05:28:46 -07:00
$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 ,
2023-02-14 16:31:03 -07:00
'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 );
}
}