Преглед на файлове

[Uploadable] Added some tests.

comfortablynumb преди 13 години
родител
ревизия
89ded91e6a
променени са 2 файла, в които са добавени 131 реда и са изтрити 15 реда
  1. 11 14
      lib/Gedmo/Uploadable/Mapping/Validator.php
  2. 120 1
      tests/Gedmo/Uploadable/Mapping/ValidatorTest.php

+ 11 - 14
lib/Gedmo/Uploadable/Mapping/Validator.php

@@ -117,22 +117,18 @@ class Validator
 
         $refl = $meta->getReflectionClass();
 
-        if ($config['pathMethod'] !== '') {
-            if (!$refl->hasMethod($config['pathMethod'])) {
-                throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have method "%s"!',
-                    $meta->name,
-                    $config['pathMethod']
-                ));
-            }
+        if ($config['pathMethod'] !== '' && !$refl->hasMethod($config['pathMethod'])) {
+            throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have method "%s"!',
+                $meta->name,
+                $config['pathMethod']
+            ));
         }
 
-        if ($config['callback'] !== '') {
-            if (!$refl->hasMethod($config['callback'])) {
-                throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have method "%s"!',
-                    $meta->name,
-                    $config['callback']
-                ));
-            }
+        if ($config['callback'] !== '' && !$refl->hasMethod($config['callback'])) {
+            throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have method "%s"!',
+                $meta->name,
+                $config['callback']
+            ));
         }
 
         $config['maxSize'] = (double) $config['maxSize'];
@@ -171,6 +167,7 @@ class Validator
                 break;
             default:
                 $ok = false;
+
                 if (class_exists($config['filenameGenerator'])) {
                     $refl = new \ReflectionClass($config['filenameGenerator']);
 

+ 120 - 1
tests/Gedmo/Uploadable/Mapping/ValidatorTest.php

@@ -21,6 +21,8 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
     public function setUp()
     {
         $this->meta = $this->getMock('Doctrine\ORM\Mapping\ClassMetadata', array(), array(), '', false);
+
+        Validator::$enableMimeTypesConfigException = false;
     }
 
     /**
@@ -108,9 +110,13 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
         $this->meta->expects($this->once())
             ->method('getReflectionClass')
             ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
+        $this->meta->expects($this->any())
+            ->method('getFieldMapping')
+            ->will($this->returnValue(array('type' => 'someType')));
 
         $config = array(
-            'fileMimeTypeField' => 'someField',
+            'fileMimeTypeField' => '',
+            'fileSizeField'     => '',
             'filePathField'     => 'someField',
             'pathMethod'        => '',
             'callback'          => '',
@@ -126,6 +132,90 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
         );
     }
 
+    /**
+     * @expectedException Gedmo\Exception\InvalidMappingException
+     */
+    public function test_validateConfiguration_ifFilenameGeneratorValueIsValidButDoesntImplementNeededInterfaceThrowException()
+    {
+        $this->meta->expects($this->once())
+            ->method('getReflectionClass')
+            ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
+        $this->meta->expects($this->any())
+            ->method('getFieldMapping')
+            ->will($this->returnValue(array('type' => 'someType')));
+
+        $config = array(
+            'fileMimeTypeField' => '',
+            'fileSizeField'     => '',
+            'filePathField'     => 'someField',
+            'pathMethod'        => '',
+            'callback'          => '',
+            'filenameGenerator' => 'DateTime',
+            'maxSize'           => 0,
+            'allowedTypes'      => '',
+            'disallowedTypes'   => ''
+        );
+
+        Validator::validateConfiguration(
+            $this->meta,
+            $config
+        );
+    }
+
+    public function test_validateConfiguration_ifFilenameGeneratorValueIsValidThenDontThrowException()
+    {
+        $this->meta->expects($this->once())
+            ->method('getReflectionClass')
+            ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
+        $this->meta->expects($this->any())
+            ->method('getFieldMapping')
+            ->will($this->returnValue(array('type' => 'string')));
+
+        $config = array(
+            'fileMimeTypeField' => '',
+            'fileSizeField'     => '',
+            'filePathField'     => 'someField',
+            'pathMethod'        => '',
+            'callback'          => '',
+            'filenameGenerator' => 'SHA1',
+            'maxSize'           => 0,
+            'allowedTypes'      => '',
+            'disallowedTypes'   => ''
+        );
+
+        Validator::validateConfiguration(
+            $this->meta,
+            $config
+        );
+    }
+
+    public function test_validateConfiguration_ifFilenameGeneratorValueIsAValidClassThenDontThrowException()
+    {
+        $this->meta->expects($this->once())
+            ->method('getReflectionClass')
+            ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
+        $this->meta->expects($this->any())
+            ->method('getFieldMapping')
+            ->will($this->returnValue(array('type' => 'string')));
+
+        $config = array(
+            'fileMimeTypeField' => '',
+            'fileSizeField'     => '',
+            'filePathField'     => 'someField',
+            'pathMethod'        => '',
+            'callback'          => '',
+            'filenameGenerator' => 'Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorSha1',
+            'maxSize'           => 0,
+            'allowedTypes'      => '',
+            'disallowedTypes'   => ''
+        );
+
+        Validator::validateConfiguration(
+            $this->meta,
+            $config
+        );
+    }
+
     /**
      * @expectedException Gedmo\Exception\InvalidMappingException
      */
@@ -138,6 +228,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
         $config = array(
             'fileMimeTypeField' => 'someField',
             'filePathField'     => 'someField',
+            'fileSizeField'     => '',
             'pathMethod'        => '',
             'callback'          => '',
             'maxSize'           => -123,
@@ -150,6 +241,34 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
             $config
         );
     }
+
+    /**
+     * @expectedException Gedmo\Exception\InvalidMappingException
+     */
+    public function test_validateConfiguration_ifAllowedTypesAndDisallowedTypesAreSetThenThrowException()
+    {
+        $this->meta->expects($this->once())
+            ->method('getReflectionClass')
+            ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
+
+        Validator::$enableMimeTypesConfigException = true;
+
+        $config = array(
+            'fileMimeTypeField' => 'someField',
+            'filePathField'     => 'someField',
+            'fileSizeField'     => '',
+            'pathMethod'        => '',
+            'callback'          => '',
+            'maxSize'           => 0,
+            'allowedTypes'      => 'text/plain',
+            'disallowedTypes'   => 'text/css'
+        );
+
+        Validator::validateConfiguration(
+            $this->meta,
+            $config
+        );
+    }
 }
 
 class FakeEntity