5fe15d030eb27e41c323af706d1490c754e16752
[yaffs-website] / vendor / mikey179 / vfsStream / src / test / php / org / bovigo / vfs / vfsStreamFileTestCase.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\vfsStreamFile.
13  */
14 class vfsStreamFileTestCase extends \PHPUnit_Framework_TestCase
15 {
16     /**
17      * instance to test
18      *
19      * @var  vfsStreamFile
20      */
21     protected $file;
22
23     /**
24      * set up test environment
25      */
26     public function setUp()
27     {
28         $this->file = new vfsStreamFile('foo');
29     }
30
31     /**
32      * test default values and methods
33      *
34      * @test
35      */
36     public function defaultValues()
37     {
38         $this->assertEquals(vfsStreamContent::TYPE_FILE, $this->file->getType());
39         $this->assertEquals('foo', $this->file->getName());
40         $this->assertTrue($this->file->appliesTo('foo'));
41         $this->assertFalse($this->file->appliesTo('foo/bar'));
42         $this->assertFalse($this->file->appliesTo('bar'));
43     }
44
45     /**
46      * test setting and getting the content of a file
47      *
48      * @test
49      */
50     public function content()
51     {
52         $this->assertNull($this->file->getContent());
53         $this->assertSame($this->file, $this->file->setContent('bar'));
54         $this->assertEquals('bar', $this->file->getContent());
55         $this->assertSame($this->file, $this->file->withContent('baz'));
56         $this->assertEquals('baz', $this->file->getContent());
57     }
58
59     /**
60      * test renaming the directory
61      *
62      * @test
63      */
64     public function rename()
65     {
66         $this->file->rename('bar');
67         $this->assertEquals('bar', $this->file->getName());
68         $this->assertFalse($this->file->appliesTo('foo'));
69         $this->assertFalse($this->file->appliesTo('foo/bar'));
70         $this->assertTrue($this->file->appliesTo('bar'));
71     }
72
73     /**
74      * test reading contents from the file
75      *
76      * @test
77      */
78     public function readEmptyFile()
79     {
80         $this->assertTrue($this->file->eof());
81         $this->assertEquals(0, $this->file->size());
82         $this->assertEquals('', $this->file->read(5));
83         $this->assertEquals(5, $this->file->getBytesRead());
84         $this->assertTrue($this->file->eof());
85     }
86
87     /**
88      * test reading contents from the file
89      *
90      * @test
91      */
92     public function read()
93     {
94         $this->file->setContent('foobarbaz');
95         $this->assertFalse($this->file->eof());
96         $this->assertEquals(9, $this->file->size());
97         $this->assertEquals('foo', $this->file->read(3));
98         $this->assertEquals(3, $this->file->getBytesRead());
99         $this->assertFalse($this->file->eof());
100         $this->assertEquals(9, $this->file->size());
101         $this->assertEquals('bar', $this->file->read(3));
102         $this->assertEquals(6, $this->file->getBytesRead());
103         $this->assertFalse($this->file->eof());
104         $this->assertEquals(9, $this->file->size());
105         $this->assertEquals('baz', $this->file->read(3));
106         $this->assertEquals(9, $this->file->getBytesRead());
107         $this->assertEquals(9, $this->file->size());
108         $this->assertTrue($this->file->eof());
109         $this->assertEquals('', $this->file->read(3));
110     }
111
112     /**
113      * test seeking to offset
114      *
115      * @test
116      */
117     public function seekEmptyFile()
118     {
119         $this->assertFalse($this->file->seek(0, 55));
120         $this->assertTrue($this->file->seek(0, SEEK_SET));
121         $this->assertEquals(0, $this->file->getBytesRead());
122         $this->assertTrue($this->file->seek(5, SEEK_SET));
123         $this->assertEquals(5, $this->file->getBytesRead());
124         $this->assertTrue($this->file->seek(0, SEEK_CUR));
125         $this->assertEquals(5, $this->file->getBytesRead());
126         $this->assertTrue($this->file->seek(2, SEEK_CUR));
127         $this->assertEquals(7, $this->file->getBytesRead());
128         $this->assertTrue($this->file->seek(0, SEEK_END));
129         $this->assertEquals(0, $this->file->getBytesRead());
130         $this->assertTrue($this->file->seek(2, SEEK_END));
131         $this->assertEquals(2, $this->file->getBytesRead());
132     }
133
134     /**
135      * test seeking to offset
136      *
137      * @test
138      */
139     public function seekRead()
140     {
141         $this->file->setContent('foobarbaz');
142         $this->assertFalse($this->file->seek(0, 55));
143         $this->assertTrue($this->file->seek(0, SEEK_SET));
144         $this->assertEquals('foobarbaz', $this->file->readUntilEnd());
145         $this->assertEquals(0, $this->file->getBytesRead());
146         $this->assertTrue($this->file->seek(5, SEEK_SET));
147         $this->assertEquals('rbaz', $this->file->readUntilEnd());
148         $this->assertEquals(5, $this->file->getBytesRead());
149         $this->assertTrue($this->file->seek(0, SEEK_CUR));
150         $this->assertEquals('rbaz', $this->file->readUntilEnd());
151         $this->assertEquals(5, $this->file->getBytesRead(), 5);
152         $this->assertTrue($this->file->seek(2, SEEK_CUR));
153         $this->assertEquals('az', $this->file->readUntilEnd());
154         $this->assertEquals(7, $this->file->getBytesRead());
155         $this->assertTrue($this->file->seek(0, SEEK_END));
156         $this->assertEquals('', $this->file->readUntilEnd());
157         $this->assertEquals(9, $this->file->getBytesRead());
158         $this->assertTrue($this->file->seek(2, SEEK_END));
159         $this->assertEquals('', $this->file->readUntilEnd());
160         $this->assertEquals(11, $this->file->getBytesRead());
161     }
162
163     /**
164      * test writing data into the file
165      *
166      * @test
167      */
168     public function writeEmptyFile()
169     {
170         $this->assertEquals(3, $this->file->write('foo'));
171         $this->assertEquals('foo', $this->file->getContent());
172         $this->assertEquals(3, $this->file->size());
173         $this->assertEquals(3, $this->file->write('bar'));
174         $this->assertEquals('foobar', $this->file->getContent());
175         $this->assertEquals(6, $this->file->size());
176     }
177
178     /**
179      * test writing data into the file
180      *
181      * @test
182      */
183     public function write()
184     {
185         $this->file->setContent('foobarbaz');
186         $this->assertTrue($this->file->seek(3, SEEK_SET));
187         $this->assertEquals(3, $this->file->write('foo'));
188         $this->assertEquals('foofoobaz', $this->file->getContent());
189         $this->assertEquals(9, $this->file->size());
190         $this->assertEquals(3, $this->file->write('bar'));
191         $this->assertEquals('foofoobar', $this->file->getContent());
192         $this->assertEquals(9, $this->file->size());
193     }
194
195     /**
196      * setting and retrieving permissions for a file
197      *
198      * @test
199      * @group  permissions
200      */
201     public function permissions()
202     {
203         $this->assertEquals(0666, $this->file->getPermissions());
204         $this->assertSame($this->file, $this->file->chmod(0644));
205         $this->assertEquals(0644, $this->file->getPermissions());
206     }
207
208     /**
209      * setting and retrieving permissions for a file
210      *
211      * @test
212      * @group  permissions
213      */
214     public function permissionsSet()
215     {
216         $this->file = new vfsStreamFile('foo', 0644);
217         $this->assertEquals(0644, $this->file->getPermissions());
218         $this->assertSame($this->file, $this->file->chmod(0600));
219         $this->assertEquals(0600, $this->file->getPermissions());
220     }
221
222     /**
223      * setting and retrieving owner of a file
224      *
225      * @test
226      * @group  permissions
227      */
228     public function owner()
229     {
230         $this->assertEquals(vfsStream::getCurrentUser(), $this->file->getUser());
231         $this->assertTrue($this->file->isOwnedByUser(vfsStream::getCurrentUser()));
232         $this->assertSame($this->file, $this->file->chown(vfsStream::OWNER_USER_1));
233         $this->assertEquals(vfsStream::OWNER_USER_1, $this->file->getUser());
234         $this->assertTrue($this->file->isOwnedByUser(vfsStream::OWNER_USER_1));
235     }
236
237     /**
238      * setting and retrieving owner group of a file
239      *
240      * @test
241      * @group  permissions
242      */
243     public function group()
244     {
245         $this->assertEquals(vfsStream::getCurrentGroup(), $this->file->getGroup());
246         $this->assertTrue($this->file->isOwnedByGroup(vfsStream::getCurrentGroup()));
247         $this->assertSame($this->file, $this->file->chgrp(vfsStream::GROUP_USER_1));
248         $this->assertEquals(vfsStream::GROUP_USER_1, $this->file->getGroup());
249         $this->assertTrue($this->file->isOwnedByGroup(vfsStream::GROUP_USER_1));
250     }
251
252     /**
253      * @test
254      * @group  issue_33
255      * @since  1.1.0
256      */
257     public function truncateRemovesSuperflouosContent()
258     {
259         $this->assertEquals(11, $this->file->write("lorem ipsum"));
260         $this->assertTrue($this->file->truncate(5));
261         $this->assertEquals(5, $this->file->size());
262         $this->assertEquals('lorem', $this->file->getContent());
263     }
264
265     /**
266      * @test
267      * @group  issue_33
268      * @since  1.1.0
269      */
270     public function truncateToGreaterSizeAddsZeroBytes()
271     {
272         $this->assertEquals(11, $this->file->write("lorem ipsum"));
273         $this->assertTrue($this->file->truncate(25));
274         $this->assertEquals(25, $this->file->size());
275         $this->assertEquals("lorem ipsum\0\0\0\0\0\0\0\0\0\0\0\0\0\0", $this->file->getContent());
276     }
277
278     /**
279      * @test
280      * @group  issue_79
281      * @since  1.3.0
282      */
283     public function withContentAcceptsAnyFileContentInstance()
284     {
285         $mockFileContent = $this->getMock('org\bovigo\vfs\content\FileContent');
286         $mockFileContent->expects($this->once())
287                         ->method('content')
288                         ->will($this->returnValue('foobarbaz'));
289         $this->assertEquals(
290                 'foobarbaz',
291                 $this->file->withContent($mockFileContent)
292                            ->getContent()
293         );
294     }
295
296     /**
297      * @test
298      * @group  issue_79
299      * @expectedException  \InvalidArgumentException
300      * @since  1.3.0
301      */
302     public function withContentThrowsInvalidArgumentExceptionWhenContentIsNoStringAndNoFileContent()
303     {
304         $this->file->withContent(313);
305     }
306 }