CreateArchiveTest.php 2.7 KB

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