|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* HttpLoaderTest |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\ProxyBundle\Tests\Definition\Loader; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\ProxyBundle\Definition\Loader\HttpLoader; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* tests for the HttpLoader class |
|
13
|
|
|
* |
|
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
16
|
|
|
* @link http://swisscom.ch |
|
17
|
|
|
*/ |
|
18
|
|
|
class HttpLoaderTest extends \PHPUnit_Framework_TestCase |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var HttpLoader |
|
22
|
|
|
*/ |
|
23
|
|
|
private $sut; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var LoggerInterface |
|
27
|
|
|
*/ |
|
28
|
|
|
private $logger; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* setup |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setup() |
|
36
|
|
|
{ |
|
37
|
|
|
$response = $this->getMockBuilder('Guzzle\Http\Message\Response') |
|
38
|
|
|
->setConstructorArgs([200]) |
|
39
|
|
|
->getMock(); |
|
40
|
|
|
$response |
|
41
|
|
|
->expects($this->any()) |
|
42
|
|
|
->method("getBody") |
|
43
|
|
|
->willReturn("{ 'test': 'bablaba' }"); |
|
44
|
|
|
$curlMock = $this->getMock('Guzzle\Common\Collection'); |
|
45
|
|
|
$request = $this->getMockForAbstractClass('Guzzle\Http\Message\RequestInterface'); |
|
46
|
|
|
$request->expects($this->any()) |
|
47
|
|
|
->method("send") |
|
48
|
|
|
->withAnyParameters() |
|
49
|
|
|
->willReturn($response); |
|
50
|
|
|
$request->method('getCurlOptions') |
|
51
|
|
|
->willReturn($curlMock); |
|
52
|
|
|
$client = $this->getMockBuilder('Guzzle\Http\Client') |
|
53
|
|
|
->disableOriginalConstructor() |
|
54
|
|
|
->getMock(); |
|
55
|
|
|
$client |
|
56
|
|
|
->expects($this->any()) |
|
57
|
|
|
->method('get') |
|
58
|
|
|
->willReturn($request); |
|
59
|
|
|
$validator = $this->getMockForAbstractClass('Symfony\Component\Validator\Validator\ValidatorInterface'); |
|
60
|
|
|
|
|
61
|
|
|
$this->logger = $this->getMock('Psr\Log\Loggerinterface'); |
|
62
|
|
|
|
|
63
|
|
|
$this->sut = new HttpLoader($validator, $client, $this->logger); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* test the support method |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testSupports() |
|
72
|
|
|
{ |
|
73
|
|
|
$client = $this->getMockBuilder('Guzzle\Http\Client')->getMock(); |
|
74
|
|
|
$validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface') |
|
75
|
|
|
->getMockForAbstractClass(); |
|
76
|
|
|
$validator |
|
77
|
|
|
->expects($this->once()) |
|
78
|
|
|
->method('validate') |
|
79
|
|
|
->willReturn(array()); |
|
80
|
|
|
|
|
81
|
|
|
$sut = new HttpLoader($validator, $client, $this->logger); |
|
82
|
|
|
$this->assertTrue($sut->supports("test/test.json")); |
|
83
|
|
|
|
|
84
|
|
|
$validatorFail = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface') |
|
85
|
|
|
->getMockForAbstractClass(); |
|
86
|
|
|
$validatorFail |
|
87
|
|
|
->expects($this->once()) |
|
88
|
|
|
->method('validate') |
|
89
|
|
|
->willReturn(array("error text")); |
|
90
|
|
|
|
|
91
|
|
|
$sut = new HttpLoader($validatorFail, $client, $this->logger); |
|
92
|
|
|
$this->assertFalse($sut->supports("test/again.json")); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* load method return null |
|
97
|
|
|
* |
|
98
|
|
|
* @return HttpLoader |
|
|
|
|
|
|
99
|
|
|
*/ |
|
100
|
|
|
public function testLoadShallNotReturnNull() |
|
101
|
|
|
{ |
|
102
|
|
|
$url = "http://localhost/test.json"; |
|
103
|
|
|
$this->assertNotNull($this->sut->load($url)); |
|
104
|
|
|
|
|
105
|
|
|
$mock = $this->getMockBuilder('Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\SwaggerStrategy') |
|
106
|
|
|
->disableOriginalConstructor() |
|
107
|
|
|
->setMethods(['supports']) |
|
108
|
|
|
->getMock(); |
|
109
|
|
|
$mock |
|
110
|
|
|
->expects($this->once()) |
|
111
|
|
|
->method("supports") |
|
112
|
|
|
->willReturn(false); |
|
113
|
|
|
|
|
114
|
|
|
$this->sut->setDispersalStrategy($mock); |
|
115
|
|
|
$this->assertNotNull($this->sut->load($url)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* test the load method |
|
120
|
|
|
* |
|
121
|
|
|
* @depends testLoadShallNotReturnNull |
|
122
|
|
|
* |
|
123
|
|
|
* @return void |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testLoad() |
|
126
|
|
|
{ |
|
127
|
|
|
$apiDefinition = $this->getMockBuilder('Graviton\ProxyBundle\Definition\ApiDefinition')->getMock(); |
|
128
|
|
|
|
|
129
|
|
|
$mock = $this->getMockBuilder('Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\SwaggerStrategy') |
|
130
|
|
|
->disableOriginalConstructor() |
|
131
|
|
|
->setMethods(['supports', 'process']) |
|
132
|
|
|
->getMock(); |
|
133
|
|
|
$mock |
|
134
|
|
|
->expects($this->once()) |
|
135
|
|
|
->method("supports") |
|
136
|
|
|
->willReturn(true); |
|
137
|
|
|
$mock |
|
138
|
|
|
->expects($this->once()) |
|
139
|
|
|
->method("process") |
|
140
|
|
|
->willReturn($apiDefinition); |
|
141
|
|
|
|
|
142
|
|
|
$this->sut->setDispersalStrategy($mock); |
|
143
|
|
|
$loadedContent = $this->sut->load("http://localhost/test/url/blub"); |
|
144
|
|
|
$this->assertInstanceOf('Graviton\ProxyBundle\Definition\ApiDefinition', $loadedContent); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* test a load with cached content |
|
149
|
|
|
* |
|
150
|
|
|
* @return void |
|
151
|
|
|
*/ |
|
152
|
|
|
public function testLoadWithCache() |
|
153
|
|
|
{ |
|
154
|
|
|
$storeKey = 'testSwagger'; |
|
155
|
|
|
$cachedContent = '{"swagger": "2.0"}'; |
|
156
|
|
|
$apiDefinition = $this->getMock('Graviton\ProxyBundle\Definition\ApiDefinition'); |
|
157
|
|
|
|
|
158
|
|
|
$mock = $this->getMockBuilder('Graviton\ProxyBundle\Definition\Loader\DispersalStrategy\SwaggerStrategy') |
|
159
|
|
|
->disableOriginalConstructor() |
|
160
|
|
|
->setMethods(['supports', 'process']) |
|
161
|
|
|
->getMock(); |
|
162
|
|
|
$mock ->expects($this->once()) |
|
163
|
|
|
->method("supports") |
|
164
|
|
|
->willReturn(true); |
|
165
|
|
|
$mock->expects($this->once()) |
|
166
|
|
|
->method("process") |
|
167
|
|
|
->with($this->equalTo($cachedContent)) |
|
168
|
|
|
->willReturn($apiDefinition); |
|
169
|
|
|
$this->sut->setDispersalStrategy($mock); |
|
170
|
|
|
|
|
171
|
|
|
$cacheMock = $this->getMockBuilder('Doctrine\Common\Cache\FilesystemCache') |
|
172
|
|
|
->disableOriginalConstructor() |
|
173
|
|
|
->setMethods(['contains', 'fetch']) |
|
174
|
|
|
->getMock(); |
|
175
|
|
|
$cacheMock->expects($this->once()) |
|
176
|
|
|
->method('contains') |
|
177
|
|
|
->with($this->equalTo($storeKey)) |
|
178
|
|
|
->will($this->returnValue(true)); |
|
179
|
|
|
$cacheMock->expects($this->once()) |
|
180
|
|
|
->method('fetch') |
|
181
|
|
|
->with($this->equalTo($storeKey)) |
|
182
|
|
|
->willReturn($cachedContent); |
|
183
|
|
|
$this->sut->setCache($cacheMock, 'ProxyBundle', 1234); |
|
184
|
|
|
$this->sut->setOptions(['prefix' => $storeKey]); |
|
185
|
|
|
|
|
186
|
|
|
$content = $this->sut->load("http://localhost/test/blablabla"); |
|
187
|
|
|
$this->assertInstanceOf('Graviton\ProxyBundle\Definition\ApiDefinition', $content); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.