Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ZipHelperTest.php
1 <?php
2 /**
3  *
4  */
5 class ZipHelperTest extends CTestCase
6 {
7  protected static $destinationFolder;
8 
9  public static function setUpBeforeClass()
10  {
11  Yii::app()->getModule('updates');
12  self::$destinationFolder = Yii::getPathOfAlias('site.tests.runtime.updates.ZipHelper');
13  }
14 
15  public function setUp()
16  {
17  $matches = glob(self::$destinationFolder . '/*.zip');
18  array_walk($matches, function($file)
19  {
20  unlink($file);
21  });
22  }
23 
24  public function testCompressTextOnly()
25  {
26  $destination = self::$destinationFolder . '/textOnly.zip';
27 
28  ZipHelper::compress(array(
29  'file1.txt' => 'content of file1',
30  'file2.txt' => 'content of file2',
31  ), $destination);
32  $this->assertFileExists($destination);
33 
34  $zip = new ZipArchive();
35  $zip->open($destination);
36  $this->assertEquals('content of file1', $zip->getFromName('file1.txt'));
37  $this->assertEquals('content of file2', $zip->getFromName('file2.txt'));
38 
39 
40  }
41 
42  public function testCompressFilesOnly()
43  {
44  $destination = self::$destinationFolder . '/filesOnly.zip';
45 
46  ZipHelper::compress(array(
47  self::$destinationFolder . '/test_file1.txt',
48  self::$destinationFolder . '/test_file2.txt'
49  ), $destination);
50  $this->assertFileExists($destination);
51 
52  $zip = new ZipArchive();
53  $zip->open($destination);
54  $this->assertNotEmpty($zip->getFromName('test_file1.txt'));
55  $this->assertNotEmpty($zip->getFromName('test_file2.txt'));
56  }
57 
58  public function testCompressFilesExclude()
59  {
60  $destination = self::$destinationFolder . '/filesOnlyExclude.zip';
61 
62  ZipHelper::compress(array(
63  array(
64  'path' => self::$destinationFolder,
65  'exclude' => array('test_file1.txt')
66  )
67  ), $destination);
68  $this->assertFileExists($destination);
69 
70  $zip = new ZipArchive();
71  $zip->open($destination);
72  $this->assertEmpty($zip->getFromName('test_file1.txt'));
73  $this->assertNotEmpty($zip->getFromName('test_file2.txt'));
74  }
75 
76  public function testCompressFilesAndText()
77  {
78  $destination = self::$destinationFolder . '/filesAndText.zip';
79 
80  ZipHelper::compress(array(
81  array(
82  'path' => self::$destinationFolder,
83  ),
84  'file1.txt' => 'content of file1',
85  'file2.txt' => 'content of file2',
86  ), $destination);
87  $this->assertFileExists($destination);
88 
89  $zip = new ZipArchive();
90  $zip->open($destination);
91 
92  $this->assertNotEmpty($zip->getFromName('test_file1.txt'));
93  $this->assertNotEmpty($zip->getFromName('test_file2.txt'));
94  $this->assertEquals('content of file1', $zip->getFromName('file1.txt'));
95  $this->assertEquals('content of file2', $zip->getFromName('file2.txt'));
96  }
97 
98  public function testExtract()
99  {
100  $destination = self::$destinationFolder . '/compressed.zip';
101  ZipHelper::compress(array(
102  'compressed.txt' => 'content of compressed',
103  ), $destination);
104 
105  ZipHelper::extract($destination, self::$destinationFolder);
106  $this->assertFileExists(self::$destinationFolder . '/compressed.txt');
107  unlink(self::$destinationFolder . '/compressed.txt');
108  }
109 
110 }