Issues (3627)

Tests/Validator/UploadFieldValidatorTest.php (3 issues)

1
<?php
2
3
/*
4
 * @copyright   2015 Mautic Contributors. All rights reserved
5
 * @author      Mautic
6
 *
7
 * @link        http://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\FormBundle\Tests\Validator;
13
14
use Mautic\CoreBundle\Exception\FileInvalidException;
15
use Mautic\CoreBundle\Validator\FileUploadValidator;
16
use Mautic\FormBundle\Entity\Field;
17
use Mautic\FormBundle\Exception\FileValidationException;
18
use Mautic\FormBundle\Exception\NoFileGivenException;
19
use Mautic\FormBundle\Validator\UploadFieldValidator;
20
use Symfony\Component\HttpFoundation\File\UploadedFile;
21
use Symfony\Component\HttpFoundation\ParameterBag;
22
use Symfony\Component\HttpFoundation\Request;
23
24
class UploadFieldValidatorTest extends \PHPUnit\Framework\TestCase
25
{
26
    /**
27
     * @testdox No Files given
28
     *
29
     * @covers \Mautic\FormBundle\Validator\UploadFieldValidator::processFileValidation
30
     */
31
    public function testNoFilesGiven()
32
    {
33
        $fileUploadValidatorMock = $this->getMockBuilder(FileUploadValidator::class)
34
            ->disableOriginalConstructor()
35
            ->getMock();
36
37
        $fileUploadValidatorMock->expects($this->never())
38
            ->method('validate');
39
40
        $parameterBagMock = $this->getMockBuilder(ParameterBag::class)
41
            ->disableOriginalConstructor()
42
            ->getMock();
43
44
        $parameterBagMock->expects($this->once())
45
            ->method('get')
46
            ->with('mauticform')
47
            ->willReturn(false);
48
49
        $request        = new Request();
50
        $request->files = $parameterBagMock;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parameterBagMock of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\HttpFoundation\FileBag of property $files.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51
52
        $fileUploadValidator = new UploadFieldValidator($fileUploadValidatorMock);
53
54
        $field = new Field();
55
56
        $this->expectException(NoFileGivenException::class);
57
58
        $fileUploadValidator->processFileValidation($field, $request);
59
    }
60
61
    /**
62
     * @testdox Exception should be thrown when validation fails
63
     *
64
     * @covers \Mautic\FormBundle\Validator\UploadFieldValidator::processFileValidation
65
     */
66
    public function testValidationFailed()
67
    {
68
        $fileUploadValidatorMock = $this->getMockBuilder(FileUploadValidator::class)
69
            ->disableOriginalConstructor()
70
            ->getMock();
71
72
        $fileUploadValidatorMock->expects($this->once())
73
            ->method('validate')
74
            ->willThrowException(new FileInvalidException('Validation failed'));
75
76
        $parameterBagMock = $this->getMockBuilder(ParameterBag::class)
77
            ->disableOriginalConstructor()
78
            ->getMock();
79
80
        $fileMock = $this->getMockBuilder(UploadedFile::class)
81
            ->disableOriginalConstructor()
82
            ->getMock();
83
84
        $files = [
85
            'file' => $fileMock,
86
        ];
87
88
        $parameterBagMock->expects($this->once())
89
            ->method('get')
90
            ->with('mauticform')
91
            ->willReturn($files);
92
93
        $request        = new Request();
94
        $request->files = $parameterBagMock;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parameterBagMock of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\HttpFoundation\FileBag of property $files.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
95
96
        $fileUploadValidator = new UploadFieldValidator($fileUploadValidatorMock);
97
98
        $field = new Field();
99
        $field->setAlias('file');
100
        $field->setProperties([
101
            'allowed_file_size'       => 1,
102
            'allowed_file_extensions' => ['jpg', 'gif'],
103
        ]);
104
105
        $this->expectException(FileValidationException::class);
106
        $this->expectExceptionMessage('Validation failed');
107
108
        $fileUploadValidator->processFileValidation($field, $request);
109
    }
110
111
    /**
112
     * @testdox No validation error
113
     *
114
     * @covers \Mautic\FormBundle\Validator\UploadFieldValidator::processFileValidation
115
     */
116
    public function testFileIsValid()
117
    {
118
        $fileUploadValidatorMock = $this->getMockBuilder(FileUploadValidator::class)
119
            ->disableOriginalConstructor()
120
            ->getMock();
121
122
        $fileUploadValidatorMock->expects($this->once())
123
            ->method('validate');
124
125
        $parameterBagMock = $this->getMockBuilder(ParameterBag::class)
126
            ->disableOriginalConstructor()
127
            ->getMock();
128
129
        $fileMock = $this->getMockBuilder(UploadedFile::class)
130
            ->disableOriginalConstructor()
131
            ->getMock();
132
133
        $files = [
134
            'file' => $fileMock,
135
        ];
136
137
        $parameterBagMock->expects($this->once())
138
            ->method('get')
139
            ->with('mauticform')
140
            ->willReturn($files);
141
142
        $request        = new Request();
143
        $request->files = $parameterBagMock;
0 ignored issues
show
Documentation Bug introduced by
It seems like $parameterBagMock of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Symfony\Component\HttpFoundation\FileBag of property $files.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
144
145
        $fileUploadValidator = new UploadFieldValidator($fileUploadValidatorMock);
146
147
        $field = new Field();
148
        $field->setAlias('file');
149
        $field->setProperties([
150
            'allowed_file_size'       => 1,
151
            'allowed_file_extensions' => ['jpg', 'gif'],
152
        ]);
153
154
        $file = $fileUploadValidator->processFileValidation($field, $request);
155
156
        $this->assertSame($fileMock, $file);
157
    }
158
}
159