Skip to content

Commit

Permalink
feat: add Symfony-7 support & deprecates old PHPvs (#104)
Browse files Browse the repository at this point in the history
Co-authored-by: Sean O'Brien <obrien.sean.dev@gmail.com>
  • Loading branch information
yenfryherrerafeliz and Sean O'Brien authored Feb 5, 2024
1 parent 73495ac commit 6ee6fc4
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 65 deletions.
47 changes: 36 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,57 @@ on:
pull_request:
branches: [ master ]

permissions:
contents: read

jobs:
run:
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
strategy:
#for each of the following versions of PHP, with and without --prefer-lowest
matrix:
php-versions: ['5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4']

php-versions: ['7.2.5', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
composer-options: ['', '--prefer-lowest']
#set the name for each job
name: PHP ${{ matrix.php-versions }}
name: PHP ${{ matrix.php-versions }} ${{ matrix.composer-options }}
#set up environment variables used by unit tests
env:
AWS_ACCESS_KEY_ID: foo
AWS_SECRET_ACCESS_KEY: bar
AWS_CSM_ENABLED: false
AWS_SUPPRESS_PHP_DEPRECATION_WARNING: true
steps:
#sets up the correct version of PHP with necessary config options
- name: Setup PHP
- name: Setup PHP with Xdebug
uses: shivammathur/setup-php@v2
with:
coverage: xdebug
php-version: ${{ matrix.php-versions }}
ini-values: memory_limit=4G, phar.readonly=false
ini-values: xdebug.overload_var_dump=0, memory_limit=4G, phar.readonly=false

#checkout the codebase from github
- name: Checkout codebase
uses: actions/checkout@v3
uses: actions/checkout@v4

#validate composer files
- name: Validate composer.json and composer.lock
run: composer validate

#run composer
- name: Composer install
run: composer install
#get dependencies
- name: Install dependencies
run: composer update ${{ matrix.composer-options }} --no-interaction --prefer-source

#php 8.x requirements
- if: ${{ matrix.php-versions >= '8.0' && matrix.composer-options != '' }}
name: PHP 8.x
run: composer require --dev phpunit/phpunit "^9.5" --no-interaction --prefer-source --with-all-dependencies

#php 8.1+ requirements
- if: ${{ matrix.php-versions >= '8.1' && matrix.composer-options != '' }}
name: PHP 8.1+
run: composer require --dev guzzlehttp/guzzle "^7.4.5" --no-interaction --prefer-source --with-all-dependencies

#run tests
- name: Run test suite
run: vendor/bin/phpunit
run: make test

16 changes: 8 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
}
],
"require": {
"php": ">=5.5",
"aws/aws-sdk-php": "^3.2.6",
"symfony/config": "~2.3|~3.0|~4.0|~5.0|~6.0",
"symfony/dependency-injection": "~2.3|~3.0|~4.0|~5.0|~6.0",
"symfony/http-kernel": "~2.3|~3.0|~4.0|~5.0|~6.0"
"php": ">=7.2.5",
"aws/aws-sdk-php": "^3.279.0",
"symfony/config": "~5.0|~6.0|~7.0",
"symfony/dependency-injection": "~5.0|~6.0|~7.0",
"symfony/http-kernel": "~5.0|~6.0|~7.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8.35|^5.6.3|^6.0.0",
"symfony/framework-bundle": "~2.3|~3.0|~4.0|~5.0|~6.0",
"symfony/yaml": "~2.3|~3.0|~4.0|~5.0|~6.0"
"phpunit/phpunit": "^8.5|^9.5",
"symfony/framework-bundle": "~5.0|~6.0|~7.0",
"symfony/yaml": "~5.0|~6.0|~7.0"
},
"autoload": {
"psr-4": {
Expand Down
25 changes: 10 additions & 15 deletions src/DependencyInjection/AwsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

class AwsExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
/**
* @throws \Exception
*/
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader(
$container,
Expand Down Expand Up @@ -44,7 +47,7 @@ public function load(array $configs, ContainerBuilder $container)
}


private function createServiceDefinition($name)
private function createServiceDefinition($name): Definition
{
$clientClass = "Aws\\{$name}\\{$name}Client";
$serviceDefinition = new Definition(
Expand All @@ -53,21 +56,13 @@ class_exists($clientClass) ? $clientClass : AwsClient::class

$serviceDefinition->setLazy(true);

// Handle Symfony >= 2.6
if (method_exists($serviceDefinition, 'setFactory')) {
return $serviceDefinition->setFactory([
new Reference('aws_sdk'),
'createClient',
])->setArguments([$name]);
}

return $serviceDefinition
->setFactoryService('aws_sdk')
->setFactoryMethod('createClient')
->setArguments([$name]);
return $serviceDefinition->setFactory([
new Reference('aws_sdk'),
'createClient',
])->setArguments([$name]);
}

private function inflateServicesInConfig(array &$config)
private function inflateServicesInConfig(array &$config): void
{
array_walk($config, function (&$value) {
if (is_array($value)) {
Expand Down
10 changes: 4 additions & 6 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
// Maintain backwars compatibility, only merge when AWS_MERGE_CONFIG is set
$mergeConfig = $this->shouldMergeConfig();
Expand Down Expand Up @@ -70,8 +70,7 @@ public function getConfigTreeBuilder()
->variableNode('ua_append')->end()
->variableNode('validate')->end()
->scalarNode('version')->end()
->end()
;
->end();

//Setup config trees for each of the services
foreach (array_column(Aws\manifest(), 'namespace') as $awsService) {
Expand Down Expand Up @@ -111,14 +110,13 @@ public function getConfigTreeBuilder()
->scalarNode('version')->end()
->end()
->end()
->end()
;
->end();
}

return $treeBuilder;
}

protected function shouldMergeConfig()
protected function shouldMergeConfig(): ?string
{
# works with symfony/dotenv
if (isset($_ENV['AWS_MERGE_CONFIG'])) {
Expand Down
40 changes: 24 additions & 16 deletions tests/DependencyInjection/AwsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class AwsExtensionTest extends TestCase
Expand All @@ -24,7 +25,7 @@ class AwsExtensionTest extends TestCase
*/
protected $container;

public function setUp()
public function setUp(): void
{
$this->kernel = new AppKernel('test', true);
$this->kernel->boot();
Expand All @@ -38,9 +39,9 @@ public function setUp()
public function sdk_config_should_be_passed_directly_to_the_constructor_and_resolved_by_the_sdk()
{
$config = $this->kernel->getTestConfig()['aws'];
$s3Region = isset($config['S3']['region']) ? $config['S3']['region'] : $config['region'];
$lambdaRegion = isset($config['Lambda']['region']) ? $config['Lambda']['region'] : $config['region'];
$codeDeployRegion = isset($config['CodeDeploy']['region']) ? $config['CodeDeploy']['region'] : $config['region'];
$s3Region = $config['S3']['region'] ?? $config['region'];
$lambdaRegion = $config['Lambda']['region'] ?? $config['region'];
$codeDeployRegion = $config['CodeDeploy']['region'] ?? $config['region'];

$testService = $this->container->get('test_service');

Expand Down Expand Up @@ -76,13 +77,16 @@ public function extension_should_escape_strings_that_begin_with_at_sign()
'secret' => '@@secret'
]];
$container = $this->getMockBuilder(ContainerBuilder::class)
->setMethods(['getDefinition', 'replaceArgument'])
->onlyMethods(['getDefinition'])
->getMock();
$definition = $this->getMockBuilder(Definition::class)
->onlyMethods(['replaceArgument'])
->getMock();
$container->expects($this->once())
->method('getDefinition')
->with('aws_sdk')
->willReturnSelf();
$container->expects($this->once())
->willReturn($definition);
$definition->expects($this->once())
->method('replaceArgument')
->with(0, $this->callback(function ($arg) {
return is_array($arg)
Expand All @@ -104,13 +108,16 @@ public function extension_should_expand_service_references()
$extension = new AwsExtension;
$config = ['credentials' => '@aws_sdk'];
$container = $this->getMockBuilder(ContainerBuilder::class)
->setMethods(['getDefinition', 'replaceArgument'])
->onlyMethods(['getDefinition'])
->getMock();
$definition = $this->getMockBuilder(Definition::class)
->onlyMethods(['replaceArgument'])
->getMock();
$container->expects($this->once())
->method('getDefinition')
->with('aws_sdk')
->willReturnSelf();
$container->expects($this->once())
->willReturn($definition);
$definition->expects($this->once())
->method('replaceArgument')
->with(0, $this->callback(function ($arg) {
return is_array($arg)
Expand All @@ -137,7 +144,6 @@ public function extension_should_validate_and_merge_configs()
'stats' => [
'http' => true
],
'retries' => 5,
'endpoint' => 'http://localhost:8000',
'endpoint_discovery' => [
'enabled' => true,
Expand Down Expand Up @@ -181,13 +187,16 @@ public function extension_should_validate_and_merge_configs()
'validate' => true,
];
$container = $this->getMockBuilder(ContainerBuilder::class)
->setMethods(['getDefinition', 'replaceArgument'])
->onlyMethods(['getDefinition'])
->getMock();
$definition = $this->getMockBuilder(Definition::class)
->onlyMethods(['replaceArgument'])
->getMock();
$container->expects($this->once())
->method('getDefinition')
->with('aws_sdk')
->willReturnSelf();
$container->expects($this->once())
->willReturn($definition);
$definition->expects($this->once())
->method('replaceArgument')
->with(0, $this->callback(function ($arg) {
return is_array($arg)
Expand Down Expand Up @@ -230,9 +239,8 @@ public function extension_should_error_merging_unknown_config_options()
'foo' => 'baz'
];
$container = $this->getMockBuilder(ContainerBuilder::class)
->setMethods(['getDefinition', 'replaceArgument'])
->onlyMethods(['getDefinition'])
->getMock();

try {
$extension->load([$config, $configDev], $container);
$this->fail('Should have thrown an Error or RuntimeException');
Expand Down
4 changes: 2 additions & 2 deletions tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class ConfigurationTest extends TestCase
{
public function setUp()
public function setUp(): void
{
(new Filesystem)
->remove(implode(DIRECTORY_SEPARATOR, [
Expand Down Expand Up @@ -39,7 +39,7 @@ public function container_should_compile_and_load($format)
$this->assertNotInstanceOf(DynamoDbClient::class, $testService->getCodeDeployClient());
}

public function formatProvider()
public function formatProvider(): array
{
return [
['yml'],
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ public function __construct($env, $debug, $extension = 'yml')
parent::__construct($env, $debug);
}

public function registerBundles()
public function registerBundles(): iterable
{
return [
new FrameworkBundle(),
new AwsBundle(),
];
}

public function registerContainerConfiguration(LoaderInterface $loader)
public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load($this->getTestConfigFile($this->extension));
}
Expand All @@ -35,7 +35,7 @@ public function getTestConfig()
}


private function getTestConfigFile($extension)
private function getTestConfigFile($extension): string
{
return __DIR__ . '/config.' . $extension;
}
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/TestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,31 @@ public function __construct(
/**
* @return S3Client
*/
public function getS3Client()
public function getS3Client(): S3Client
{
return $this->s3Client;
}

/**
* @return LambdaClient
*/
public function getLambdaClient()
public function getLambdaClient(): LambdaClient
{
return $this->lambdaClient;
}

/**
* @return CodeDeployClient
*/
public function getCodeDeployClient()
public function getCodeDeployClient(): CodeDeployClient
{
return $this->codeDeployClient;
}

/**
* @return array
*/
public function getClients()
public function getClients(): array
{
return [
$this->s3Client,
Expand Down

0 comments on commit 6ee6fc4

Please sign in to comment.