Skip to content

Commit

Permalink
Merge pull request #13 from publiux/release-1.0.3
Browse files Browse the repository at this point in the history
Release 1.0.3
  • Loading branch information
publiux committed Jan 22, 2016
2 parents ea7a057 + 706acb9 commit 07212db
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 68 deletions.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ The MIT License (MIT). Please see [License File](https://github.com/publiux/lara

## Changelog

#### v1.0.3
- Fixed bug where schemeless Urls could not be used for CloudFront. Valid urls now begin with http, https, or simply '//'

#### v1.0.2
- Fixed bug where the elixir function was inadvertently omitted from the release.

Expand Down
29 changes: 3 additions & 26 deletions src/Publiux/laravelcdn/Commands/EmptyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @category Command
*
* @author Mahmoud Zalt <mahmoud@vinelab.com>
* @author Raul Ruiz <publiux@gmail.com>
*/
class EmptyCommand extends Command
{
Expand All @@ -19,7 +20,7 @@ class EmptyCommand extends Command
*
* @var string
*/
protected $name = 'cdn:empty';
protected $signature = 'cdn:empty';

/**
* The console command description.
Expand Down Expand Up @@ -50,32 +51,8 @@ public function __construct(CdnInterface $cdn)
*
* @return mixed
*/
public function fire()
public function handle()
{
$this->cdn->emptyBucket();
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
// array('cdn', InputArgument::OPTIONAL, 'cdn option.'),
];
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
// array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
];
}
}
29 changes: 3 additions & 26 deletions src/Publiux/laravelcdn/Commands/PushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @category Command
*
* @author Mahmoud Zalt <mahmoud@vinelab.com>
* @author Raul Ruiz <publiux@gmail.com>
*/
class PushCommand extends Command
{
Expand All @@ -19,7 +20,7 @@ class PushCommand extends Command
*
* @var string
*/
protected $name = 'cdn:push';
protected $signature = 'cdn:push';

/**
* The console command description.
Expand Down Expand Up @@ -50,32 +51,8 @@ public function __construct(CdnInterface $cdn)
*
* @return mixed
*/
public function fire()
public function handle()
{
$this->cdn->push();
}

/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
{
return [
// array('cdn', InputArgument::OPTIONAL, 'cdn option.'),
];
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
// array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
];
}
}
29 changes: 27 additions & 2 deletions src/Publiux/laravelcdn/Providers/AwsS3Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @property string $cloudfront_url
*
* @author Mahmoud Zalt <mahmoud@vinelab.com>
* @author Raul Ruiz <publiux@gmail.com>
*/
class AwsS3Provider extends Provider implements ProviderInterface
{
Expand Down Expand Up @@ -191,6 +192,22 @@ public function upload($assets)

// upload each asset file to the CDN
if (count($assets) > 0) {

// Review files before upload if user wishes.
/*$review = $this->console->option('review');
if ($review) {
$this->console->writeln('<fg=green>The files to be uploaded are....</fg=green>');
foreach ($assets as $file) {
$this->console->writeln('<fg=cyan>'.$file->getRealpath().'</fg=cyan>');
}
//Ask the user to confirm that they want to continue the upload.
if (!$this->console->confirm('Do you wish to continue? [y|N]')) {
$this->console->writeln('<fg=red>Upload cancelled.</fg=cyan>');
return true;
}
}*/

$this->console->writeln('<fg=yellow>Upload in progress......</fg=yellow>');
foreach ($assets as $file) {
try {
Expand Down Expand Up @@ -294,15 +311,23 @@ public function urlGenerator($path)
if ($this->getCloudFront() === true) {
$url = $this->cdn_helper->parseUrl($this->getCloudFrontUrl());

return $url['scheme'].'://'.$url['host'].'/'.$path;
if (array_key_exists('scheme', $url)) {
return $url['scheme'].'://'.$url['host'].'/'.$path;
} else {
return '//'.$url['host'].'/'.$path;
}
}

$url = $this->cdn_helper->parseUrl($this->getUrl());

$bucket = $this->getBucket();
$bucket = (!empty($bucket)) ? $bucket.'.' : '';

return $url['scheme'].'://'.$bucket.$url['host'].'/'.$path;
if (array_key_exists('scheme', $url)) {
return $url['scheme'].'://'.$bucket.$url['host'].'/'.$path;
} else {
return '//'.$bucket.$url['host'].'/'.$path;
}
}

/**
Expand Down
140 changes: 126 additions & 14 deletions tests/Publiux/laravelcdn/Providers/AwsS3ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @category Test
*
* @author Mahmoud Zalt <mahmoud@vinelab.com>
* @author Raul Ruiz <publiux@gmail.com>
*/
class AwsS3ProviderTest extends TestCase
{
Expand All @@ -20,31 +21,21 @@ public function setUp()

$this->url = 'http://www.google.com';
$this->cdn_url = 'http://my-bucket-name.www.google.com/public/css/cool/style.css';
$this->cloudfront_url_fullscheme = 'http://cool.cloudfront.net/public/css/cool/style.css';
$this->cloudfront_url_noscheme = '//cool.cloudfront.net/public/css/cool/style.css';
$this->path = 'public/css/cool/style.css';
$this->path_url = 'http://www.google.com/public/css/cool/style.css';
$this->pased_url = parse_url($this->url);

$this->m_console = M::mock('Symfony\Component\Console\Output\ConsoleOutput');
$this->m_console->shouldReceive('writeln')->atLeast(2);

$this->m_validator = M::mock('Publiux\laravelcdn\Validators\Contracts\ProviderValidatorInterface');
$this->m_validator->shouldReceive('validate');

$this->m_helper = M::mock('Publiux\laravelcdn\CdnHelper');
$this->m_helper->shouldReceive('parseUrl')
->andReturn($this->pased_url);

$this->m_spl_file = M::mock('Symfony\Component\Finder\SplFileInfo');
$this->m_spl_file->shouldReceive('getPathname')->andReturn('vinelab/cdn/tests/Vinelab/Cdn/AwsS3ProviderTest.php');
$this->m_spl_file->shouldReceive('getPathname')->andReturn('publiux/laravelcdn/tests/Publiux/laravelcdn/AwsS3ProviderTest.php');
$this->m_spl_file->shouldReceive('getRealPath')->andReturn(__DIR__.'/AwsS3ProviderTest.php');

$this->p_awsS3Provider = M::mock('\Publiux\laravelcdn\Providers\AwsS3Provider[connect]',
[
$this->m_console,
$this->m_validator,
$this->m_helper,
]);

$this->m_s3 = M::mock('Aws\S3\S3Client');
$this->m_s3->shouldReceive('factory')->andReturn('Aws\S3\S3Client');
$m_command = M::mock('Aws\Command');
Expand All @@ -56,6 +47,45 @@ public function setUp()
->andReturn($m_command1);

$this->m_s3->shouldReceive('execute');
}

public function setupNonCloudfrontTest()
{
$this->m_helper = M::mock('Publiux\laravelcdn\CdnHelper');
$this->m_helper->shouldReceive('parseUrl')
->andReturn(parse_url($this->url));

$this->p_awsS3Provider = M::mock('\Publiux\laravelcdn\Providers\AwsS3Provider[connect]',
[
$this->m_console,
$this->m_validator,
$this->m_helper,
]);

$this->p_awsS3Provider->setS3Client($this->m_s3);

$this->p_awsS3Provider->shouldReceive('connect')->andReturn(true);
}

public function setupCloudfrontTest($fullScheme = false)
{
$this->m_helper = M::mock('Publiux\laravelcdn\CdnHelper');

if ($fullScheme) {
$this->m_helper->shouldReceive('parseUrl')
->andReturn(parse_url($this->cloudfront_url_fullscheme));
} else {
$this->m_helper->shouldReceive('parseUrl')
->andReturn(parse_url($this->cloudfront_url_noscheme));
}

$this->p_awsS3Provider = M::mock('\Publiux\laravelcdn\Providers\AwsS3Provider[connect]',
[
$this->m_console,
$this->m_validator,
$this->m_helper,
]);

$this->p_awsS3Provider->setS3Client($this->m_s3);

$this->p_awsS3Provider->shouldReceive('connect')->andReturn(true);
Expand Down Expand Up @@ -95,6 +125,8 @@ public function testInitializingObject()
],
];

$this->setupNonCloudfrontTest();

$awsS3Provider_obj = $this->p_awsS3Provider->init($configurations);

assertInstanceOf('Publiux\laravelcdn\Providers\AwsS3Provider', $awsS3Provider_obj);
Expand Down Expand Up @@ -128,14 +160,16 @@ public function testUploadingAssets()
],
];

