This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Mrzard\OpenExchangeRatesBundle\Tests; |
||
4 | |||
5 | use GuzzleHttp\Psr7\Response; |
||
6 | use Mrzard\OpenExchangeRates\Service\OpenExchangeRatesService; |
||
7 | |||
8 | class OpenExchangeRatesServiceTest extends \PHPUnit_Framework_TestCase |
||
9 | { |
||
10 | /** |
||
11 | * Get service configuration |
||
12 | * |
||
13 | * @return array |
||
14 | */ |
||
15 | public function getServiceConfig() |
||
16 | { |
||
17 | return array( |
||
18 | array( |
||
19 | 'f4k3', |
||
20 | array( |
||
21 | 'base_currency' => 'USD', |
||
22 | 'https' => false |
||
23 | ) |
||
24 | ) |
||
25 | ); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @dataProvider getServiceConfig |
||
30 | * Test that the functions can run |
||
31 | * @param $appId |
||
32 | * @param $config |
||
33 | */ |
||
34 | public function testService($appId, array $config) |
||
35 | { |
||
36 | $fakeClient = $this->mockClient($this->mockResponse()); |
||
37 | $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient); |
||
38 | $latest = $testingService->getLatest(array(), null); |
||
39 | static::assertTrue($latest['ok'], 'getLatest failed'); |
||
40 | |||
41 | $fakeClient = $this->mockClient($this->mockResponse()); |
||
42 | $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient); |
||
43 | $latest = $testingService->getLatest(array('EUR'), null); |
||
44 | static::assertTrue($latest['ok'], 'getLatest failed'); |
||
45 | |||
46 | $fakeClient = $this->mockClient($this->mockResponse()); |
||
47 | $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient); |
||
48 | $latest = $testingService->getLatest(array('EUR'), 'USD'); |
||
49 | static::assertTrue($latest['ok'], 'getLatest failed'); |
||
50 | |||
51 | $fakeClient = $this->mockClient($this->mockResponse()); |
||
52 | $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient); |
||
53 | $latest = $testingService->getLatest(array(), 'USD'); |
||
54 | static::assertTrue($latest['ok'], 'getLatest failed'); |
||
55 | |||
56 | $fakeClient = $this->mockClient($this->mockResponse()); |
||
57 | $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient); |
||
58 | $currencies = $testingService->getCurrencies(); |
||
59 | static::assertTrue($currencies['ok'], 'getCurrencies failed'); |
||
60 | |||
61 | $fakeClient = $this->mockClient($this->mockResponse()); |
||
62 | $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient); |
||
63 | $convertCurrency = $testingService->convertCurrency(10, 'EUR', 'USD'); |
||
64 | static::assertTrue($convertCurrency['ok'], 'convertCurrency failed'); |
||
65 | |||
66 | $fakeClient = $this->mockClient($this->mockResponse()); |
||
67 | $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient); |
||
68 | $getHistorical = $testingService->getHistorical(new \DateTime('2014-01-01')); |
||
69 | static::assertTrue($getHistorical['ok'], 'getHistorical failed'); |
||
70 | |||
71 | $fakeClient = $this->mockClient($this->mockResponse()); |
||
72 | $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient); |
||
73 | $getHistorical = $testingService->getHistorical(new \DateTime('2014-01-01'), array(), 'USD'); |
||
74 | static::assertTrue($getHistorical['ok'], 'getHistorical failed'); |
||
75 | |||
76 | $fakeClient = $this->mockClient($this->mockResponse()); |
||
77 | $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient); |
||
78 | $getHistorical = $testingService->getHistorical(new \DateTime('2014-01-01'), array('EUR'), 'USD'); |
||
79 | static::assertTrue($getHistorical['ok'], 'getHistorical failed'); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @dataProvider getServiceConfig |
||
84 | * Test that the class can be instantiated |
||
85 | * @param $appId |
||
86 | * @param $config |
||
87 | */ |
||
88 | public function testInstantiation($appId, array $config) |
||
89 | { |
||
90 | $service = new OpenExchangeRatesService($appId, $config, $this->mockClient(null)); |
||
91 | static::assertTrue($service instanceof OpenExchangeRatesService, 'Creation failed'); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @dataProvider getServiceConfig |
||
96 | * Test what happens when an error is thrown |
||
97 | * @param $appId |
||
98 | * @param $config |
||
99 | */ |
||
100 | public function testError($appId, array $config) |
||
101 | { |
||
102 | //all request will return a fake response |
||
103 | $fakeResponse = $this->mockResponse(); |
||
104 | |||
105 | //create our fake client |
||
106 | $fakeClient = $this->mockClient($fakeResponse); |
||
107 | |||
108 | //make send throw an exception |
||
109 | $fakeClient->expects(static::any())->method('request')->willThrowException( |
||
110 | new \Exception('testException') |
||
111 | ); |
||
112 | |||
113 | $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient); |
||
114 | |||
115 | static::assertArrayHasKey('error', $testingService->getCurrencies(), 'Error was not properly checked'); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @dataProvider getServiceConfig |
||
120 | * Test general config |
||
121 | * @param $appId |
||
122 | * @param $config |
||
123 | */ |
||
124 | public function testConfig($appId, array $config) |
||
125 | { |
||
126 | $fakeClient = $this->mockClient($this->mockResponse()); |
||
127 | $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient); |
||
128 | |||
129 | static::assertEquals($config['https'], $testingService->useHttps(), 'https config mismatch'); |
||
130 | |||
131 | static::assertEquals( |
||
132 | $config['base_currency'], |
||
133 | $testingService->getBaseCurrency(), |
||
134 | 'base_currency config mismatch' |
||
135 | ); |
||
136 | |||
137 | $testingService->setHttps(true); |
||
138 | static::assertTrue($testingService->useHttps(), 'https setter failed'); |
||
139 | |||
140 | static::assertEquals( |
||
141 | 'https://openexchangerates.org/api', |
||
142 | $testingService->getEndPoint(), |
||
143 | 'Endpoint does not look right' |
||
144 | ); |
||
145 | |||
146 | $testingService->setHttps(false); |
||
147 | static::assertEquals( |
||
148 | 'http://openexchangerates.org/api', |
||
149 | $testingService->getEndPoint(), |
||
150 | 'Endpoint does not look right' |
||
151 | ); |
||
152 | |||
153 | $testingService->setBaseCurrency('EUR'); |
||
154 | static::assertEquals( |
||
155 | 'EUR', |
||
156 | $testingService->getBaseCurrency(), |
||
157 | 'base currency setter failed' |
||
158 | ); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * @return \PHPUnit_Framework_MockObject_MockObject |
||
163 | */ |
||
164 | View Code Duplication | private function mockRequest() |
|
0 ignored issues
–
show
|
|||
165 | { |
||
166 | $fakeRequest = $this |
||
167 | ->getMockBuilder('Mrzard\OpenExchangeRates\Service\HttpRequestInterface') |
||
168 | ->setMethods(array('request')) |
||
169 | ->getMock(); |
||
170 | $fakeRequest |
||
171 | ->expects(static::any()) |
||
172 | ->method('request') |
||
173 | ->will(static::returnSelf()); |
||
174 | |||
175 | return $fakeRequest; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return \PHPUnit_Framework_MockObject_MockObject|\Mrzard\OpenExchangeRates\Service\HttpResponseInterface |
||
180 | */ |
||
181 | private function mockResponse() |
||
182 | { |
||
183 | $fakeResponse = $this |
||
184 | ->getMockBuilder('Mrzard\OpenExchangeRates\Service\HttpResponseInterface') |
||
185 | ->setMethods(array('getWrappedResponse', 'getBody')) |
||
186 | ->getMock(); |
||
187 | $responseObject = new Response(200, ['Content-Type' => 'application/json'], '{"ok":true}'); |
||
188 | $fakeResponse |
||
189 | ->expects(static::any()) |
||
190 | ->method('getWrappedResponse') |
||
191 | ->willReturn($responseObject); |
||
192 | $fakeResponse |
||
193 | ->expects(static::any()) |
||
194 | ->method('getBody') |
||
195 | ->willReturn($responseObject->getBody()); |
||
196 | |||
197 | return $fakeResponse; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * @param $fakeResponse |
||
202 | * @return \PHPUnit_Framework_MockObject_MockObject|\Mrzard\OpenExchangeRates\Service\HttpClientInterface |
||
203 | */ |
||
204 | View Code Duplication | private function mockClient($fakeResponse) |
|
0 ignored issues
–
show
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. ![]() |
|||
205 | { |
||
206 | $fakeClient = $this |
||
207 | ->getMockBuilder('Mrzard\OpenExchangeRates\Service\HttpClientInterface') |
||
208 | ->setMethods(array('request')) |
||
209 | ->getMock(); |
||
210 | |||
211 | //our client will always return a our request |
||
212 | $fakeClient |
||
213 | ->expects(static::any()) |
||
214 | ->method('request') |
||
215 | ->withAnyParameters() |
||
216 | ->will(static::returnValue($fakeResponse)); |
||
217 | |||
218 | return $fakeClient; |
||
219 | } |
||
220 | } |
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.