File: StreamHelper.php

package info (click to toggle)
php-http-interop-http-factory-tests 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 152 kB
  • sloc: php: 549; makefile: 7
file content (52 lines) | stat: -rw-r--r-- 1,093 bytes parent folder | download
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
50
51
52
<?php

namespace Interop\Http\Factory;

use RuntimeException;
use function fopen;
use function fwrite;
use function is_file;
use function rewind;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;

trait StreamHelper
{
    protected static $tempFiles = [];

    protected function createTemporaryFile()
    {
        $file = tempnam(sys_get_temp_dir(), 'http_factory_tests_');

        if($file === false){
            throw new RuntimeException('could not create temp file');
        }

        static::$tempFiles[] = $file;

        return $file;
    }

    protected function createTemporaryResource($content = null)
    {
        $file = $this->createTemporaryFile();
        $resource = fopen($file, 'r+');

        if ($content) {
            fwrite($resource, $content);
            rewind($resource);
        }

        return $resource;
    }

    public static function tearDownAfterClass(): void
    {
        foreach (static::$tempFiles as $tempFile) {
            if (is_file($tempFile)) {
                unlink($tempFile);
            }
        }
    }
}