From d052b399c3593fd3ccb85ac131e3a8cfdc7b8321 Mon Sep 17 00:00:00 2001 From: mahmoud Date: Wed, 10 Sep 2014 10:23:31 +0300 Subject: [PATCH 1/9] update readme file (combine the url back) --- readme.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 22d91b7..52ba262 100755 --- a/readme.md +++ b/readme.md @@ -118,16 +118,10 @@ Specify what to be ignored. #### URL -Set the URL protocol: +Set the CDN URL: ```php - 'protocol' => 'https', -``` - -Set the CDN domain: - -```php - 'domain' => 's3.amazonaws.com', + 'url' => 'https://s3.amazonaws.com', ``` #### Threshold From 5b5a8ad3f4b950f650e01916033070b8be55f1ac Mon Sep 17 00:00:00 2001 From: mahmoud Date: Wed, 10 Sep 2014 12:28:30 +0300 Subject: [PATCH 2/9] update the CdnTest class mode the code from the setUp() to the testing() and ad some mock rules to be updated more after few commits --- tests/Vinelab/Cdn/CdnTest.php | 78 ++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 19 deletions(-) diff --git a/tests/Vinelab/Cdn/CdnTest.php b/tests/Vinelab/Cdn/CdnTest.php index 3eff6c2..49ccc63 100755 --- a/tests/Vinelab/Cdn/CdnTest.php +++ b/tests/Vinelab/Cdn/CdnTest.php @@ -9,42 +9,82 @@ public function setUp() { parent::setUp(); - $this->m_finder = M::mock('Vinelab\Cdn\Contracts\FinderInterface'); - $this->m_finder->shouldReceive('read')->once()->andReturn(New Collection()); + $this->m_spl_file_info = M::mock('Symfony\Component\Finder\SplFileInfo'); + + } + public function tearDown() + { + M::close(); + parent::tearDown(); + } + + public function testPushCommandReturnTrue() + { $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_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_finder = M::mock('Vinelab\Cdn\Contracts\FinderInterface'); + $this->m_finder->shouldReceive('read') + ->with($this->m_asset) + ->once() + ->andReturn(New Collection()); $this->m_provider = M::mock('Vinelab\Cdn\Providers\Provider'); - $this->m_provider->shouldReceive('upload')->once()->andReturn(true); + $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_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->m_helper->shouldReceive('getConfigurations') + ->once() + ->andReturn([]); $this->cdn = new \Vinelab\Cdn\Cdn( - $this->m_finder, - $this->m_asset, - $this->m_provider_factory, - $this->m_helper); + $this->m_finder, + $this->m_asset, + $this->m_provider_factory, + $this->m_helper); - } - public function tearDown() - { - M::close(); - parent::tearDown(); + $result = $this->cdn->push(); + + assertEquals($result, true); } - public function testPushingCommand() + public function testPushCommand() { - $result = $this->cdn->push(); + + $m_consol = M::mock('Symfony\Component\Console\Output\ConsoleOutput'); + $finder = new \Vinelab\Cdn\Finder($m_consol); + $asset = new \Vinelab\Cdn\Asset(); + $provider_factory = new \Vinelab\Cdn\ProviderFactory(); + $config = new \Illuminate\Config\Repository; + $helper = new \Vinelab\Cdn\CdnHelper($config); + + $cdn = new \Vinelab\Cdn\Cdn( $finder, + $asset, + $provider_factory, + $helper + ); + + $result = $cdn->push(); + assertEquals($result, true); } + } From 95415cd264980d8d57c00da9dd28286fb8d6cd5e Mon Sep 17 00:00:00 2001 From: mahmoud Date: Wed, 10 Sep 2014 12:32:38 +0300 Subject: [PATCH 3/9] move the AwsS3ProviderTest to a Providers folder to mirror the codebase directly --- tests/Vinelab/Cdn/{ => Providers}/AwsS3ProviderTest.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/Vinelab/Cdn/{ => Providers}/AwsS3ProviderTest.php (100%) diff --git a/tests/Vinelab/Cdn/AwsS3ProviderTest.php b/tests/Vinelab/Cdn/Providers/AwsS3ProviderTest.php similarity index 100% rename from tests/Vinelab/Cdn/AwsS3ProviderTest.php rename to tests/Vinelab/Cdn/Providers/AwsS3ProviderTest.php From 4c04c5d457cadee61f45d32711ac438d1722371f Mon Sep 17 00:00:00 2001 From: mahmoud Date: Wed, 10 Sep 2014 12:43:50 +0300 Subject: [PATCH 4/9] Rename all test functions. making them more descriptive of what is being tested. --- tests/Vinelab/Cdn/AssetTest.php | 2 +- tests/Vinelab/Cdn/CdnFacadeTest.php | 4 ++-- tests/Vinelab/Cdn/FinderTest.php | 4 ++-- tests/Vinelab/Cdn/ProviderFactoryTest.php | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Vinelab/Cdn/AssetTest.php b/tests/Vinelab/Cdn/AssetTest.php index 7e19cb2..eee62d9 100755 --- a/tests/Vinelab/Cdn/AssetTest.php +++ b/tests/Vinelab/Cdn/AssetTest.php @@ -18,7 +18,7 @@ public function tearDown() parent::tearDown(); } - public function testParseAndFillConfiguration() + public function testInitReturningAssetObject() { $dir = 'foo'; diff --git a/tests/Vinelab/Cdn/CdnFacadeTest.php b/tests/Vinelab/Cdn/CdnFacadeTest.php index edd5f12..927b365 100755 --- a/tests/Vinelab/Cdn/CdnFacadeTest.php +++ b/tests/Vinelab/Cdn/CdnFacadeTest.php @@ -31,7 +31,7 @@ public function tearDown() parent::tearDown(); } - public function testAssetUrlGenerator() + public function testAssetIsCallingUrlGenerator() { $this->provider->shouldReceive('urlGenerator') ->with($this->asset_path) @@ -47,7 +47,7 @@ public function testAssetUrlGenerator() /** * @expectedException \Vinelab\Cdn\Exceptions\EmptyPathException */ - public function testAssetWithEmptyParameter() + public function testAssetThrowsExceptionWhenEmptyParameter() { $this->facade->asset(null); } diff --git a/tests/Vinelab/Cdn/FinderTest.php b/tests/Vinelab/Cdn/FinderTest.php index 2a724d3..437667b 100755 --- a/tests/Vinelab/Cdn/FinderTest.php +++ b/tests/Vinelab/Cdn/FinderTest.php @@ -16,7 +16,7 @@ public function tearDown() parent::tearDown(); } - public function testReadingAllowedDirectories() + public function testReadReturnCorrectDataType() { $asset_holder = new \Vinelab\Cdn\Asset; @@ -43,7 +43,7 @@ public function testReadingAllowedDirectories() /** * @expectedException \InvalidArgumentException */ - public function testReadingAllowedDirectoriesWithEmptyAssetHolder() + public function testReadThrowsException() { $asset_holder = new \Vinelab\Cdn\Asset; diff --git a/tests/Vinelab/Cdn/ProviderFactoryTest.php b/tests/Vinelab/Cdn/ProviderFactoryTest.php index 8098bb7..acbf933 100755 --- a/tests/Vinelab/Cdn/ProviderFactoryTest.php +++ b/tests/Vinelab/Cdn/ProviderFactoryTest.php @@ -18,7 +18,7 @@ public function tearDown() parent::tearDown(); } - public function testAwsS3ProviderCreation() + public function testCreateReturnCorrectProviderObject() { $configurations = ['default' => 'aws.s3']; @@ -40,7 +40,7 @@ public function testAwsS3ProviderCreation() /** * @expectedException \Vinelab\Cdn\Exceptions\MissingConfigurationException */ - public function testMissingDefaultProviderConfiguration() + public function testCreateThrowsExceptionWhenMissingDefaultConfiguration() { $configurations = ['default' => '']; From 3e51d16b78d76b86f775b6b09207f82a9b7a1d37 Mon Sep 17 00:00:00 2001 From: mahmoud Date: Wed, 10 Sep 2014 13:53:24 +0300 Subject: [PATCH 5/9] add test for the private function cleanPath() in testCleanPathIsCleaning --- tests/TestCase.php | 19 +++++++++++++++++++ tests/Vinelab/Cdn/CdnFacadeTest.php | 10 ++++++++++ 2 files changed, 29 insertions(+) diff --git a/tests/TestCase.php b/tests/TestCase.php index 46cd541..26d6d06 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -23,4 +23,23 @@ public function tearDown() parent::tearDown(); } + + /** + * Call protected/private method of a class. + * + * @param object &$object Instantiated object that we will run method on. + * @param string $methodName Method name to call + * @param array $parameters Array of parameters to pass into method. + * + * @return mixed Method return. + */ + public function invokeMethod(&$object, $methodName, array $parameters = array()) + { + $reflection = new \ReflectionClass(get_class($object)); + $method = $reflection->getMethod($methodName); + $method->setAccessible(true); + + return $method->invokeArgs($object, $parameters); + } + } diff --git a/tests/Vinelab/Cdn/CdnFacadeTest.php b/tests/Vinelab/Cdn/CdnFacadeTest.php index 927b365..a59d464 100755 --- a/tests/Vinelab/Cdn/CdnFacadeTest.php +++ b/tests/Vinelab/Cdn/CdnFacadeTest.php @@ -44,6 +44,16 @@ public function testAssetIsCallingUrlGenerator() assertEquals($result, $this->cdn_url); } + public function testCleanPathIsCleaning() + { + $path = '/foo/bar/'; + $cleaned_path = 'foo/bar'; + // invoke the private function cleanPath() + $result = $this->invokeMethod($this->facade, 'cleanPath', array($path)); + assertEquals($result, $cleaned_path); + } + + /** * @expectedException \Vinelab\Cdn\Exceptions\EmptyPathException */ From c2d0e3825f3b34887ef704fe1ca6cbdf8cb8951e Mon Sep 17 00:00:00 2001 From: mahmoud Date: Wed, 10 Sep 2014 15:10:38 +0300 Subject: [PATCH 6/9] add to the phpunit.xml file to activate code coverage --- phpunit.xml | 9 ++++++++- tests/Vinelab/Cdn/CdnFacadeTest.php | 1 - 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 7c098b3..60a6355 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -15,4 +15,11 @@ ./tests/Vinelab - \ No newline at end of file + + + + + + + + diff --git a/tests/Vinelab/Cdn/CdnFacadeTest.php b/tests/Vinelab/Cdn/CdnFacadeTest.php index a59d464..b3b14ed 100755 --- a/tests/Vinelab/Cdn/CdnFacadeTest.php +++ b/tests/Vinelab/Cdn/CdnFacadeTest.php @@ -53,7 +53,6 @@ public function testCleanPathIsCleaning() assertEquals($result, $cleaned_path); } - /** * @expectedException \Vinelab\Cdn\Exceptions\EmptyPathException */ From ef2855a1c274f4d647e92e41d1cf4e0b60e54209 Mon Sep 17 00:00:00 2001 From: mahmoud Date: Fri, 12 Sep 2014 11:40:27 +0300 Subject: [PATCH 7/9] include illuminate/config package to be used in testing --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 8ff54e6..f208427 100755 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ "require": { "php": ">=5.4.0", "illuminate/support": "4.*", + "illuminate/config": "4.*", "symfony/finder": "2.5.*@dev", "symfony/console": "2.*", "guzzlehttp/guzzle": "~4.0", From 5feb4cc6ea931841c0a97304be9160e6a80f8d64 Mon Sep 17 00:00:00 2001 From: mahmoud Date: Fri, 12 Sep 2014 11:42:02 +0300 Subject: [PATCH 8/9] comment out the tag for faster testing to prevent generating the code coverage statistics after every test. it can be added when needed only --- phpunit.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 60a6355..2689f54 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -16,9 +16,9 @@ - - - + + + From 3f0566bf23318e8d8cfda58458949f15ea24d90b Mon Sep 17 00:00:00 2001 From: mahmoud Date: Fri, 12 Sep 2014 11:48:58 +0300 Subject: [PATCH 9/9] write an integration test for the Push function the push function is responsible for reading directories --- src/Vinelab/Cdn/Cdn.php | 8 ++-- src/Vinelab/Cdn/CdnHelper.php | 2 +- tests/Vinelab/Cdn/CdnTest.php | 89 +++++++++++++++++++++++++++++++++-- 3 files changed, 91 insertions(+), 8 deletions(-) diff --git a/src/Vinelab/Cdn/Cdn.php b/src/Vinelab/Cdn/Cdn.php index 4b4e871..a48337a 100755 --- a/src/Vinelab/Cdn/Cdn.php +++ b/src/Vinelab/Cdn/Cdn.php @@ -40,10 +40,10 @@ class Cdn implements CdnInterface{ * @internal param \Vinelab\Cdn\Repository $configurations */ public function __construct( - FinderInterface $finder, - AssetInterface $asset_holder, - ProviderFactoryInterface $provider_factory, - CdnHelperInterface $helper + FinderInterface $finder, + AssetInterface $asset_holder, + ProviderFactoryInterface $provider_factory, + CdnHelperInterface $helper ) { $this->finder = $finder; $this->asset_holder = $asset_holder; diff --git a/src/Vinelab/Cdn/CdnHelper.php b/src/Vinelab/Cdn/CdnHelper.php index 00f3c5d..dfcc40a 100644 --- a/src/Vinelab/Cdn/CdnHelper.php +++ b/src/Vinelab/Cdn/CdnHelper.php @@ -44,7 +44,7 @@ public function getConfigurations() $configurations = $this->configurations->get('cdn::cdn'); if ( ! $configurations) { - throw new MissingConfigurationFileException('CDN Configurations file not found'); + throw new MissingConfigurationFileException("CDN 'config file' (cdn.php) not found"); } return $configurations; diff --git a/tests/Vinelab/Cdn/CdnTest.php b/tests/Vinelab/Cdn/CdnTest.php index 49ccc63..a98aebd 100755 --- a/tests/Vinelab/Cdn/CdnTest.php +++ b/tests/Vinelab/Cdn/CdnTest.php @@ -10,7 +10,6 @@ public function setUp() parent::setUp(); $this->m_spl_file_info = M::mock('Symfony\Component\Finder\SplFileInfo'); - } public function tearDown() @@ -65,15 +64,99 @@ public function testPushCommandReturnTrue() assertEquals($result, true); } + /** + * Integration Test + */ public function testPushCommand() { + $configuration_file = [ + 'default' => 'aws.s3', + 'url' => 'https://s3.amazonaws.com', + 'threshold' => 10, + 'providers' => [ + 'aws' => [ + 's3' => [ + 'credentials' => [ + 'key' => 'keeeeeeeeeeeeeeeeeeeeeeey', + 'secret' => 'ssssssssccccccccccctttttt', + ], + 'buckets' => [ + 'bbbuuuucccctttt' => '*', + ], + 'acl' => 'public-read' + ], + ], + ], + 'include' => [ + 'directories' => [__DIR__], + 'extensions' => [], + 'patterns' => [], + ], + 'exclude' => [ + 'directories' => [], + 'files' => [], + 'extensions' => [], + 'patterns' => [], + 'hidden' => true, + ], + ]; $m_consol = M::mock('Symfony\Component\Console\Output\ConsoleOutput'); + $m_consol->shouldReceive('writeln') + ->atLeast(1); + $finder = new \Vinelab\Cdn\Finder($m_consol); + $asset = new \Vinelab\Cdn\Asset(); + $provider_factory = new \Vinelab\Cdn\ProviderFactory(); - $config = new \Illuminate\Config\Repository; - $helper = new \Vinelab\Cdn\CdnHelper($config); + + $m_config = M::mock('Illuminate\Config\Repository'); + $m_config->shouldReceive('get') + ->with('cdn::cdn') + ->once() + ->andReturn($configuration_file); + + $helper = new \Vinelab\Cdn\CdnHelper($m_config); + + $m_console = M::mock('Symfony\Component\Console\Output\ConsoleOutput'); + $m_console->shouldReceive('writeln') + ->atLeast(2); + + $m_validator = M::mock('Vinelab\Cdn\Validators\Contracts\ProviderValidatorInterface'); + $m_validator->shouldReceive('validate'); + + $m_helper = M::mock('Vinelab\Cdn\CdnHelper'); + + $m_spl_file = M::mock('Symfony\Component\Finder\SplFileInfo'); + $m_spl_file->shouldReceive('getPathname') + ->andReturn('vinelab/cdn/tests/Vinelab/Cdn/AwsS3ProviderTest.php'); + $m_spl_file->shouldReceive('getRealPath') + ->andReturn(__DIR__ . '/AwsS3ProviderTest.php'); + + $p_aws_s3_provider = M::mock('\Vinelab\Cdn\Providers\AwsS3Provider[connect]', array + ($m_console, $m_validator, $m_helper)); + + $m_s3 = M::mock('Aws\S3\S3Client'); + $m_s3->shouldReceive('factory') + ->andReturn('Aws\S3\S3Client'); + $m_s3->shouldReceive('getCommand'); + $p_aws_s3_provider->setS3Client($m_s3); + + $m_batch = M::mock('Guzzle\Batch\BatchBuilder'); + $m_batch->shouldReceive('factory') + ->andReturn('Guzzle\Batch\BatchBuilder'); + $m_batch->shouldReceive('add'); + $m_batch->shouldReceive('getHistory') + ->andReturn(null); + $p_aws_s3_provider->setBatchBuilder($m_batch); + + $p_aws_s3_provider->shouldReceive('connect') + ->andReturn(true); + + \Illuminate\Support\Facades\App::shouldReceive('make') + ->once() + ->andReturn($p_aws_s3_provider); $cdn = new \Vinelab\Cdn\Cdn( $finder, $asset,