Skip to content

Commit

Permalink
Remove values for each file-related property on remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Koc committed May 6, 2017
1 parent 9fdcc61 commit 68eab14
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Handler/UploadHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function remove($obj, $fieldName)
$this->dispatch(Events::PRE_REMOVE, new Event($obj, $mapping));

$this->storage->remove($obj, $mapping);
$mapping->setFileName($obj, null);
$mapping->erase($obj);

$this->dispatch(Events::POST_REMOVE, new Event($obj, $mapping));
}
Expand Down
12 changes: 12 additions & 0 deletions Mapping/PropertyMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ public function setFileName($obj, $value)
$this->writeProperty($obj, 'name', $value);
}

/**
* Removes value for each file-related property of the given object.
*
* @param object $obj The object
*/
public function erase($obj)
{
foreach (['name', 'size', 'mimeType', 'originalName'] as $property) {
$this->writeProperty($obj, $property, null);
}
}

/**
* Reads property of the given object.
*
Expand Down
15 changes: 12 additions & 3 deletions Tests/Handler/UploadHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Vich\UploaderBundle\Tests\Handler;

use Vich\TestBundle\Entity\Article;
use Vich\UploaderBundle\Event\Event;
use Vich\UploaderBundle\Event\Events;
use Vich\UploaderBundle\Handler\UploadHandler;
use Vich\UploaderBundle\Tests\DummyEntity;
use Vich\UploaderBundle\Tests\TestCase;

/**
Expand All @@ -18,10 +18,13 @@ class UploadHandlerTest extends TestCase
protected $injector;
protected $dispatcher;
protected $mapping;
/**
* @var Article
*/
protected $object;
protected $handler;

const FILE_FIELD = 'file_field';
const FILE_FIELD = 'image';

public function setUp()
{
Expand All @@ -30,7 +33,7 @@ public function setUp()
$this->injector = $this->getInjectorMock();
$this->dispatcher = $this->getDispatcherMock();
$this->mapping = $this->getPropertyMappingMock();
$this->object = new DummyEntity();
$this->object = new Article();

$this->handler = new UploadHandler($this->factory, $this->storage, $this->injector, $this->dispatcher);
$this->factory
Expand Down Expand Up @@ -167,6 +170,12 @@ public function testRemove()
->with($this->object)
->will($this->returnValue('something not null'));

$this->mapping
->expects($this->once())
->method('erase')
->with($this->object)
->will($this->returnValue(null));

$this->storage
->expects($this->once())
->method('remove')
Expand Down
26 changes: 26 additions & 0 deletions Tests/Mapping/PropertyMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Vich\UploaderBundle\Tests\Mapping;

use Vich\TestBundle\Entity\Article;
use Vich\UploaderBundle\Mapping\PropertyMapping;
use Vich\UploaderBundle\Naming\NamerInterface;
use Vich\UploaderBundle\Tests\DummyEntity;
Expand Down Expand Up @@ -181,4 +182,29 @@ public function directoryProvider()
['other_dir\\sub_dir\\', 'other_dir\\sub_dir'],
];
}

public function testErase()
{
$object = new Article();

$object->setImageName('generated.jpeg');
$object->setOriginalNameField('original.jpeg');
$object->setMimeTypeField('image/jpeg');
$object->setSizeField(100);

$prop = new PropertyMapping(
'imageName', 'image', [
'size' => 'sizeField',
'mimeType' => 'mimeTypeField',
'originalName' => 'originalNameField',
]
);

$prop->erase($object);

$this->assertNull($object->getImageName());
$this->assertNull($object->getOriginalNameField());
$this->assertNull($object->getMimeTypeField());
$this->assertNull($object->getSizeField());
}
}
12 changes: 7 additions & 5 deletions Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
namespace Vich\UploaderBundle\Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\PropertyMapping;
use Vich\UploaderBundle\Mapping\PropertyMappingFactory;

class TestCase extends BaseTestCase
{
/**
* @return UploadedFile
*/
protected function getUploadedFileMock()
{
return $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')
return $this->getMockBuilder(UploadedFile::class)
->setConstructorArgs(['lala', 'lala', $mimeType = null, $size = null, $error = 'other than UPLOAD_ERR_OK', $test = true])
->getMock();
}
Expand All @@ -20,19 +24,17 @@ protected function getUploadedFileMock()
*/
protected function getPropertyMappingMock()
{
return $this->getMockBuilder('Vich\UploaderBundle\Mapping\PropertyMapping')
return $this->getMockBuilder(PropertyMapping::class)
->disableOriginalConstructor()
->getMock();
}

/**
* Creates a mock property mapping factory.
*
* @return PropertyMappingFactory
*/
protected function getPropertyMappingFactoryMock()
{
return $this->getMockBuilder('Vich\UploaderBundle\Mapping\PropertyMappingFactory')
return $this->getMockBuilder(PropertyMappingFactory::class)
->disableOriginalConstructor()
->getMock();
}
Expand Down

0 comments on commit 68eab14

Please sign in to comment.