3 * This file is part of vfsStream.
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
8 * @package org\bovigo\vfs
10 namespace org\bovigo\vfs;
12 * Stream wrapper to mock file system requests.
16 class vfsStreamWrapperRecordingProxy extends vfsStreamWrapper
19 * list of called methods for a stream
23 protected static $calledMethods = array();
25 * currently opened path
32 * records method call for given path
34 * @param string $method
37 protected static function recordMethodCall($method, $path)
39 if (isset(self::$calledMethods[$path]) === false) {
40 self::$calledMethods[$path] = array();
43 self::$calledMethods[$path][] = $method;
47 * returns recorded method calls for given path
50 * @return array<string>
52 public static function getMethodCalls($path)
54 if (isset(self::$calledMethods[$path]) === true) {
55 return self::$calledMethods[$path];
62 * helper method for setting up vfsStream with the proxy
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
69 public static function setup($rootDirName = 'root', $permissions = null)
71 self::$root = vfsStream::newDirectory($rootDirName, $permissions);
72 if (true === self::$registered) {
76 if (@stream_wrapper_register(vfsStream::SCHEME, __CLASS__) === false) {
77 throw new vfsStreamException('A handler has already been registered for the ' . vfsStream::SCHEME . ' protocol.');
80 self::$registered = true;
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
93 public function stream_open($path, $mode, $options, $opened_path)
96 self::recordMethodCall('stream_open', $this->path);
97 return parent::stream_open($path, $mode, $options, $opened_path);
103 public function stream_close()
105 self::recordMethodCall('stream_close', $this->path);
106 return parent::stream_close();
110 * read the stream up to $count bytes
112 * @param int $count amount of bytes to read
115 public function stream_read($count)
117 self::recordMethodCall('stream_read', $this->path);
118 return parent::stream_read($count);
122 * writes data into the stream
124 * @param string $data
125 * @return int amount of bytes written
127 public function stream_write($data)
129 self::recordMethodCall('stream_write', $this->path);
130 return parent::stream_write($data);
134 * checks whether stream is at end of file
138 public function stream_eof()
140 self::recordMethodCall('stream_eof', $this->path);
141 return parent::stream_eof();
145 * returns the current position of the stream
149 public function stream_tell()
151 self::recordMethodCall('stream_tell', $this->path);
152 return parent::stream_tell();
156 * seeks to the given offset
162 public function stream_seek($offset, $whence)
164 self::recordMethodCall('stream_seek', $this->path);
165 return parent::stream_seek($offset, $whence);
169 * flushes unstored data into storage
173 public function stream_flush()
175 self::recordMethodCall('stream_flush', $this->path);
176 return parent::stream_flush();
180 * returns status of stream
184 public function stream_stat()
186 self::recordMethodCall('stream_stat', $this->path);
187 return parent::stream_stat();
191 * retrieve the underlaying resource
193 * @param int $cast_as
196 public function stream_cast($cast_as)
198 self::recordMethodCall('stream_cast', $this->path);
199 return parent::stream_cast($cast_as);
203 * set lock status for stream
205 * @param int $operation
208 public function stream_lock($operation)
210 self::recordMethodCall('stream_link', $this->path);
211 return parent::stream_lock($operation);
215 * remove the data under the given path
217 * @param string $path
220 public function unlink($path)
222 self::recordMethodCall('unlink', $path);
223 return parent::unlink($path);
227 * rename from one path to another
229 * @param string $path_from
230 * @param string $path_to
233 public function rename($path_from, $path_to)
235 self::recordMethodCall('rename', $path_from);
236 return parent::rename($path_from, $path_to);
240 * creates a new directory
242 * @param string $path
244 * @param int $options
247 public function mkdir($path, $mode, $options)
249 self::recordMethodCall('mkdir', $path);
250 return parent::mkdir($path, $mode, $options);
254 * removes a directory
256 * @param string $path
257 * @param int $options
260 public function rmdir($path, $options)
262 self::recordMethodCall('rmdir', $path);
263 return parent::rmdir($path, $options);
269 * @param string $path
270 * @param int $options
273 public function dir_opendir($path, $options)
276 self::recordMethodCall('dir_opendir', $this->path);
277 return parent::dir_opendir($path, $options);
281 * reads directory contents
285 public function dir_readdir()
287 self::recordMethodCall('dir_readdir', $this->path);
288 return parent::dir_readdir();
292 * reset directory iteration
296 public function dir_rewinddir()
298 self::recordMethodCall('dir_rewinddir', $this->path);
299 return parent::dir_rewinddir();
307 public function dir_closedir()
309 self::recordMethodCall('dir_closedir', $this->path);
310 return parent::dir_closedir();
314 * returns status of url
316 * @param string $path path of url to return status for
317 * @param int $flags flags set by the stream API
320 public function url_stat($path, $flags)
322 self::recordMethodCall('url_stat', $path);
323 return parent::url_stat($path, $flags);