Skip to content

Commit

Permalink
Merge pull request #18 from Vinelab/update/general
Browse files Browse the repository at this point in the history
Generic updates with better explanation and tests.
  • Loading branch information
Mulkave committed Sep 19, 2014
2 parents ecb7045 + 3f0566b commit 5463810
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 40 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 8 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@
<directory suffix=".php">./tests/Vinelab</directory>
</testsuite>
</testsuites>
</phpunit>

<!--<logging>-->
<!--<log type="coverage-html" target="./CodeCoverage/"/>-->
<!--</logging>-->

</phpunit>


10 changes: 2 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/Vinelab/Cdn/Cdn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Vinelab/Cdn/CdnHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
2 changes: 1 addition & 1 deletion tests/Vinelab/Cdn/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function tearDown()
parent::tearDown();
}

public function testParseAndFillConfiguration()
public function testInitReturningAssetObject()
{
$dir = 'foo';

Expand Down
13 changes: 11 additions & 2 deletions tests/Vinelab/Cdn/CdnFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function tearDown()
parent::tearDown();
}

public function testAssetUrlGenerator()
public function testAssetIsCallingUrlGenerator()
{
$this->provider->shouldReceive('urlGenerator')
->with($this->asset_path)
Expand All @@ -44,10 +44,19 @@ public function testAssetUrlGenerator()
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
*/
public function testAssetWithEmptyParameter()
public function testAssetThrowsExceptionWhenEmptyParameter()
{
$this->facade->asset(null);
}
Expand Down
161 changes: 142 additions & 19 deletions tests/Vinelab/Cdn/CdnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,165 @@ 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()
/**
* Integration Test
*/
public function testPushCommand()
{
$result = $this->cdn->push();
$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();

$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,
$provider_factory,
$helper
);

$result = $cdn->push();

assertEquals($result, true);
}


}
4 changes: 2 additions & 2 deletions tests/Vinelab/Cdn/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function tearDown()
parent::tearDown();
}

public function testReadingAllowedDirectories()
public function testReadReturnCorrectDataType()
{
$asset_holder = new \Vinelab\Cdn\Asset;

Expand All @@ -43,7 +43,7 @@ public function testReadingAllowedDirectories()
/**
* @expectedException \InvalidArgumentException
*/
public function testReadingAllowedDirectoriesWithEmptyAssetHolder()
public function testReadThrowsException()
{
$asset_holder = new \Vinelab\Cdn\Asset;

Expand Down
4 changes: 2 additions & 2 deletions tests/Vinelab/Cdn/ProviderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function tearDown()
parent::tearDown();
}

public function testAwsS3ProviderCreation()
public function testCreateReturnCorrectProviderObject()
{
$configurations = ['default' => 'aws.s3'];

Expand All @@ -40,7 +40,7 @@ public function testAwsS3ProviderCreation()
/**
* @expectedException \Vinelab\Cdn\Exceptions\MissingConfigurationException
*/
public function testMissingDefaultProviderConfiguration()
public function testCreateThrowsExceptionWhenMissingDefaultConfiguration()
{
$configurations = ['default' => ''];

Expand Down
File renamed without changes.

0 comments on commit 5463810

Please sign in to comment.