Commit a50a3f90 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Ajout des tests des listeners

parent af08e6ad
......@@ -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
}
}
......
......@@ -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;
......
<?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);
}
}
<?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);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment