浏览代码

[Uploadable] Added some tests

comfortablynumb 13 年之前
父节点
当前提交
12da91e78a

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

@@ -62,7 +62,7 @@ class UploadableListener extends MappedEventSubscriber
 
             if ($config = $this->getConfiguration($om, $meta->name)) {
                 if (isset($config['uploadable']) && $config['uploadable']) {
-                    $this->processFile($om, $uow, $ea, $meta, $config, $object, self::ACTION_INSERT);
+                    $this->processFile($uow, $ea, $meta, $config, $object, self::ACTION_INSERT);
                 }
             }
         }
@@ -73,7 +73,7 @@ class UploadableListener extends MappedEventSubscriber
             if ($config = $this->getConfiguration($om, $meta->name)) {
                 if (isset($config['uploadable']) && $config['uploadable']) {
                     $this->removeFile($meta, $config, $object);
-                    $this->processFile($om, $uow, $ea, $meta, $config, $object, self::ACTION_UPDATE);
+                    $this->processFile($uow, $ea, $meta, $config, $object, self::ACTION_UPDATE);
                 }
             }
         }
@@ -93,7 +93,6 @@ class UploadableListener extends MappedEventSubscriber
      * If it's a Uploadable object, verify if the file was uploaded.
      * If that's the case, process it.
      *
-     * @param ObjectManager
      * @param UnitOfWork
      * @param AdapterInterface
      * @param ClassMetadata
@@ -103,7 +102,7 @@ class UploadableListener extends MappedEventSubscriber
      *
      * @return void
      */
-    public function processFile(ObjectManager $om, UnitOfWork $uow, AdapterInterface $ea, ClassMetadata $meta, array $config, $object, $action)
+    public function processFile(UnitOfWork $uow, AdapterInterface $ea, ClassMetadata $meta, array $config, $object, $action)
     {
         $refl = $meta->getReflectionClass();
         $pathMethod = $refl->getMethod($config['pathMethod']);

+ 5 - 11
tests/Gedmo/Uploadable/Fixture/Entity/Article.php

@@ -24,7 +24,7 @@ class Article
     private $title;
 
     /**
-     * @ORM\OneToMany(targetEntity="File", mappedBy="article")
+     * @ORM\OneToMany(targetEntity="File", mappedBy="article", cascade={"persist", "remove"})
      */
     private $files;
 
@@ -59,19 +59,13 @@ class Article
         return $this->filePath;
     }
 
-    /**
-     * @Gedmo\UploadablePath
-     */
-    public function getPath()
+    public function getFiles()
     {
-        return __DIR__.'/../../../../temp/uploadable';
+        return $this->files;
     }
 
-    /**
-     * @Gedmo\UploadableFilesArrayIndex
-     */
-    public function getFilesArrayIndex()
+    public function addFile(File $file)
     {
-        return '[image]';
+        $this->files[] = $file;
     }
 }

+ 1 - 1
tests/Gedmo/Uploadable/Fixture/Entity/File.php

@@ -20,7 +20,7 @@ class File
     private $id;
 
     /**
-     * @ORM\Column(name="title", type="string")
+     * @ORM\Column(name="title", type="string", nullable=true)
      */
     private $title;
 

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

@@ -139,11 +139,39 @@ class UploadableEntityTest extends BaseTestCaseORM
 
     public function testEntityWithUploadableEntities()
     {
+        $artRepo = $this->em->getRepository(self::ARTICLE_CLASS);
         $article = new Article();
         $article->setTitle('Test');
 
+        $file1 = new File();
+        $file2 = new File();
+        $file3 = new File();
+
+        $article->addFile($file1);
+        $article->addFile($file2);
+        $article->addFile($file3);
+
+        $filesArrayIndex = strtr($file1->getFilesArrayIndex(), array('[' => '', ']' => ''));
+
+        $fileInfo = $this->generateUploadedFile($filesArrayIndex);
+        $fileInfo2 = $this->generateUploadedFile($filesArrayIndex);
+        $fileInfo3 = $this->generateUploadedFile($filesArrayIndex);
+
+        $_FILES[$fileInfo['index']] = array($fileInfo, $fileInfo2, $fileInfo3);
+
         $this->em->persist($article);
+
         $this->em->flush();
+
+        $art = $artRepo->findOneByTitle('Test');
+        $files = $art->getFiles();
+        $file1Path = $file1->getPath().DIRECTORY_SEPARATOR.$fileInfo['name'];
+        $file2Path = $file2->getPath().DIRECTORY_SEPARATOR.$fileInfo['name'];
+        $file3Path = $file3->getPath().DIRECTORY_SEPARATOR.$fileInfo['name'];
+
+        $this->assertEquals($file1Path, $files[0]->getFilePath());
+        $this->assertEquals($file2Path, $files[1]->getFilePath());
+        $this->assertEquals($file3Path, $files[2]->getFilePath());
     }
 
     public function testGetItemFromArrayMethod()