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  array_walk(glob(self::$destinationFolder . '/*.zip'), function($file)
18  {
19  unlink($file);
20  });
21  }
22 
23  public function testCompressTextOnly()
24  {
25  $destination = self::$destinationFolder . '/textOnly.zip';
26 
27  ZipHelper::compress(array(
28  'file1.txt' => 'content of file1',
29  'file2.txt' => 'content of file2',
30  ), $destination);
31  $this->assertFileExists($destination);
32 
33  $zip = new ZipArchive();
34  $zip->open($destination);
35  $this->assertEquals('content of file1', $zip->getFromName('file1.txt'));
36  $this->assertEquals('content of file2', $zip->getFromName('file2.txt'));
37 
38 
39  }
40 
41  public function testCompressFilesOnly()
42  {
43  $destination = self::$destinationFolder . '/filesOnly.zip';
44 
45  ZipHelper::compress(array(
46  self::$destinationFolder . '/test_file1.txt',
47  self::$destinationFolder . '/test_file2.txt'
48  ), $destination);
49  $this->assertFileExists($destination);
50 
51  $zip = new ZipArchive();
52  $zip->open($destination);
53  $this->assertNotEmpty($zip->getFromName('test_file1.txt'));
54  $this->assertNotEmpty($zip->getFromName('test_file2.txt'));
55  }
56 
57  public function testCompressFilesExclude()
58  {
59  $destination = self::$destinationFolder . '/filesOnlyExclude.zip';
60 
61  ZipHelper::compress(array(
62  array(
63  'path' => self::$destinationFolder,
64  'exclude' => array('test_file1.txt')
65  )
66  ), $destination);
67  $this->assertFileExists($destination);
68 
69  $zip = new ZipArchive();
70  $zip->open($destination);
71  $this->assertEmpty($zip->getFromName('test_file1.txt'));
72  $this->assertNotEmpty($zip->getFromName('test_file2.txt'));
73  }
74 
75  public function testCompressFilesAndText()
76  {
77  $destination = self::$destinationFolder . '/filesAndText.zip';
78 
79  ZipHelper::compress(array(
80  array(
81  'path' => self::$destinationFolder,
82  ),
83  'file1.txt' => 'content of file1',
84  'file2.txt' => 'content of file2',
85  ), $destination);
86  $this->assertFileExists($destination);
87 
88  $zip = new ZipArchive();
89  $zip->open($destination);
90 
91  $this->assertNotEmpty($zip->getFromName('test_file1.txt'));
92  $this->assertNotEmpty($zip->getFromName('test_file2.txt'));
93  $this->assertEquals('content of file1', $zip->getFromName('file1.txt'));
94  $this->assertEquals('content of file2', $zip->getFromName('file2.txt'));
95  }
96 
97  public function testExtract()
98  {
99  $destination = self::$destinationFolder . '/compressed.zip';
100  ZipHelper::compress(array(
101  'compressed.txt' => 'content of compressed',
102  ), $destination);
103 
104  ZipHelper::extract($destination, self::$destinationFolder);
105  $this->assertFileExists(self::$destinationFolder . '/compressed.txt');
106  unlink(self::$destinationFolder . '/compressed.txt');
107  }
108 
109 }