28
28
*/
29
29
class ZipArchiveTest extends \PHPUnit_Framework_TestCase
30
30
{
31
+
31
32
/**
32
- * Test all methods
33
+ * Test close method exception
33
34
*
34
- * @covers ::<public>
35
+ * @expectedException \PhpOffice\PhpWord\Exception\Exception
36
+ * @expectedExceptionMessage Could not close zip file
37
+ * @covers ::close
35
38
*/
36
- public function testZipArchive ()
39
+ public function testCloseException ()
37
40
{
38
- // Preparation
39
- $ existingFile = __DIR__ . "/../_files/documents/sheet.xls " ;
40
- $ zipFile = __DIR__ . "/../_files/documents/ziptest.zip " ;
41
- $ destination1 = __DIR__ . "/../_files/documents/extract1 " ;
42
- $ destination2 = __DIR__ . "/../_files/documents/extract2 " ;
43
- @mkdir ($ destination1 );
44
- @mkdir ($ destination2 );
45
-
46
- Settings::setZipClass ('PhpOffice\PhpWord\Shared\ZipArchive ' );
41
+ $ zipFile = __DIR__ . "/../_files/documents/ziptest.zip " ;
47
42
48
43
$ object = new ZipArchive ();
49
44
$ object ->open ($ zipFile , ZipArchive::CREATE );
50
- $ object ->addFile ($ existingFile , 'xls/new.xls ' );
51
45
$ object ->addFromString ('content/string.txt ' , 'Test ' );
52
- $ object ->close ();
53
- $ object ->open ($ zipFile );
54
-
55
- // Run tests
56
- $ this ->assertEquals (0 , $ object ->locateName ('xls/new.xls ' ));
57
- $ this ->assertFalse ($ object ->locateName ('blablabla ' ));
58
46
59
- $ this ->assertEquals ('Test ' , $ object ->getFromName ('content/string.txt ' ));
60
- $ this ->assertEquals ('Test ' , $ object ->getFromName ('/content/string.txt ' ));
47
+ // Lock the file
48
+ $ fp = fopen ($ zipFile , "w " );
49
+ flock ($ fp , LOCK_EX );
61
50
62
- $ this -> assertFalse ( $ object -> getNameIndex (- 1 ));
63
- $ this -> assertEquals ( ' content/string.txt ' , $ object ->getNameIndex ( 1 ) );
51
+ // Closing the file should throws an exception
52
+ $ object ->close ( );
64
53
65
- $ this ->assertFalse ($ object ->extractTo ('blablabla ' ));
66
- $ this ->assertTrue ($ object ->extractTo ($ destination1 ));
67
- $ this ->assertTrue ($ object ->extractTo ($ destination2 , 'xls/new.xls ' ));
68
- $ this ->assertFalse ($ object ->extractTo ($ destination2 , 'blablabla ' ));
54
+ // Unlock the file
55
+ flock ($ fp , LOCK_UN );
56
+ fclose ($ fp );
69
57
70
- // Cleanup
71
- $ this ->deleteDir ($ destination1 );
72
- $ this ->deleteDir ($ destination2 );
73
58
@unlink ($ zipFile );
74
59
}
75
60
76
61
/**
77
62
* Test all methods
78
63
*
64
+ * @param string $zipClass
79
65
* @covers ::<public>
80
66
*/
81
- public function testPCLZip ( )
67
+ public function testZipArchive ( $ zipClass = ' ZipArchive ' )
82
68
{
83
69
// Preparation
84
70
$ existingFile = __DIR__ . "/../_files/documents/sheet.xls " ;
@@ -88,7 +74,7 @@ public function testPCLZip()
88
74
@mkdir ($ destination1 );
89
75
@mkdir ($ destination2 );
90
76
91
- Settings::setZipClass (' ZipArchive ' );
77
+ Settings::setZipClass ($ zipClass );
92
78
93
79
$ object = new ZipArchive ();
94
80
$ object ->open ($ zipFile , ZipArchive::CREATE );
@@ -118,6 +104,16 @@ public function testPCLZip()
118
104
@unlink ($ zipFile );
119
105
}
120
106
107
+ /**
108
+ * Test PclZip
109
+ *
110
+ * @covers ::<public>
111
+ */
112
+ public function testPCLZip ()
113
+ {
114
+ $ this ->testZipArchive ('PhpOffice\PhpWord\Shared\ZipArchive ' );
115
+ }
116
+
121
117
/**
122
118
* Delete directory
123
119
*
0 commit comments