Skip to content

Commit 7884373

Browse files
committed
fixed existing tests and some mistakes revealed by them
1 parent 85dd84a commit 7884373

File tree

12 files changed

+40
-37
lines changed

12 files changed

+40
-37
lines changed

Tests/Controller/AbstractValidationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testEvents()
5656
// event data
5757
$validationCount = 0;
5858

59-
$dispatcher->addListener(UploadEvents::VALIDATION, function(ValidationEvent $event) use (&$validationCount) {
59+
$dispatcher->addListener(UploadEvents::VALIDATION, function() use (&$validationCount) {
6060
++ $validationCount;
6161
});
6262

Tests/Controller/BlueimpValidationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testEvents()
5252
// event data
5353
$validationCount = 0;
5454

55-
$dispatcher->addListener(UploadEvents::VALIDATION, function(ValidationEvent $event) use (&$validationCount) {
55+
$dispatcher->addListener(UploadEvents::VALIDATION, function() use (&$validationCount) {
5656
++ $validationCount;
5757
});
5858

Tests/Uploader/Chunk/ChunkManagerTest.php renamed to Tests/Uploader/Chunk/Storage/FilesystemStorageTest.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
<?php
22

3-
namespace Oneup\UploaderBundle\Tests\Uploader\Chunk;
3+
namespace Oneup\UploaderBundle\Tests\Uploader\Chunk\Storage;
44

55
use Symfony\Component\Finder\Finder;
66
use Symfony\Component\Filesystem\Filesystem;
7+
use Oneup\UploaderBundle\Uploader\Chunk\Storage\FilesystemStorage;
78

8-
use Oneup\UploaderBundle\Uploader\Chunk\ChunkManager;
9-
10-
class ChunkManagerTest extends \PHPUnit_Framework_TestCase
9+
class FilesystemStorageTest extends \PHPUnit_Framework_TestCase
1110
{
1211
protected $tmpDir;
1312

@@ -49,7 +48,7 @@ public function testChunkCleanup()
4948
{
5049
// get a manager configured with a max-age of 5 minutes
5150
$maxage = 5 * 60;
52-
$manager = $this->getManager($maxage);
51+
$manager = $this->getStorage();
5352
$numberOfFiles = 10;
5453

5554
$finder = new Finder();
@@ -58,7 +57,7 @@ public function testChunkCleanup()
5857
$this->fillDirectory($numberOfFiles);
5958
$this->assertCount(10, $finder);
6059

61-
$manager->clear();
60+
$manager->clear($maxage);
6261

6362
$this->assertTrue(is_dir($this->tmpDir));
6463
$this->assertTrue(is_writeable($this->tmpDir));
@@ -75,18 +74,17 @@ public function testClearIfDirectoryDoesNotExist()
7574
$filesystem = new Filesystem();
7675
$filesystem->remove($this->tmpDir);
7776

78-
$manager = $this->getManager(10);
79-
$manager->clear();
77+
$manager = $this->getStorage();
78+
$manager->clear(10);
8079

8180
// yey, no exception
8281
$this->assertTrue(true);
8382
}
8483

85-
protected function getManager($maxage)
84+
protected function getStorage()
8685
{
87-
return new ChunkManager(array(
88-
'directory' => $this->tmpDir,
89-
'maxage' => $maxage
86+
return new FilesystemStorage(array(
87+
'directory' => $this->tmpDir
9088
));
9189
}
9290

Tests/Uploader/Naming/UniqidNamerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ class UniqidNamerTest extends \PHPUnit_Framework_TestCase
88
{
99
public function testNamerReturnsName()
1010
{
11-
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
11+
$file = $this->getMockBuilder('Oneup\UploaderBundle\Uploader\File\FilesystemFile')
1212
->disableOriginalConstructor()
1313
->getMock()
1414
;
1515

1616
$file
1717
->expects($this->any())
18-
->method('guessExtension')
18+
->method('getExtension')
1919
->will($this->returnValue('jpeg'))
2020
;
2121

@@ -25,14 +25,14 @@ public function testNamerReturnsName()
2525

2626
public function testNamerReturnsUniqueName()
2727
{
28-
$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
28+
$file = $this->getMockBuilder('Oneup\UploaderBundle\Uploader\File\FilesystemFile')
2929
->disableOriginalConstructor()
3030
->getMock()
3131
;
3232

3333
$file
3434
->expects($this->any())
35-
->method('guessExtension')
35+
->method('getExtension')
3636
->will($this->returnValue('jpeg'))
3737
;
3838

Tests/Uploader/Storage/FilesystemStorageTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Oneup\UploaderBundle\Tests\Uploader\Storage;
44

5+
use Oneup\UploaderBundle\Uploader\File\FilesystemFile;
56
use Symfony\Component\Finder\Finder;
67
use Symfony\Component\Filesystem\Filesystem;
78
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -26,7 +27,7 @@ public function setUp()
2627

2728
public function testUpload()
2829
{
29-
$payload = new UploadedFile($this->file, 'grumpycat.jpeg', null, null, null, true);
30+
$payload = new FilesystemFile(new UploadedFile($this->file, 'grumpycat.jpeg', null, null, null, true));
3031
$storage = new FilesystemStorage($this->directory);
3132
$storage->upload($payload, 'notsogrumpyanymore.jpeg');
3233

Tests/Uploader/Storage/GaufretteStorageTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Oneup\UploaderBundle\Tests\Uploader\Storage;
44

5+
use Oneup\UploaderBundle\Uploader\File\FilesystemFile;
56
use Symfony\Component\Finder\Finder;
67
use Symfony\Component\Filesystem\Filesystem;
78
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -33,7 +34,7 @@ public function setUp()
3334

3435
public function testUpload()
3536
{
36-
$payload = new UploadedFile($this->file, 'grumpycat.jpeg', null, null, null, true);
37+
$payload = new FilesystemFile(new UploadedFile($this->file, 'grumpycat.jpeg', null, null, null, true));
3738
$this->storage->upload($payload, 'notsogrumpyanymore.jpeg');
3839

3940
$finder = new Finder();

Tests/Uploader/Storage/OrphanageStorageTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Oneup\UploaderBundle\Tests\Uploader\Storage;
44

5+
use Oneup\UploaderBundle\Uploader\File\FilesystemFile;
56
use Symfony\Component\Finder\Finder;
67
use Symfony\Component\Filesystem\Filesystem;
78
use Symfony\Component\HttpFoundation\File\UploadedFile;
@@ -39,7 +40,7 @@ public function setUp()
3940
fwrite($pointer, str_repeat('A', 1024), 1024);
4041
fclose($pointer);
4142

42-
$this->payloads[] = new UploadedFile($file, $i . 'grumpycat.jpeg', null, null, null, true);
43+
$this->payloads[] = new FilesystemFile(new UploadedFile($file, $i . 'grumpycat.jpeg', null, null, null, true));
4344
}
4445

4546
// create underlying storage

Uploader/File/FilesystemFile.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
namespace Oneup\UploaderBundle\Uploader\File;
44

5+
use Symfony\Component\HttpFoundation\File\File;
56
use Symfony\Component\HttpFoundation\File\UploadedFile;
67

78
class FilesystemFile extends UploadedFile implements FileInterface
89
{
9-
public function __construct(UploadedFile $file)
10+
public function __construct(File $file)
1011
{
11-
parent::__construct($file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType(), $file->getClientSize(), $file->getError(), true);
12+
if ($file instanceof UploadedFile) {
13+
parent::__construct($file->getPathname(), $file->getClientOriginalName(), $file->getClientMimeType(), $file->getClientSize(), $file->getError(), true);
14+
} else {
15+
parent::__construct($file->getPathname(), $file->getBasename(), $file->getMimeType(), $file->getSize(), 0, true);
16+
}
1217

1318
}
1419

1520
public function getExtension()
1621
{
17-
// If the file is in tmp, it has no extension, but the wrapper object
18-
// will have the original extension, otherwise it is better to rely
19-
// on the actual extension
20-
return parent::getExtension() ? :$this->getClientOriginalExtension();
22+
return $this->getClientOriginalExtension();
2123
}
2224
}

Uploader/Storage/FilesystemStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Oneup\UploaderBundle\Uploader\Storage;
44

5-
use Oneup\UploaderBundle\Uploader\File\FilesystemFile;
5+
use Oneup\UploaderBundle\Uploader\File\FileInterface;
66

77
class FilesystemStorage implements StorageInterface
88
{
@@ -13,7 +13,7 @@ public function __construct($directory)
1313
$this->directory = $directory;
1414
}
1515

16-
public function upload($file, $name, $path = null)
16+
public function upload(FileInterface $file, $name, $path = null)
1717
{
1818
$path = is_null($path) ? $name : sprintf('%s/%s', $path, $name);
1919
$path = sprintf('%s/%s', $this->directory, $path);

Uploader/Storage/GaufretteStorage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ class GaufretteStorage extends StreamManager implements StorageInterface
1212
{
1313
protected $streamWrapperPrefix;
1414

15-
public function __construct(Filesystem $filesystem, $bufferSize, $streamWrapperPrefix)
15+
public function __construct(Filesystem $filesystem, $bufferSize, $streamWrapperPrefix = null)
1616
{
1717
$this->filesystem = $filesystem;
1818
$this->bufferSize = $bufferSize;
1919
$this->streamWrapperPrefix = $streamWrapperPrefix;
2020
}
2121

22-
public function upload($file, $name, $path = null)
22+
public function upload(FileInterface $file, $name, $path = null)
2323
{
2424
$path = is_null($path) ? $name : sprintf('%s/%s', $path, $name);
2525

0 commit comments

Comments
 (0)