ExtractArchiveTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Alchemy\Zippy\Functional;
  3. use Symfony\Component\Finder\Finder;
  4. class ExtractArchiveTest extends FunctionalTestCase
  5. {
  6. public function testOpen()
  7. {
  8. $adapter = $this->getAdapter();
  9. $archiveFile = $this->getArchiveFileForAdapter($adapter);
  10. $archive = $adapter->open($archiveFile);
  11. return $archive;
  12. }
  13. /**
  14. * @depends testOpen
  15. */
  16. public function testExtract($archive)
  17. {
  18. $target = __DIR__ . '/samples/tmp';
  19. $archive->extract($target);
  20. $finder = new Finder();
  21. $finder
  22. ->files()
  23. ->in($target);
  24. $files2find = array(
  25. '/directory/README.md',
  26. '/directory/photo.jpg'
  27. );
  28. foreach ($finder as $file) {
  29. $this->assertEquals(0, strpos($file->getPathname(), $target));
  30. $member = substr($file->getPathname(), strlen($target));
  31. $this->assertTrue(in_array($member, $files2find));
  32. unset($files2find[array_search($member, $files2find)]);
  33. }
  34. $this->assertEquals(array(), $files2find);
  35. }
  36. /**
  37. * @depends testOpen
  38. */
  39. public function testExtractOnExistingFilesCanOverwrite($archive)
  40. {
  41. $random = (string) uniqid(mt_rand(), true);
  42. $target = __DIR__ . '/samples/tmp';
  43. $files2find = array(
  44. '/directory/README.md',
  45. '/directory/photo.jpg'
  46. );
  47. foreach ($files2find as $file) {
  48. $file2create = $target . $file;
  49. if (!is_dir(dirname($file2create))) {
  50. mkdir(dirname($file2create), 0777, true);
  51. }
  52. file_put_contents($file2create, $random);
  53. }
  54. $archive->extract($target, true);
  55. $finder = new Finder();
  56. $finder
  57. ->files()
  58. ->in($target);
  59. foreach ($finder as $file) {
  60. $this->assertEquals(0, strpos($file->getPathname(), $target));
  61. $this->assertNotEquals($random, file_get_contents($file->getPathname()));
  62. $member = substr($file->getPathname(), strlen($target));
  63. $this->assertTrue(in_array($member, $files2find));
  64. unset($files2find[array_search($member, $files2find)]);
  65. }
  66. $this->assertEquals(array(), $files2find);
  67. }
  68. }