steering_docs/php-tech/tests.md
šØ CRITICAL - Must be completed BEFORE any code generation
# Step 1: List available knowledge bases
ListKnowledgeBases()
# Step 2: Query coding standards (REQUIRED)
QueryKnowledgeBases("coding-standards-KB", "PHP-code-example-standards")
# Step 3: Query implementation patterns (REQUIRED)
QueryKnowledgeBases("PHP-premium-KB", "PHP implementation patterns testing")
# Step 4: AWS service research (REQUIRED)
search_documentation("What is [AWS Service] and what are its key API operations?")
read_documentation("https://docs.aws.amazon.com/[service]/latest/[relevant-page]")
FAILURE TO COMPLETE KNOWLEDGE BASE CONSULTATION WILL RESULT IN INCORRECT CODE STRUCTURE
Generate comprehensive test suites including unit tests and integration tests using PHPUnit framework with proper mocking and AWS data structures.
example_code/{service}/tests/
āāā {Service}Test.php # Unit and integration tests
āāā phpunit.xml # PHPUnit configuration
MANDATORY phpunit.xml structure:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="../vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="unit">
<directory>.</directory>
</testsuite>
</testsuites>
<groups>
<include>
<group>unit</group>
<group>integ</group>
</include>
</groups>
</phpunit>
<?php
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use PHPUnit\Framework\TestCase;
use Aws\MockHandler;
use Aws\Result;
use Aws\CommandInterface;
use Psr\Http\Message\RequestInterface;
class ServiceTest extends TestCase
{
private $mockHandler;
private $service;
protected function setUp(): void
{
$this->mockHandler = new MockHandler();
$this->service = new ServiceClass([
'handler' => $this->mockHandler,
'region' => 'us-east-1'
]);
}
/**
* @group unit
*/
public function testHelloService(): void
{
$this->mockHandler->append(new Result(['Buckets' => []]));
$result = $this->service->helloService();
$this->assertIsArray($result);
}
/**
* @group integ
*/
public function testIntegrationExample(): void
{
// Integration test with real AWS service
$result = $this->service->realServiceCall();
$this->assertNotNull($result);
}
}
/**
* @group unit
* @dataProvider errorCodeProvider
*/
public function testActionWithVariousConditions(?string $errorCode): void
{
// Arrange
$paramValue = 'test-value';
$expectedResponse = new Result([
'ResponseKey' => 'response-value'
]);
if ($errorCode === null) {
$this->mockHandler->append($expectedResponse);
} else {
$this->mockHandler->append(function (CommandInterface $cmd, RequestInterface $req) use ($errorCode) {
return new \Aws\Exception\AwsException($errorCode, $cmd);
});
}
// Act & Assert
if ($errorCode === null) {
$result = $this->service->performAction($paramValue);
$this->assertEquals('response-value', $result['ResponseKey']);
} else {
$this->expectException(\Aws\Exception\AwsException::class);
$this->service->performAction($paramValue);
}
}
public function errorCodeProvider(): array
{
return [
'success' => [null],
'bad_request' => ['BadRequestException'],
'internal_error' => ['InternalServerErrorException'],
];
}
/**
* @group integ
*/
public function testServiceIntegration(): void
{
// Arrange
$service = new ServiceClass();
// Act - This should not raise an exception
$result = $service->listResources();
// Assert - Verify result structure
$this->assertIsArray($result);
}
/**
* @group integ
*/
public function testResourceLifecycleIntegration(): void
{
// Arrange
$service = new ServiceClass();
$resourceId = null;
try {
// Act - Create resource
$resourceId = $service->createResource('test-resource');
$this->assertNotNull($resourceId);
// Use resource
$result = $service->getResource($resourceId);
$this->assertNotNull($result);
} finally {
// Clean up
if ($resourceId !== null) {
try {
$service->deleteResource($resourceId);
} catch (Exception $e) {
// Ignore cleanup errors
}
}
}
}
phpunit --group unit
phpunit --group integ
phpunit
@group unit, @group integ)@group integ annotation@group unit annotationtest prefixtearDown() methodstestable_readline function instead of readline so tests can be passed values rather than read from stdin