Skip to content

Commit 37649f2

Browse files
committed
Merge pull request #1 from jeremeamia/laravel5
Updated tests and removed the user-agent modification code.
2 parents ba5f302 + 56f50bd commit 37649f2

9 files changed

+85
-225
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
],
1414
"require": {
1515
"php": ">=5.4.0",
16-
"aws/aws-sdk-php": "~2.2",
16+
"aws/aws-sdk-php": "~2.4",
1717
"illuminate/support": "~5.0"
1818
},
1919
"require-dev": {
20+
"laravel/framework": "~5.0",
2021
"phpunit/phpunit": "~4.0"
2122
},
2223
"autoload": {

config/config.php

+5-23
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
1-
<?php
2-
/**
3-
* Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License").
6-
* You may not use this file except in compliance with the License.
7-
* A copy of the License is located at
8-
*
9-
* http://aws.amazon.com/apache2.0
10-
*
11-
* or in the "license" file accompanying this file. This file is distributed
12-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13-
* express or implied. See the License for the specific language governing
14-
* permissions and limitations under the License.
15-
*/
16-
17-
return [
1+
<?php return [
182

193
/*
204
|--------------------------------------------------------------------------
215
| Your AWS Credentials
226
|--------------------------------------------------------------------------
237
|
248
| In order to communicate with an AWS service, you must provide your AWS
25-
| credentials including your AWS Access Key ID and your AWS Secret Key.
9+
| credentials including your AWS Access Key ID and AWS Secret Access Key.
2610
|
2711
| To use credentials from your credentials file or environment or to use
2812
| IAM Instance Profile credentials, please remove these config settings from
29-
| your config or make sure they are null. For more information see:
13+
| your config or make sure they are null. For more information, see:
3014
| http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/configuration.html
3115
|
3216
*/
@@ -40,10 +24,8 @@
4024
|
4125
| Many AWS services are available in multiple regions. You should specify
4226
| the AWS region you would like to use, but please remember that not every
43-
| service is available in every region.
44-
|
45-
| These are the regions: us-east-1, us-west-1, us-west-2, us-gov-west-1
46-
| eu-west-1, sa-east-1, ap-northeast-1, ap-southeast-1, ap-southeast-2
27+
| service is available in every region. To see what regions are available,
28+
| see: http://docs.aws.amazon.com/general/latest/gr/rande.html
4729
|
4830
*/
4931
'region' => env('AWS_REGION', 'us-east-1'),

phpunit.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false"
33
backupStaticAttributes="false"
4-
bootstrap="./tests/bootstrap.php"
4+
bootstrap="./vendor/autoload.php"
55
colors="false"
66
convertErrorsToExceptions="true"
77
convertNoticesToExceptions="true"
@@ -16,6 +16,12 @@
1616
</testsuite>
1717
</testsuites>
1818

19+
<php>
20+
<env name="AWS_ACCESS_KEY_ID" value="foo"/>
21+
<env name="AWS_SECRET_ACCESS_KEY" value="bar"/>
22+
<env name="AWS_REGION" value="baz"/>
23+
</php>
24+
1925
<filter>
2026
<whitelist addUncoveredFilesFromWhitelist="false">
2127
<directory suffix=".php">src</directory>

src/AwsFacade.php

+4-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
1-
<?php
2-
/**
3-
* Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License").
6-
* You may not use this file except in compliance with the License.
7-
* A copy of the License is located at
8-
*
9-
* http://aws.amazon.com/apache2.0
10-
*
11-
* or in the "license" file accompanying this file. This file is distributed
12-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13-
* express or implied. See the License for the specific language governing
14-
* permissions and limitations under the License.
15-
*/
16-
17-
namespace Aws\Laravel;
1+
<?php namespace Aws\Laravel;
182

193
use Aws\Common\Client\AwsClientInterface;
204
use Illuminate\Support\Facades\Facade;
@@ -24,8 +8,8 @@
248
*
259
* @method static AwsClientInterface get($name, $throwAway = false) Get a client from the service builder
2610
*/
27-
class AwsFacade extends Facade
28-
{
11+
class AwsFacade extends Facade {
12+
2913
/**
3014
* Get the registered name of the component.
3115
*
@@ -35,4 +19,5 @@ protected static function getFacadeAccessor()
3519
{
3620
return 'aws';
3721
}
22+
3823
}

src/AwsServiceProvider.php

+9-39
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,12 @@
1-
<?php
2-
/**
3-
* Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License").
6-
* You may not use this file except in compliance with the License.
7-
* A copy of the License is located at
8-
*
9-
* http://aws.amazon.com/apache2.0
10-
*
11-
* or in the "license" file accompanying this file. This file is distributed
12-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13-
* express or implied. See the License for the specific language governing
14-
* permissions and limitations under the License.
15-
*/
16-
17-
namespace Aws\Laravel;
1+
<?php namespace Aws\Laravel;
182

193
use Aws\Common\Aws;
20-
use Aws\Common\Client\UserAgentListener;
21-
use Guzzle\Common\Event;
22-
use Guzzle\Service\Client;
23-
use Illuminate\Foundation\Application;
244
use Illuminate\Support\ServiceProvider;
255

266
/**
277
* AWS SDK for PHP service provider for Laravel applications
288
*/
29-
class AwsServiceProvider extends ServiceProvider
30-
{
31-
32-
const VERSION = '2.0.0';
9+
class AwsServiceProvider extends ServiceProvider {
3310

3411
/**
3512
* Indicates if loading of the provider is deferred.
@@ -60,21 +37,13 @@ public function boot()
6037
public function register()
6138
{
6239
$this->app->singleton('aws', function ($app) {
63-
// Instantiate the AWS service builder
64-
$aws = Aws::factory($app['config']->get('aws'));
40+
// Retrieve config.
41+
$config = $app['config']->get('aws');
42+
if (isset($config['config_file'])) {
43+
$config = $config['config_file'];
44+
}
6545

66-
// Attach an event listener that will append the Laravel and module version numbers to the user agent string
67-
$aws->getEventDispatcher()->addListener('service_builder.create_client', function (Event $event) {
68-
$clientConfig = $event['client']->getConfig();
69-
$commandParams = $clientConfig->get(Client::COMMAND_PARAMS) ?: [];
70-
$userAgentSuffix = 'Laravel/' . Application::VERSION . ' L5MOD/' . AwsServiceProvider::VERSION;
71-
72-
$clientConfig->set(Client::COMMAND_PARAMS, array_merge_recursive($commandParams, [
73-
UserAgentListener::OPTION => $userAgentSuffix,
74-
]));
75-
});
76-
77-
return $aws;
46+
return Aws::factory($config);
7847
});
7948

8049
$this->app->alias('aws', 'Aws\Common\Aws');
@@ -89,4 +58,5 @@ public function provides()
8958
{
9059
return ['aws', 'Aws\Common\Aws'];
9160
}
61+
9262
}

tests/Aws/Laravel/Test/AwsFacadeTest.php

-38
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
<?php
2-
/**
3-
* Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License").
6-
* You may not use this file except in compliance with the License.
7-
* A copy of the License is located at
8-
*
9-
* http://aws.amazon.com/apache2.0
10-
*
11-
* or in the "license" file accompanying this file. This file is distributed
12-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13-
* express or implied. See the License for the specific language governing
14-
* permissions and limitations under the License.
15-
*/
16-
17-
namespace Aws\Laravel\Test;
18-
19-
/**
20-
* AwsServiceProvider test cases
21-
*/
22-
class AwsServiceProviderTest extends AwsServiceProviderTestCase
23-
{
24-
public function testRegisterAwsServiceProviderWithGlobalConfig()
1+
<?php namespace Aws\Laravel\Test;
2+
3+
use Aws\Laravel\AwsFacade as AWS;
4+
use Aws\Laravel\AwsServiceProvider;
5+
use Illuminate\Config\Repository;
6+
use Illuminate\Foundation\Application;
7+
8+
class AwsServiceProviderTest extends \PHPUnit_Framework_TestCase {
9+
10+
public function testFacadeCanBeResolvedToServiceInstance()
11+
{
12+
$app = $this->setupApplication();
13+
$this->setupServiceProvider($app);
14+
15+
// Mount facades
16+
AWS::setFacadeApplication($app);
17+
18+
// Get an instance of a client (S3) via its facade
19+
$s3 = AWS::get('s3');
20+
$this->assertInstanceOf('Aws\S3\S3Client', $s3);
21+
}
22+
23+
public function testRegisterAwsServiceProviderWithConfigFile()
2524
{
2625
$app = $this->setupApplication();
2726
$this->setupServiceProvider($app);
2827

2928
// Simulate global config; specify config file
30-
$app['config']->set('aws', array(
29+
$app['config']->set('aws', [
3130
'config_file' => __DIR__ . '/test_services.json'
32-
));
31+
]);
3332

3433
// Get an instance of a client (S3)
3534
/** @var $s3 \Aws\S3\S3Client */
@@ -39,15 +38,9 @@ public function testRegisterAwsServiceProviderWithGlobalConfig()
3938
// Verify that the client received the credentials from the global config
4039
$this->assertEquals('change_me', $s3->getCredentials()->getAccessKeyId());
4140
$this->assertEquals('change_me', $s3->getCredentials()->getSecretKey());
42-
43-
// Make sure the user agent contains Laravel information
44-
$command = $s3->getCommand('ListBuckets');
45-
$request = $command->prepare();
46-
$s3->dispatch('command.before_send', array('command' => $command));
47-
$this->assertRegExp('/.+Laravel\/.+L4MOD\/.+/', (string) $request->getHeader('User-Agent'));
4841
}
4942

50-
public function testRegisterAwsServiceProviderWithPackageConfig()
43+
public function testRegisterAwsServiceProviderWithPackageConfigAndEnv()
5144
{
5245
$app = $this->setupApplication();
5346
$this->setupServiceProvider($app);
@@ -58,8 +51,9 @@ public function testRegisterAwsServiceProviderWithPackageConfig()
5851
$this->assertInstanceOf('Aws\S3\S3Client', $s3);
5952

6053
// Verify that the client received the credentials from the package config
61-
$this->assertEquals('YOUR_AWS_ACCESS_KEY_ID', $s3->getCredentials()->getAccessKeyId());
62-
$this->assertEquals('YOUR_AWS_SECRET_KEY', $s3->getCredentials()->getSecretKey());
54+
$this->assertEquals('foo', $s3->getCredentials()->getAccessKeyId());
55+
$this->assertEquals('bar', $s3->getCredentials()->getSecretKey());
56+
$this->assertEquals('baz', $s3->getRegion());
6357
}
6458

6559
public function testServiceNameIsProvided()
@@ -68,4 +62,33 @@ public function testServiceNameIsProvided()
6862
$provider = $this->setupServiceProvider($app);
6963
$this->assertContains('aws', $provider->provides());
7064
}
65+
66+
/**
67+
* @return Application
68+
*/
69+
private function setupApplication()
70+
{
71+
// Create the application such that the config is loaded
72+
$app = new Application();
73+
$app->setBasePath(sys_get_temp_dir());
74+
$app->instance('config', new Repository());
75+
76+
return $app;
77+
}
78+
79+
/**
80+
* @param Application $app
81+
*
82+
* @return AwsServiceProvider
83+
*/
84+
private function setupServiceProvider(Application $app)
85+
{
86+
// Create and register the provider
87+
$provider = new AwsServiceProvider($app);
88+
$app->register($provider);
89+
$provider->boot();
90+
91+
return $provider;
92+
}
93+
7194
}

0 commit comments

Comments
 (0)