Skip to content

Commit

Permalink
added ability to set FileNameGenerator from Factory (#75)
Browse files Browse the repository at this point in the history
* added ability to set FileNameGenerator from Factory

* - fixed code styling
- fixed compatible setUp method

* - made request changes

* updated version of php unit

* updated version of php unit

* updated version of php unit
  • Loading branch information
kruglikdenis authored and adelowo committed Feb 14, 2019
1 parent a551d8c commit 1b7327f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ before_script:
notifications:
email: false

script: phpunit
script: vendor/bin/phpunit
21 changes: 17 additions & 4 deletions src/FileUpload/FileUploadFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FileUpload;

use FileUpload\FileNameGenerator;
use FileUpload\FileSystem\FileSystem;
use FileUpload\PathResolver\PathResolver;
use FileUpload\Validator\Validator;
Expand All @@ -10,7 +11,7 @@ class FileUploadFactory
{
/**
* Validator to be used in the factory
* @var array
* @var Validator[]
*/
protected $validators;

Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand Down
46 changes: 23 additions & 23 deletions tests/FileUpload/FileNameGenerator/MD5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion tests/FileUpload/Validator/DimensionValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public function testSetErrorMessages()
);
}

protected function setUp()
public function setUp()
{
$this->directory = __DIR__ . '/../../fixtures/';
}
Expand Down
2 changes: 1 addition & 1 deletion tests/FileUpload/Validator/MimeTypeValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/';

Expand Down
2 changes: 1 addition & 1 deletion tests/FileUpload/Validator/SizeValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class SizeValidatorTest extends TestCase
protected $validator;
protected $file;

protected function setUp()
public function setUp()
{
$this->directory = __DIR__ . '/../../fixtures/';

Expand Down

0 comments on commit 1b7327f

Please sign in to comment.