diff --git a/src/Vinelab/Cdn/Asset.php b/src/Vinelab/Cdn/Asset.php index 5e8ce15..c692641 100755 --- a/src/Vinelab/Cdn/Asset.php +++ b/src/Vinelab/Cdn/Asset.php @@ -18,7 +18,7 @@ class Asset implements AssetInterface{ * * @var array */ - private $default_include = [ + protected $default_include = [ 'directories' => ['public'], 'extensions' => [], 'patterns' => [], @@ -29,7 +29,7 @@ class Asset implements AssetInterface{ * * @var array */ - private $default_exclude = [ + protected $default_exclude = [ 'directories' => [], 'files' => [], 'extensions' => [], @@ -40,40 +40,40 @@ class Asset implements AssetInterface{ /** * @var Array */ - public $included_directories; + protected $included_directories; /** * @var Array */ - public $included_files; + protected $included_files; /** * @var Array */ - public $included_extensions; + protected $included_extensions; /** * @var Array */ - public $included_patterns; + protected $included_patterns; /** * @var Array */ - public $excluded_directories; + protected $excluded_directories; /** * @var Array */ - public $excluded_files; + protected $excluded_files; /** * @var Array */ - public $excluded_extensions; + protected $excluded_extensions; /** * @var Array */ - public $excluded_patterns; + protected $excluded_patterns; /* * @var boolean */ - public $exclude_hidden; + protected $exclude_hidden; /* * Allowed assets for upload (found in included_directories) @@ -113,7 +113,7 @@ public function init($configurations = array()) * * @param $configurations */ - public function parseAndFillConfiguration($configurations) + private function parseAndFillConfiguration($configurations) { $this->default_include = isset($configurations['include']) ? array_merge($this->default_include, $configurations['include']) : $this->default_include; @@ -179,7 +179,7 @@ public function getExcludedPatterns() } /** - * @return mixed + * @return Collection */ public function getAssets() { diff --git a/src/Vinelab/Cdn/Cdn.php b/src/Vinelab/Cdn/Cdn.php index 5a3435e..4b4e871 100755 --- a/src/Vinelab/Cdn/Cdn.php +++ b/src/Vinelab/Cdn/Cdn.php @@ -69,7 +69,7 @@ public function push() // return an instance of the corresponding Provider concrete according to the configuration $provider = $this->provider_factory->create($configurations); - $provider->upload($this->asset_holder->getAssets()); + return $provider->upload($this->asset_holder->getAssets()); } } diff --git a/src/Vinelab/Cdn/CdnFacade.php b/src/Vinelab/Cdn/CdnFacade.php index fe284b0..4d1cbee 100755 --- a/src/Vinelab/Cdn/CdnFacade.php +++ b/src/Vinelab/Cdn/CdnFacade.php @@ -7,6 +7,8 @@ use Vinelab\Cdn\Contracts\ProviderFactoryInterface; use Vinelab\Cdn\Contracts\CdnFacadeInterface; use Vinelab\Cdn\Contracts\CdnHelperInterface; +use Vinelab\Cdn\Validators\CdnFacadeValidator; +use Vinelab\Cdn\Exceptions\EmptyPathException; /** * Class CdnFacade @@ -14,6 +16,11 @@ */ class CdnFacade implements CdnFacadeInterface{ + /** + * @var Contracts\ProviderFactoryInterface + */ + protected $provider_factory; + /** * @var instance of the default provider object */ @@ -24,20 +31,28 @@ class CdnFacade implements CdnFacadeInterface{ */ protected $helper; + /** + * @var Validators\CdnFacadeValidator + */ + protected $cdn_facade_validator; + /** * Calls the provider initializer * * @param ProviderFactoryInterface $provider_factory * @param Contracts\CdnHelperInterface $helper + * @param Validators\CdnFacadeValidator $cdn_facade_validator * * @internal param \Vinelab\Cdn\Repository $configurations */ public function __construct( ProviderFactoryInterface $provider_factory, - CdnHelperInterface $helper + CdnHelperInterface $helper, + CdnFacadeValidator $cdn_facade_validator ) { $this->provider_factory = $provider_factory; $this->helper = $helper; + $this->cdn_facade_validator = $cdn_facade_validator; $this->init(); } @@ -49,10 +64,14 @@ public function __construct( * * @param $path * - * @return string + * @return mixed + * @throws Exceptions\EmptyPathException */ public function asset($path) { + if ( ! isset($path)) + throw new EmptyPathException('Path does not exist.'); + // remove slashes from begging and ending of the path then call the // url generator of the provider return $this->provider->urlGenerator($this->cleanPath($path)); @@ -85,6 +104,4 @@ private function init() $this->provider = $this->provider_factory->create($configurations); } - - } diff --git a/src/Vinelab/Cdn/CdnHelper.php b/src/Vinelab/Cdn/CdnHelper.php index 933b102..00f3c5d 100644 --- a/src/Vinelab/Cdn/CdnHelper.php +++ b/src/Vinelab/Cdn/CdnHelper.php @@ -35,6 +35,9 @@ public function __construct(Repository $configurations) /** * Check if the config file exist and return it or * throw an exception + * + * @return array + * @throws Exceptions\MissingConfigurationFileException */ public function getConfigurations() { diff --git a/src/Vinelab/Cdn/CdnServiceProvider.php b/src/Vinelab/Cdn/CdnServiceProvider.php index c2b1d36..3fe2b10 100755 --- a/src/Vinelab/Cdn/CdnServiceProvider.php +++ b/src/Vinelab/Cdn/CdnServiceProvider.php @@ -73,8 +73,18 @@ public function register() ); $this->app->bind( - 'Vinelab\Cdn\Validators\Contracts\ConfigurationsInterface', - 'Vinelab\Cdn\Validators\Configurations' + 'Vinelab\Cdn\Validators\Contracts\ProviderValidatorInterface', + 'Vinelab\Cdn\Validators\ProviderValidator' + ); + + $this->app->bind( + 'Vinelab\Cdn\Validators\Contracts\CdnFacadeValidatorInterface', + 'Vinelab\Cdn\Validators\CdnFacadeValidator' + ); + + $this->app->bind( + 'Vinelab\Cdn\Validators\Contracts\ValidatorInterface', + 'Vinelab\Cdn\Validators\Validator' ); // register the commands: @@ -106,6 +116,8 @@ public function register() }); + // registering the package (for the config files) + $this->package("vinelab/cdn"); } diff --git a/src/Vinelab/Cdn/Contracts/AssetInterface.php b/src/Vinelab/Cdn/Contracts/AssetInterface.php index f265b01..31a9b81 100755 --- a/src/Vinelab/Cdn/Contracts/AssetInterface.php +++ b/src/Vinelab/Cdn/Contracts/AssetInterface.php @@ -7,7 +7,6 @@ interface AssetInterface { public function init($configurations); - public function parseAndFillConfiguration($configurations); public function getIncludedDirectories(); public function getIncludedExtensions(); diff --git a/src/Vinelab/Cdn/Exceptions/Exceptions.php b/src/Vinelab/Cdn/Exceptions/Exceptions.php index f693f34..0d9a401 100644 --- a/src/Vinelab/Cdn/Exceptions/Exceptions.php +++ b/src/Vinelab/Cdn/Exceptions/Exceptions.php @@ -6,4 +6,5 @@ class CdnException extends \RuntimeException {} class MissingConfigurationFileException extends CdnException {} class MissingConfigurationException extends CdnException {} class UnsupportedProviderException extends CdnException {} +class EmptyPathException extends CdnException {} diff --git a/src/Vinelab/Cdn/Finder.php b/src/Vinelab/Cdn/Finder.php index 519bce8..4f4856c 100755 --- a/src/Vinelab/Cdn/Finder.php +++ b/src/Vinelab/Cdn/Finder.php @@ -82,7 +82,7 @@ private function includeThis(AssetInterface $asset_holder) // include files with this extensions foreach ($asset_holder->getIncludedExtensions() as $extension) { - $this->name('*'.$extension); + $this->name('*' . $extension); } // include patterns diff --git a/src/Vinelab/Cdn/Providers/AwsS3Provider.php b/src/Vinelab/Cdn/Providers/AwsS3Provider.php index 01accd5..3a72c06 100755 --- a/src/Vinelab/Cdn/Providers/AwsS3Provider.php +++ b/src/Vinelab/Cdn/Providers/AwsS3Provider.php @@ -4,7 +4,7 @@ * @author Mahmoud Zalt */ -use Vinelab\Cdn\Validators\Contracts\ConfigurationsInterface; +use Vinelab\Cdn\Validators\Contracts\ProviderValidatorInterface; use Vinelab\Cdn\Providers\Contracts\ProviderInterface; use Symfony\Component\Console\Output\ConsoleOutput; use Vinelab\Cdn\Contracts\CdnHelperInterface; @@ -79,17 +79,19 @@ class AwsS3Provider extends Provider implements ProviderInterface{ /** * @param \Symfony\Component\Console\Output\ConsoleOutput $console - * @param \Vinelab\Cdn\Validators\Contracts\ConfigurationsInterface $configurations + * @param \Vinelab\Cdn\Validators\Contracts\ProviderValidatorInterface $provider_validator * @param \Vinelab\Cdn\Contracts\CdnHelperInterface $cdn_helper + * + * @internal param \Vinelab\Cdn\Validators\Contracts\ConfigurationsInterface $configurations */ public function __construct( - ConsoleOutput $console, - ConfigurationsInterface $configurations, - CdnHelperInterface $cdn_helper + ConsoleOutput $console, + ProviderValidatorInterface $provider_validator, + CdnHelperInterface $cdn_helper ) { - $this->console = $console; - $this->configurations = $configurations; - $this->cdn_helper = $cdn_helper; + $this->console = $console; + $this->provider_validator = $provider_validator; + $this->cdn_helper = $cdn_helper; } /** @@ -116,7 +118,7 @@ public function init($configurations) ]; // check if any required configuration is missed - $this->configurations->validate($supplier, $this->rules); + $this->provider_validator->validate($supplier, $this->rules); $this->supplier = $supplier; @@ -126,32 +128,54 @@ public function init($configurations) /** * Create a cdn instance and create a batch builder instance + * + * @return bool */ - private function connect() + public function connect() { - // Instantiate an S3 client - $this->s3_client = S3Client::factory( array( - 'key' => $this->credential_key, - 'secret' => $this->credential_secret, + try{ + + // Instantiate an S3 client + $this->setS3Client( + S3Client::factory( array( + 'key' => $this->credential_key, + 'secret' => $this->credential_secret, + ) ) ); - // Initialize the batch builder - $this->batch = BatchBuilder::factory() - ->transferCommands($this->threshold) - ->autoFlushAt($this->threshold) - ->build(); + // Initialize the batch builder + $this->setBatchBuilder( + BatchBuilder::factory() + ->transferCommands($this->threshold) + ->autoFlushAt($this->threshold) + ->keepHistory() + ->build() + ); + + }catch (\Exception $e){ + + return false; + } + + return true; } /** * Upload assets * * @param $assets + * + * @return bool */ public function upload($assets) { // connect before uploading - $this->connect(); + $connected = $this->connect(); + + if ( ! $connected) { + return false; + } // user terminal message $this->console->writeln('Uploading in progress...'); @@ -164,28 +188,31 @@ public function upload($assets) 'Bucket' => $this->getBucket(), // the bucket name 'Key' => $file->getPathName(), // the path of the file on the server (CDN) - 'Body' => fopen($file->getRealpath(), 'r'), // the path of the path locally + 'Body' => fopen($file->getRealPath(), 'r'), // the path of the path locally 'ACL' => $this->acl, // the permission of the file ])); } catch (S3Exception $e) { - echo "There was an error uploading this file ($file->getRealpath()).\n"; + $this->console->writeln("Error while uploading: ($file->getRealpath())"); + return false; } - } - // Execute batch. - $commands = $this->batch->flush(); + $commands = $this->batch->getHistory(); + + if ($commands) { + foreach ($commands as $command) { - // Fix: in small threshold output is not available (batch related thing) - foreach ($commands as $command) { - $result = $command->getResult(); - // user terminal message - $this->console->writeln('URL: ' . $result->get('ObjectURL') . ''); + $result = $command->getResult(); + // user terminal message + $this->console->writeln('URL: ' . $result->get('ObjectURL') . ''); + } } // user terminal message $this->console->writeln('Upload completed successfully.'); + + return true; } /** @@ -203,6 +230,23 @@ public function urlGenerator($path) return $url['scheme'] . '://' . $this->getBucket() . '.' . $url['host'] . '/' . $path; } + /** + * @param $s3_client + */ + public function setS3Client($s3_client) + { + $this->s3_client = $s3_client; + } + + /** + * @param $batch + */ + public function setBatchBuilder($batch) + { + $this->batch = $batch; + } + + /** * @return string */ diff --git a/src/Vinelab/Cdn/Providers/Contracts/ProviderInterface.php b/src/Vinelab/Cdn/Providers/Contracts/ProviderInterface.php index 98c7156..6907424 100755 --- a/src/Vinelab/Cdn/Providers/Contracts/ProviderInterface.php +++ b/src/Vinelab/Cdn/Providers/Contracts/ProviderInterface.php @@ -16,4 +16,8 @@ public function getUrl(); public function getBucket(); + public function setS3Client($s3_client); + + public function setBatchBuilder($batch); + } diff --git a/src/Vinelab/Cdn/Validators/CdnFacadeValidator.php b/src/Vinelab/Cdn/Validators/CdnFacadeValidator.php new file mode 100644 index 0000000..cc74775 --- /dev/null +++ b/src/Vinelab/Cdn/Validators/CdnFacadeValidator.php @@ -0,0 +1,15 @@ + + */ + +use Vinelab\Cdn\Validators\Contracts\CdnFacadeValidatorInterface; + +/** + * Class CdnFacadeValidator + * @package Vinelab\Cdn\Validators + */ +class CdnFacadeValidator extends Validator implements CdnFacadeValidatorInterface{ + +} diff --git a/src/Vinelab/Cdn/Validators/Contracts/ConfigurationsInterface.php b/src/Vinelab/Cdn/Validators/Contracts/CdnFacadeValidatorInterface.php similarity index 73% rename from src/Vinelab/Cdn/Validators/Contracts/ConfigurationsInterface.php rename to src/Vinelab/Cdn/Validators/Contracts/CdnFacadeValidatorInterface.php index ca512db..dd9fffc 100755 --- a/src/Vinelab/Cdn/Validators/Contracts/ConfigurationsInterface.php +++ b/src/Vinelab/Cdn/Validators/Contracts/CdnFacadeValidatorInterface.php @@ -4,6 +4,6 @@ * @author Mahmoud Zalt */ -interface ConfigurationsInterface{ +interface CdnFacadeValidatorInterface{ } diff --git a/src/Vinelab/Cdn/Validators/Contracts/ProviderValidatorInterface.php b/src/Vinelab/Cdn/Validators/Contracts/ProviderValidatorInterface.php new file mode 100755 index 0000000..4b3343b --- /dev/null +++ b/src/Vinelab/Cdn/Validators/Contracts/ProviderValidatorInterface.php @@ -0,0 +1,11 @@ + + */ + +interface ProviderValidatorInterface{ + + public function validate($configuration, $required); + +} diff --git a/src/Vinelab/Cdn/Validators/Contracts/ValidatorInterface.php b/src/Vinelab/Cdn/Validators/Contracts/ValidatorInterface.php new file mode 100755 index 0000000..7e750d2 --- /dev/null +++ b/src/Vinelab/Cdn/Validators/Contracts/ValidatorInterface.php @@ -0,0 +1,9 @@ + + */ + +interface ValidatorInterface{ + +} diff --git a/src/Vinelab/Cdn/Validators/Configurations.php b/src/Vinelab/Cdn/Validators/ProviderValidator.php similarity index 86% rename from src/Vinelab/Cdn/Validators/Configurations.php rename to src/Vinelab/Cdn/Validators/ProviderValidator.php index 6ee682f..94c9085 100644 --- a/src/Vinelab/Cdn/Validators/Configurations.php +++ b/src/Vinelab/Cdn/Validators/ProviderValidator.php @@ -5,13 +5,13 @@ */ use Vinelab\Cdn\Exceptions\MissingConfigurationException; -use Vinelab\Cdn\Validators\Contracts\ConfigurationsInterface; +use Vinelab\Cdn\Validators\Contracts\ProviderValidatorInterface; /** * Class Configurations * @package Vinelab\Cdn\Validators */ -class Configurations implements ConfigurationsInterface{ +class ProviderValidator extends Validator implements ProviderValidatorInterface{ /** * Checks for any required configuration is missed diff --git a/src/Vinelab/Cdn/Validators/Validator.php b/src/Vinelab/Cdn/Validators/Validator.php new file mode 100644 index 0000000..8204d40 --- /dev/null +++ b/src/Vinelab/Cdn/Validators/Validator.php @@ -0,0 +1,20 @@ + + */ + +use Vinelab\Cdn\Validators\Contracts\ValidatorInterface; + +/** + * Main Validator Class + * + * Class Validator + * @package Vinelab\Cdn\Validators + */ +class Validator implements ValidatorInterface{ + + + + +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 061695f..46cd541 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -6,8 +6,6 @@ use Mockery as M; use PHPUnit_Framework_TestCase as PHPUnit; - - class TestCase extends PHPUnit { public function __construct() diff --git a/tests/Vinelab/Cdn/AssetTest.php b/tests/Vinelab/Cdn/AssetTest.php new file mode 100755 index 0000000..7e19cb2 --- /dev/null +++ b/tests/Vinelab/Cdn/AssetTest.php @@ -0,0 +1,155 @@ +asset = new \Vinelab\Cdn\Asset; + } + + public function tearDown() + { + M::close(); + parent::tearDown(); + } + + public function testParseAndFillConfiguration() + { + $dir = 'foo'; + + $result = $this->asset->init([ + 'include' => [ + 'directories' => $dir, + ], + ]); + + // check the returned object is of type Vinelab\Cdn\Asset + assertEquals($result, $this->asset); + } + + public function testIncludedDirectories() + { + $dir = 'foo'; + + $this->asset->init([ + 'include' => [ + 'directories' => $dir, + ], + ]); + + $result = $this->asset->getIncludedDirectories(); + + assertEquals($result, $dir); + } + + public function testIncludedExtensions() + { + $ext = 'foo'; + + $this->asset->init([ + 'include' => [ + 'extensions' => $ext, + ], + ]); + + $result = $this->asset->getIncludedExtensions(); + + assertEquals($result, $ext); + } + + public function testIncludedPatterns() + { + $pat = 'foo'; + + $this->asset->init([ + 'include' => [ + 'patterns' => $pat, + ], + ]); + + $result = $this->asset->getIncludedPatterns(); + + assertEquals($result, $pat); + } + + public function testExcludedDirectories() + { + $dir = 'foo'; + + $this->asset->init([ + 'exclude' => [ + 'directories' => $dir, + ], + ]); + + $result = $this->asset->getExcludedDirectories(); + + assertEquals($result, $dir); + } + + public function testExcludedFiles() + { + $dir = 'foo'; + + $this->asset->init([ + 'exclude' => [ + 'files' => $dir, + ], + ]); + + $result = $this->asset->getExcludedFiles(); + + assertEquals($result, $dir); + } + + public function testExcludedExtensions() + { + $dir = 'foo'; + + $this->asset->init([ + 'exclude' => [ + 'extensions' => $dir, + ], + ]); + + $result = $this->asset->getExcludedExtensions(); + + assertEquals($result, $dir); + } + + public function testExcludedPatterns() + { + $dir = 'foo'; + + $this->asset->init([ + 'exclude' => [ + 'patterns' => $dir, + ], + ]); + + $result = $this->asset->getExcludedPatterns(); + + assertEquals($result, $dir); + } + + public function testExcludedHidden() + { + $bol = true; + + $this->asset->init([ + 'exclude' => [ + 'hidden' => $bol, + ], + ]); + + $result = $this->asset->getExcludeHidden(); + + assertEquals($result, $bol); + } + +} diff --git a/tests/Vinelab/Cdn/AwsS3ProviderTest.php b/tests/Vinelab/Cdn/AwsS3ProviderTest.php new file mode 100755 index 0000000..ace97df --- /dev/null +++ b/tests/Vinelab/Cdn/AwsS3ProviderTest.php @@ -0,0 +1,84 @@ +m_console = M::mock('Symfony\Component\Console\Output\ConsoleOutput'); + $this->m_console->shouldReceive('writeln')->atLeast(2); + + $this->m_validator = M::mock('Vinelab\Cdn\Validators\Contracts\ProviderValidatorInterface'); + $this->m_validator->shouldReceive('validate'); + + $this->m_helper = M::mock('Vinelab\Cdn\CdnHelper'); + + $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('getRealPath')->andReturn(__DIR__ . '/AwsS3ProviderTest.php'); + + $this->p_awsS3Provider = M::mock('\Vinelab\Cdn\Providers\AwsS3Provider[connect]', array + ($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'); + $this->m_s3->shouldReceive('getCommand'); + $this->p_awsS3Provider->setS3Client($this->m_s3); + + $this->m_batch = M::mock('Guzzle\Batch\BatchBuilder'); + $this->m_batch->shouldReceive('factory')->andReturn('Guzzle\Batch\BatchBuilder'); + $this->m_batch->shouldReceive('add'); + $this->m_batch->shouldReceive('getHistory')->andReturn(null); + $this->p_awsS3Provider->setBatchBuilder($this->m_batch); + + $this->p_awsS3Provider->shouldReceive('connect')->andReturn(true); + + $this->configurations = [ + 'default' => 'aws.s3', + 'url' => 'https://s3.amazonaws.com', + 'threshold' => 10, + 'providers' => [ + 'aws' => [ + 's3' => [ + 'credentials' => [ + 'key' => 'XXXXXXX', + 'secret' => 'YYYYYYY', + ], + 'buckets' => [ + 'ZZZZZZZ' => '*', + ], + 'acl' => 'public-read' + ], + ], + ], + ]; + + } + + public function tearDown() + { + M::close(); + parent::tearDown(); + } + + public function testInitializingObject() + { + $returned = $this->p_awsS3Provider->init($this->configurations); + + assertInstanceOf('Vinelab\Cdn\Providers\AwsS3Provider', $returned); + } + + public function testUploadingAssets() + { + $this->p_awsS3Provider->init($this->configurations); + + $result = $this->p_awsS3Provider->upload(new Collection([$this->m_spl_file])); + + assertEquals(true, $result); + } + +} diff --git a/tests/Vinelab/Cdn/CdnFacadeTest.php b/tests/Vinelab/Cdn/CdnFacadeTest.php new file mode 100755 index 0000000..edd5f12 --- /dev/null +++ b/tests/Vinelab/Cdn/CdnFacadeTest.php @@ -0,0 +1,55 @@ +asset_path = 'public/foo/bar.php'; + $this->cdn_url = 'https://amazon.foo.bar.com'; + + $this->provider = M::mock('Vinelab\Cdn\Providers\AwsS3Provider'); + + $this->provider_factory = M::mock('Vinelab\Cdn\Contracts\ProviderFactoryInterface'); + $this->provider_factory->shouldReceive('create')->once()->andReturn($this->provider); + + $this->helper = M::mock('Vinelab\Cdn\Contracts\CdnHelperInterface'); + $this->helper->shouldReceive('getConfigurations')->once()->andReturn([]); + + $this->validator = new \Vinelab\Cdn\Validators\CdnFacadeValidator; + + $this->facade = new \Vinelab\Cdn\CdnFacade( + $this->provider_factory, $this->helper, $this->validator); + } + + public function tearDown() + { + M::close(); + parent::tearDown(); + } + + public function testAssetUrlGenerator() + { + $this->provider->shouldReceive('urlGenerator') + ->with($this->asset_path) + ->once() + ->andReturn($this->cdn_url); + + $result = $this->facade->asset($this->asset_path); + + // assert is calling the url generator + assertEquals($result, $this->cdn_url); + } + + /** + * @expectedException \Vinelab\Cdn\Exceptions\EmptyPathException + */ + public function testAssetWithEmptyParameter() + { + $this->facade->asset(null); + } + +} diff --git a/tests/Vinelab/Cdn/CdnTest.php b/tests/Vinelab/Cdn/CdnTest.php new file mode 100755 index 0000000..3eff6c2 --- /dev/null +++ b/tests/Vinelab/Cdn/CdnTest.php @@ -0,0 +1,50 @@ +m_finder = M::mock('Vinelab\Cdn\Contracts\FinderInterface'); + $this->m_finder->shouldReceive('read')->once()->andReturn(New Collection()); + + $this->m_asset = M::mock('Vinelab\Cdn\Contracts\AssetInterface'); + $this->m_asset->shouldReceive('init')->once()->andReturn($this->m_asset); + $this->m_asset->shouldReceive('setAssets')->once(); + $this->m_asset->shouldReceive('getAssets')->once()->andReturn(New Collection()); + + + $this->m_provider = M::mock('Vinelab\Cdn\Providers\Provider'); + $this->m_provider->shouldReceive('upload')->once()->andReturn(true); + + $this->m_provider_factory = M::mock('Vinelab\Cdn\Contracts\ProviderFactoryInterface'); + $this->m_provider_factory->shouldReceive('create')->once()->andReturn($this->m_provider); + + $this->m_helper = M::mock('Vinelab\Cdn\Contracts\CdnHelperInterface'); + $this->m_helper->shouldReceive('getConfigurations')->once()->andReturn(array()); + + $this->cdn = new \Vinelab\Cdn\Cdn( + $this->m_finder, + $this->m_asset, + $this->m_provider_factory, + $this->m_helper); + + } + + public function tearDown() + { + M::close(); + parent::tearDown(); + } + + public function testPushingCommand() + { + $result = $this->cdn->push(); + assertEquals($result, true); + } + +} diff --git a/tests/Vinelab/Cdn/FinderTest.php b/tests/Vinelab/Cdn/FinderTest.php index d8dd796..2a724d3 100755 --- a/tests/Vinelab/Cdn/FinderTest.php +++ b/tests/Vinelab/Cdn/FinderTest.php @@ -39,4 +39,24 @@ public function testReadingAllowedDirectories() assertEquals($result, new Collection($result->all())); } + + /** + * @expectedException \InvalidArgumentException + */ + public function testReadingAllowedDirectoriesWithEmptyAssetHolder() + { + $asset_holder = new \Vinelab\Cdn\Asset; + + $asset_holder->init(array('include' => [])); + + $console_output = M::mock('Symfony\Component\Console\Output\ConsoleOutput'); + $console_output->shouldReceive('writeln') + ->atLeast(1); + + $finder = new \Vinelab\Cdn\Finder($console_output); + + $finder->read($asset_holder); + + } + } diff --git a/tests/Vinelab/Cdn/ProviderFactoryTest.php b/tests/Vinelab/Cdn/ProviderFactoryTest.php new file mode 100755 index 0000000..8098bb7 --- /dev/null +++ b/tests/Vinelab/Cdn/ProviderFactoryTest.php @@ -0,0 +1,54 @@ +provider_factory = new \Vinelab\Cdn\ProviderFactory; + + } + + public function tearDown() + { + M::close(); + parent::tearDown(); + } + + public function testAwsS3ProviderCreation() + { + $configurations = ['default' => 'aws.s3']; + + $m_aws_s3 = M::mock('Vinelab\Cdn\Providers\AwsS3Provider'); + + \Illuminate\Support\Facades\App::shouldReceive('make')->once()->andReturn($m_aws_s3); + + $m_aws_s3->shouldReceive('init') + ->with($configurations) + ->once() + ->andReturn($m_aws_s3); + + $provider = $this->provider_factory->create($configurations); + + assertEquals($provider, $m_aws_s3); + } + + + /** + * @expectedException \Vinelab\Cdn\Exceptions\MissingConfigurationException + */ + public function testMissingDefaultProviderConfiguration() + { + $configurations = ['default' => '']; + + $m_aws_s3 = M::mock('Vinelab\Cdn\Providers\AwsS3Provider'); + + \Illuminate\Support\Facades\App::shouldReceive('make')->once()->andReturn($m_aws_s3); + + $this->provider_factory->create($configurations); + } + +}