VirusScannerListenerTest.php 3.18 KiB
<?php declare(strict_types=1);
/*
 * Copyright (C) 2015-2017 IRSTEA
 * All rights reserved.
 */
namespace Irstea\FileUploadBundle\Tests\Listener;
use Irstea\FileUploadBundle\Event\FileUploadCompleteEvent;
use Irstea\FileUploadBundle\Listener\VirusScannerListener;
use Irstea\FileUploadBundle\Model\UploadedFileInterface;
use PHPUnit_Framework_MockObject_MockObject;
use PHPUnit_Framework_TestCase;
use Xenolope\Quahog\Client;
use Xenolope\Quahog\Exception\ConnectionException;
/**
 * 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 $client;
    /**
     * @var FileUploadCompleteEvent
    protected $event;
    /**
     * @var PHPUnit_Framework_MockObject_MockObject
    protected $file;
    /**
     * {@inheritdoc}
    protected function setUp()
        $this->client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock();
        $this->file = $this->getMock(UploadedFileInterface::class);
        $this->file->expects($this->any())->method('getLocalPath')->willReturn('foopath');
        $this->file->expects($this->any())->method('getMetadata')->willReturn([]);
        $this->event = new FileUploadCompleteEvent($this->file);
        $this->listener = new VirusScannerListener($this->client);
    public function testOnFileUploadCompletedResultOk()
        $result = [
            'filename' => 'footpath',
            'reason'   => null,
            'status'   => Client::RESULT_OK,
        $this->client->expects($this->once())->method('scanFile')->with('foopath')->willReturn($result);
        $this->file->expects($this->once())->method('setMetadata')->with(['virus_scanner' => ['has_virus' => false]]);
        $this->listener->onFileUploadCompleted($this->event);
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
/** * @expectedException \Irstea\FileUploadBundle\Exception\RejectedFileException */ public function testOnFileUploadCompletedVirusFound() { $result = [ 'filename' => 'foopath', 'reason' => 'Terrible virus', 'status' => Client::RESULT_FOUND, ]; $this->client->expects($this->once()) ->method('scanFile') ->with('foopath') ->willReturn($result); $this->file->expects($this->once()) ->method('setMetadata') ->with(['virus_scanner' => ['has_virus' => true, 'detected' => 'Terrible virus']]); $this->listener->onFileUploadCompleted($this->event); } /** * @expectedException \Xenolope\Quahog\Exception\ConnectionException */ public function testGetClientFailed() { $this->client->expects($this->once())->method('ping')->willThrowException(new ConnectionException()); $this->listener->onFileUploadCompleted($this->event); } public function testGetClientSucceed() { $this->client->expects($this->once())->method('ping')->willReturn(true); $this->listener->onFileUploadCompleted($this->event); } }