Completed
Push — master ( 850634...9eeabf )
by Gonzalo
06:26
created

OpenExchangeRatesServiceTest::testError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
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
72
    /**
73
     * @dataProvider getServiceConfig
74
     * Test that the class can be instantiated
75
     * @param $appId
76
     * @param $config
77
     */
78
    public function testInstantiation($appId, array $config)
79
    {
80
        $service = new OpenExchangeRatesService($appId, $config, $this->mockClient(null));
81
        static::assertTrue($service instanceof OpenExchangeRatesService, 'Creation failed');
82
    }
83
84
    /**
85
     * @dataProvider getServiceConfig
86
     * Test what happens when an error is thrown
87
     * @param $appId
88
     * @param $config
89
     */
90
    public function testError($appId, array $config)
91
    {
92
        //all request will return a fake response
93
        $fakeResponse = $this->mockResponse();
94
95
        //create our fake client
96
        $fakeClient = $this->mockClient($fakeResponse);
97
98
        //make send throw an exception
99
        $fakeClient->expects(static::any())->method('request')->willThrowException(
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Mrzard\OpenExchangeRates...ice\HttpClientInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
100
            new \Exception('testException')
101
        );
102
103
        $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient);
104
105
        static::assertArrayHasKey('error', $testingService->getCurrencies(), 'Error was not properly checked');
106
    }
107
108
    /**
109
     * @dataProvider getServiceConfig
110
     * Test general config
111
     * @param $appId
112
     * @param $config
113
     */
114
    public function testConfig($appId, array $config)
115
    {
116
        $fakeClient = $this->mockClient($this->mockRequest(), $this->mockResponse());
0 ignored issues
show
Unused Code introduced by
The call to OpenExchangeRatesServiceTest::mockClient() has too many arguments starting with $this->mockResponse().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
117
        $testingService = new OpenExchangeRatesService($appId, $config, $fakeClient);
118
119
        static::assertEquals($config['https'], $testingService->useHttps(), 'https config mismatch');
120
121
        static::assertEquals(
122
            $config['base_currency'],
123
            $testingService->getBaseCurrency(),
124
            'base_currency config mismatch'
125
        );
126
127
        $testingService->setHttps(true);
128
        static::assertTrue($testingService->useHttps(), 'https setter failed');
129
130
        static::assertEquals(
131
            'https://openexchangerates.org/api',
132
            $testingService->getEndPoint(),
133
            'Endpoint does not look right'
134
        );
135
136
        $testingService->setHttps(false);
137
        static::assertEquals(
138
            'http://openexchangerates.org/api',
139
            $testingService->getEndPoint(),
140
            'Endpoint does not look right'
141
        );
142
143
        $testingService->setBaseCurrency('EUR');
144
        static::assertEquals(
145
            'EUR',
146
            $testingService->getBaseCurrency(),
147
            'base currency setter failed'
148
        );
149
    }
150
151
    /**
152
     * @return \PHPUnit_Framework_MockObject_MockObject
153
     */
154 View Code Duplication
    private function mockRequest()
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...
155
    {
156
        $fakeRequest = $this
157
            ->getMockBuilder('Mrzard\OpenExchangeRates\Service\HttpRequestInterface')
158
            ->setMethods(array('request'))
159
            ->getMock();
160
        $fakeRequest
161
            ->expects(static::any())
162
            ->method('request')
163
            ->will(static::returnSelf());
164
165
        return $fakeRequest;
166
    }
167
168
    /**
169
     * @return \PHPUnit_Framework_MockObject_MockObject|\Mrzard\OpenExchangeRates\Service\HttpResponseInterface
170
     */
171
    private function mockResponse()
172
    {
173
        $fakeResponse = $this
174
            ->getMockBuilder('Mrzard\OpenExchangeRates\Service\HttpResponseInterface')
175
            ->setMethods(array('getResponse'))
176
            ->getMock();
177
        $fakeResponse
178
            ->expects(static::any())
179
            ->method('getResponse')
180
            ->willReturn(new Response(200, ['Content-Type' => 'application/json'], '{"ok":true}'));
181
182
        return $fakeResponse;
183
    }
184
185
    /**
186
     * @param $fakeResponse
187
     * @return \PHPUnit_Framework_MockObject_MockObject|\Mrzard\OpenExchangeRates\Service\HttpClientInterface
188
     */
189 View Code Duplication
    private function mockClient($fakeResponse)
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...
190
    {
191
        $fakeClient = $this
192
            ->getMockBuilder('Mrzard\OpenExchangeRates\Service\HttpClientInterface')
193
            ->setMethods(array('request'))
194
            ->getMock();
195
196
        //our client will always return a our request
197
        $fakeClient
198
            ->expects(static::any())
199
            ->method('request')
200
            ->withAnyParameters()
201
            ->will(static::returnValue($fakeResponse));
202
203
        return $fakeClient;
204
    }
205
}