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\vfsStreamFile.
14 class vfsStreamFileTestCase extends \PHPUnit_Framework_TestCase
24 * set up test environment
26 public function setUp()
28 $this->file = new vfsStreamFile('foo');
32 * test default values and methods
36 public function defaultValues()
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'));
46 * test setting and getting the content of a file
50 public function content()
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());
60 * test renaming the directory
64 public function rename()
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'));
74 * test reading contents from the file
78 public function readEmptyFile()
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());
88 * test reading contents from the file
92 public function read()
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));
113 * test seeking to offset
117 public function seekEmptyFile()
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());
135 * test seeking to offset
139 public function seekRead()
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());
164 * test writing data into the file
168 public function writeEmptyFile()
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());
179 * test writing data into the file
183 public function write()
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());
196 * setting and retrieving permissions for a file
201 public function permissions()
203 $this->assertEquals(0666, $this->file->getPermissions());
204 $this->assertSame($this->file, $this->file->chmod(0644));
205 $this->assertEquals(0644, $this->file->getPermissions());
209 * setting and retrieving permissions for a file
214 public function permissionsSet()
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());
223 * setting and retrieving owner of a file
228 public function owner()
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));
238 * setting and retrieving owner group of a file
243 public function group()
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));
257 public function truncateRemovesSuperflouosContent()
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());
270 public function truncateToGreaterSizeAddsZeroBytes()
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());
283 public function withContentAcceptsAnyFileContentInstance()
285 $mockFileContent = $this->getMock('org\bovigo\vfs\content\FileContent');
286 $mockFileContent->expects($this->once())
288 ->will($this->returnValue('foobarbaz'));
291 $this->file->withContent($mockFileContent)
299 * @expectedException \InvalidArgumentException
302 public function withContentThrowsInvalidArgumentExceptionWhenContentIsNoStringAndNoFileContent()
304 $this->file->withContent(313);