Skip to content

Allowing no file in the PathProcessor #614

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

Merged
Merged
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ parameters:
identifier: missingType.generics

-
message: "#^Parameter \\#1 \\$field of method Cake\\\\Datasource\\\\EntityInterface\\:\\:get\\(\\) expects string, array\\<int, string\\>\\|string given\\.$#"
message: '#^Parameter \#1 \$field of method Cake\\Datasource\\EntityInterface\:\:get\(\) expects string, array\<string\>\|string given\.$#'
count: 1
path: src/File/Path/DefaultProcessor.php

Expand Down
8 changes: 4 additions & 4 deletions src/File/Path/Basepath/DefaultTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ public function basepath(): string
if ($value === null) {
throw new LogicException(sprintf(
'Field value for substitution is missing: %s',
$field
$field,
));
} elseif (!is_scalar($value)) {
throw new LogicException(sprintf(
'Field value for substitution must be a integer, float, string or boolean: %s',
$field
$field,
));
} elseif (strlen((string)$value) < 1) {
throw new LogicException(sprintf(
'Field value for substitution must be non-zero in length: %s',
$field
$field,
));
}

Expand All @@ -74,7 +74,7 @@ public function basepath(): string
return str_replace(
array_keys($replacements),
array_values($replacements),
$path
$path,
);
}
}
2 changes: 1 addition & 1 deletion src/File/Path/DefaultProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct(
EntityInterface $entity,
UploadedFileInterface|string $data,
string $field,
array $settings
array $settings,
) {
$this->table = $table;
$this->entity = $entity;
Expand Down
2 changes: 1 addition & 1 deletion src/File/Path/Filename/DefaultTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function filename(): string
$this->entity,
$this->data,
$this->field,
$this->settings
$this->settings,
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/File/Path/ProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
EntityInterface $entity,
string|UploadedFileInterface $data,
string $field,
array $settings
array $settings,
);

/**
Expand Down
2 changes: 1 addition & 1 deletion src/File/Transformer/DefaultTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
protected EntityInterface $entity,
protected UploadedFileInterface $data,
protected string $field,
protected array $settings
protected array $settings,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/File/Transformer/TransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
EntityInterface $entity,
UploadedFileInterface $data,
string $field,
array $settings
array $settings,
);

/**
Expand Down
2 changes: 1 addition & 1 deletion src/File/Writer/DefaultWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
protected EntityInterface $entity,
protected ?UploadedFileInterface $data,
protected string $field,
protected array $settings
protected array $settings,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/File/Writer/WriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
EntityInterface $entity,
?UploadedFileInterface $data,
string $field,
array $settings
array $settings,
);

/**
Expand Down
14 changes: 9 additions & 5 deletions src/Model/Behavior/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra
$result = true;

foreach ($this->getConfig(null, []) as $field => $settings) {
if (in_array($field, $this->protectedFieldNames) || Hash::get($settings, 'keepFilesOnDelete', true)) {
if (
in_array($field, $this->protectedFieldNames)
|| Hash::get($settings, 'keepFilesOnDelete', true)
|| $entity->get($field) === null
) {
continue;
}

Expand Down Expand Up @@ -206,7 +210,7 @@ public function getPathProcessor(
EntityInterface $entity,
string|UploadedFileInterface $data,
string $field,
array $settings
array $settings,
): ProcessorInterface {
/** @var class-string<\Josegonzalez\Upload\File\Path\ProcessorInterface> $processorClass */
$processorClass = Hash::get($settings, 'pathProcessor', DefaultProcessor::class);
Expand All @@ -227,7 +231,7 @@ public function getWriter(
EntityInterface $entity,
?UploadedFileInterface $data,
string $field,
array $settings
array $settings,
): WriterInterface {
/** @var class-string<\Josegonzalez\Upload\File\Writer\WriterInterface> $writerClass */
$writerClass = Hash::get($settings, 'writer', DefaultWriter::class);
Expand Down Expand Up @@ -262,7 +266,7 @@ public function constructFiles(
UploadedFileInterface $data,
string $field,
array $settings,
array $pathinfo
array $pathinfo,
): array {
$basepath = $pathinfo['basepath'];
$filename = $pathinfo['filename'];
Expand All @@ -284,7 +288,7 @@ public function constructFiles(
} else {
throw new UnexpectedValueException(sprintf(
"'transformer' not set to instance of TransformerInterface: %s",
$transformerClass
$transformerClass,
));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stub/ChildBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function constructFiles(
UploadedFileInterface $data,
string $field,
array $settings,
array $pathinfo
array $pathinfo,
): array {
$files = parent::constructFiles($entity, $data, $field, $settings, $pathinfo);
$this->constructedFiles = $files;
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/File/Transformer/DefaultTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testTransform()
{
$this->assertEquals(
[$this->uploadedFile->getStream()->getMetadata('uri') => 'foo.txt'],
$this->transformer->transform('foo.txt')
$this->transformer->transform('foo.txt'),
);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/File/Writer/DefaultWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function setUp(): void
$this->entity,
$this->data,
$this->field,
$this->settings
$this->settings,
);

$this->vfs = Vfs::setup('tmp');
Expand Down
50 changes: 35 additions & 15 deletions tests/TestCase/Model/Behavior/UploadBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function setUp(): void
1,
UPLOAD_ERR_OK,
'derp',
'text/plain'
'text/plain',
),
];

Expand All @@ -45,7 +45,7 @@ public function setUp(): void
fopen('php://temp', 'wb+'),
0,
UPLOAD_ERR_NO_FILE,
'derp'
'derp',
),
];
$this->configError = [
Expand Down Expand Up @@ -284,13 +284,13 @@ public function testBeforeMarshalDataAsArray()
->will($this->returnValue($this->settings));

$data = new ArrayObject(
$this->transformUploadedFilesToArray($this->dataOk)
$this->transformUploadedFilesToArray($this->dataOk),
);
$behavior->beforeMarshal(new Event('fake.event'), $data, new ArrayObject());
$this->assertEquals(new ArrayObject($this->transformUploadedFilesToArray($this->dataOk)), $data);

$data = new ArrayObject(
$this->transformUploadedFilesToArray($this->dataError)
$this->transformUploadedFilesToArray($this->dataError),
);
$behavior->beforeMarshal(new Event('fake.event'), $data, new ArrayObject());
$this->assertEquals(new ArrayObject([]), $data);
Expand Down Expand Up @@ -524,7 +524,7 @@ public function testAfterDeleteUsesPathProcessorToDetectPathToTheFile()
->with('dir')
->will($this->returnValue(false));

$this->entity->expects($this->exactly(2))
$this->entity->expects($this->exactly(3))
->method('get')
->with('field')
->will($this->returnValue($field));
Expand Down Expand Up @@ -601,7 +601,7 @@ public function testAfterDeleteNoDeleteCallback()
$this->configOk['field']['deleteCallback'] = null;

$behavior->setConfig($this->configOk);
$this->entity->expects($this->exactly(2))
$this->entity->expects($this->exactly(3))
->method('get')
->with('field')
->will($this->returnValue($field));
Expand Down Expand Up @@ -642,7 +642,7 @@ public function testAfterDeleteUsesDeleteCallback()
};

