ZipAdapterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. namespace Alchemy\Zippy\Tests\Adapter;
  3. use Alchemy\Zippy\Adapter\ZipAdapter;
  4. use Alchemy\Zippy\Tests\TestCase;
  5. use Alchemy\Zippy\Parser\ParserFactory;
  6. class ZipAdapterTest extends TestCase
  7. {
  8. protected static $zipFile;
  9. /**
  10. * @var ZipAdapter
  11. */
  12. protected $adapter;
  13. public static function setUpBeforeClass()
  14. {
  15. self::$zipFile = sprintf('%s/%s.zip', self::getResourcesPath(), ZipAdapter::getName());
  16. if (file_exists(self::$zipFile)) {
  17. unlink(self::$zipFile);
  18. }
  19. }
  20. public static function tearDownAfterClass()
  21. {
  22. if (file_exists(self::$zipFile)) {
  23. unlink(self::$zipFile);
  24. }
  25. }
  26. public function setUp()
  27. {
  28. $inflator = $deflator = $this->getMockBuilder('Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactory')
  29. ->disableOriginalConstructor()
  30. ->setMethods(array('useBinary'))
  31. ->getMock();
  32. $outputParser = ParserFactory::create(ZipAdapter::getName());
  33. $this->adapter = new ZipAdapter($outputParser, $this->getResourceManagerMock(), $inflator, $deflator);
  34. }
  35. /**
  36. * @expectedException Alchemy\Zippy\Exception\NotSupportedException
  37. */
  38. public function testCreateNoFiles()
  39. {
  40. $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
  41. $this->adapter->setInflator($this->getZippyMockBuilder($mockProcessBuilder));
  42. $this->adapter->create(self::$zipFile, array());
  43. }
  44. public function testCreate()
  45. {
  46. $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
  47. $mockProcessBuilder
  48. ->expects($this->at(0))
  49. ->method('add')
  50. ->with($this->equalTo('-r'))
  51. ->will($this->returnSelf());
  52. $mockProcessBuilder
  53. ->expects($this->at(1))
  54. ->method('add')
  55. ->with($this->equalTo(self::$zipFile))
  56. ->will($this->returnSelf());
  57. $mockProcessBuilder
  58. ->expects($this->at(2))
  59. ->method('setWorkingDirectory')
  60. ->will($this->returnSelf());
  61. $mockProcessBuilder
  62. ->expects($this->at(3))
  63. ->method('add')
  64. ->with($this->equalTo('lalala'))
  65. ->will($this->returnSelf());
  66. $mockProcessBuilder
  67. ->expects($this->once())
  68. ->method('getProcess')
  69. ->will($this->returnValue($this->getSuccessFullMockProcess()));
  70. $manager = $this->getResourceManagerMock(__DIR__, array('lalala'));
  71. $outputParser = ParserFactory::create(ZipAdapter::getName());
  72. $deflator = $this->getMockBuilder('Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactory')
  73. ->disableOriginalConstructor()
  74. ->setMethods(array('useBinary'))
  75. ->getMock();
  76. $this->adapter = new ZipAdapter($outputParser, $manager, $this->getZippyMockBuilder($mockProcessBuilder), $deflator);
  77. $this->adapter->create(self::$zipFile, array(__FILE__));
  78. return self::$zipFile;
  79. }
  80. /**
  81. * @depends testCreate
  82. */
  83. public function testOpen($zipFile)
  84. {
  85. $archive = $this->adapter->open($this->getResource($zipFile));
  86. $this->assertInstanceOf('Alchemy\Zippy\Archive\ArchiveInterface', $archive);
  87. }
  88. public function testListMembers()
  89. {
  90. $resource = $this->getResource(self::$zipFile);
  91. $archive = $this->adapter->open($resource);
  92. $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
  93. $mockProcessBuilder
  94. ->expects($this->at(0))
  95. ->method('add')
  96. ->with($this->equalTo('-l'))
  97. ->will($this->returnSelf());
  98. $mockProcessBuilder
  99. ->expects($this->at(1))
  100. ->method('add')
  101. ->with($this->equalTo($resource->getResource()))
  102. ->will($this->returnSelf());
  103. $mockProcessBuilder
  104. ->expects($this->once())
  105. ->method('getProcess')
  106. ->will($this->returnValue($this->getSuccessFullMockProcess()));
  107. $this->adapter->setDeflator($this->getZippyMockBuilder($mockProcessBuilder));
  108. $this->adapter->listMembers($resource);
  109. }
  110. public function testAddFile()
  111. {
  112. $resource = $this->getResource(self::$zipFile);
  113. $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
  114. $mockProcessBuilder
  115. ->expects($this->at(0))
  116. ->method('add')
  117. ->with($this->equalTo('-r'))
  118. ->will($this->returnSelf());
  119. $mockProcessBuilder
  120. ->expects($this->at(1))
  121. ->method('add')
  122. ->with($this->equalTo('-u'))
  123. ->will($this->returnSelf());
  124. $mockProcessBuilder
  125. ->expects($this->at(2))
  126. ->method('add')
  127. ->with($this->equalTo($resource->getResource()))
  128. ->will($this->returnSelf());
  129. $mockProcessBuilder
  130. ->expects($this->once())
  131. ->method('getProcess')
  132. ->will($this->returnValue($this->getSuccessFullMockProcess()));
  133. $this->adapter->setInflator($this->getZippyMockBuilder($mockProcessBuilder));
  134. $this->adapter->add($resource, array(__DIR__ . '/../TestCase.php'));
  135. }
  136. public function testgetInflatorVersion()
  137. {
  138. $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
  139. $mockProcessBuilder
  140. ->expects($this->at(0))
  141. ->method('add')
  142. ->with($this->equalTo('-h'))
  143. ->will($this->returnSelf());
  144. $mockProcessBuilder
  145. ->expects($this->once())
  146. ->method('getProcess')
  147. ->will($this->returnValue($this->getSuccessFullMockProcess()));
  148. $this->adapter->setParser($this->getMock('Alchemy\Zippy\Parser\ParserInterface'));
  149. $this->adapter->setInflator($this->getZippyMockBuilder($mockProcessBuilder));
  150. $this->adapter->getInflatorVersion();
  151. }
  152. public function testgetDeflatorVersion()
  153. {
  154. $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
  155. $mockProcessBuilder
  156. ->expects($this->at(0))
  157. ->method('add')
  158. ->with($this->equalTo('-h'))
  159. ->will($this->returnSelf());
  160. $mockProcessBuilder
  161. ->expects($this->once())
  162. ->method('getProcess')
  163. ->will($this->returnValue($this->getSuccessFullMockProcess()));
  164. $this->adapter->setParser($this->getMock('Alchemy\Zippy\Parser\ParserInterface'));
  165. $this->adapter->setDeflator($this->getZippyMockBuilder($mockProcessBuilder));
  166. $this->adapter->getDeflatorVersion();
  167. }
  168. public function testRemoveMembers()
  169. {
  170. $resource = $this->getResource(self::$zipFile);
  171. $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
  172. $mockProcessBuilder
  173. ->expects($this->at(0))
  174. ->method('add')
  175. ->with($this->equalTo('-d'))
  176. ->will($this->returnSelf());
  177. $mockProcessBuilder
  178. ->expects($this->at(1))
  179. ->method('add')
  180. ->with($this->equalTo($resource->getResource()))
  181. ->will($this->returnSelf());
  182. $mockProcessBuilder
  183. ->expects($this->at(2))
  184. ->method('add')
  185. ->with($this->equalTo(__DIR__ . '/../TestCase.php'))
  186. ->will($this->returnSelf());
  187. $mockProcessBuilder
  188. ->expects($this->at(3))
  189. ->method('add')
  190. ->with($this->equalTo('path-to-file'))
  191. ->will($this->returnSelf());
  192. $mockProcessBuilder
  193. ->expects($this->once())
  194. ->method('getProcess')
  195. ->will($this->returnValue($this->getSuccessFullMockProcess()));
  196. $archiveFileMock = $this->getMock('Alchemy\Zippy\Archive\MemberInterface');
  197. $archiveFileMock
  198. ->expects($this->any())
  199. ->method('getLocation')
  200. ->will($this->returnValue('path-to-file'));
  201. $this->adapter->setInflator($this->getZippyMockBuilder($mockProcessBuilder));
  202. $this->adapter->remove($resource, array(
  203. __DIR__ . '/../TestCase.php',
  204. $archiveFileMock
  205. ));
  206. }
  207. public function testExtract()
  208. {
  209. $resource = $this->getResource(self::$zipFile);
  210. $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
  211. $mockProcessBuilder
  212. ->expects($this->at(0))
  213. ->method('add')
  214. ->with($this->equalTo('-o'))
  215. ->will($this->returnSelf());
  216. $mockProcessBuilder
  217. ->expects($this->at(1))
  218. ->method('add')
  219. ->with($this->equalTo($resource->getResource()))
  220. ->will($this->returnSelf());
  221. $mockProcessBuilder
  222. ->expects($this->once())
  223. ->method('getProcess')
  224. ->will($this->returnValue($this->getSuccessFullMockProcess()));
  225. $this->adapter->setDeflator($this->getZippyMockBuilder($mockProcessBuilder));
  226. $dir = $this->adapter->extract($resource);
  227. $pathinfo = pathinfo(self::$zipFile);
  228. $this->assertEquals($pathinfo['dirname'], $dir->getPath());
  229. }
  230. public function testExtractWithExtractDirPrecised()
  231. {
  232. $resource = $this->getResource(self::$zipFile);
  233. $mockProcessBuilder = $this->getMock('Symfony\Component\Process\ProcessBuilder');
  234. $mockProcessBuilder
  235. ->expects($this->at(0))
  236. ->method('add')
  237. ->with($this->equalTo($resource->getResource()))
  238. ->will($this->returnSelf());
  239. $mockProcessBuilder
  240. ->expects($this->at(1))
  241. ->method('add')
  242. ->with($this->equalTo('-d'))
  243. ->will($this->returnSelf());
  244. $mockProcessBuilder
  245. ->expects($this->at(2))
  246. ->method('add')
  247. ->with($this->equalTo(__DIR__))
  248. ->will($this->returnSelf());
  249. $mockProcessBuilder
  250. ->expects($this->at(3))
  251. ->method('add')
  252. ->with($this->equalTo(__FILE__))
  253. ->will($this->returnSelf());
  254. $mockProcessBuilder
  255. ->expects($this->once())
  256. ->method('getProcess')
  257. ->will($this->returnValue($this->getSuccessFullMockProcess()));
  258. $this->adapter->setDeflator($this->getZippyMockBuilder($mockProcessBuilder));
  259. $this->adapter->extractMembers($resource, array(__FILE__), __DIR__);
  260. }
  261. public function testGetName()
  262. {
  263. $this->assertEquals('zip', ZipAdapter::getName());
  264. }
  265. public function testGetDefaultInflatorBinaryName()
  266. {
  267. $this->assertEquals(array('zip'), ZipAdapter::getDefaultInflatorBinaryName());
  268. }
  269. public function testGetDefaultDeflatorBinaryName()
  270. {
  271. $this->assertEquals(array('unzip'), ZipAdapter::getDefaultDeflatorBinaryName());
  272. }
  273. private function getSuccessFullMockProcess()
  274. {
  275. $mockProcess = $this
  276. ->getMockBuilder('Symfony\Component\Process\Process')
  277. ->disableOriginalConstructor()
  278. ->getMock();
  279. $mockProcess
  280. ->expects($this->once())
  281. ->method('run');
  282. $mockProcess
  283. ->expects($this->once())
  284. ->method('isSuccessful')
  285. ->will($this->returnValue(true));
  286. return $mockProcess;
  287. }
  288. private function getZippyMockBuilder($mockedProcessBuilder)
  289. {
  290. $mockBuilder = $this->getMock('Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface');
  291. $mockBuilder
  292. ->expects($this->once())
  293. ->method('create')
  294. ->will($this->returnValue($mockedProcessBuilder));
  295. return $mockBuilder;
  296. }
  297. }