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 * Test for org\bovigo\vfs\vfsStreamWrapper.
16 class vfsStreamWrapperFileTimesTestCase extends \PHPUnit_Framework_TestCase
25 * URL of bar directory
38 * set up test environment
40 public function setUp()
45 ->lastAttributeModified(50);
46 $this->fooUrl = vfsStream::url('root/foo.txt');
47 $this->barUrl = vfsStream::url('root/bar');
48 $this->bazUrl = vfsStream::url('root/bar/baz.txt');
52 * helper assertion for the tests
54 * @param string $url url to check
55 * @param vfsStreamContent $content content to compare
57 protected function assertFileTimesEqualStreamTimes($url, vfsStreamContent $content)
59 $this->assertEquals(filemtime($url), $content->filemtime());
60 $this->assertEquals(fileatime($url), $content->fileatime());
61 $this->assertEquals(filectime($url), $content->filectime());
69 public function openFileChangesAttributeTimeOnly()
71 $file = vfsStream::newFile('foo.txt')
73 ->at(vfsStreamWrapper::getRoot())
76 ->lastAttributeModified(100);
77 fclose(fopen($this->fooUrl, 'rb'));
78 $this->assertGreaterThan(time() - 2, fileatime($this->fooUrl));
79 $this->assertLessThanOrEqual(time(), fileatime($this->fooUrl));
80 $this->assertLessThanOrEqual(100, filemtime($this->fooUrl));
81 $this->assertEquals(100, filectime($this->fooUrl));
82 $this->assertFileTimesEqualStreamTimes($this->fooUrl, $file);
90 public function fileGetContentsChangesAttributeTimeOnly()
92 $file = vfsStream::newFile('foo.txt')
94 ->at(vfsStreamWrapper::getRoot())
97 ->lastAttributeModified(100);
98 file_get_contents($this->fooUrl);
99 $this->assertGreaterThan(time() - 2, fileatime($this->fooUrl));
100 $this->assertLessThanOrEqual(time(), fileatime($this->fooUrl));
101 $this->assertLessThanOrEqual(100, filemtime($this->fooUrl));
102 $this->assertEquals(100, filectime($this->fooUrl));
103 $this->assertFileTimesEqualStreamTimes($this->fooUrl, $file);
111 public function openFileWithTruncateChangesAttributeAndModificationTime()
113 $file = vfsStream::newFile('foo.txt')
114 ->withContent('test')
115 ->at(vfsStreamWrapper::getRoot())
118 ->lastAttributeModified(100);
119 fclose(fopen($this->fooUrl, 'wb'));
120 $this->assertGreaterThan(time() - 2, filemtime($this->fooUrl));
121 $this->assertGreaterThan(time() - 2, fileatime($this->fooUrl));
122 $this->assertLessThanOrEqual(time(), filemtime($this->fooUrl));
123 $this->assertLessThanOrEqual(time(), fileatime($this->fooUrl));
124 $this->assertEquals(100, filectime($this->fooUrl));
125 $this->assertFileTimesEqualStreamTimes($this->fooUrl, $file);
132 public function readFileChangesAccessTime()
134 $file = vfsStream::newFile('foo.txt')
135 ->withContent('test')
136 ->at(vfsStreamWrapper::getRoot())
139 ->lastAttributeModified(100);
140 $fp = fopen($this->fooUrl, 'rb');
145 $this->assertLessThanOrEqual($openTime, filemtime($this->fooUrl));
146 $this->assertLessThanOrEqual($openTime + 3, fileatime($this->fooUrl));
147 $this->assertEquals(100, filectime($this->fooUrl));
148 $this->assertFileTimesEqualStreamTimes($this->fooUrl, $file);
155 public function writeFileChangesModificationTime()
157 $file = vfsStream::newFile('foo.txt')
158 ->at(vfsStreamWrapper::getRoot())
161 ->lastAttributeModified(100);
162 $fp = fopen($this->fooUrl, 'wb');
167 $this->assertLessThanOrEqual($openTime + 3, filemtime($this->fooUrl));
168 $this->assertLessThanOrEqual($openTime, fileatime($this->fooUrl));
169 $this->assertEquals(100, filectime($this->fooUrl));
170 $this->assertFileTimesEqualStreamTimes($this->fooUrl, $file);
178 public function createNewFileSetsAllTimesToCurrentTime()
180 file_put_contents($this->fooUrl, 'test');
181 $this->assertLessThanOrEqual(time(), filemtime($this->fooUrl));
182 $this->assertEquals(fileatime($this->fooUrl), filectime($this->fooUrl));
183 $this->assertEquals(fileatime($this->fooUrl), filemtime($this->fooUrl));
184 $this->assertFileTimesEqualStreamTimes($this->fooUrl, vfsStreamWrapper::getRoot()->getChild('foo.txt'));
191 public function createNewFileChangesAttributeAndModificationTimeOfContainingDirectory()
193 $dir = vfsStream::newDirectory('bar')
194 ->at(vfsStreamWrapper::getRoot())
197 ->lastAttributeModified(100);
198 file_put_contents($this->bazUrl, 'test');
199 $this->assertLessThanOrEqual(time(), filemtime($this->barUrl));
200 $this->assertLessThanOrEqual(time(), filectime($this->barUrl));
201 $this->assertEquals(100, fileatime($this->barUrl));
202 $this->assertFileTimesEqualStreamTimes($this->barUrl, $dir);
209 public function addNewFileNameWithLinkFunctionChangesAttributeTimeOfOriginalFile()
211 $this->markTestSkipped('Links are currently not supported by vfsStream.');
218 public function addNewFileNameWithLinkFunctionChangesAttributeAndModificationTimeOfDirectoryContainingLink()
220 $this->markTestSkipped('Links are currently not supported by vfsStream.');
227 public function removeFileChangesAttributeAndModificationTimeOfContainingDirectory()
229 $dir = vfsStream::newDirectory('bar')
230 ->at(vfsStreamWrapper::getRoot());
231 $file = vfsStream::newFile('baz.txt')
235 ->lastAttributeModified(100);
236 $dir->lastModified(100)
238 ->lastAttributeModified(100);
239 unlink($this->bazUrl);
240 $this->assertLessThanOrEqual(time(), filemtime($this->barUrl));
241 $this->assertLessThanOrEqual(time(), filectime($this->barUrl));
242 $this->assertEquals(100, fileatime($this->barUrl));
243 $this->assertFileTimesEqualStreamTimes($this->barUrl, $dir);
250 public function renameFileChangesAttributeAndModificationTimeOfAffectedDirectories()
252 $target = vfsStream::newDirectory('target')
253 ->at(vfsStreamWrapper::getRoot())
256 ->lastAttributeModified(200);
257 $source = vfsStream::newDirectory('bar')
258 ->at(vfsStreamWrapper::getRoot());
259 $file = vfsStream::newFile('baz.txt')
263 ->lastAttributeModified(300);
264 $source->lastModified(100)
266 ->lastAttributeModified(100);
267 rename($this->bazUrl, vfsStream::url('root/target/baz.txt'));
268 $this->assertLessThanOrEqual(time(), filemtime($this->barUrl));
269 $this->assertLessThanOrEqual(time(), filectime($this->barUrl));
270 $this->assertEquals(100, fileatime($this->barUrl));
271 $this->assertFileTimesEqualStreamTimes($this->barUrl, $source);
272 $this->assertLessThanOrEqual(time(), filemtime(vfsStream::url('root/target')));
273 $this->assertLessThanOrEqual(time(), filectime(vfsStream::url('root/target')));
274 $this->assertEquals(200, fileatime(vfsStream::url('root/target')));
275 $this->assertFileTimesEqualStreamTimes(vfsStream::url('root/target'), $target);
282 public function renameFileDoesNotChangeFileTimesOfFileItself()
284 $target = vfsStream::newDirectory('target')
285 ->at(vfsStreamWrapper::getRoot())
288 ->lastAttributeModified(200);
289 $source = vfsStream::newDirectory('bar')
290 ->at(vfsStreamWrapper::getRoot());
291 $file = vfsStream::newFile('baz.txt')
295 ->lastAttributeModified(300);
296 $source->lastModified(100)
298 ->lastAttributeModified(100);
299 rename($this->bazUrl, vfsStream::url('root/target/baz.txt'));
300 $this->assertEquals(300, filemtime(vfsStream::url('root/target/baz.txt')));
301 $this->assertEquals(300, filectime(vfsStream::url('root/target/baz.txt')));
302 $this->assertEquals(300, fileatime(vfsStream::url('root/target/baz.txt')));
303 $this->assertFileTimesEqualStreamTimes(vfsStream::url('root/target/baz.txt'), $file);
310 public function changeFileAttributesChangesAttributeTimeOfFileItself()
312 $this->markTestSkipped('Changing file attributes via stream wrapper for self-defined streams is not supported by PHP.');