|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* test the file sender (aka uploader) service |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\ImportExport\Tests\Service; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\ImportExport\Service\FileSender; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author List of contributors <https://github.com/libgraviton/import-export/graphs/contributors> |
|
12
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
13
|
|
|
* @link http://swisscom.ch |
|
14
|
|
|
*/ |
|
15
|
|
|
class FileSenderTest extends \PHPUnit_Framework_TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* checks if FileSender does nothing if nothing is needed |
|
19
|
|
|
* |
|
20
|
|
|
* @dataProvider requestTypeProvider |
|
21
|
|
|
* @covers Graviton\ImportExport\Service\FileSender |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $type type of request to test |
|
24
|
|
|
* @return void |
|
25
|
|
|
*/ |
|
26
|
|
View Code Duplication |
public function testSenderDoesPlainRequestWithoutUploadField($type) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
$method = 'PUT'; |
|
29
|
|
|
$uri = 'http://localhost/file/example'; |
|
30
|
|
|
$options = []; |
|
31
|
|
|
|
|
32
|
|
|
$clientMock = $this->getMockBuilder('GuzzleHttp\Client')->getMock(); |
|
33
|
|
|
|
|
34
|
|
|
$promiseMock = $this->createMock('GuzzleHttp\Promise\Promise'); |
|
35
|
|
|
|
|
36
|
|
|
$clientMock |
|
37
|
|
|
->method($type) |
|
38
|
|
|
->with($method, $uri, $options) |
|
39
|
|
|
->will($this->returnValue($promiseMock)); |
|
40
|
|
|
|
|
41
|
|
|
$sut = new FileSender( |
|
42
|
|
|
$clientMock |
|
43
|
|
|
); |
|
44
|
|
|
$this->assertEquals($promiseMock, $sut->$type($method, $uri, $options)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* checks if FileSender does nothing if it gets a falsy file path |
|
49
|
|
|
* |
|
50
|
|
|
* This test was added post-implementation, be sure to check if it is really what |
|
51
|
|
|
* you need if you read this. |
|
52
|
|
|
* |
|
53
|
|
|
* @dataProvider requestTypeProvider |
|
54
|
|
|
* @covers Graviton\ImportExport\Service\FileSender |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $type type of request to test |
|
57
|
|
|
* @return void |
|
58
|
|
|
*/ |
|
59
|
|
View Code Duplication |
public function testSenderDoesPlainRequestWithFalsyUploadField($type) |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
$method = 'PUT'; |
|
62
|
|
|
$uri = 'http://localhost/file/example'; |
|
63
|
|
|
$options = [ |
|
64
|
|
|
'upload' => '' |
|
65
|
|
|
]; |
|
66
|
|
|
$expectedOptions = []; |
|
67
|
|
|
|
|
68
|
|
|
$clientMock = $this->getMockBuilder('GuzzleHttp\Client')->getMock(); |
|
69
|
|
|
|
|
70
|
|
|
$promiseMock = $this->createMock('GuzzleHttp\Promise\Promise'); |
|
71
|
|
|
|
|
72
|
|
|
$clientMock |
|
73
|
|
|
->method($type) |
|
74
|
|
|
->with($method, $uri, $expectedOptions) |
|
75
|
|
|
->will($this->returnValue($promiseMock)); |
|
76
|
|
|
|
|
77
|
|
|
$sut = new FileSender( |
|
78
|
|
|
$clientMock |
|
79
|
|
|
); |
|
80
|
|
|
$this->assertEquals($promiseMock, $sut->$type($method, $uri, $options)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* test if FileSender excepts to being called without json in upload cases |
|
85
|
|
|
* |
|
86
|
|
|
* @dataProvider requestTypeProvider |
|
87
|
|
|
* @covers Graviton\ImportExport\Service\FileSender |
|
88
|
|
|
* |
|
89
|
|
|
* @return void |
|
90
|
|
|
*/ |
|
91
|
|
|
public function testExceptsToBeingCalledWithoutJsonDataInUploadCase($type) |
|
92
|
|
|
{ |
|
93
|
|
|
$method = 'PUT'; |
|
94
|
|
|
$uri = 'http://localhost/file/example'; |
|
95
|
|
|
$options = [ |
|
96
|
|
|
'upload' => __DIR__ . '/fixtures/file.txt' |
|
97
|
|
|
// look ma no 'json' => '{}' |
|
98
|
|
|
]; |
|
99
|
|
|
|
|
100
|
|
|
$clientMock = $this->getMockBuilder('GuzzleHttp\Client')->getMock(); |
|
101
|
|
|
|
|
102
|
|
|
$this->expectException('\RuntimeException'); |
|
103
|
|
|
$this->expectExceptionMessage('No json option passed'); |
|
104
|
|
|
|
|
105
|
|
|
$sut = new FileSender( |
|
106
|
|
|
$clientMock |
|
107
|
|
|
); |
|
108
|
|
|
$sut->$type($method, $uri, $options);; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* checks if FileSender mangles the options before passing them to the client |
|
114
|
|
|
* |
|
115
|
|
|
* @dataProvider requestTypeProvider |
|
116
|
|
|
* @covers Graviton\ImportExport\Service\FileSender |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $type type of request to test |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
|
public function testSenderManglesOptionsIfUploadHasBeenPassed($type) |
|
122
|
|
|
{ |
|
123
|
|
|
$method = 'PUT'; |
|
124
|
|
|
$uri = 'http://localhost/file/example'; |
|
125
|
|
|
$options = [ |
|
126
|
|
|
'upload' => __DIR__ . '/fixtures/file.txt', |
|
127
|
|
|
'json' => new \stdClass() |
|
128
|
|
|
]; |
|
129
|
|
|
$expectedOptions = [ |
|
130
|
|
|
'query' => [ |
|
131
|
|
|
'metadata' => '{}' |
|
132
|
|
|
], |
|
133
|
|
|
'body' => file_get_contents(__DIR__ . '/fixtures/file.txt') |
|
134
|
|
|
]; |
|
135
|
|
|
|
|
136
|
|
|
$clientMock = $this->getMockBuilder('GuzzleHttp\Client')->getMock(); |
|
137
|
|
|
|
|
138
|
|
|
$promiseMock = $this->createMock('GuzzleHttp\Promise\Promise'); |
|
139
|
|
|
|
|
140
|
|
|
$clientMock |
|
141
|
|
|
->method($type) |
|
142
|
|
|
->with($method, $uri, $expectedOptions) |
|
143
|
|
|
->will($this->returnValue($promiseMock)); |
|
144
|
|
|
|
|
145
|
|
|
$sut = new FileSender( |
|
146
|
|
|
$clientMock |
|
147
|
|
|
); |
|
148
|
|
|
$this->assertEquals($promiseMock, $sut->$type($method, $uri, $options)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return array |
|
153
|
|
|
*/ |
|
154
|
|
|
public function requestTypeProvider() |
|
155
|
|
|
{ |
|
156
|
|
|
return [ |
|
157
|
|
|
'async' => ['requestAsync'], |
|
158
|
|
|
'sync' => ['request'], |
|
159
|
|
|
]; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
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.