|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Sandro Keil (https://sandro-keil.de) |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/sandrokeil/interop-config for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2017-2017 Sandro Keil |
|
7
|
|
|
* @license http://github.com/sandrokeil/interop-config/blob/master/LICENSE.md New BSD License |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace InteropTest\Config\TestAsset; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Helper class to catch outputs and inputs to emulate CLI in unit tests |
|
14
|
|
|
* |
|
15
|
|
|
* @see http://no2.php.net/manual/en/stream.streamwrapper.example-1.php |
|
16
|
|
|
*/ |
|
17
|
|
|
class TestStream |
|
18
|
|
|
{ |
|
19
|
|
|
private $position; |
|
20
|
|
|
private $varname; |
|
21
|
|
|
|
|
22
|
|
|
public static $data = []; |
|
23
|
|
|
public static $inputStack = []; |
|
24
|
|
|
|
|
25
|
|
|
public function stream_open($path, $mode, $options, &$opened_path) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
$this->varname = parse_url($path)['host']; |
|
28
|
|
|
$this->position = 0; |
|
29
|
|
|
self::$data[$this->varname] = ''; |
|
30
|
|
|
|
|
31
|
|
|
return true; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function stream_read($count) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
if (!empty(self::$inputStack)) { |
|
37
|
|
|
return array_shift(self::$inputStack); |
|
38
|
|
|
} |
|
39
|
|
|
return ''; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function stream_write($data) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$left = substr(self::$data[$this->varname], 0, $this->position); |
|
45
|
|
|
$right = substr(self::$data[$this->varname], $this->position + strlen($data)); |
|
46
|
|
|
self::$data[$this->varname] = $left . $data . $right; |
|
47
|
|
|
$this->position += strlen($data); |
|
48
|
|
|
return strlen($data); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function stream_tell() |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
return $this->position; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function stream_eof() |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
return $this->position >= strlen(self::$data[$this->varname]); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function stream_seek($offset, $whence) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
switch ($whence) { |
|
64
|
|
|
case SEEK_SET: |
|
65
|
|
|
if ($offset < strlen(self::$data[$this->varname]) && $offset >= 0) { |
|
66
|
|
|
$this->position = $offset; |
|
67
|
|
|
return true; |
|
68
|
|
|
} else { |
|
69
|
|
|
return false; |
|
70
|
|
|
} |
|
71
|
|
|
break; |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
case SEEK_CUR: |
|
74
|
|
|
if ($offset >= 0) { |
|
75
|
|
|
$this->position += $offset; |
|
76
|
|
|
return true; |
|
77
|
|
|
} else { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
break; |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
case SEEK_END: |
|
83
|
|
|
if (strlen(self::$data[$this->varname]) + $offset >= 0) { |
|
84
|
|
|
$this->position = strlen(self::$data[$this->varname]) + $offset; |
|
85
|
|
|
return true; |
|
86
|
|
|
} else { |
|
87
|
|
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
break; |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
default: |
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function stream_metadata($path, $option, $var) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
if ($option == STREAM_META_TOUCH) { |
|
99
|
|
|
$url = parse_url($path); |
|
100
|
|
|
$varname = $url['host']; |
|
101
|
|
|
if (!isset(self::$data[$varname])) { |
|
102
|
|
|
self::$data[$varname] = ''; |
|
103
|
|
|
} |
|
104
|
|
|
return true; |
|
105
|
|
|
} |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.