123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace Alchemy\Zippy\Tests;
- use Alchemy\Zippy\Zippy;
- use Alchemy\Zippy\Exception\NoAdapterOnPlatformException;
- use Alchemy\Zippy\Exception\FormatNotSupportedException;
- use Alchemy\Zippy\Exception\RuntimeException;
- class ZippyTest extends TestCase
- {
- /** @test */
- public function itShouldCreateAnArchive()
- {
- $filename = 'file.zippo';
- $fileToAdd = 'file1';
- $recursive = true;
- $adapter = $this->getSupportedAdapter();
- $adapter->expects($this->once())
- ->method('create')
- ->with($this->equalTo($filename), $this->equalTo($fileToAdd), $this->equalTo($recursive));
- $adapters = array($adapter);
- $strategy = $this->getStrategy('zippo', $adapters);
- $zippy = new Zippy($this->getContainer());
- $zippy->addStrategy($strategy);
- $zippy->create($filename, $fileToAdd, $recursive);
- }
- /** @test */
- public function itShouldCreateAnArchiveByForcingType()
- {
- $filename = 'file';
- $fileToAdd = 'file1';
- $recursive = true;
- $adapter = $this->getSupportedAdapter();
- $adapter->expects($this->once())
- ->method('create')
- ->with($this->equalTo($filename), $this->equalTo($fileToAdd), $this->equalTo($recursive));
- $adapters = array($adapter);
- $strategy = $this->getStrategy('zippo', $adapters);
- $zippy = new Zippy($this->getContainer());
- $zippy->addStrategy($strategy);
- $zippy->create($filename, $fileToAdd, $recursive, 'zippo');
- }
- /** @test */
- public function itShouldNotCreateAndThrowAnException()
- {
- $filename = 'file';
- $fileToAdd = 'file1';
- $recursive = true;
- $adapter = $this->getSupportedAdapter();
- $adapter->expects($this->never())->method('create');
- $adapters = array($adapter);
- $strategy = $this->getStrategy('zippo', $adapters);
- $zippy = new Zippy($this->getContainer());
- $zippy->addStrategy($strategy);
- try {
- $zippy->create($filename, $fileToAdd, $recursive, 'zippotte');
- $this->fail('Should have raised an exception');
- } catch (RuntimeException $e) {
- }
- }
- /** @test */
- public function itShouldOpenAnArchive()
- {
- $filename = 'file.zippo';
- $adapter = $this->getSupportedAdapter();
- $adapter->expects($this->once())
- ->method('open')
- ->with($this->equalTo($filename));
- $adapters = array($adapter);
- $strategy = $this->getStrategy('zippo', $adapters);
- $zippy = new Zippy($this->getContainer());
- $zippy->addStrategy($strategy);
- $zippy->open($filename);
- }
- /** @test */
- public function itShouldExposeContainerPassedOnConstructor()
- {
- $container = $this->getContainer();
- $zippy = new Zippy($container);
- $this->assertEquals($container, $zippy->adapters);
- }
- /** @test */
- public function itShouldRegisterStrategies()
- {
- $adapters = array($this->getSupportedAdapter());
- $strategy = $this->getStrategy('zippo', $adapters);
- $zippy = new Zippy($this->getContainer());
- $zippy->addStrategy($strategy);
- $this->assertEquals(array('zippo' => array($strategy)), $zippy->getStrategies());
- }
- /** @test */
- public function registerTwoStrategiesWithSameExtensionShouldBeinRightOrder()
- {
- $adapters1 = array($this->getSupportedAdapter());
- $strategy1 = $this->getStrategy('zippo', $adapters1);
- $adapters2 = array($this->getSupportedAdapter());
- $strategy2 = $this->getStrategy('zippo', $adapters2);
- $zippy = new Zippy($this->getContainer());
- $zippy->addStrategy($strategy1);
- $zippy->addStrategy($strategy2);
- $this->assertEquals(array('zippo' => array($strategy2, $strategy1)), $zippy->getStrategies());
- }
- /** @test */
- public function registerAStrategyTwiceShouldMoveItToLastAdded()
- {
- $adapters1 = array($this->getSupportedAdapter());
- $strategy1 = $this->getStrategy('zippo', $adapters1);
- $adapters2 = array($this->getSupportedAdapter());
- $strategy2 = $this->getStrategy('zippo', $adapters2);
- $zippy = new Zippy($this->getContainer());
- $zippy->addStrategy($strategy1);
- $zippy->addStrategy($strategy2);
- $zippy->addStrategy($strategy1);
- $this->assertEquals(array('zippo' => array($strategy1, $strategy2)), $zippy->getStrategies());
- }
- /** @test */
- public function itShouldReturnAnAdapterCorrespondingToTheRightStrategy()
- {
- $adapters = array($this->getSupportedAdapter());
- $strategy = $this->getStrategy('zippo', $adapters);
- $zippy = new Zippy($this->getContainer());
- $zippy->addStrategy($strategy);
- $this->assertEquals($adapters[0], $zippy->getAdapterFor('zippo'));
- $this->assertEquals($adapters[0], $zippy->getAdapterFor('.zippo'));
- $this->assertEquals($adapters[0], $zippy->getAdapterFor('ziPPo'));
- $this->assertEquals($adapters[0], $zippy->getAdapterFor('.ZIPPO'));
- }
- /** @test */
- public function itShouldThrowAnExceptionIfNoAdapterSupported()
- {
- $adapters = array($this->getNotSupportedAdapter());
- $strategy = $this->getStrategy('zippo', $adapters);
- $zippy = new Zippy($this->getContainer());
- $zippy->addStrategy($strategy);
- try {
- $zippy->getAdapterFor('zippo');
- $this->fail('Should have raised an exception');
- } catch (NoAdapterOnPlatformException $e) {
- }
- }
- /** @test */
- public function itShouldThrowAnExceptionIfFormatNotSupported()
- {
- $adapters = array($this->getSupportedAdapter());
- $strategy = $this->getStrategy('zippotte', $adapters);
- $zippy = new Zippy($this->getContainer());
- $zippy->addStrategy($strategy);
- try {
- $zippy->getAdapterFor('zippo');
- $this->fail('Should have raised an exception');
- } catch (FormatNotSupportedException $e) {
- }
- }
- /** @test */
- public function loadShouldRegisterStrategies()
- {
- $zippy = Zippy::load();
- $this->assertCount(7, $zippy->getStrategies());
- $this->assertArrayHasKey('zip', $zippy->getStrategies());
- $this->assertArrayHasKey('tar', $zippy->getStrategies());
- $this->assertArrayHasKey('tar.gz', $zippy->getStrategies());
- $this->assertArrayHasKey('tar.bz2', $zippy->getStrategies());
- $this->assertArrayHasKey('tbz2', $zippy->getStrategies());
- $this->assertArrayHasKey('tb2', $zippy->getStrategies());
- $this->assertArrayHasKey('tgz', $zippy->getStrategies());
- }
- private function getStrategy($extension, $adapters)
- {
- $strategy = $this->getMock('Alchemy\Zippy\FileStrategy\FileStrategyInterface');
- $strategy->expects($this->any())
- ->method('getFileExtension')
- ->will($this->returnValue($extension));
- $strategy->expects($this->any())
- ->method('getAdapters')
- ->will($this->returnValue($adapters));
- return $strategy;
- }
- private function getSupportedAdapter()
- {
- $adapter = $this->getMock('Alchemy\Zippy\Adapter\AdapterInterface');
- $adapter->expects($this->any())
- ->method('isSupported')
- ->will($this->returnValue(true));
- return $adapter;
- }
- private function getNotSupportedAdapter()
- {
- $adapter = $this->getMock('Alchemy\Zippy\Adapter\AdapterInterface');
- $adapter->expects($this->any())
- ->method('isSupported')
- ->will($this->returnValue(false));
- return $adapter;
- }
- private function getContainer()
- {
- return $this->getMock('Alchemy\Zippy\Adapter\AdapterContainer');
- }
- }
|