Explorar el Código

[Uploadable] Some minor fixes. Added more tests.

comfortablynumb hace 13 años
padre
commit
79caada507

+ 4 - 4
lib/Gedmo/Uploadable/UploadableListener.php

@@ -241,7 +241,7 @@ class UploadableListener extends MappedEventSubscriber
     public function moveFile(array $fileInfo, $path, $overwrite = false, $appendNumber = false)
     {
         if ($fileInfo['error'] > 0) {
-            switch ($fileInfo) {
+            switch ($fileInfo['error']) {
                 case 1:
                     $msg = 'Size of uploaded file "%s" exceeds limit imposed by directive "upload_max_filesize" in php.ini';
 
@@ -312,14 +312,14 @@ class UploadableListener extends MappedEventSubscriber
             }
         }
 
-        if ($this->moveUploadedFile($fileInfo['tmp_name'], $info['filePath'])) {
-            return $info;
-        } else {
+        if (!$this->moveUploadedFile($fileInfo['tmp_name'], $info['filePath'])) {
             throw new UploadableUploadException(sprintf('File "%s" was not uploaded, or there was a problem moving it to the location "%s".',
                 $fileInfo['fileName'],
                 $path
             ));
         }
+
+        return $info;
     }
 
     /**

+ 32 - 0
tests/Gedmo/Uploadable/UploadableEntityTest.php

@@ -225,6 +225,38 @@ class UploadableEntityTest extends BaseTestCaseORM
         return $info;
     }
 
+    public function testUploadExceptions()
+    {
+        $exceptions = array(
+            1 => 'Gedmo\Exception\UploadableIniSizeException',
+            2 => 'Gedmo\Exception\UploadableFormSizeException',
+            3 => 'Gedmo\Exception\UploadablePartialException',
+            4 => 'Gedmo\Exception\UploadableNoFileException',
+            6 => 'Gedmo\Exception\UploadableNoTmpDirException',
+            7 => 'Gedmo\Exception\UploadableCantWriteException',
+            8 => 'Gedmo\Exception\UploadableExtensionException',
+            999 => 'Gedmo\Exception\UploadableUploadException'
+        );
+
+        foreach ($exceptions as $error => $exceptionClass) {
+            $this->exceptionTester($error, $exceptionClass);
+        }
+    }
+
+    protected function exceptionTester($error, $exceptionClass)
+    {
+        $this->setExpectedException($exceptionClass);
+
+        $file = new File();
+        $fileInfo = $this->generateUploadedFile();
+        $fileInfo['error'] = $error;
+
+        $file->setFileInfo($fileInfo);
+
+        $this->em->persist($file);
+        $this->em->flush();
+    }
+
     protected function getUsedEntityFixtures()
     {
         return array(