Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double directory separator in constructFiles when following docs #379

Merged
merged 1 commit into from
Mar 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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