diff --git a/src/Vinelab/Cdn/Providers/AwsS3Provider.php b/src/Vinelab/Cdn/Providers/AwsS3Provider.php index 3a72c06..6075a5c 100755 --- a/src/Vinelab/Cdn/Providers/AwsS3Provider.php +++ b/src/Vinelab/Cdn/Providers/AwsS3Provider.php @@ -227,7 +227,10 @@ public function urlGenerator($path) { $url = $this->cdn_helper->parseUrl($this->getUrl()); - return $url['scheme'] . '://' . $this->getBucket() . '.' . $url['host'] . '/' . $path; + $bucket = $this->getBucket(); + $bucket = ( ! empty($bucket) ) ? $bucket . '.' : ''; + + return $url['scheme'] . '://' . $bucket . $url['host'] . '/' . $path; } /** diff --git a/tests/Vinelab/Cdn/Providers/AwsS3ProviderTest.php b/tests/Vinelab/Cdn/Providers/AwsS3ProviderTest.php index ace97df..d743a49 100755 --- a/tests/Vinelab/Cdn/Providers/AwsS3ProviderTest.php +++ b/tests/Vinelab/Cdn/Providers/AwsS3ProviderTest.php @@ -9,6 +9,12 @@ public function setUp() { parent::setUp(); + $this->url = 'http://www.google.com'; + $this->cdn_url = 'http://ZZZZZZZ.www.google.com/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); @@ -16,6 +22,8 @@ public function setUp() $this->m_validator->shouldReceive('validate'); $this->m_helper = M::mock('Vinelab\Cdn\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'); @@ -36,8 +44,17 @@ public function setUp() $this->p_awsS3Provider->setBatchBuilder($this->m_batch); $this->p_awsS3Provider->shouldReceive('connect')->andReturn(true); - - $this->configurations = [ + } + + public function tearDown() + { + M::close(); + parent::tearDown(); + } + + public function testInitializingObject() + { + $configurations = [ 'default' => 'aws.s3', 'url' => 'https://s3.amazonaws.com', 'threshold' => 10, @@ -57,28 +74,96 @@ public function setUp() ], ]; + $awsS3Provider_obj = $this->p_awsS3Provider->init($configurations); + + assertInstanceOf('Vinelab\Cdn\Providers\AwsS3Provider', $awsS3Provider_obj); } - public function tearDown() + public function testUploadingAssets() { - M::close(); - parent::tearDown(); + $configurations = [ + 'default' => 'aws.s3', + 'url' => 'https://s3.amazonaws.com', + 'threshold' => 10, + 'providers' => [ + 'aws' => [ + 's3' => [ + 'credentials' => [ + 'key' => 'XXXXXXX', + 'secret' => 'YYYYYYY', + ], + 'buckets' => [ + 'ZZZZZZZ' => '*', + ], + 'acl' => 'public-read' + ], + ], + ], + ]; + + $this->p_awsS3Provider->init($configurations); + + $result = $this->p_awsS3Provider->upload(new Collection([$this->m_spl_file])); + + assertEquals(true, $result); } - public function testInitializingObject() + public function testUrlGenerator() { - $returned = $this->p_awsS3Provider->init($this->configurations); + $configurations = [ + 'default' => 'aws.s3', + 'url' => 'https://s3.amazonaws.com', + 'threshold' => 10, + 'providers' => [ + 'aws' => [ + 's3' => [ + 'credentials' => [ + 'key' => 'XXXXXXX', + 'secret' => 'YYYYYYY', + ], + 'buckets' => [ + 'ZZZZZZZ' => '*', + ], + 'acl' => 'public-read' + ], + ], + ], + ]; + + $this->p_awsS3Provider->init($configurations); - assertInstanceOf('Vinelab\Cdn\Providers\AwsS3Provider', $returned); + $result = $this->p_awsS3Provider->urlGenerator($this->path); + + assertEquals($this->cdn_url, $result); } - public function testUploadingAssets() + public function testEmptyUrlGenerator() { - $this->p_awsS3Provider->init($this->configurations); + $configurations = [ + 'default' => 'aws.s3', + 'url' => 'https://s3.amazonaws.com', + 'threshold' => 10, + 'providers' => [ + 'aws' => [ + 's3' => [ + 'credentials' => [ + 'key' => 'XXXXXXX', + 'secret' => 'YYYYYYY', + ], + 'buckets' => [ + '' => '*', + ], + 'acl' => 'public-read' + ], + ], + ], + ]; - $result = $this->p_awsS3Provider->upload(new Collection([$this->m_spl_file])); + $this->p_awsS3Provider->init($configurations); - assertEquals(true, $result); + $result = $this->p_awsS3Provider->urlGenerator($this->path); + + assertEquals($this->path_url, $result); } }