Skip to content

Commit

Permalink
Fixed an issue with the recent PR and added a test to make sure the p…
Browse files Browse the repository at this point in the history
…rovider works when no config is provided
  • Loading branch information
jeremeamia committed Mar 7, 2013
1 parent 5fbfa64 commit 37dc939
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/Aws/Laravel/AwsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

use Aws\Common\Aws;
use Aws\Common\Client\UserAgentListener;
use Aws\Common\Exception\RuntimeException;
use Guzzle\Common\Event;
use Guzzle\Service\Client;
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;

/**
* AWS SDK for PHP service provider for Laravel applications
Expand All @@ -36,15 +35,19 @@ public function register()
{
$this->app['aws'] = $this->app->share(function ($app) {
// Instantiate the AWS service builder
$config = isset($app['config']['aws']) ? $app['config']['aws'] : array();
$config = (isset($app['config']) && isset($app['config']['aws'])) ? $app['config']['aws'] : array();
$aws = Aws::factory($config);

// Attach an event listener that will append the Laravel version number in the user agent string
$aws->getEventDispatcher()->addListener('service_builder.create_client', function (Event $event) {
// The version number is only available in BETA4+, so an extra check is needed
$version = defined('Illuminate\Foundation\Application::VERSION') ? Application::VERSION : '4.0.0';

// Add the listener to modify the UA string
$clientConfig = $event['client']->getConfig();
$commandParams = $clientConfig->get(Client::COMMAND_PARAMS) ?: array();
$clientConfig->set(Client::COMMAND_PARAMS, array_merge_recursive($commandParams, array(
UserAgentListener::OPTION => 'Laravel' . ( defined('Application::VERSION') ? '/' . Application::VERSION : '' ),
UserAgentListener::OPTION => "Laravel/{$version}",
)));
});

Expand Down
22 changes: 22 additions & 0 deletions tests/Aws/Laravel/Tests/AwsServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function testRegisterAwsServiceProvider()
$provider->boot();

// Get an instance of a client (S3) to use for testing
/** @var $s3 \Aws\S3\S3Client */
$s3 = $app['aws']->get('s3');
$this->assertInstanceOf('Aws\S3\S3Client', $s3);

// Verify that the app and clients created by the SDK receive the provided credentials
$this->assertEquals('your-aws-access-key-id', $app['config']['aws']['key']);
Expand All @@ -54,4 +56,24 @@ public function testRegisterAwsServiceProvider()
$s3->dispatch('command.before_send', array('command' => $command));
$this->assertRegExp('/.+Laravel\/.+/', $request->getHeader('User-Agent', true));
}

/**
* @expectedException \Aws\Common\Exception\InstanceProfileCredentialsException
*/
public function testNoConfigProvided()
{
// Setup the Laravel app and AWS service provider
$app = new Application();
$provider = new AwsServiceProvider($app);
$app->register($provider);
$provider->boot();

// Make sure we can still get the S3Client
/** @var $s3 \Aws\S3\S3Client */
$s3 = $app['aws']->get('s3');
$this->assertInstanceOf('Aws\S3\S3Client', $s3);

// Trigger the expected exception
$s3->getCredentials()->getAccessKeyId();
}
}

0 comments on commit 37dc939

Please sign in to comment.