ZipExtensionAdapterTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace Alchemy\Zippy\Tests\Adapter;
  3. use Alchemy\Zippy\Tests\TestCase;
  4. use Alchemy\Zippy\Adapter\ZipExtensionAdapter;
  5. use Alchemy\Zippy\Adapter\Resource\ZipArchiveResource;
  6. class ZipExtensionAdapterTest extends TestCase
  7. {
  8. private $adapter;
  9. public function setUp()
  10. {
  11. $this->adapter = new ZipExtensionAdapter($this->getResourceManagerMock());
  12. }
  13. public function testNewInstance()
  14. {
  15. $adapter = ZipExtensionAdapter::newInstance();
  16. $this->assertInstanceOf('Alchemy\Zippy\Adapter\ZipExtensionAdapter', $adapter);
  17. }
  18. /**
  19. * @expectedException Alchemy\Zippy\Exception\NotSupportedException
  20. */
  21. public function testCreateNoFiles()
  22. {
  23. $this->adapter->create(__DIR__ . '/zip-file.zip', array());
  24. }
  25. public function testCreate()
  26. {
  27. $file = __DIR__ . '/zip-file.zip';
  28. $manager = $this->getResourceManagerMock(__DIR__, array(__FILE__));
  29. $this->adapter = new ZipExtensionAdapter($manager);
  30. $archive = $this->adapter->create($file, array(__FILE__));
  31. $this->assertInstanceOf('Alchemy\Zippy\Archive\Archive', $archive);
  32. $this->assertFileExists($file);
  33. unlink($file);
  34. }
  35. /**
  36. * @expectedException Alchemy\Zippy\Exception\RuntimeException
  37. */
  38. public function testOpenWithWrongFileName()
  39. {
  40. $file = __DIR__ . '/zip-file.zip';
  41. $this->adapter->open($file);
  42. }
  43. public function testOpen()
  44. {
  45. $file = __DIR__ . '/zip-file.zip';
  46. touch($file);
  47. $archive = $this->adapter->open($file);
  48. $this->assertInstanceOf('Alchemy\Zippy\Archive\Archive', $archive);
  49. unlink($file);
  50. }
  51. public function testIsSupported()
  52. {
  53. $this->assertInternalType('boolean', $this->adapter->isSupported());
  54. }
  55. public function testGetName()
  56. {
  57. $this->assertInternalType('string', $this->adapter->getName());
  58. }
  59. public function testListMembers()
  60. {
  61. $resource = $this->getMockBuilder('\ZipArchive')
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $members = $this->adapter->listMembers(new ZipArchiveResource($resource));
  65. $this->assertInternalType('array', $members);
  66. }
  67. public function testExtract()
  68. {
  69. $resource = $this->getMockBuilder('\ZipArchive')
  70. ->disableOriginalConstructor()
  71. ->getMock();
  72. $resource->expects($this->once())
  73. ->method('extractTo')
  74. ->with($this->equalTo(__DIR__), $this->anything())
  75. ->will($this->returnValue(true));
  76. $this->adapter->extract(new ZipArchiveResource($resource), __DIR__);
  77. }
  78. /**
  79. * @expectedException Alchemy\Zippy\Exception\InvalidArgumentException
  80. */
  81. public function testExtractOnError()
  82. {
  83. $resource = $this->getMockBuilder('\ZipArchive')
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $resource->expects($this->once())
  87. ->method('extractTo')
  88. ->with($this->equalTo(__DIR__), $this->anything())
  89. ->will($this->returnValue(false));
  90. $this->adapter->extract(new ZipArchiveResource($resource), __DIR__);
  91. }
  92. /**
  93. * @expectedException Alchemy\Zippy\Exception\InvalidArgumentException
  94. */
  95. public function testExtractWithInvalidTarget()
  96. {
  97. $resource = $this->getMockBuilder('\ZipArchive')
  98. ->disableOriginalConstructor()
  99. ->getMock();
  100. $this->adapter->extract(new ZipArchiveResource($resource), __DIR__ . '/boursin');
  101. }
  102. /**
  103. * @expectedException Alchemy\Zippy\Exception\InvalidArgumentException
  104. */
  105. public function testExtractWithInvalidTarget2()
  106. {
  107. $resource = $this->getMockBuilder('\ZipArchive')
  108. ->disableOriginalConstructor()
  109. ->getMock();
  110. $this->adapter->extract(new ZipArchiveResource($resource));
  111. }
  112. public function testRemove()
  113. {
  114. $resource = $this->getMockBuilder('\ZipArchive')
  115. ->disableOriginalConstructor()
  116. ->getMock();
  117. $files = array(
  118. 'one-file.jpg',
  119. 'second-file.jpg',
  120. );
  121. $resource->expects($this->exactly(2))
  122. ->method('locateName')
  123. ->will($this->returnValue(true));
  124. $resource->expects($this->exactly(2))
  125. ->method('deleteName')
  126. ->will($this->returnValue(true));
  127. $this->adapter->remove(new ZipArchiveResource($resource), $files);
  128. }
  129. /**
  130. * @expectedException Alchemy\Zippy\Exception\InvalidArgumentException
  131. */
  132. public function testRemoveWithLocateFailing()
  133. {
  134. $resource = $this->getMockBuilder('\ZipArchive')
  135. ->disableOriginalConstructor()
  136. ->getMock();
  137. $files = array(
  138. 'one-file.jpg'
  139. );
  140. $resource->expects($this->once())
  141. ->method('locateName')
  142. ->with($this->equalTo('one-file.jpg'))
  143. ->will($this->returnValue(false));
  144. $this->adapter->remove(new ZipArchiveResource($resource), $files);
  145. }
  146. /**
  147. * @expectedException Alchemy\Zippy\Exception\RuntimeException
  148. */
  149. public function testRemoveWithDeleteFailing()
  150. {
  151. $resource = $this->getMockBuilder('\ZipArchive')
  152. ->disableOriginalConstructor()
  153. ->getMock();
  154. $files = array(
  155. 'one-file.jpg'
  156. );
  157. $resource->expects($this->once())
  158. ->method('locateName')
  159. ->with($this->equalTo('one-file.jpg'))
  160. ->will($this->returnValue(true));
  161. $resource->expects($this->once())
  162. ->method('deleteName')
  163. ->with($this->equalTo('one-file.jpg'))
  164. ->will($this->returnValue(false));
  165. $this->adapter->remove(new ZipArchiveResource($resource), $files);
  166. }
  167. public function testAdd()
  168. {
  169. $resource = $this->getMockBuilder('\ZipArchive')
  170. ->disableOriginalConstructor()
  171. ->getMock();
  172. $resource->expects($this->once())
  173. ->method('addFile')
  174. ->will($this->returnValue(true));
  175. $resource->expects($this->once())
  176. ->method('addEmptyDir')
  177. ->will($this->returnValue(true));
  178. $dir = __DIR__ . '/temp-dir';
  179. if (!is_dir($dir)) {
  180. mkdir($dir);
  181. }
  182. $files = array(
  183. __FILE__,
  184. $dir,
  185. );
  186. $manager = $this->getResourceManagerMock(__DIR__, $files);
  187. $this->adapter = new ZipExtensionAdapter($manager);
  188. $this->adapter->add(new ZipArchiveResource($resource), $files);
  189. rmdir($dir);
  190. }
  191. /**
  192. * @expectedException Alchemy\Zippy\Exception\RuntimeException
  193. */
  194. public function testAddFailOnFile()
  195. {
  196. $resource = $this->getMockBuilder('\ZipArchive')
  197. ->disableOriginalConstructor()
  198. ->getMock();
  199. $resource->expects($this->once())
  200. ->method('addFile')
  201. ->will($this->returnValue(false));
  202. $dir = __DIR__ . '/temp-dir';
  203. if (!is_dir($dir)) {
  204. mkdir($dir);
  205. }
  206. $files = array(
  207. __FILE__,
  208. $dir,
  209. );
  210. $manager = $this->getResourceManagerMock(__DIR__, $files);
  211. $this->adapter = new ZipExtensionAdapter($manager);
  212. $this->adapter->add(new ZipArchiveResource($resource), $files);
  213. }
  214. /**
  215. * @expectedException Alchemy\Zippy\Exception\RuntimeException
  216. */
  217. public function testAddFailOnDir()
  218. {
  219. $resource = $this->getMockBuilder('\ZipArchive')
  220. ->disableOriginalConstructor()
  221. ->getMock();
  222. $resource->expects($this->once())
  223. ->method('addFile')
  224. ->will($this->returnValue(true));
  225. $resource->expects($this->once())
  226. ->method('addEmptyDir')
  227. ->will($this->returnValue(false));
  228. $dir = __DIR__ . '/temp-dir';
  229. if (!is_dir($dir)) {
  230. mkdir($dir);
  231. }
  232. $files = array(
  233. __FILE__,
  234. $dir,
  235. );
  236. $manager = $this->getResourceManagerMock(__DIR__, $files);
  237. $this->adapter = new ZipExtensionAdapter($manager);
  238. $this->adapter->add(new ZipArchiveResource($resource), $files);
  239. }
  240. }