1ed84b5ae85b3cb2d1d7c465ba6dcd9bcde8516b
[yaffs-website] / vendor / mikey179 / vfsStream / src / test / php / org / bovigo / vfs / proxy / vfsStreamWrapperRecordingProxy.php
1 <?php
2 /**
3  * This file is part of vfsStream.
4  *
5  * For the full copyright and license information, please view the LICENSE
6  * file that was distributed with this source code.
7  *
8  * @package  org\bovigo\vfs
9  */
10 namespace org\bovigo\vfs;
11 /**
12  * Stream wrapper to mock file system requests.
13  *
14  * @since  0.10.0
15  */
16 class vfsStreamWrapperRecordingProxy extends vfsStreamWrapper
17 {
18     /**
19      * list of called methods for a stream
20      *
21      * @var  array
22      */
23     protected static $calledMethods = array();
24     /**
25      * currently opened path
26      *
27      * @var  string
28      */
29     protected $path;
30
31     /**
32      * records method call for given path
33      *
34      * @param  string  $method
35      * @param  string  $path
36      */
37     protected static function recordMethodCall($method, $path)
38     {
39         if (isset(self::$calledMethods[$path]) === false) {
40             self::$calledMethods[$path] = array();
41         }
42
43         self::$calledMethods[$path][] = $method;
44     }
45
46     /**
47      * returns recorded method calls for given path
48      *
49      * @param   string         $path
50      * @return  array<string>
51      */
52     public static function getMethodCalls($path)
53     {
54         if (isset(self::$calledMethods[$path]) === true) {
55             return self::$calledMethods[$path];
56         }
57
58         return array();
59     }
60
61     /**
62      * helper method for setting up vfsStream with the proxy
63      *
64      * @param   string              $rootDirName  optional  name of root directory
65      * @param   int                 $permissions  optional  file permissions of root directory
66      * @return  vfsStreamDirectory
67      * @throws  vfsStreamException
68      */
69     public static function setup($rootDirName = 'root', $permissions = null)
70     {
71         self::$root = vfsStream::newDirectory($rootDirName, $permissions);
72         if (true === self::$registered) {
73             return self::$root;
74         }
75
76         if (@stream_wrapper_register(vfsStream::SCHEME, __CLASS__) === false) {
77             throw new vfsStreamException('A handler has already been registered for the ' . vfsStream::SCHEME . ' protocol.');
78         }
79
80         self::$registered = true;
81         return self::$root;
82     }
83
84     /**
85      * open the stream
86      *
87      * @param   string  $path         the path to open
88      * @param   string  $mode         mode for opening
89      * @param   string  $options      options for opening
90      * @param   string  $opened_path  full path that was actually opened
91      * @return  bool
92      */
93     public function stream_open($path, $mode, $options, $opened_path)
94     {
95         $this->path = $path;
96         self::recordMethodCall('stream_open', $this->path);
97         return parent::stream_open($path, $mode, $options, $opened_path);
98     }
99
100     /**
101      * closes the stream
102      */
103     public function stream_close()
104     {
105         self::recordMethodCall('stream_close', $this->path);
106         return parent::stream_close();
107     }
108
109     /**
110      * read the stream up to $count bytes
111      *
112      * @param   int     $count  amount of bytes to read
113      * @return  string
114      */
115     public function stream_read($count)
116     {
117         self::recordMethodCall('stream_read', $this->path);
118         return parent::stream_read($count);
119     }
120
121     /**
122      * writes data into the stream
123      *
124      * @param   string  $data
125      * @return  int     amount of bytes written
126      */
127     public function stream_write($data)
128     {
129         self::recordMethodCall('stream_write', $this->path);
130         return parent::stream_write($data);
131     }
132
133     /**
134      * checks whether stream is at end of file
135      *
136      * @return  bool
137      */
138     public function stream_eof()
139     {
140         self::recordMethodCall('stream_eof', $this->path);
141         return parent::stream_eof();
142     }
143
144     /**
145      * returns the current position of the stream
146      *
147      * @return  int
148      */
149     public function stream_tell()
150     {
151         self::recordMethodCall('stream_tell', $this->path);
152         return parent::stream_tell();
153     }
154
155     /**
156      * seeks to the given offset
157      *
158      * @param   int   $offset
159      * @param   int   $whence
160      * @return  bool
161      */
162     public function stream_seek($offset, $whence)
163     {
164         self::recordMethodCall('stream_seek', $this->path);
165         return parent::stream_seek($offset, $whence);
166     }
167
168     /**
169      * flushes unstored data into storage
170      *
171      * @return  bool
172      */
173     public function stream_flush()
174     {
175         self::recordMethodCall('stream_flush', $this->path);
176         return parent::stream_flush();
177     }
178
179     /**
180      * returns status of stream
181      *
182      * @return  array
183      */
184     public function stream_stat()
185     {
186         self::recordMethodCall('stream_stat', $this->path);
187         return parent::stream_stat();
188     }
189
190     /**
191      * retrieve the underlaying resource
192      *
193      * @param   int  $cast_as
194      * @return  bool
195      */
196     public function stream_cast($cast_as)
197     {
198         self::recordMethodCall('stream_cast', $this->path);
199         return parent::stream_cast($cast_as);
200     }
201
202     /**
203      * set lock status for stream
204      *
205      * @param   int   $operation
206      * @return  bool
207      */
208     public function stream_lock($operation)
209     {
210         self::recordMethodCall('stream_link', $this->path);
211         return parent::stream_lock($operation);
212     }
213
214     /**
215      * remove the data under the given path
216      *
217      * @param   string  $path
218      * @return  bool
219      */
220     public function unlink($path)
221     {
222         self::recordMethodCall('unlink', $path);
223         return parent::unlink($path);
224     }
225
226     /**
227      * rename from one path to another
228      *
229      * @param   string  $path_from
230      * @param   string  $path_to
231      * @return  bool
232      */
233     public function rename($path_from, $path_to)
234     {
235         self::recordMethodCall('rename', $path_from);
236         return parent::rename($path_from, $path_to);
237     }
238
239     /**
240      * creates a new directory
241      *
242      * @param   string  $path
243      * @param   int     $mode
244      * @param   int     $options
245      * @return  bool
246      */
247     public function mkdir($path, $mode, $options)
248     {
249         self::recordMethodCall('mkdir', $path);
250         return parent::mkdir($path, $mode, $options);
251     }
252
253     /**
254      * removes a directory
255      *
256      * @param   string  $path
257      * @param   int     $options
258      * @return  bool
259      */
260     public function rmdir($path, $options)
261     {
262         self::recordMethodCall('rmdir', $path);
263         return parent::rmdir($path, $options);
264     }
265
266     /**
267      * opens a directory
268      *
269      * @param   string  $path
270      * @param   int     $options
271      * @return  bool
272      */
273     public function dir_opendir($path, $options)
274     {
275         $this->path = $path;
276         self::recordMethodCall('dir_opendir', $this->path);
277         return parent::dir_opendir($path, $options);
278     }
279
280     /**
281      * reads directory contents
282      *
283      * @return  string
284      */
285     public function dir_readdir()
286     {
287         self::recordMethodCall('dir_readdir', $this->path);
288         return parent::dir_readdir();
289     }
290
291     /**
292      * reset directory iteration
293      *
294      * @return  bool
295      */
296     public function dir_rewinddir()
297     {
298         self::recordMethodCall('dir_rewinddir', $this->path);
299         return parent::dir_rewinddir();
300     }
301
302     /**
303      * closes directory
304      *
305      * @return  bool
306      */
307     public function dir_closedir()
308     {
309         self::recordMethodCall('dir_closedir', $this->path);
310         return parent::dir_closedir();
311     }
312
313     /**
314      * returns status of url
315      *
316      * @param   string  $path   path of url to return status for
317      * @param   int     $flags  flags set by the stream API
318      * @return  array
319      */
320     public function url_stat($path, $flags)
321     {
322         self::recordMethodCall('url_stat', $path);
323         return parent::url_stat($path, $flags);
324     }
325 }