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