Skip to content

Commit

Permalink
Merge pull request #67 from nlegoff/fix-strategy
Browse files Browse the repository at this point in the history
Fix strategy implementation
  • Loading branch information
nlegoff committed Dec 10, 2014
2 parents 28dc545 + e5fe6e9 commit f903a84
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 91 deletions.
49 changes: 49 additions & 0 deletions src/Alchemy/Zippy/FileStrategy/AbstractFileStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Alchemy\Zippy\FileStrategy;

use Alchemy\Zippy\Adapter\AdapterContainer;
use Alchemy\Zippy\Exception\RuntimeException;

abstract class AbstractFileStrategy implements FileStrategyInterface
{
protected $container;

public function __construct(AdapterContainer $container)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function getAdapters()
{
$services = array();
foreach ($this->getServiceNames() as $serviceName) {
try {
$services[] = $this->container[$serviceName];
} catch (RuntimeException $e) {

}
}

return $services;
}

/**
* Returns an array of service names that defines adapters.
*
* @return string[]
*/
abstract protected function getServiceNames();
}
17 changes: 4 additions & 13 deletions src/Alchemy/Zippy/FileStrategy/TB2FileStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@

namespace Alchemy\Zippy\FileStrategy;

use Alchemy\Zippy\Adapter\AdapterContainer;

