33247f6bd9b9dbfd9c8dbb6d5c8c269bab002032
[yaffs-website] / vendor / mikey179 / vfsStream / src / test / php / org / bovigo / vfs / vfsStreamWrapperFileTimesTestCase.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  * Test for org\bovigo\vfs\vfsStreamWrapper.
13  *
14  * @since  0.9.0
15  */
16 class vfsStreamWrapperFileTimesTestCase extends \PHPUnit_Framework_TestCase
17 {
18     /**
19      * URL of foo.txt file
20      *
21      * @var  string
22      */
23     protected $fooUrl;
24     /**
25      * URL of bar directory
26      *
27      * @var  string
28      */
29     protected $barUrl;
30     /**
31      * URL of baz.txt file
32      *
33      * @var  string
34      */
35     protected $bazUrl;
36
37     /**
38      * set up test environment
39      */
40     public function setUp()
41     {
42         vfsStream::setup()
43                  ->lastModified(50)
44                  ->lastAccessed(50)
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');
49     }
50
51     /**
52      * helper assertion for the tests
53      *
54      * @param  string            $url      url to check
55      * @param  vfsStreamContent  $content  content to compare
56      */
57     protected function assertFileTimesEqualStreamTimes($url, vfsStreamContent $content)
58     {
59         $this->assertEquals(filemtime($url), $content->filemtime());
60         $this->assertEquals(fileatime($url), $content->fileatime());
61         $this->assertEquals(filectime($url), $content->filectime());
62     }
63
64     /**
65      * @test
66      * @group  issue_7
67      * @group  issue_26
68      */
69     public function openFileChangesAttributeTimeOnly()
70     {
71         $file = vfsStream::newFile('foo.txt')
72                          ->withContent('test')
73                          ->at(vfsStreamWrapper::getRoot())
74                          ->lastModified(100)
75                          ->lastAccessed(100)
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);
83     }
84
85     /**
86      * @test
87      * @group  issue_7
88      * @group  issue_26
89      */
90     public function fileGetContentsChangesAttributeTimeOnly()
91     {
92         $file = vfsStream::newFile('foo.txt')
93                          ->withContent('test')
94                          ->at(vfsStreamWrapper::getRoot())
95                          ->lastModified(100)
96                          ->lastAccessed(100)
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);
104     }
105
106     /**
107      * @test
108      * @group  issue_7
109      * @group  issue_26
110      */
111     public function openFileWithTruncateChangesAttributeAndModificationTime()
112     {
113         $file = vfsStream::newFile('foo.txt')
114                          ->withContent('test')
115                          ->at(vfsStreamWrapper::getRoot())
116                          ->lastModified(100)
117                          ->lastAccessed(100)
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);
126     }
127
128     /**
129      * @test
130      * @group  issue_7
131      */
132     public function readFileChangesAccessTime()
133     {
134         $file = vfsStream::newFile('foo.txt')
135                          ->withContent('test')
136                          ->at(vfsStreamWrapper::getRoot())
137                          ->lastModified(100)
138                          ->lastAccessed(100)
139                          ->lastAttributeModified(100);
140         $fp = fopen($this->fooUrl, 'rb');
141         $openTime = time();
142         sleep(2);
143         fread($fp, 1024);
144         fclose($fp);
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);
149     }
150
151     /**
152      * @test
153      * @group  issue_7
154      */
155     public function writeFileChangesModificationTime()
156     {
157         $file = vfsStream::newFile('foo.txt')
158                          ->at(vfsStreamWrapper::getRoot())
159                          ->lastModified(100)
160                          ->lastAccessed(100)
161                          ->lastAttributeModified(100);
162         $fp = fopen($this->fooUrl, 'wb');
163         $openTime = time();
164         sleep(2);
165         fwrite($fp, 'test');
166         fclose($fp);
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);
171
172     }
173
174     /**
175      * @test
176      * @group  issue_7
177      */
178     public function createNewFileSetsAllTimesToCurrentTime()
179     {
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'));
185     }
186
187     /**
188      * @test
189      * @group  issue_7
190      */
191     public function createNewFileChangesAttributeAndModificationTimeOfContainingDirectory()
192     {
193         $dir = vfsStream::newDirectory('bar')
194                         ->at(vfsStreamWrapper::getRoot())
195                         ->lastModified(100)
196                         ->lastAccessed(100)
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);
203     }
204
205     /**
206      * @test
207      * @group  issue_7
208      */
209     public function addNewFileNameWithLinkFunctionChangesAttributeTimeOfOriginalFile()
210     {
211         $this->markTestSkipped('Links are currently not supported by vfsStream.');
212     }
213
214     /**
215      * @test
216      * @group  issue_7
217      */
218     public function addNewFileNameWithLinkFunctionChangesAttributeAndModificationTimeOfDirectoryContainingLink()
219     {
220         $this->markTestSkipped('Links are currently not supported by vfsStream.');
221     }
222
223     /**
224      * @test
225      * @group  issue_7
226      */
227     public function removeFileChangesAttributeAndModificationTimeOfContainingDirectory()
228     {
229         $dir = vfsStream::newDirectory('bar')
230                         ->at(vfsStreamWrapper::getRoot());
231         $file = vfsStream::newFile('baz.txt')
232                          ->at($dir)
233                          ->lastModified(100)
234                          ->lastAccessed(100)
235                          ->lastAttributeModified(100);
236         $dir->lastModified(100)
237             ->lastAccessed(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);
244     }
245
246     /**
247      * @test
248      * @group  issue_7
249      */
250     public function renameFileChangesAttributeAndModificationTimeOfAffectedDirectories()
251     {
252         $target = vfsStream::newDirectory('target')
253                            ->at(vfsStreamWrapper::getRoot())
254                            ->lastModified(200)
255                            ->lastAccessed(200)
256                            ->lastAttributeModified(200);
257         $source = vfsStream::newDirectory('bar')
258                            ->at(vfsStreamWrapper::getRoot());
259         $file = vfsStream::newFile('baz.txt')
260                          ->at($source)
261                          ->lastModified(300)
262                          ->lastAccessed(300)
263                          ->lastAttributeModified(300);
264         $source->lastModified(100)
265                ->lastAccessed(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);
276     }
277
278     /**
279      * @test
280      * @group  issue_7
281      */
282     public function renameFileDoesNotChangeFileTimesOfFileItself()
283     {
284         $target = vfsStream::newDirectory('target')
285                            ->at(vfsStreamWrapper::getRoot())
286                            ->lastModified(200)
287                            ->lastAccessed(200)
288                            ->lastAttributeModified(200);
289         $source = vfsStream::newDirectory('bar')
290                            ->at(vfsStreamWrapper::getRoot());
291         $file = vfsStream::newFile('baz.txt')
292                          ->at($source)
293                          ->lastModified(300)
294                          ->lastAccessed(300)
295                          ->lastAttributeModified(300);
296         $source->lastModified(100)
297                ->lastAccessed(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);
304     }
305
306     /**
307      * @test
308      * @group  issue_7
309      */
310     public function changeFileAttributesChangesAttributeTimeOfFileItself()
311     {
312         $this->markTestSkipped('Changing file attributes via stream wrapper for self-defined streams is not supported by PHP.');
313     }
314 }