Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f56a6ef

Browse filesBrowse files
pborrelifabpot
authored andcommitted
[HttpFoundation] File/File full coverage
1 parent e540349 commit f56a6ef
Copy full SHA for f56a6ef

File tree

Expand file treeCollapse file tree

2 files changed

+71
-2
lines changed
Filter options
  • src/Symfony/Component/HttpFoundation/File
  • tests/Symfony/Tests/Component/HttpFoundation/File
Expand file treeCollapse file tree

2 files changed

+71
-2
lines changed

‎src/Symfony/Component/HttpFoundation/File/File.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/File/File.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ public function getMimeType()
605605
*/
606606
public function size()
607607
{
608-
if (false === ($size = filesize($this->getPath()))) {
608+
if (false === ($size = @filesize($this->getPath()))) {
609609
throw new FileException(sprintf('Could not read file size of %s', $this->getPath()));
610610
}
611611

@@ -623,7 +623,7 @@ protected function doMove($directory, $filename)
623623
{
624624
$newPath = $directory . DIRECTORY_SEPARATOR . $filename;
625625

626-
if (!rename($this->getPath(), $newPath)) {
626+
if (!@rename($this->getPath(), $newPath)) {
627627
throw new FileException(sprintf('Could not move file %s to %s', $this->getPath(), $newPath));
628628
}
629629

‎tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php

Copy file name to clipboardExpand all lines: tests/Symfony/Tests/Component/HttpFoundation/File/FileTest.php
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,16 @@ public function testGetPathReturnsAbsolutePath()
2828
$this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.gif', $this->file->getPath());
2929
}
3030

31+
public function test__toString()
32+
{
33+
$this->assertEquals(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'test.gif', (string) $this->file);
34+
}
35+
3136
public function testGetWebPathReturnsPathRelativeToDocumentRoot()
3237
{
3338
File::setDocumentRoot(__DIR__);
3439

40+
$this->assertEquals(__DIR__, File::getDocumentRoot());
3541
$this->assertEquals('/Fixtures/test.gif', $this->file->getWebPath());
3642
}
3743

@@ -42,11 +48,24 @@ public function testGetWebPathReturnsEmptyPathIfOutsideDocumentRoot()
4248
$this->assertEquals('', $this->file->getWebPath());
4349
}
4450

51+
public function testSetDocumentRootThrowsLogicExceptionWhenNotExists()
52+
{
53+
$this->setExpectedException('LogicException');
54+
55+
File::setDocumentRoot(__DIR__.'/Fixtures/not_here');
56+
}
57+
4558
public function testGetNameReturnsNameWithExtension()
4659
{
4760
$this->assertEquals('test.gif', $this->file->getName());
4861
}
4962

63+
public function testGetExtensionReturnsEmptyString()
64+
{
65+
$file = new File(__DIR__.'/Fixtures/test');
66+
$this->assertEquals('', $file->getExtension());
67+
}
68+
5069
public function testGetExtensionReturnsExtensionWithDot()
5170
{
5271
$this->assertEquals('.gif', $this->file->getExtension());
@@ -66,6 +85,13 @@ public function testGetMimeTypeUsesMimeTypeGuessers()
6685
$this->assertEquals('image/gif', $this->file->getMimeType());
6786
}
6887

88+
public function testGetDefaultExtensionWithoutGuesser()
89+
{
90+
$file = new File(__DIR__.'/Fixtures/directory/.empty');
91+
92+
$this->assertEquals('.empty', $file->getDefaultExtension());
93+
}
94+
6995
public function testGetDefaultExtensionIsBasedOnMimeType()
7096
{
7197
$file = new File(__DIR__.'/Fixtures/test');
@@ -76,11 +102,33 @@ public function testGetDefaultExtensionIsBasedOnMimeType()
76102
$this->assertEquals('.gif', $file->getDefaultExtension());
77103
}
78104

105+
public function testConstructWhenFileNotExists()
106+
{
107+
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');
108+
109+
new File(__DIR__.'/Fixtures/not_here');
110+
}
111+
79112
public function testSizeReturnsFileSize()
80113
{
81114
$this->assertEquals(filesize($this->file->getPath()), $this->file->size());
82115
}
83116

117+
public function testSizeFailing()
118+
{
119+
$dir = __DIR__.DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'directory';
120+
$path = $dir.DIRECTORY_SEPARATOR.'test.copy.gif';
121+
@unlink($path);
122+
copy(__DIR__.'/Fixtures/test.gif', $path);
123+
124+
$file = new File($path);
125+
@unlink($path);
126+
127+
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');
128+
$file->size($path);
129+
130+
}
131+
84132
public function testMove()
85133
{
86134
$path = __DIR__.'/Fixtures/test.copy.gif';
@@ -121,6 +169,27 @@ public function testMoveWithNewName()
121169
@unlink($targetPath);
122170
}
123171

172+
public function testMoveFailing()
173+
{
174+
$path = __DIR__.'/Fixtures/test.copy.gif';
175+
$targetPath = '/thisfolderwontexist';
176+
@unlink($path);
177+
@unlink($targetPath);
178+
copy(__DIR__.'/Fixtures/test.gif', $path);
179+
180+
$file = new File($path);
181+
182+
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileException');
183+
$file->move($targetPath);
184+
185+
$this->assertFileExists($path);
186+
$this->assertFileNotExists($path.$targetPath.'test.gif');
187+
$this->assertEquals($path, $file->getPath());
188+
189+
@unlink($path);
190+
@unlink($targetPath);
191+
}
192+
124193
public function testRename()
125194
{
126195
$path = __DIR__.'/Fixtures/test.copy.gif';

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.