From a50a3f905dc0a57e97b8a77392798cce280b17c6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr>
Date: Thu, 29 Jan 2015 15:18:07 +0100
Subject: [PATCH] Ajout des tests des listeners

---
 Listener/UploadedFileListener.php           |  17 +--
 Listener/VirusScannerListener.php           |   2 -
 Tests/Listener/UploadedFileListenerTest.php | 148 ++++++++++++++++++++
 Tests/Listener/VirusScannerListenerTest.php | 102 ++++++++++++++
 4 files changed, 259 insertions(+), 10 deletions(-)
 create mode 100644 Tests/Listener/UploadedFileListenerTest.php
 create mode 100644 Tests/Listener/VirusScannerListenerTest.php

diff --git a/Listener/UploadedFileListener.php b/Listener/UploadedFileListener.php
index 30f918a7..db50e2dd 100644
--- a/Listener/UploadedFileListener.php
+++ b/Listener/UploadedFileListener.php
@@ -8,8 +8,9 @@
 namespace Irstea\FileUploadBundle\Listener;
 
 use Doctrine\ORM\Event\LifecycleEventArgs;
+use Gaufrette\Exception\FileNotFound;
 use Gaufrette\Filesystem;
-use Irstea\FileUploadBundle\Entity\UploadedFile;
+use Irstea\FileUploadBundle\Model\UploadedFileInterface;
 
 /**
  * Listener qui traite les opérations sur les fichiers uploadés.
@@ -33,20 +34,20 @@ class UploadedFileListener
 
     /** Passe le filesystem à l'entité.
      *
-     * @param UploadedFile $file
+     * @param UploadedFileInterface $file
      * @param LifecycleEventArgs $event
      */
-    public function postLoad(UploadedFile $file, LifecycleEventArgs $event)
+    public function postLoad(UploadedFileInterface $file, LifecycleEventArgs $event)
     {
         $file->setFilesystem($this->filesystem);
     }
 
     /** Renomme le fichier disque lorsque l'on change l'attribut 'path'.
      *
-     * @param UploadedFile $file
+     * @param UploadedFileInterface $file
      * @param LifecycleEventArgs $event
      */