$this->setupNonCloudfrontTest();

$this->p_awsS3Provider->init($configurations);

$result = $this->p_awsS3Provider->upload(new Collection([$this->m_spl_file]));

assertEquals(true, $result);
}

public function testUrlGenerator()
public function testUrlGeneratorS3()
{
$configurations = [
'default' => 'AwsS3',
Expand Down Expand Up @@ -163,13 +197,89 @@ public function testUrlGenerator()
],
];

$this->setupNonCloudfrontTest();

$this->p_awsS3Provider->init($configurations);

$result = $this->p_awsS3Provider->urlGenerator($this->path);

assertEquals($this->cdn_url, $result);
}

public function testUrlGeneratorCloudFrontFullScheme()
{
$configurations = [
'default' => 'AwsS3',
'url' => 'https://s3.amazonaws.com',
'threshold' => 10,
'providers' => [
'aws' => [
's3' => [
'region' => 'us-standard',
'version' => 'latest',
'buckets' => [
'my-bucket-name' => '*',
],
'acl' => 'public-read',
'cloudfront' => [
'use' => true,
'cdn_url' => 'http://cool.cloudfront.net',
],
'metadata' => [],
'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')),
'cache-control' => 'max-age=2628000',
'version' => null,
],
],
],
];

$this->setupCloudfrontTest(true);

$this->p_awsS3Provider->init($configurations);

$result = $this->p_awsS3Provider->urlGenerator($this->path);

assertEquals($this->cloudfront_url_fullscheme, $result);
}

public function testUrlGeneratorCloudFrontNoScheme()
{
$configurations = [
'default' => 'AwsS3',
'url' => 'https://s3.amazonaws.com',
'threshold' => 10,
'providers' => [
'aws' => [
's3' => [
'region' => 'us-standard',
'version' => 'latest',
'buckets' => [
'my-bucket-name' => '*',
],
'acl' => 'public-read',
'cloudfront' => [
'use' => true,
'cdn_url' => '//cool.cloudfront.net',
],
'metadata' => [],
'expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')),
'cache-control' => 'max-age=2628000',
'version' => null,
],
],
],
];

$this->setupCloudfrontTest(false);

$this->p_awsS3Provider->init($configurations);

$result = $this->p_awsS3Provider->urlGenerator($this->path);

assertEquals($this->cloudfront_url_noscheme, $result);
}

public function testEmptyUrlGenerator()
{
$configurations = [
Expand Down Expand Up @@ -198,6 +308,8 @@ public function testEmptyUrlGenerator()
],
];

$this->setupNonCloudfrontTest();

$this->p_awsS3Provider->init($configurations);

$result = $this->p_awsS3Provider->urlGenerator($this->path);
Expand Down

0 comments on commit 07212db

Please sign in to comment.