tests: mock parser tests file access
[lhc/web/wiklou.git] / tests / phpunit / mocks / filebackend / MockFileBackend.php
1 <?php
2 /**
3 * Simulation (mock) of a backend storage.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup FileBackend
22 * @author Antoine Musso <hashar@free.fr>
23 */
24
25 /**
26 * Class simulating a backend store.
27 *
28 * @ingroup FileBackend
29 * @since 1.22
30 */
31 class MockFileBackend extends FileBackendStore {
32
33 protected $mocked = array();
34
35 /** Poor man debugging */
36 protected function debug( $msg = '' ) {
37 wfDebug( wfGetCaller() . "$msg\n" );
38 }
39
40 public function isPathUsableInternal( $storagePath ) {
41 return true;
42 }
43
44 protected function doCreateInternal( array $params ) {
45 if( isset( $params['content'] ) ) {
46 $content = $params['content'];
47 } else {
48 $content = 'Default mocked file content';
49 }
50 $this->debug(serialize($params));
51 $dst = $params['dst'];
52 $this->mocked[$dst] = $content;
53 return Status::newGood();
54 }
55
56 protected function doStoreInternal( array $params ) {
57 $this->debug(serialize($params));
58 return $this->doCreateInternal( $params );
59 }
60
61 protected function doCopyInternal( array $params ) {
62 $this->debug(serialize($params));
63 $src = $params['src'];
64 $dst = $params['dst'];
65 $this->mocked[$dst] = $this->mocked[$src];
66 return Status::newGood();
67 }
68
69 protected function doDeleteInternal( array $params ) {
70 $this->debug(serialize($params));
71 $src = $params['src'];
72 unset( $this->mocked[$src] );
73 return Status::newGood();
74 }
75
76 protected function doGetFileStat( array $params ) {
77 $src = $params['src'];
78 if( array_key_exists( $src, $this->mocked ) ) {
79 $this->debug( "('$src') found" );
80 return array(
81 'mtime' => wfTimestamp( TS_MW ),
82 'size' => strlen( $this->mocked[$src] ),
83 # No sha1, stat does not need it.
84 );
85 } else {
86 $this->debug( "('$src') not found" );
87 return false;
88 }
89 }
90
91 protected function doGetLocalCopyMulti( array $params ) {
92 $tmpFiles = array(); // (path => MockFSFile)
93
94 $this->debug( '(' . serialize($params) . ')' );
95 foreach( $params['srcs'] as $src ) {
96 $tmpFiles[$src] = new MockFSFile(
97 wfTempDir() . '/' . wfRandomString(32)
98 );
99 }
100 return $tmpFiles;
101 }
102
103 protected function doDirectoryExists( $container, $dir, array $params ) {
104 $this->debug();
105 return true;
106 }
107
108 public function getDirectoryListInternal( $container, $dir, array $params ) {
109 $this->debug();
110 return array();
111 }
112
113 public function getFileListInternal( $container, $dir, array $params ) {
114 $this->debug();
115 return array();
116 }
117
118 protected function directoriesAreVirtual() {
119 $this->debug();
120 return true;
121 }
122 }