summaryrefslogtreecommitdiff
path: root/tests/BaseTestCase.php
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2018-09-02 18:58:32 -0400
committerPaul Duncan <pabs@pablotron.org>2018-09-03 09:54:47 -0400
commite4850cba6fd67a6d4d9d8c969281e57447c17fd9 (patch)
tree29fb04954bdabde80a2208d6fa88c575cc7ed802 /tests/BaseTestCase.php
parentd70e1b519d27e5b0090412c566bee79d57f7838e (diff)
downloadzipstream-php-e4850cba6fd67a6d4d9d8c969281e57447c17fd9.tar.bz2
zipstream-php-e4850cba6fd67a6d4d9d8c969281e57447c17fd9.zip
add tests/ and composer test script
Diffstat (limited to 'tests/BaseTestCase.php')
-rw-r--r--tests/BaseTestCase.php49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php
new file mode 100644
index 0000000..8934e96
--- /dev/null
+++ b/tests/BaseTestCase.php
@@ -0,0 +1,49 @@
+<?php
+declare(strict_types = 1);
+
+namespace Pablotron\ZipStream\Tests;
+
+use PHPUnit\Framework\TestCase;
+use \Pablotron\ZipStream\ZipStream;
+use \Pablotron\ZipStream\FileWriter;
+
+class BaseTestCase extends TestCase {
+ protected function open_archive(string $path) {
+ # open archive, check for error
+ $zip = new \ZipArchive();
+ if ($zip->open($path) !== true) {
+ throw new Exception("ZipArchive#open() failed: $dst_name");
+ }
+
+ # return archive
+ return $zip;
+ }
+
+ protected function with_temp_file(callable $cb) : void {
+ # build path to temp file
+ $path = tempnam('/tmp', 'zipstream-test');
+ try {
+ # pass to test
+ $cb($path);
+ } finally {
+ if (file_exists($path)) {
+ # remove temp file if it exists
+ unlink($path);
+ }
+ }
+ }
+
+ protected function with_temp_zip(callable $zip_cb, callable $test_cb = null) {
+ $this->with_temp_file(function(string $dst_path) use ($zip_cb, $test_cb) {
+ # create zip, pass to zip callback
+ ZipStream::send($dst_path, $zip_cb, [
+ 'output' => new FileWriter(),
+ ]);
+
+ if ($test_cb) {
+ # pass to test callback
+ $test_cb($dst_path);
+ }
+ });
+ }
+};