-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from nlegoff/fix-strategy
Fix strategy implementation
- Loading branch information
Showing
10 changed files
with
178 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
tests/Alchemy/Zippy/Tests/FileStrategy/AbstractFileStrategyTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters