Passed
Push — master ( 48c775...23192d )
by Alessandro
01:22
created

CurrencyConverterTest::testGetRates()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests;
4
5
use CurrencyConverter\CurrencyConverter;
6
use CurrencyConverter\Rates;
7
8
class CurrencyConverterTest extends \PHPUnit\Framework\TestCase
9
{
10
    /**
11
     * @expectedException  \Exception
12
     */
13
    public function testConvertWithNotExistingApiKey()
14
    {
15
        $currencyConverter = new CurrencyConverter('apiKey');
16
        $currencyConverter->convert('foo', 'bar', random_int(1, 999999));
17
    }
18
19 View Code Duplication
    public function testConvertWithinValidCurrencyProvided()
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...
20
    {
21
        $fromCurrency = 'EUR';
22
        $toCurrency = 'USD';
23
        $expected = 100;
24
25
        $rates = $this->prophesize(Rates::class);
26
        $rates->getRates($fromCurrency, $toCurrency)->willReturn($expected);
27
28
        $currencyConverter = new CurrencyConverter('apiKey');
29
        $currencyConverter->setRates($rates->reveal());
30
        $result = $currencyConverter->convert($fromCurrency, $toCurrency, random_int(1, 999999));
31
32
        $this->assertNotEquals($expected, $result);
33
    }
34
35 View Code Duplication
    public function testConvertWithNotValidResponseReturn0()
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...
36
    {
37
        $fromCurrency = 'EUR';
38
        $toCurrency = 'USD';
39
        $value = 0;
40
41
        $rates = $this->prophesize(Rates::class);
42
        $rates->getRates($fromCurrency, $toCurrency)->willReturn($value);
43
44
        $currencyConverter = new CurrencyConverter('apiKey');
45
        $currencyConverter->setRates($rates->reveal());
46
        $result = $currencyConverter->convert($fromCurrency, $toCurrency, random_int(1, 999999));
47
48
        $this->assertEquals(0, $result);
49
    }
50
51 View Code Duplication
    public function testGetRates()
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...
52
    {
53
        $fromCurrency = 'EUR';
54
        $toCurrency = 'USD';
55
        $value = 100;
56
57
        $rates = $this->prophesize(Rates::class);
58
        $rates->getRates($fromCurrency, $toCurrency)->willReturn($value);
59
60
        $currencyConverter = new CurrencyConverter('apiKey');
61
        $currencyConverter->setRates($rates->reveal());
62
        $result = $currencyConverter->getRates($fromCurrency, $toCurrency);
63
64
        $this->assertEquals($value, $result);
65
    }
66
67
    public function setApiCaller()
68
    {
69
        $apiCallerMock = $this->prophesize(ApiCaller::class);
70
71
        $currencyConverter = new CurrencyConverter('apiKey');
72
        $currencyConverter->setApiCaller($apiCaller->reveal());
0 ignored issues
show
Bug introduced by
The variable $apiCaller seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
73
        
74
        $apiCaller = $currencyConverter->getApiCaller();
75
        $this->assertEquals($apiCallerMock, $apiCaller);
76
    }
77
}
78