Completed
Push — master ( 463d73...47ba96 )
by Gerrit
02:04
created

shouldResolveRequestFileArgumentToOriginalname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 9.52
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (C) 2017  Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\SymfonyGenerics\Tests\Unit\Arguments;
12
13
use PHPUnit\Framework\TestCase;
14
use Addiks\SymfonyGenerics\Arguments\RequestFileArgument;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\FileBag;
17
use Symfony\Component\HttpFoundation\File\UploadedFile;
18
use Symfony\Component\HttpFoundation\RequestStack;
19
use InvalidArgumentException;
20
21
final class RequestFileArgumentTest extends TestCase
22
{
23
24
    /**
25
     * @test
26
     */
27
    public function shouldResolveRequestFileArgumentToObject()
28
    {
29
        /** @var UploadedFile $file */
30
        $file = $this->createMock(UploadedFile::class);
31
        $file->method('getPathname')->willReturn("data://,some-data-in-file");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...tion\File\UploadedFile>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
33
        /** @var Request $request */
34
        $request = $this->createMock(Request::class);
35
        $request->files = $this->createMock(FileBag::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...ndation\FileBag::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<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...
36
        $request->files->expects($this->once())->method('get')->with(
37
            $this->equalTo("some-key")
38
        )->willReturn($file);
39
40
        /** @var RequestStack $requestStack */
41
        $requestStack = $this->createMock(RequestStack::class);
42
        $requestStack->method("getCurrentRequest")->willReturn($request);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...oundation\RequestStack>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
44
        $argument = new RequestFileArgument($requestStack, "some-key", "object");
45
46
        /** @var mixed $actualResult */
47
        $actualResult = $argument->resolve();
48
49
        $this->assertSame($file, $actualResult);
50
    }
51
52
    /**
53
     * @test
54
     */
55 View Code Duplication
    public function shouldResolveRequestFileArgumentToOriginalname()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        /** @var UploadedFile $file */
58
        $file = $this->createMock(UploadedFile::class);
59
        $file->method('getPathname')->willReturn("data://,some-data-in-file");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...tion\File\UploadedFile>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
        $file->method('getClientOriginalName')->willReturn("some-original-name");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...tion\File\UploadedFile>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
62
        /** @var Request $request */
63
        $request = $this->createMock(Request::class);
64
        $request->files = $this->createMock(FileBag::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...ndation\FileBag::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<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...
65
        $request->files->expects($this->once())->method('get')->with(
66
            $this->equalTo("some-key")
67
        )->willReturn($file);
68
69
        /** @var RequestStack $requestStack */
70
        $requestStack = $this->createMock(RequestStack::class);
71
        $requestStack->method("getCurrentRequest")->willReturn($request);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...oundation\RequestStack>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
73
        $argument = new RequestFileArgument($requestStack, "some-key", "originalname");
74
75
        /** @var mixed $actualResult */
76
        $actualResult = $argument->resolve();
77
78
        $this->assertEquals("some-original-name", $actualResult);
79
    }
80
81
    /**
82
     * @test
83
     */
84 View Code Duplication
    public function shouldResolveRequestFileArgumentToFilename()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        /** @var UploadedFile $file */
87
        $file = $this->createMock(UploadedFile::class);
88
        $file->method('getPathname')->willReturn("data://,some-data-in-file");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...tion\File\UploadedFile>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
        $file->method('getFilename')->willReturn("some-file-name");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...tion\File\UploadedFile>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
91
        /** @var Request $request */
92
        $request = $this->createMock(Request::class);
93
        $request->files = $this->createMock(FileBag::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...ndation\FileBag::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<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...
94
        $request->files->expects($this->once())->method('get')->with(
95
            $this->equalTo("some-key")
96
        )->willReturn($file);
97
98
        /** @var RequestStack $requestStack */
99
        $requestStack = $this->createMock(RequestStack::class);
100
        $requestStack->method("getCurrentRequest")->willReturn($request);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...oundation\RequestStack>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
102
        $argument = new RequestFileArgument($requestStack, "some-key", "filename");
103
104
        /** @var mixed $actualResult */
105
        $actualResult = $argument->resolve();
106
107
        $this->assertEquals("some-file-name", $actualResult);
108
    }
109
110
    /**
111
     * @test
112
     */
113
    public function shouldResolveRequestFileArgumentToContent()
114
    {
115
        /** @var UploadedFile $file */
116
        $file = $this->createMock(UploadedFile::class);
117
        $file->method('getPathname')->willReturn("data://,some-data-in-file");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...tion\File\UploadedFile>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
119
        /** @var Request $request */
120
        $request = $this->createMock(Request::class);
121
        $request->files = $this->createMock(FileBag::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...ndation\FileBag::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<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...
122
        $request->files->expects($this->once())->method('get')->with(
123
            $this->equalTo("some-key")
124
        )->willReturn($file);
125
126
        /** @var RequestStack $requestStack */
127
        $requestStack = $this->createMock(RequestStack::class);
128
        $requestStack->method("getCurrentRequest")->willReturn($request);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...oundation\RequestStack>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
130
        $argument = new RequestFileArgument($requestStack, "some-key", "content");
131
132
        /** @var mixed $actualResult */
133
        $actualResult = $argument->resolve();
134
135
        $this->assertEquals("some-data-in-file", $actualResult);
136
    }
137
138
    /**
139
     * @test
140
     */
141 View Code Duplication
    public function shouldResolveRequestFileArgumentToMimeType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
    {
143
        /** @var UploadedFile $file */
144
        $file = $this->createMock(UploadedFile::class);
145
        $file->method('getPathname')->willReturn("data://,some-data-in-file");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...tion\File\UploadedFile>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
        $file->method('getMimeType')->willReturn("text/some-mime-type");
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...tion\File\UploadedFile>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
147
148
        /** @var Request $request */
149
        $request = $this->createMock(Request::class);
150
        $request->files = $this->createMock(FileBag::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(\Symfo...ndation\FileBag::class) of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<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...
151
        $request->files->expects($this->once())->method('get')->with(
152
            $this->equalTo("some-key")
153
        )->willReturn($file);
154
155
        /** @var RequestStack $requestStack */
156
        $requestStack = $this->createMock(RequestStack::class);
157
        $requestStack->method("getCurrentRequest")->willReturn($request);
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<Symfony\Component...oundation\RequestStack>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
158
159
        $argument = new RequestFileArgument($requestStack, "some-key", "mimetype");
160
161
        /** @var mixed $actualResult */
162
        $actualResult = $argument->resolve();
163
164
        $this->assertEquals("text/some-mime-type", $actualResult);
165
    }
166
167
    /**
168
     * @test
169
     */
170 View Code Duplication
    public function shouldRejectResolvingWithoutRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    {
172
        $this->expectException(InvalidArgumentException::class);
173
174
        $requestStack = $this->createMock(RequestStack::class);
175
        $requestStack->method('getCurrentRequest')->willReturn(null);
176
177
        $argument = new RequestFileArgument($requestStack, "some-key", "content");
0 ignored issues
show
Documentation introduced by
$requestStack is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...oundation\RequestStack>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
178
        $argument->resolve();
179
    }
180
181
}
182