-    public function postUpdate(UploadedFile $file, LifecycleEventArgs $event)
+    public function postUpdate(UploadedFileInterface $file, LifecycleEventArgs $event)
     {
         $changes = $event->getEntityManager()->getUnitOfWork()->getEntityChangeSet($file);
 
@@ -59,14 +60,14 @@ class UploadedFileListener
 
     /** Supprime le fichier disque lorsque l'on supprime l'entité.
      *
-     * @param UploadedFile $file
+     * @param UploadedFileInterface $file
      * @param LifecycleEventArgs $event
      */
-    public function postRemove(UploadedFile $file, LifecycleEventArgs $event)
+    public function postRemove(UploadedFileInterface $file, LifecycleEventArgs $event)
     {
         try {
             $this->filesystem->delete($file->getPath());
-        } catch(\Gaufrette\Exception\FileNotFound $ex) {
+        } catch(FileNotFound $ex) {
             // NOOP
         }
     }
diff --git a/Listener/VirusScannerListener.php b/Listener/VirusScannerListener.php
index d9f37c17..f3e4ff4b 100644
--- a/Listener/VirusScannerListener.php
+++ b/Listener/VirusScannerListener.php
@@ -8,8 +8,6 @@
 namespace Irstea\FileUploadBundle\Listener;
 
 use CL\Tissue\Adapter\AdapterInterface;
-use CL\Tissue\Model\Detection;
-use CL\Tissue\Model\ScanResult;
 use Irstea\FileUploadBundle\Event\FileUploadCompleteEvent;
 use Irstea\FileUploadBundle\Exception\RejectedFileException;
 
diff --git a/Tests/Listener/UploadedFileListenerTest.php b/Tests/Listener/UploadedFileListenerTest.php
new file mode 100644
index 00000000..26856805
--- /dev/null
+++ b/Tests/Listener/UploadedFileListenerTest.php
@@ -0,0 +1,148 @@
+<?php
+
+namespace Irstea\FileUploadBundle\Listener;
+
+use Doctrine\ORM\Event\LifecycleEventArgs;
+use Gaufrette\Exception\FileNotFound;
+use PHPUnit_Framework_MockObject_MockObject;
+use PHPUnit_Framework_TestCase;
+
+/**
+ * Generated by PHPUnit_SkeletonGenerator on 2015-01-29 at 14:56:00.
+ */
+class UploadedFileListenerTest extends PHPUnit_Framework_TestCase
+{
+
+    /**
+     * @var UploadedFileListener
+     */
+    protected $listener;
+
+    /**
+     * @var PHPUnit_Framework_MockObject_MockObject
+     */
+    protected $filesystem;
+
+    /**
+     * @var PHPUnit_Framework_MockObject_MockObject
+     */
+    protected $file;
+
+    /**
+     * @var PHPUnit_Framework_MockObject_MockObject
+     */
+    protected $om;
+
+    /**
+     * @var PHPUnit_Framework_MockObject_MockObject
+     */
+    protected $unitOfWork;
+
+    /**
+     * @var PHPUnit_Framework_MockObject_MockObject
+     */
+    protected $event;
+
+    /**
+     * Sets up the fixture, for example, opens a network connection.
+     * This method is called before a test is executed.
+     */
+    protected function setUp()
+    {
+        $this->filesystem = $this->getMockBuilder('\Gaufrette\Filesystem')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $this->listener = new UploadedFileListener($this->filesystem);
+
+        $this->file = $this->getMockBuilder('\Irstea\FileUploadBundle\Entity\UploadedFile')
+            ->setMethods(['setFilesystem', 'getPath'])
+            ->getMockForAbstractClass();
+
+        $this->unitOfWork = $this->getMockBuilder('\Doctrine\ORM\UnitOfWork')
+            ->disableOriginalConstructor()
+            ->setMethods(['getEntityChangeSet'])
+            ->getMock();
+
+        $this->om = $this->getMockBuilder('\Doctrine\ORM\EntityManager')
+            ->setMethods(['getUnitOfWork'])
+            ->disableOriginalConstructor()
+            ->getMock();
+        $this->om->expects($this->any())->method('getUnitOfWork')->willReturn($this->unitOfWork);
+
+        $this->event = new LifecycleEventArgs($this->file, $this->om);
+    }
+
+
+    /**
+     * @covers Irstea\FileUploadBundle\Listener\UploadedFileListener::postLoad
+     * @todo   Implement testPostLoad().
+     */
+    public function testPostLoad()
+    {
+        $this->file->expects($this->once())->method('setFilesystem')->with($this->identicalTo($this->filesystem));
+
+        $this->listener->postLoad($this->file, $this->event);
+    }
+
+    /**
+     * @covers Irstea\FileUploadBundle\Listener\UploadedFileListener::postUpdate
+     */
+    public function testPostUpdateNoPathChange()
+    {
+        $this->unitOfWork->expects($this->once())->method('getEntityChangeSet')->with($this->file)->willReturn([]);
+
+        $this->filesystem->expects($this->never())->method('rename');
+
+        $this->listener->postUpdate($this->file, $this->event);
+    }
+
+    /**
+     * @covers Irstea\FileUploadBundle\Listener\UploadedFileListener::postUpdate
+     */
+    public function testPostUpdatePathChanged()
+    {
+        $this->unitOfWork->expects($this->once())->method('getEntityChangeSet')->with($this->file)->willReturn(['path' => ['before', 'after']]);
+
+        $this->filesystem->expects($this->once())->method('rename')->with('before', 'after');
+
+        $this->listener->postUpdate($this->file, $this->event);
+    }
+
+    /**
+     * @covers Irstea\FileUploadBundle\Listener\UploadedFileListener::postRemove
+     */
+    public function testPostRemove()
+    {
+        $this->file->expects($this->once())->method('getPath')->willReturn('foobar');
+
+        $this->filesystem->expects($this->once())->method('delete')->with('foobar');
+
+        $this->listener->postRemove($this->file, $this->event);
+    }
+
+    /**
+     * @covers Irstea\FileUploadBundle\Listener\UploadedFileListener::postRemove
+     */
+    public function testPostRemoveIgnoreFileNotFound()
+    {
+        $this->file->expects($this->once())->method('getPath')->willReturn('foobar');
+
+        $this->filesystem->expects($this->once())->method('delete')->with('foobar')->willThrowException(new FileNotFound('foobar'));
+
+        $this->listener->postRemove($this->file, $this->event);
+    }
+
+    /**
+     * @covers Irstea\FileUploadBundle\Listener\UploadedFileListener::postRemove
+     * @expectedException \InvalidArgumentException
+     */
+    public function testPostRemovePropagateOtherExceptions()
+    {
+        $this->file->expects($this->once())->method('getPath')->willReturn('foobar');
+
+        $this->filesystem->expects($this->once())->method('delete')->with('foobar')->willThrowException(new \InvalidArgumentException());
+
+        $this->listener->postRemove($this->file, $this->event);
+    }
+}
diff --git a/Tests/Listener/VirusScannerListenerTest.php b/Tests/Listener/VirusScannerListenerTest.php
new file mode 100644
index 00000000..9551c616
--- /dev/null
+++ b/Tests/Listener/VirusScannerListenerTest.php
@@ -0,0 +1,102 @@
+<?php
+
+namespace Irstea\FileUploadBundle\Tests\Listener;
+
+use CL\Tissue\Model\Detection;
+use CL\Tissue\Model\ScanResult;
+use Irstea\FileUploadBundle\Event\FileUploadCompleteEvent;
+use Irstea\FileUploadBundle\Listener\VirusScannerListener;
+use PHPUnit_Framework_MockObject_MockObject;
+use PHPUnit_Framework_TestCase;
+
+/**
+ * Generated by PHPUnit_SkeletonGenerator on 2015-01-29 at 14:43:16.
+ */
+class VirusScannerListenerTest extends PHPUnit_Framework_TestCase
+{
+
+    /**
+     * @var VirusScannerListener
+     */
+    protected $listener;
+
+    /**
+     * @var PHPUnit_Framework_MockObject_MockObject
+     */
+    protected $scanner;
+
+    /**
+     * @var FileUploadCompleteEvent
+     */
+    protected $event;
+
+    /**
+     * @var PHPUnit_Framework_MockObject_MockObject
+     */
+    protected $file;
+
+    protected function setUp()
+    {
+        $this->file = $this->getMock('\Irstea\FileUploadBundle\Model\UploadedFileInterface');
+        $this->file->expects($this->once())->method('getLocalPath')->willReturn('foopath');
+        $this->file->expects($this->once())->method('getMetadata')->willReturn([]);
+
+        $this->event = new FileUploadCompleteEvent($this->file);
+
+        $this->scanner = $this->getMock('CL\Tissue\Adapter\AdapterInterface');
+
+        $this->listener = new VirusScannerListener($this->scanner);
+    }
+
+    public function testOnFileUploadCompletedResultOk()
+    {
+        $result = new ScanResult(
+            ['foopath'],
+            ['foopath'],
+            []
+        );
+        $this->scanner->expects($this->once())->method('scan')->with(['foopath'])->willReturn($result);
+
+        $this->file->expects($this->once())->method('setMetadata')->with(['virus_scanner' => ['has_virus' => false]]);
+
+        $this->listener->onFileUploadCompleted($this->event);
+    }
+
+    /**
+     * @expectedException Irstea\FileUploadBundle\Exception\RejectedFileException
+     */
+    public function testOnFileUploadCompletedVirusFound()
+    {
+        $result = new ScanResult(
+            ['foopath'],
+            ['foopath'],
+            [new Detection('foopath', Detection::TYPE_VIRUS, 'BAR')]
+        );
+        $this->scanner->expects($this->once())->method('scan')->with(['foopath'])->willReturn($result);
+
+        $this->file->expects($this->once())->method('setMetadata')->with(
+            ['virus_scanner' => ['has_virus' => true, 'detected' => 'BAR']]
+        );
+
+        $this->listener->onFileUploadCompleted($this->event);
+    }
+
+/**
+     * @expectedException Irstea\FileUploadBundle\Exception\RejectedFileException
+     */
+    public function testOnFileUploadCompletedVirusFoundNoDescription()
+    {
+        $result = new ScanResult(
+            ['foopath'],
+            ['foopath'],
+            [new Detection('foopath', Detection::TYPE_VIRUS)]
+        );
+        $this->scanner->expects($this->once())->method('scan')->with(['foopath'])->willReturn($result);
+
+        $this->file->expects($this->once())->method('setMetadata')->with(
+            ['virus_scanner' => ['has_virus' => true]]
+        );
+
+        $this->listener->onFileUploadCompleted($this->event);
+    }
+}
-- 
GitLab