aboutsummaryrefslogtreecommitdiff
path: root/tests/BaseTestCase.php
blob: 8934e96c82d29fd939086a2d79e322c9758dd19b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
      }
    });
  }
};