aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/maennchen/zipstream-php/test/FaultInjectionResource.php
blob: 3d4440e8a41b7221eb0fc630de308729f75eea46 (plain) (blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

declare(strict_types=1);

namespace ZipStream\Test;

class FaultInjectionResource
{
    public const NAME = 'zipstream-php-test-broken-resource';

    /** @var resource */
    public $context;

    private array $injectFaults;

    private string $mode;

    /**
     * @return resource
     */
    public static function getResource(array $injectFaults)
    {
        self::register();

        return fopen(self::NAME . '://foobar', 'rw+', false, self::createStreamContext($injectFaults));
    }

    public function stream_open(string $path, string $mode, int $options, string &$opened_path = null): bool
    {
        $options = stream_context_get_options($this->context);

        if (!isset($options[self::NAME]['injectFaults'])) {
            return false;
        }

        $this->mode = $mode;
        $this->injectFaults = $options[self::NAME]['injectFaults'];

        if ($this->shouldFail(__FUNCTION__)) {
            return false;
        }

        return true;
    }

    public function stream_write(string $data)
    {
        if ($this->shouldFail(__FUNCTION__)) {
            return false;
        }
        return true;
    }

    public function stream_eof()
    {
        return true;
    }

    public function stream_seek(int $offset, int $whence): bool
    {
        if ($this->shouldFail(__FUNCTION__)) {
            return false;
        }

        return true;
    }

    public function stream_tell(): int
    {
        if ($this->shouldFail(__FUNCTION__)) {
            return false;
        }

        return 0;
    }

    public static function register(): void
    {
        if (!in_array(self::NAME, stream_get_wrappers(), true)) {
            stream_wrapper_register(self::NAME, __CLASS__);
        }
    }

    public function stream_stat(): array
    {
        static $modeMap = [
            'r'  => 33060,
            'rb' => 33060,
            'r+' => 33206,
            'w'  => 33188,
            'wb' => 33188,
        ];

        return [
            'dev'     => 0,
            'ino'     => 0,
            'mode'    => $modeMap[$this->mode],
            'nlink'   => 0,
            'uid'     => 0,
            'gid'     => 0,
            'rdev'    => 0,
            'size'    => 0,
            'atime'   => 0,
            'mtime'   => 0,
            'ctime'   => 0,
            'blksize' => 0,
            'blocks'  => 0,
        ];
    }

    public function url_stat(string $path, int $flags): array
    {
        return [
            'dev'     => 0,
            'ino'     => 0,
            'mode'    => 0,
            'nlink'   => 0,
            'uid'     => 0,
            'gid'     => 0,
            'rdev'    => 0,
            'size'    => 0,
            'atime'   => 0,
            'mtime'   => 0,
            'ctime'   => 0,
            'blksize' => 0,
            'blocks'  => 0,
        ];
    }

    private static function createStreamContext(array $injectFaults)
    {
        return stream_context_create([
            self::NAME => ['injectFaults' => $injectFaults],
        ]);
    }

    private function shouldFail(string $function): bool
    {
        return in_array($function, $this->injectFaults, true);
    }
}