Skip to content

Commit

Permalink
Merge pull request #379 from jorisvaesen/master
Browse files Browse the repository at this point in the history
Fix double directory separator in constructFiles when following docs
  • Loading branch information
josegonzalez committed Mar 27, 2016
2 parents c298568 + 0a9f083 commit d123726
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ passed in under each field in your behavior configuration.
- Default: (string)
``Josegonzalez\Upload\File\Transformer\DefaultTransformer``

- ``path``: A path relative to the ``filesystem.root``. Should end in ``{DS}``
- ``path``: A path relative to the ``filesystem.root``.

- Default: (string)
``'webroot{DS}files{DS}{model}{DS}{field}{DS}'``
Expand Down
5 changes: 3 additions & 2 deletions src/Model/Behavior/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,20 @@ public function getWriter(Entity $entity, $data, $field, $settings)
*/
public function constructFiles(Entity $entity, $data, $field, $settings, $basepath)
{
$basepath = (substr($basepath, -1) == DS ? $basepath : $basepath . DS);
$default = 'Josegonzalez\Upload\File\Transformer\DefaultTransformer';
$transformerClass = Hash::get($settings, 'transformer', $default);
$results = [];
if (is_subclass_of($transformerClass, 'Josegonzalez\Upload\File\Transformer\TransformerInterface')) {
$transformer = new $transformerClass($this->_table, $entity, $data, $field, $settings);
$results = $transformer->transform();
foreach ($results as $key => $value) {
$results[$key] = $basepath . '/' . $value;
$results[$key] = $basepath . $value;
}
} elseif (is_callable($transformerClass)) {
$results = $transformerClass($this->_table, $entity, $data, $field, $settings);
foreach ($results as $key => $value) {
$results[$key] = $basepath . '/' . $value;
$results[$key] = $basepath . $value;
}
} else {
throw new UnexpectedValueException(sprintf(
Expand Down
36 changes: 36 additions & 0 deletions tests/TestCase/Model/Behavior/UploadBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,27 @@ public function testConstructFiles()
$this->assertEquals(['path/to/file/on/disk' => 'some/path/file.txt'], $files);
}

public function testConstructFilesWithBasePathEndingDS()
{
$files = $this->behavior->constructFiles(
$this->entity,
['tmp_name' => 'path/to/file/on/disk', 'name' => 'file.txt'],
'field',
[],
'path/'
);
$this->assertEquals(['path/to/file/on/disk' => 'path/file.txt'], $files);

$files = $this->behavior->constructFiles(
$this->entity,
['tmp_name' => 'path/to/file/on/disk', 'name' => 'file.txt'],
'field',
[],
'some/path/'
);
$this->assertEquals(['path/to/file/on/disk' => 'some/path/file.txt'], $files);
}

public function testConstructFilesWithCallable()
{
$callable = function () {
Expand All @@ -324,6 +345,21 @@ public function testConstructFilesWithCallable()
$this->assertEquals(['path/to/callable/file/on/disk' => 'some/path/file.text'], $files);
}

public function testConstructFilesWithCallableAndBasePathEndingDS()
{
$callable = function () {
return ['path/to/callable/file/on/disk' => 'file.text'];
};
$files = $this->behavior->constructFiles(
$this->entity,
['tmp_name' => 'path/to/file/on/disk', 'name' => 'file.txt'],
'field',
['transformer' => $callable],
'some/path/'
);
$this->assertEquals(['path/to/callable/file/on/disk' => 'some/path/file.text'], $files);
}

public function testConstructFilesException()
{
$this->setExpectedException('UnexpectedValueException', "'transformer' not set to instance of TransformerInterface: UnexpectedValueException");
Expand Down

0 comments on commit d123726

Please sign in to comment.