diff --git a/Listener/UploadedFileListener.php b/Listener/UploadedFileListener.php index 30f918a777cb5489d3b2d19452fdaa9a44c65be0..db50e2ddb3f67ca7a1bb21e0d276f6a590cd2555 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 d9f37c1789e4ff6388460365656f28852dbe0ace..f3e4ff4b7f91db45231c3ef4c999d15f15f966fd 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 0000000000000000000000000000000000000000..26856805a778f56ac60595886f160639314af01d --- /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 0000000000000000000000000000000000000000..9551c61637a6d1eb20f8f29cca30b3ebfe2a7fda --- /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); + } +}