Skip to content

Commit

Permalink
added storage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dustin10 committed Jan 29, 2012
1 parent dd9c5b5 commit 6ea9845
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Storage/FileSystemStorage.php
Expand Up @@ -61,6 +61,9 @@ public function remove($obj)
foreach ($mappings as $mapping) {
if ($mapping->getDeleteOnRemove()) {
$name = $mapping->getFileNameProperty()->getValue($obj);
if (null === $name) {
continue;
}

unlink(sprintf('%s/%s', $mapping->getUploadDir(), $name));
}
Expand Down
141 changes: 137 additions & 4 deletions Tests/Storage/FileSystemStorageTest.php
Expand Up @@ -26,19 +26,152 @@ public function setUp()
}

/**
* Tests the upload method.
* Tests the upload method skips a mapping which has a null
* uploadable property value.
*/
public function testUpload()
public function testUploadSkipsMappingOnNullFile()
{
$obj = new DummyEntity();

$mapping = $this->getMock('Vich\UploaderBundle\Mapping\PropertyMapping');

$mapping
->expects($this->once())
->method('getPropertyValue')
->will($this->returnValue(null));

$mapping
->expects($this->never())
->method('hasNamer');

$mapping
->expects($this->never())
->method('getNamer');

$mapping
->expects($this->never())
->method('getFileNameProperty');

$this->factory
->expects($this->once())
->method('fromObject')
->with($obj)
->will($this->returnValue(array($mapping)));

$storage = new FileSystemStorage($this->factory);
$storage->upload($obj);
}

/**
* Test the remove method.
* Tests the upload method skips a mapping which has an uploadable
* field property value that is not an instance of UploadedFile.
*/
public function testRemove()
public function testUploadSkipsMappingOnNonUploadedFileInstance()
{
$obj = new DummyEntity();

$mapping = $this->getMock('Vich\UploaderBundle\Mapping\PropertyMapping');

$file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')
->disableOriginalConstructor()
->getMock();

$mapping
->expects($this->once())
->method('getPropertyValue')
->will($this->returnValue($file));

$mapping
->expects($this->never())
->method('hasNamer');

$mapping
->expects($this->never())
->method('getNamer');

$mapping
->expects($this->never())
->method('getFileNameProperty');

$this->factory
->expects($this->once())
->method('fromObject')
->with($obj)
->will($this->returnValue(array($mapping)));

$storage = new FileSystemStorage($this->factory);
$storage->upload($obj);
}

/**
* Test the remove method does not remove a file that is configured
* to not be deleted upon removal of the entity.
*/
public function testRemoveSkipsConfiguredNotToDeleteOnRemove()
{
$obj = new DummyEntity();

$mapping = $this->getMock('Vich\UploaderBundle\Mapping\PropertyMapping');

$mapping
->expects($this->once())
->method('getDeleteOnRemove')
->will($this->returnValue(false));

$mapping
->expects($this->never())
->method('getFileNameProperty');

$this->factory
->expects($this->once())
->method('fromObject')
->with($obj)
->will($this->returnValue(array($mapping)));

$storage = new FileSystemStorage($this->factory);
$storage->remove($obj);
}

/**
* Test the remove method skips trying to remove a file whose file name
* property value returns null.
*/
public function testRemoveSkipsNullFileNameProperty()
{
$obj = new DummyEntity();

$prop = $this->getMockBuilder('\ReflectionProperty')
->disableOriginalConstructor()
->getMock();
$prop
->expects($this->once())
->method('getValue')
->with($obj)
->will($this->returnValue(null));

$mapping = $this->getMock('Vich\UploaderBundle\Mapping\PropertyMapping');
$mapping
->expects($this->once())
->method('getDeleteOnRemove')
->will($this->returnValue(true));

$mapping
->expects($this->once())
->method('getFileNameProperty')
->will($this->returnValue($prop));

$mapping
->expects($this->never())
->method('getUploadDir');

$this->factory
->expects($this->once())
->method('fromObject')
->with($obj)
->will($this->returnValue(array($mapping)));

$storage = new FileSystemStorage($this->factory);
$storage->remove($obj);
}

/**
Expand Down

0 comments on commit 6ea9845

Please sign in to comment.