123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <?php
- namespace Alchemy\Zippy\Tests\Adapter;
- use Alchemy\Zippy\Adapter\ZipAdapter;
- use Alchemy\Zippy\Tests\TestCase;
- use Alchemy\Zippy\Parser\ParserFactory;
- class ZipAdapterTest extends TestCase
- {
- protected static $zipFile;
- /**
- * @var ZipAdapter
- */
- protected $adapter;
- public static function setUpBeforeClass()
- {
- self::$zipFile = sprintf('%s/%s.zip', self::getResourcesPath(), ZipAdapter::getName());
- if (file_exists(self::$zipFile)) {
- unlink(self::$zipFile);
- }
- }
- public static function tearDownAfterClass()
- {
- if (file_exists(self::$zipFile)) {
- unlink(self::$zipFile);
- }
- }
- public function setUp()
- {
- $inflator = $deflator = $this->getMockBuilder('Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactory')
- ->disableOriginalConstructor()
- ->setMethods(array('useBinary'))
- ->getMock();
- $outputParser = ParserFactory::create(ZipAdapter::getName());
- $this->adapter = new ZipAdapter($outputParser, $this->getResourceManagerMock(), $inflator, $deflator);
- }
- /**
- * @expectedException Alchemy\Zippy\Exception\NotSupportedException
- */
- public function testCreateNoFiles()
- {
- $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
- $this->adapter->setInflator($this->getZippyMockBuilder($mockProcessBuilder));
- $this->adapter->create(self::$zipFile, array());
- }
- public function testCreate()
- {
- $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
- $mockProcessBuilder
- ->expects($this->at(0))
- ->method('add')
- ->with($this->equalTo('-r'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(1))
- ->method('add')
- ->with($this->equalTo(self::$zipFile))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(2))
- ->method('setWorkingDirectory')
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(3))
- ->method('add')
- ->with($this->equalTo('lalala'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->once())
- ->method('getProcess')
- ->will($this->returnValue($this->getSuccessFullMockProcess()));
- $manager = $this->getResourceManagerMock(__DIR__, array('lalala'));
- $outputParser = ParserFactory::create(ZipAdapter::getName());
- $deflator = $this->getMockBuilder('Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactory')
- ->disableOriginalConstructor()
- ->setMethods(array('useBinary'))
- ->getMock();
- $this->adapter = new ZipAdapter($outputParser, $manager, $this->getZippyMockBuilder($mockProcessBuilder), $deflator);
- $this->adapter->create(self::$zipFile, array(__FILE__));
- return self::$zipFile;
- }
- /**
- * @depends testCreate
- */
- public function testOpen($zipFile)
- {
- $archive = $this->adapter->open($this->getResource($zipFile));
- $this->assertInstanceOf('Alchemy\Zippy\Archive\ArchiveInterface', $archive);
- }
- public function testListMembers()
- {
- $resource = $this->getResource(self::$zipFile);
- $archive = $this->adapter->open($resource);
- $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
- $mockProcessBuilder
- ->expects($this->at(0))
- ->method('add')
- ->with($this->equalTo('-l'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(1))
- ->method('add')
- ->with($this->equalTo($resource->getResource()))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->once())
- ->method('getProcess')
- ->will($this->returnValue($this->getSuccessFullMockProcess()));
- $this->adapter->setDeflator($this->getZippyMockBuilder($mockProcessBuilder));
- $this->adapter->listMembers($resource);
- }
- public function testAddFile()
- {
- $resource = $this->getResource(self::$zipFile);
- $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
- $mockProcessBuilder
- ->expects($this->at(0))
- ->method('add')
- ->with($this->equalTo('-r'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(1))
- ->method('add')
- ->with($this->equalTo('-u'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(2))
- ->method('add')
- ->with($this->equalTo($resource->getResource()))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->once())
- ->method('getProcess')
- ->will($this->returnValue($this->getSuccessFullMockProcess()));
- $this->adapter->setInflator($this->getZippyMockBuilder($mockProcessBuilder));
- $this->adapter->add($resource, array(__DIR__ . '/../TestCase.php'));
- }
- public function testgetInflatorVersion()
- {
- $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
- $mockProcessBuilder
- ->expects($this->at(0))
- ->method('add')
- ->with($this->equalTo('-h'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->once())
- ->method('getProcess')
- ->will($this->returnValue($this->getSuccessFullMockProcess()));
- $this->adapter->setParser($this->getMock('Alchemy\Zippy\Parser\ParserInterface'));
- $this->adapter->setInflator($this->getZippyMockBuilder($mockProcessBuilder));
- $this->adapter->getInflatorVersion();
- }
- public function testgetDeflatorVersion()
- {
- $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
- $mockProcessBuilder
- ->expects($this->at(0))
- ->method('add')
- ->with($this->equalTo('-h'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->once())
- ->method('getProcess')
- ->will($this->returnValue($this->getSuccessFullMockProcess()));
- $this->adapter->setParser($this->getMock('Alchemy\Zippy\Parser\ParserInterface'));
- $this->adapter->setDeflator($this->getZippyMockBuilder($mockProcessBuilder));
- $this->adapter->getDeflatorVersion();
- }
- public function testRemoveMembers()
- {
- $resource = $this->getResource(self::$zipFile);
- $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
- $mockProcessBuilder
- ->expects($this->at(0))
- ->method('add')
- ->with($this->equalTo('-d'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(1))
- ->method('add')
- ->with($this->equalTo($resource->getResource()))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(2))
- ->method('add')
- ->with($this->equalTo(__DIR__ . '/../TestCase.php'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(3))
- ->method('add')
- ->with($this->equalTo('path-to-file'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->once())
- ->method('getProcess')
- ->will($this->returnValue($this->getSuccessFullMockProcess()));
- $archiveFileMock = $this->getMock('Alchemy\Zippy\Archive\MemberInterface');
- $archiveFileMock
- ->expects($this->any())
- ->method('getLocation')
- ->will($this->returnValue('path-to-file'));
- $this->adapter->setInflator($this->getZippyMockBuilder($mockProcessBuilder));
- $this->adapter->remove($resource, array(
- __DIR__ . '/../TestCase.php',
- $archiveFileMock
- ));
- }
- public function testExtract()
- {
- $resource = $this->getResource(self::$zipFile);
- $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
- $mockProcessBuilder
- ->expects($this->at(0))
- ->method('add')
- ->with($this->equalTo('-o'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(1))
- ->method('add')
- ->with($this->equalTo($resource->getResource()))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->once())
- ->method('getProcess')
- ->will($this->returnValue($this->getSuccessFullMockProcess()));
- $this->adapter->setDeflator($this->getZippyMockBuilder($mockProcessBuilder));
- $dir = $this->adapter->extract($resource);
- $pathinfo = pathinfo(self::$zipFile);
- $this->assertEquals($pathinfo['dirname'], $dir->getPath());
- }
- public function testExtractWithExtractDirPrecised()
- {
- $resource = $this->getResource(self::$zipFile);
- $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
- $mockProcessBuilder
- ->expects($this->at(0))
- ->method('add')
- ->with($this->equalTo($resource->getResource()))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(1))
- ->method('add')
- ->with($this->equalTo('-d'))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(2))
- ->method('add')
- ->with($this->equalTo(__DIR__))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->at(3))
- ->method('add')
- ->with($this->equalTo(__FILE__))
- ->will($this->returnSelf());
- $mockProcessBuilder
- ->expects($this->once())
- ->method('getProcess')
- ->will($this->returnValue($this->getSuccessFullMockProcess()));
- $this->adapter->setDeflator($this->getZippyMockBuilder($mockProcessBuilder));
- $this->adapter->extractMembers($resource, array(__FILE__), __DIR__);
- }
- public function testGetName()
- {
- $this->assertEquals('zip', ZipAdapter::getName());
- }
- public function testGetDefaultInflatorBinaryName()
- {
- $this->assertEquals(array('zip'), ZipAdapter::getDefaultInflatorBinaryName());
- }
- public function testGetDefaultDeflatorBinaryName()
- {
- $this->assertEquals(array('unzip'), ZipAdapter::getDefaultDeflatorBinaryName());
- }
- private function getSuccessFullMockProcess()
- {
- $mockProcess = $this
- ->getMockBuilder('Symfony\Component\Process\Process')
- ->disableOriginalConstructor()
- ->getMock();
- $mockProcess
- ->expects($this->once())
- ->method('run');
- $mockProcess
- ->expects($this->once())
- ->method('isSuccessful')
- ->will($this->returnValue(true));
- return $mockProcess;
- }
- private function getZippyMockBuilder($mockedProcessBuilder)
- {
- $mockBuilder = $this->getMock('Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface');
- $mockBuilder
- ->expects($this->once())
- ->method('create')
- ->will($this->returnValue($mockedProcessBuilder));
- return $mockBuilder;
- }
- }
|