diff --git a/.travis.yml b/.travis.yml index b3d75aa..b848430 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,4 +13,4 @@ before_script: notifications: email: false -script: phpunit +script: vendor/bin/phpunit diff --git a/src/FileUpload/FileUploadFactory.php b/src/FileUpload/FileUploadFactory.php index 9e6d562..e771083 100644 --- a/src/FileUpload/FileUploadFactory.php +++ b/src/FileUpload/FileUploadFactory.php @@ -2,6 +2,7 @@ namespace FileUpload; +use FileUpload\FileNameGenerator; use FileUpload\FileSystem\FileSystem; use FileUpload\PathResolver\PathResolver; use FileUpload\Validator\Validator; @@ -10,7 +11,7 @@ class FileUploadFactory { /** * Validator to be used in the factory - * @var array + * @var Validator[] */ protected $validators; @@ -26,20 +27,29 @@ class FileUploadFactory */ protected $filesystem; + /** + * FileNameGenerator to be used in the factory + * @var FileNameGenerator\FileNameGenerator + */ + protected $fileNameGenerator; + /** * Construct new factory with the given modules * @param PathResolver $pathresolver - * @param FileSystem $filesystem - * @param array $validators + * @param FileSystem $filesystem + * @param array $validators + * @param FileNameGenerator\FileNameGenerator|null $fileNameGenerator */ public function __construct( PathResolver $pathresolver, FileSystem $filesystem, - $validators = [] + $validators = [], + FileNameGenerator\FileNameGenerator $fileNameGenerator = null ) { $this->pathresolver = $pathresolver; $this->filesystem = $filesystem; $this->validators = $validators; + $this->fileNameGenerator = $fileNameGenerator; } /** @@ -53,6 +63,9 @@ public function create($upload, $server) $fileupload = new FileUpload($upload, $server); $fileupload->setPathResolver($this->pathresolver); $fileupload->setFileSystem($this->filesystem); + if (null !== $this->fileNameGenerator) { + $fileupload->setFileNameGenerator($this->fileNameGenerator); + } foreach ($this->validators as $validator) { $fileupload->addValidator($validator); diff --git a/tests/FileUpload/FileNameGenerator/MD5Test.php b/tests/FileUpload/FileNameGenerator/MD5Test.php index 3bf511d..df57055 100644 --- a/tests/FileUpload/FileNameGenerator/MD5Test.php +++ b/tests/FileUpload/FileNameGenerator/MD5Test.php @@ -16,11 +16,11 @@ public function setUp() $playground_path = __DIR__ . '/../../playground'; $fixtures_path = __DIR__ . '/../../fixtures'; - if (! is_dir($playground_path)) { + if (!is_dir($playground_path)) { mkdir($playground_path); } - if (! is_dir($playground_path . '/uploaded')) { + if (!is_dir($playground_path . '/uploaded')) { mkdir($playground_path . '/uploaded'); } @@ -45,13 +45,13 @@ public function testGenerator() $filename = "picture.jpg"; $new_filename = md5("picture") . ".jpg"; - $server = [ 'CONTENT_TYPE' => 'image/jpg', 'CONTENT_LENGTH' => 30321 ]; - $file = [ 'tmp_name' => $playground_path . '/real-image.jpg', - 'name' => 'real-image.jpg', - 'size' => 30321, - 'type' => 'image/jpg', - 'error' => 0 - ]; + $server = ['CONTENT_TYPE' => 'image/jpg', 'CONTENT_LENGTH' => 30321]; + $file = ['tmp_name' => $playground_path . '/real-image.jpg', + 'name' => 'real-image.jpg', + 'size' => 30321, + 'type' => 'image/jpg', + 'error' => 0 + ]; $fileUpload = new FileUpload($file, $server, $generator); $fileUpload->setFileSystem(new Mock()); @@ -70,13 +70,13 @@ public function testUploadFailsBecauseFileAlreadyExistsOnTheFileSystem() $filename = "real-image.jpg"; - $server = [ 'CONTENT_TYPE' => 'image/jpg', 'CONTENT_LENGTH' => 30321 ]; - $file = [ 'tmp_name' => $playground_path . '/uploaded/real-image.jpg', - 'name' => 'real-image.jpg', - 'size' => 30321, - 'type' => 'image/jpg', - 'error' => 0 - ]; + $server = ['CONTENT_TYPE' => 'image/jpg', 'CONTENT_LENGTH' => 30321]; + $file = ['tmp_name' => $playground_path . '/uploaded/real-image.jpg', + 'name' => 'real-image.jpg', + 'size' => 30321, + 'type' => 'image/jpg', + 'error' => 0 + ]; $fileUpload = new FileUpload($file, $server, $generator); $fileUpload->setFileSystem(new Mock()); @@ -96,13 +96,13 @@ public function testFileIsUploadedDespiteAlreadyExistingOnTheFileSystem() $filename = "real-image.jpg"; $newFileName = md5("real-image") . '.jpg'; - $server = [ 'CONTENT_TYPE' => 'image/jpg', 'CONTENT_LENGTH' => 30321 ]; - $file = [ 'tmp_name' => $playground_path . '/uploaded/real-image.jpg', - 'name' => 'real-image.jpg', - 'size' => 30321, - 'type' => 'image/jpg', - 'error' => 0 - ]; + $server = ['CONTENT_TYPE' => 'image/jpg', 'CONTENT_LENGTH' => 30321]; + $file = ['tmp_name' => $playground_path . '/uploaded/real-image.jpg', + 'name' => 'real-image.jpg', + 'size' => 30321, + 'type' => 'image/jpg', + 'error' => 0 + ]; $fileUpload = new FileUpload($file, $server, $generator); $fileUpload->setFileSystem(new Mock()); diff --git a/tests/FileUpload/Validator/DimensionValidatorTest.php b/tests/FileUpload/Validator/DimensionValidatorTest.php index 2f2af59..84d21e3 100644 --- a/tests/FileUpload/Validator/DimensionValidatorTest.php +++ b/tests/FileUpload/Validator/DimensionValidatorTest.php @@ -297,7 +297,7 @@ public function testSetErrorMessages() ); } - protected function setUp() + public function setUp() { $this->directory = __DIR__ . '/../../fixtures/'; } diff --git a/tests/FileUpload/Validator/MimeTypeValidatorTest.php b/tests/FileUpload/Validator/MimeTypeValidatorTest.php index 2ba6046..f4f7f78 100644 --- a/tests/FileUpload/Validator/MimeTypeValidatorTest.php +++ b/tests/FileUpload/Validator/MimeTypeValidatorTest.php @@ -42,7 +42,7 @@ public function testInvalidMimeType() $this->assertFalse($this->validator->validate($file, $_FILES['file']['size'])); } - protected function setUp() + public function setUp() { $this->directory = __DIR__ . '/../../fixtures/'; diff --git a/tests/FileUpload/Validator/SizeValidatorTest.php b/tests/FileUpload/Validator/SizeValidatorTest.php index 1bf4a1f..abf265e 100644 --- a/tests/FileUpload/Validator/SizeValidatorTest.php +++ b/tests/FileUpload/Validator/SizeValidatorTest.php @@ -11,7 +11,7 @@ class SizeValidatorTest extends TestCase protected $validator; protected $file; - protected function setUp() + public function setUp() { $this->directory = __DIR__ . '/../../fixtures/';