$behavior->setConfig($this->configOk);
$this->entity->expects($this->exactly(4))
$this->entity->expects($this->exactly(5))
->method('get')
->with('field')
->will($this->returnValue($field));
Expand Down Expand Up @@ -687,6 +687,26 @@ public function testAfterDeleteWithProtectedFieldName()
$this->assertTrue($behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject()));
}

public function testAfterDeleteWithNullableFileField()
{
$methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']);
$behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior')
->onlyMethods($methods)
->setConstructorArgs([$this->table, $this->settings])
->getMock();
$behavior->setConfig($this->configOk);

$this->entity->expects($this->once())
->method('get')
->with('field')
->will($this->returnValue(null));

$behavior->expects($this->never())
->method('getPathProcessor');

$behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject());
}

public function testGetWriter()
{
$processor = $this->behavior->getWriter($this->entity, new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'), 'field', []);
Expand All @@ -700,7 +720,7 @@ public function testConstructFiles()
new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'),
'field',
[],
['basepath' => 'path', 'filename' => 'file.txt']
['basepath' => 'path', 'filename' => 'file.txt'],
);
$this->assertEquals(['php://temp' => 'path/file.txt'], $files);

Expand All @@ -709,7 +729,7 @@ public function testConstructFiles()
new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'),
'field',
[],
['basepath' => 'some/path', 'filename' => 'file.txt']
['basepath' => 'some/path', 'filename' => 'file.txt'],
);
$this->assertEquals(['php://temp' => 'some/path/file.txt'], $files);
}
Expand All @@ -721,7 +741,7 @@ public function testConstructFilesWithBasePathEndingDS()
new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'),
'field',
[],
['basepath' => 'path/', 'filename' => 'file.txt']
['basepath' => 'path/', 'filename' => 'file.txt'],
);
$this->assertEquals(['php://temp' => 'path/file.txt'], $files);

Expand All @@ -730,7 +750,7 @@ public function testConstructFilesWithBasePathEndingDS()
new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'),
'field',
[],
['basepath' => 'some/path/', 'filename' => 'file.txt']
['basepath' => 'some/path/', 'filename' => 'file.txt'],
);
$this->assertEquals(['php://temp' => 'some/path/file.txt'], $files);
}
Expand All @@ -745,7 +765,7 @@ public function testConstructFilesWithCallable()
new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'),
'field',
['transformer' => $callable],
['basepath' => 'some/path', 'filename' => 'file.txt']
['basepath' => 'some/path', 'filename' => 'file.txt'],
);
$this->assertEquals(['php://temp' => 'some/path/file.text'], $files);
}
Expand All @@ -760,7 +780,7 @@ public function testConstructFilesWithCallableAndBasePathEndingDS()
new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK),
'field',
['transformer' => $callable],
['basepath' => 'some/path', 'filename' => 'file.txt']
['basepath' => 'some/path', 'filename' => 'file.txt'],
);
$this->assertEquals(['php://temp' => 'some/path/file.text'], $files);
}
Expand All @@ -773,7 +793,7 @@ public function testConstructFilesException()
new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'),
'field',
['transformer' => 'UnexpectedValueException'],
['basepath' => 'path', 'filename' => 'file.txt']
['basepath' => 'path', 'filename' => 'file.txt'],
);
}

Expand Down Expand Up @@ -821,7 +841,7 @@ function (UploadedFileInterface $file) {
'size' => $file->getSize(),
];
},
$data
$data,
);
}
}