eZ components PHP Cross Reference Developer Tools

Source: /Archive/tests/compressed_archives/zlib_test.php - 179 lines - 5478 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
   4   * @license http://ez.no/licenses/new_bsd New BSD License
   5   * @filesource
   6   * @package Archive
   7   * @version 1.4.1
   8   * @subpackage Tests
   9   */
  10  
  11  require_once( dirname( __FILE__ ) . "/../testdata.php" );
  12  require_once(dirname(__FILE__) . "/../archive_test_case.php");
  13  
  14  /**
  15   * @package Archive
  16   * @version 1.4.1
  17   * @subpackage Tests
  18   */
  19  class ezcArchiveZlibTest extends ezcArchiveTestCase
  20  {
  21      protected function setUp()
  22      {
  23          $this->createTempDir( "ezcArchive_" );
  24          date_default_timezone_set( "UTC" ); 
  25      }
  26  
  27      protected function tearDown()
  28      {
  29          $this->removeTempDir();
  30      }
  31  
  32      public function testCreateTar()
  33      {
  34          $dir = $this->getTempDir();
  35          $archive = ezcArchive::open( "$dir/mytar.tar", ezcArchive::TAR_USTAR );
  36          file_put_contents( "$dir/a.txt", "Hello world!" );
  37          $archive->append( "$dir/a.txt", $dir );
  38          $archive->close();
  39  
  40          exec( "tar -cf $dir/gnutar.tar --format=ustar -C $dir a.txt" );
  41  
  42          $this->assertEquals( file_get_contents( "$dir/gnutar.tar" ), file_get_contents( "$dir/mytar.tar" ) );
  43      }
  44  
  45      public function testCreateGzippedTar()
  46      {
  47          $dir = $this->getTempDir();
  48          $archive = ezcArchive::open( "compress.zlib://$dir/mytar.tar.gz", ezcArchive::TAR_USTAR );
  49          file_put_contents( "$dir/a.txt", "Hello world!" );
  50          $archive->append( "$dir/a.txt", $dir );
  51          $archive->close();
  52  
  53          exec( "tar -cf $dir/gnutar.tar --format=ustar -C $dir a.txt" );
  54          exec( "gunzip $dir/mytar.tar.gz" );
  55  
  56          $this->assertEquals( file_get_contents( "$dir/gnutar.tar" ), file_get_contents( "$dir/mytar.tar" ) );
  57      }
  58  
  59      public function testWriteToExistingGzippedTar()
  60      {
  61          // Create an archive with one file.
  62          $dir = $this->getTempDir();
  63          $archive = ezcArchive::open( "compress.zlib://$dir/mytar.tar.gz", ezcArchive::TAR_USTAR );
  64          file_put_contents( "$dir/a.txt", "Hello world!" );
  65          $archive->append( "$dir/a.txt", $dir );
  66          $archive->close();
  67  
  68          // Reopen it and append another file.
  69          $archive = ezcArchive::open( "compress.zlib://$dir/mytar.tar.gz", ezcArchive::TAR_USTAR );
  70  
  71          try
  72          {
  73              $archive->append( "$dir/a.txt", $dir );
  74              $this->fail( "Expected a 'cannot-append' exception" );
  75          }
  76          catch ( ezcArchiveException $e )
  77          {
  78  
  79          }
  80          $archive->close();
  81      }
  82  
  83      public function testCreateNewGzippedTarArchiveTogetherWithReadingEntries()
  84      {
  85          $dir = $this->getTempDir();
  86  
  87          // Create some test data.
  88          file_put_contents( "$dir/a.txt", "AAAAAAAAAAA" );
  89          file_put_contents( "$dir/b.txt", "BBBBBBBBBBB" );
  90          file_put_contents( "$dir/c.txt", "CCCCCCCCCCC" );
  91  
  92          // Create a new archive
  93          $archive = ezcArchive::open( "compress.zlib://$dir/mytar.tar.gz", ezcArchive::TAR_USTAR );
  94          $archive->append( "$dir/a.txt", $dir );
  95          $archive->rewind();
  96          $a = $archive->current();
  97          $this->assertEquals( "a.txt", $a->getPath( false ) );
  98  
  99          $archive->rewind();
 100          $archive->append( "$dir/b.txt", $dir );
 101  
 102          $a = $archive->current();
 103          $this->assertEquals( "a.txt", $a->getPath( false ) );
 104  
 105          $a = $archive->next();
 106          $this->assertEquals( "b.txt", $a->getPath( false ) );
 107  
 108          $archive->rewind();
 109          $archive->close();
 110  
 111          // Reopen it and append another file.
 112          $archive = ezcArchive::open( "compress.zlib://$dir/mytar.tar.gz", ezcArchive::TAR_USTAR );
 113  
 114          try
 115          {
 116              $archive->append( "$dir/a.txt", $dir );
 117              $this->fail( "Expected a 'cannot-append' exception" );
 118          }
 119          catch ( ezcArchiveException $e )
 120          {
 121  
 122          }
 123          $archive->close();
 124      }
 125  
 126      public function testAppendToCurrentException()
 127      {
 128          $dir = $this->getTempDir();
 129          $archive = ezcArchive::open( "compress.zlib://$dir/mytar.tar.gz", ezcArchive::TAR_USTAR );
 130          file_put_contents( "$dir/a.txt", "AAAAAAAAAAA" );
 131  
 132          try
 133          {
 134              $archive->appendToCurrent( "$dir/a.txt", $dir );
 135              $this->fail( "Except an exception that the file couldn't be appended." );
 136          }
 137          catch ( ezcArchiveException $e )
 138          {
 139          }
 140      }
 141  
 142      public function testCloseException()
 143      {
 144          $dir = $this->getTempDir();
 145          $archive = ezcArchive::open( "compress.zlib://$dir/mytar.tar.gz", ezcArchive::TAR_USTAR );
 146          file_put_contents( "$dir/a.txt", "AAAAAAAAAAA" );
 147          $archive->append( "$dir/a.txt", $dir );
 148          $archive->close();
 149  
 150          try
 151          {
 152              $archive->append( "$dir/a.txt", $dir );
 153              $this->fail( "Except an exception that the file couldn't be appended." );
 154          }
 155          catch ( ezcArchiveException $e )
 156          {
 157          }
 158      }
 159  
 160      public function testListing()
 161      {
 162          $dir = $this->getTempDir();
 163          $archive = ezcArchive::open( "compress.zlib://$dir/mytar.tar.gz", ezcArchive::TAR_USTAR );
 164          file_put_contents( "$dir/a.txt", "AAAAAAAAAAA" );
 165          $archive->append( "$dir/a.txt", $dir );
 166          file_put_contents( "$dir/a.txt", "AAAAAAAAAAA" );
 167          $archive->append( "$dir/a.txt", $dir );
 168  
 169          $a = $archive->getListing();
 170          $this->assertEquals( 2, sizeof( $a ) );
 171  
 172      }
 173  
 174      public static function suite()
 175      {
 176          return new PHPUnit_Framework_TestSuite( __CLASS__ );
 177      }
 178  }
 179  ?>

title

Description

title

Description

title

Description

title

title

Body