Permalink
Browse files

added storage tests

  • Loading branch information...
1 parent dd9c5b5 commit 6ea98456830dcae59fa2a517fee665a3d8645e43 @dustin10 dustin10 committed Jan 29, 2012
Showing with 140 additions and 4 deletions.
  1. +3 −0 Storage/FileSystemStorage.php
  2. +137 −4 Tests/Storage/FileSystemStorageTest.php
@@ -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));
}
@@ -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);
}
/**

0 comments on commit 6ea9845

Please sign in to comment.