|
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
|
|
|
* @param string $type type of request to test |
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testExceptsToBeingCalledWithoutJsonDataInUploadCase($type) |
|
93
|
|
|
{ |
|
94
|
|
|
$method = 'PUT'; |
|
95
|
|
|
$uri = 'http://localhost/file/example'; |
|
96
|
|
|
$options = [ |
|
97
|
|
|
'upload' => __DIR__ . '/fixtures/file.txt' |
|
98
|
|
|
// look ma no 'json' => '{}' |
|
99
|
|
|
]; |
|
100
|
|
|
|
|
101
|
|
|
$clientMock = $this->getMockBuilder('GuzzleHttp\Client')->getMock(); |
|
102
|
|
|
|
|
103
|
|
|
$this->expectException('\RuntimeException'); |
|
104
|
|
|
$this->expectExceptionMessage('No json option passed'); |
|
105
|
|
|
|
|
106
|
|
|
$sut = new FileSender( |
|
107
|
|
|
$clientMock |
|
108
|
|
|
); |
|
109
|
|
|
$sut->$type($method, $uri, $options); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* checks if FileSender mangles the options before passing them to the client |
|
115
|
|
|
* |
|
116
|
|
|
* @dataProvider requestTypeProvider |
|
117
|
|
|
* @covers Graviton\ImportExport\Service\FileSender |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $type type of request to test |
|
120
|
|
|
* @return void |
|
121
|
|
|
*/ |
|
122
|
|
|
public function testSenderManglesOptionsIfUploadHasBeenPassed($type) |
|
123
|
|
|
{ |
|
124
|
|
|
$method = 'PUT'; |
|
125
|
|
|
$uri = 'http://localhost/file/example'; |
|
126
|
|
|
$options = [ |
|
127
|
|
|
'upload' => __DIR__ . '/fixtures/file.txt', |
|
128
|
|
|
'json' => new \stdClass() |
|
129
|
|
|
]; |
|
130
|
|
|
$expectedOptions = [ |
|
131
|
|
|
'query' => [ |
|
132
|
|
|
'metadata' => '{}' |
|
133
|
|
|
] |
|
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( |
|
143
|
|
|
$method, |
|
144
|
|
|
$uri, |
|
145
|
|
|
$this->callback(function ($options) use ($expectedOptions) { |
|
146
|
|
|
$this->assertArrayHasKey('body', $options); |
|
147
|
|
|
$this->assertInternalType('resource', $options['body']); |
|
148
|
|
|
|
|
149
|
|
|
unset($options['body']); |
|
150
|
|
|
$this->assertEquals($options, $expectedOptions); |
|
151
|
|
|
|
|
152
|
|
|
return true; |
|
153
|
|
|
}) |
|
154
|
|
|
) |
|
155
|
|
|
->will($this->returnValue($promiseMock)); |
|
156
|
|
|
|
|
157
|
|
|
$sut = new FileSender( |
|
158
|
|
|
$clientMock |
|
159
|
|
|
); |
|
160
|
|
|
$this->assertEquals($promiseMock, $sut->$type($method, $uri, $options)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return array |
|
165
|
|
|
*/ |
|
166
|
|
|
public function requestTypeProvider() |
|
167
|
|
|
{ |
|
168
|
|
|
return [ |
|
169
|
|
|
'async' => ['requestAsync'], |
|
170
|
|
|
'sync' => ['request'], |
|
171
|
|
|
]; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|