class TB2FileStrategy implements FileStrategyInterface
class TB2FileStrategy extends AbstractFileStrategy
{
private $container;

public function __construct(AdapterContainer $container)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function getAdapters()
protected function getServiceNames()
{
return array(
$this->container['Alchemy\\Zippy\\Adapter\\GNUTar\\TarBz2GNUTarAdapter'],
$this->container['Alchemy\\Zippy\\Adapter\\BSDTar\\TarBz2BSDTarAdapter'],
'Alchemy\\Zippy\\Adapter\\GNUTar\\TarBz2GNUTarAdapter',
'Alchemy\\Zippy\\Adapter\\BSDTar\\TarBz2BSDTarAdapter'
);
}

Expand Down
17 changes: 4 additions & 13 deletions src/Alchemy/Zippy/FileStrategy/TBz2FileStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@

namespace Alchemy\Zippy\FileStrategy;

use Alchemy\Zippy\Adapter\AdapterContainer;

class TBz2FileStrategy implements FileStrategyInterface
class TBz2FileStrategy extends AbstractFileStrategy
{
private $container;

public function __construct(AdapterContainer $container)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function getAdapters()
protected function getServiceNames()
{
return array(
$this->container['Alchemy\\Zippy\\Adapter\\GNUTar\\TarBz2GNUTarAdapter'],
$this->container['Alchemy\\Zippy\\Adapter\\BSDTar\\TarBz2BSDTarAdapter'],
'Alchemy\\Zippy\\Adapter\\GNUTar\\TarBz2GNUTarAdapter',
'Alchemy\\Zippy\\Adapter\\BSDTar\\TarBz2BSDTarAdapter'
);
}

Expand Down
17 changes: 4 additions & 13 deletions src/Alchemy/Zippy/FileStrategy/TGzFileStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@

namespace Alchemy\Zippy\FileStrategy;

use Alchemy\Zippy\Adapter\AdapterContainer;

class TGzFileStrategy implements FileStrategyInterface
class TGzFileStrategy extends AbstractFileStrategy
{
private $container;

public function __construct(AdapterContainer $container)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function getAdapters()
protected function getServiceNames()
{
return array(
$this->container['Alchemy\\Zippy\\Adapter\\GNUTar\\TarGzGNUTarAdapter'],
$this->container['Alchemy\\Zippy\\Adapter\\BSDTar\\TarGzBSDTarAdapter'],
'Alchemy\\Zippy\\Adapter\\GNUTar\\TarGzGNUTarAdapter',
'Alchemy\\Zippy\\Adapter\\BSDTar\\TarGzBSDTarAdapter'
);
}

Expand Down
17 changes: 4 additions & 13 deletions src/Alchemy/Zippy/FileStrategy/TarBz2FileStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@

namespace Alchemy\Zippy\FileStrategy;

use Alchemy\Zippy\Adapter\AdapterContainer;

class TarBz2FileStrategy implements FileStrategyInterface
class TarBz2FileStrategy extends AbstractFileStrategy
{
private $container;

public function __construct(AdapterContainer $container)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function getAdapters()
protected function getServiceNames()
{
return array(
$this->container['Alchemy\\Zippy\\Adapter\\GNUTar\\TarBz2GNUTarAdapter'],
$this->container['Alchemy\\Zippy\\Adapter\\BSDTar\\TarBz2BSDTarAdapter'],
'Alchemy\\Zippy\\Adapter\\GNUTar\\TarBz2GNUTarAdapter',
'Alchemy\\Zippy\\Adapter\\BSDTar\\TarBz2BSDTarAdapter'
);
}

Expand Down
17 changes: 4 additions & 13 deletions src/Alchemy/Zippy/FileStrategy/TarFileStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@

namespace Alchemy\Zippy\FileStrategy;

use Alchemy\Zippy\Adapter\AdapterContainer;

class TarFileStrategy implements FileStrategyInterface
class TarFileStrategy extends AbstractFileStrategy
{
private $container;

public function __construct(AdapterContainer $container)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function getAdapters()
protected function getServiceNames()
{
return array(
$this->container['Alchemy\\Zippy\\Adapter\\GNUTar\\TarGNUTarAdapter'],
$this->container['Alchemy\\Zippy\\Adapter\\BSDTar\\TarBSDTarAdapter'],
'Alchemy\\Zippy\\Adapter\\GNUTar\\TarGNUTarAdapter',
'Alchemy\\Zippy\\Adapter\\BSDTar\\TarBSDTarAdapter'
);
}

Expand Down
17 changes: 4 additions & 13 deletions src/Alchemy/Zippy/FileStrategy/TarGzFileStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@

namespace Alchemy\Zippy\FileStrategy;

use Alchemy\Zippy\Adapter\AdapterContainer;

class TarGzFileStrategy implements FileStrategyInterface
class TarGzFileStrategy extends AbstractFileStrategy
{
private $container;

public function __construct(AdapterContainer $container)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function getAdapters()
protected function getServiceNames()
{
return array(
$this->container['Alchemy\\Zippy\\Adapter\\GNUTar\\TarGzGNUTarAdapter'],
$this->container['Alchemy\\Zippy\\Adapter\\BSDTar\\TarGzBSDTarAdapter'],
'Alchemy\\Zippy\\Adapter\\GNUTar\\TarGzGNUTarAdapter',
'Alchemy\\Zippy\\Adapter\\BSDTar\\TarGzBSDTarAdapter'
);
}

Expand Down
17 changes: 4 additions & 13 deletions src/Alchemy/Zippy/FileStrategy/ZipFileStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@

namespace Alchemy\Zippy\FileStrategy;

use Alchemy\Zippy\Adapter\AdapterContainer;

class ZipFileStrategy implements FileStrategyInterface
class ZipFileStrategy extends AbstractFileStrategy
{
private $container;

public function __construct(AdapterContainer $container)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function getAdapters()
protected function getServiceNames()
{
return array(
$this->container['Alchemy\\Zippy\\Adapter\\ZipAdapter'],
$this->container['Alchemy\\Zippy\\Adapter\\ZipExtensionAdapter'],
'Alchemy\\Zippy\\Adapter\\ZipAdapter',
'Alchemy\\Zippy\\Adapter\\ZipExtensionAdapter'
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Alchemy\Zippy\Tests\FileStrategy;

use Alchemy\Zippy\Adapter\AdapterContainer;
use Alchemy\Zippy\Tests\TestCase;
use Alchemy\Zippy\Exception\RuntimeException;

class AbstractFileStrategyTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
*/
public function testGetAdaptersWithNoDefinedServices()
{
$container = AdapterContainer::load();

$stub = $this->getMockForAbstractClass('Alchemy\Zippy\FileStrategy\AbstractFileStrategy', array($container));
$stub->expects($this->any())
->method('getServiceNames')
->will($this->returnValue(array(
'Unknown\Services'
)));


$adapters = $stub->getAdapters();
$this->assertInternalType('array', $adapters);
$this->assertCount(0, $adapters);
}

public function testGetAdapters()
{
$container = AdapterContainer::load();

$stub = $this->getMockForAbstractClass('Alchemy\Zippy\FileStrategy\AbstractFileStrategy', array($container));
$stub->expects($this->any())
->method('getServiceNames')
->will($this->returnValue(array(
'Alchemy\\Zippy\\Adapter\\ZipAdapter',
'Alchemy\\Zippy\\Adapter\\ZipExtensionAdapter'
)));

$adapters = $stub->getAdapters();
$this->assertInternalType('array', $adapters);
$this->assertCount(2, $adapters);
foreach ($adapters as $adapter) {
$this->assertInstanceOf('Alchemy\\Zippy\\Adapter\\AdapterInterface', $adapter);
}
}

public function testGetAdaptersWithAdapterThatRaiseAnException()
{
$adapterMock = $this->getMock('Alchemy\Zippy\Adapter\AdapterInterface');
$container = $this->getMock('Alchemy\Zippy\Adapter\AdapterContainer');
$container
->expects($this->at(0))
->method('offsetGet')
->with($this->equalTo('Alchemy\\Zippy\\Adapter\\ZipAdapter'))
->will($this->returnValue($adapterMock));

$container
->expects($this->at(1))
->method('offsetGet')
->with($this->equalTo('Alchemy\\Zippy\\Adapter\\ZipExtensionAdapter'))
->will($this->throwException(new RuntimeException()));

$stub = $this->getMockForAbstractClass('Alchemy\Zippy\FileStrategy\AbstractFileStrategy', array($container));
$stub->expects($this->any())
->method('getServiceNames')
->will($this->returnValue(array(
'Alchemy\\Zippy\\Adapter\\ZipAdapter',
'Alchemy\\Zippy\\Adapter\\ZipExtensionAdapter'
)));

$adapters = $stub->getAdapters();
$this->assertInternalType('array', $adapters);
$this->assertCount(1, $adapters);
foreach ($adapters as $adapter) {
$this->assertSame($adapterMock, $adapter);
}
}
}
19 changes: 19 additions & 0 deletions tests/Alchemy/Zippy/Tests/FileStrategy/FileStrategyTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Alchemy\Zippy\Tests\FileStrategy;

use Alchemy\Zippy\Adapter\AdapterInterface;
use Alchemy\Zippy\Exception\RuntimeException;
use Alchemy\Zippy\Tests\TestCase;
use Alchemy\Zippy\FileStrategy\FileStrategyInterface;

Expand Down Expand Up @@ -55,6 +56,24 @@ public function getAdaptersShouldReturnAnArrayOfAdapter()
}
}

/** @test */
public function getAdaptersShouldReturnAnArrayOfAdapterEvenIfAdapterRaiseAnException()
{
$container = $this->getMock('Alchemy\Zippy\Adapter\AdapterContainer');
$container
->expects($this->any())
->method('offsetGet')
->will($this->throwException(new RuntimeException()));

$adapters = $this->getStrategy($container)->getAdapters();

$this->assertInternalType('array', $adapters);

foreach ($adapters as $adapter) {
$this->assertInstanceOf('Alchemy\\Zippy\\Adapter\\AdapterInterface', $adapter);
}
}

/**
* @return FileStrategyInterface
*/
Expand Down

0 comments on commit f903a84

Please sign in to comment.