VirusScannerListenerTest.php 3.13 KB
Newer Older
Guillaume Perréal's avatar
Guillaume Perréal committed
1
<?php declare(strict_types=1);
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
2
/*
3
 * Copyright (C) 2015-2018 IRSTEA
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
4
5
 * All rights reserved.
 */
6
7
8
9
10

namespace Irstea\FileUploadBundle\Tests\Listener;

use Irstea\FileUploadBundle\Event\FileUploadCompleteEvent;
use Irstea\FileUploadBundle\Listener\VirusScannerListener;
Predhumeau Manon's avatar
CS.    
Predhumeau Manon committed
11
use Irstea\FileUploadBundle\Model\UploadedFileInterface;
12
13
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
14
use Xenolope\Quahog\Client;
15
use Xenolope\Quahog\Exception\ConnectionException;
16
17
18
19

/**
 * Generated by PHPUnit_SkeletonGenerator on 2015-01-29 at 14:43:16.
 */
20
class VirusScannerListenerTest extends TestCase
21
22
23
24
25
26
27
{
    /**
     * @var VirusScannerListener
     */
    protected $listener;

    /**
28
     * @var Client|MockObject
29
     */
30
    protected $client;
31
32
33
34
35
36
37

    /**
     * @var FileUploadCompleteEvent
     */
    protected $event;

    /**
38
     * @var UploadedFileInterface|MockObject
39
40
41
     */
    protected $file;

42
    /**
Predhumeau Manon's avatar
CS.    
Predhumeau Manon committed
43
     * {@inheritdoc}
44
     */
45
46
    protected function setUp()
    {
47
        $this->client = $this->getMockBuilder(Client::class)->disableOriginalConstructor()->getMock();
48

49
        $this->file = $this->createMock(UploadedFileInterface::class);
50
51
        $this->file->expects($this->any())->method('getLocalPath')->willReturn('foopath');
        $this->file->expects($this->any())->method('getMetadata')->willReturn([]);
52

53
        $this->event = new FileUploadCompleteEvent($this->file);
54

55
        $this->listener = new VirusScannerListener($this->client);
56
57
    }

58
    public function testOnFileUploadCompletedResultOk()
59
    {
60
61
        $result = [
            'filename' => 'footpath',
Predhumeau Manon's avatar
CS.    
Predhumeau Manon committed
62
63
            'reason'   => null,
            'status'   => Client::RESULT_OK,
64
        ];
65

66
67
        $this->client->expects($this->once())->method('scanFile')->with('foopath')->willReturn($result);
        $this->file->expects($this->once())->method('setMetadata')->with(['virus_scanner' => ['has_virus' => false]]);
68
69
70
71

        $this->listener->onFileUploadCompleted($this->event);
    }

Raidelet Nicolas's avatar
CS    
Raidelet Nicolas committed
72
    /**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
73
     * @expectedException \Irstea\FileUploadBundle\Exception\RejectedFileException
74
     */
75
    public function testOnFileUploadCompletedVirusFound()
76
    {
77
        $result = [
Predhumeau Manon's avatar
CS.    
Predhumeau Manon committed
78
79
80
            'filename' => 'foopath',
            'reason'   => 'Terrible virus',
            'status'   => Client::RESULT_FOUND,
81
82
        ];

83
        $this->client->expects($this->once())
84
85
86
87
            ->method('scanFile')
            ->with('foopath')
            ->willReturn($result);

88
        $this->file->expects($this->once())
89
90
            ->method('setMetadata')
            ->with(['virus_scanner' => ['has_virus' => true, 'detected' => 'Terrible virus']]);
91
92
93

        $this->listener->onFileUploadCompleted($this->event);
    }
94
95

    /**
Predhumeau Manon's avatar
CS.    
Predhumeau Manon committed
96
     * @expectedException \Xenolope\Quahog\Exception\ConnectionException
97
98
99
     */
    public function testGetClientFailed()
    {
100
        $this->client->expects($this->once())->method('ping')->willThrowException(new ConnectionException());
101
102
103
104
105
        $this->listener->onFileUploadCompleted($this->event);
    }

    public function testGetClientSucceed()
    {
106
        $this->client->expects($this->once())->method('ping')->willReturn(true);
107
108
        $this->listener->onFileUploadCompleted($this->event);
    }
109
}