Skip to content

Commit

Permalink
Fixed BC break
Browse files Browse the repository at this point in the history
  • Loading branch information
Yorick van Klinken committed Oct 13, 2020
1 parent 7e49d89 commit 77680dc
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function isNull()
/**
* Move the file to the given uploadDir and save the filename
*/
public function upload($webDir, Filesystem $fileSystem)
public function upload($uploadDir, $webDir, ?Filesystem $fileSystem)
{
// the file property can be empty if the field is not required
if (null === $this->file) {
Expand All @@ -94,9 +94,20 @@ public function upload($webDir, Filesystem $fileSystem)
$uuid = uniqid();
$this->setUuid($uuid);

$url = \sprintf('%s%s/%s', $webDir, $uuid, $safeFileName);
$content = \file_get_contents($this->file->getPathname());
$fileSystem->write($url, $content);
if ($fileSystem) {
$url = \sprintf('%s%s/%s', $webDir, $uuid, $safeFileName);
$content = \file_get_contents($this->file->getPathname());
$fileSystem->write($url, $content);
} else {
@trigger_error(
'Not passing the filesystem as the third argument of upload is deprecated since KunstmaanMediaBundle 5.7 and will be required in KunstmaanMediaBundle 6.0.',
E_USER_DEPRECATED
);

// move takes the target directory and then the target filename to move to
$this->file->move(sprintf('%s/%s', $uploadDir, $uuid), $safeFileName);
$url = sprintf('%s%s/', $webDir, $uuid) . $safeFileName;
}

// set the path property to the filename where you'ved saved the file
$this->fileName = $safeFileName;
Expand All @@ -118,9 +129,15 @@ public function upload($webDir, Filesystem $fileSystem)
*/
public function onValidPost(Form $form, FormBuilderInterface $formBuilder, Request $request, ContainerInterface $container)
{
$uploadDir = $container->getParameter('form_submission_rootdir');
$webDir = $container->getParameter('form_submission_webdir');
$fileSystem = $container->get('kunstmaan_form.filesystem');
$this->upload($webDir, $fileSystem);

$fileSystem = null;
if ($container->has('kunstmaan_form.filesystem')) {
$fileSystem = $container->get('kunstmaan_form.filesystem');
}

$this->upload($uploadDir, $webDir, $fileSystem);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,57 @@ public function testUpload()
->disableOriginalConstructor()
->getMock();

$container->expects($this->any())
->method('has')
->willReturn(true);

$container->expects($this->any())
->method('get')
->willReturn($filesystem);

$object->onValidPost($form, $builder, $request, $container);
$this->assertNull($object->upload('..', $filesystem));
$object->upload(__DIR__ . '/../../Resources/assets/', $filesystem);
$this->assertNull($object->upload('...', '..', $filesystem));
$object->upload(__DIR__ . '/../../Resources/assets/', __DIR__ . '/../../Resources/assets/', $filesystem);
}

public function testUploadBC()
{
$object = $this->object;
$this->assertNull($object->upload('..', '..', null));

$file = $this->getMockBuilder(UploadedFile::class)
->disableOriginalConstructor()
->getMock();

$file->expects($this->any())
->method('move')
->willReturn(true);

$object->file = $file;
$object->upload(__DIR__ . '/../../Resources/assets/', __DIR__ . '/../../Resources/assets/', null);

$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();

$builder = $this->getMockBuilder(FormBuilder::class)
->disableOriginalConstructor()
->getMock();

$request = new Request();

$container = $this->getMockBuilder(Container::class)
->disableOriginalConstructor()
->getMock();

$container->expects($this->any())
->method('getParameter')
->willReturn('whatever');

$container->expects($this->any())
->method('has')
->willReturn(false);

$object->onValidPost($form, $builder, $request, $container);
}
}

0 comments on commit 77680dc

Please sign in to comment.