Test Setup Failed
Push — master ( ac8464...9588f8 )
by Andrey
03:17
created

testDenominationGetRateExchanges()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
/**
3
 * (c) itmedia.by <[email protected]>
4
 */
5
6
namespace Submarine\NbrbExchangeRatesBundle\Tests\Provider;
7
8
9
use PHPUnit\Framework\TestCase;
10
use Submarine\NbrbExchangeRatesBundle\Client\ApiClient;
11
use Submarine\NbrbExchangeRatesBundle\Exception\UndefinedCurrencyException;
12
use Submarine\NbrbExchangeRatesBundle\ExchangeRate;
13
use Submarine\NbrbExchangeRatesBundle\Provider\ExchangeRatesProvider;
14
use Submarine\NbrbExchangeRatesBundle\Tests\Client\ApiClientTest;
15
16
class ExchangeRatesProviderTest extends TestCase
17
{
18
19
    private function getProvider()
20
    {
21
        $apiClient = new ApiClient(ApiClientTest::URL_RATES, ApiClientTest::URL_RATES_DYNAMIC, 3, 3);
22
        return new ExchangeRatesProvider($apiClient, true);
23
    }
24
25
26
    private $ratesList = [
27
        'USD' => [
28
            'code' => 'USD',
29
            'num_code' => 840,
30
            'scale' => 1,
31
            'name' => 'Доллар США',
32
            'rate' => 19958
33
        ],
34
        'EUR' => [
35
            'code' => 'EUR',
36
            'num_code' => 978,
37
            'scale' => 1,
38
            'name' => 'Евро',
39
            'rate' => 22747
40
        ],
41
        'PLN' => [
42
            'code' => 'PLN',
43
            'num_code' => 985,
44
            'scale' => 1,
45
            'name' => 'Злотый',
46
            'rate' => 5305.16
47
        ],
48
    ];
49
50
    private $dynamicRates = [
51
        'USD' => [
52
            '04/12/2016' => 19958,
53
            '04/13/2016' => 19864,
54
            '04/14/2016' => 19849,
55
            '04/15/2016' => 19989,
56
            '04/16/2016' => 19907,
57
        ]
58
    ];
59
60
61
    public function testGetAllRatesExchanges()
62
    {
63
        $provider = $this->getProvider();
64
65
        $date = new \DateTime();
66
        $date->setDate(2016, 4, 12);
67
        $rates = $provider->getAllRatesExchanges($date);
68
69
        foreach ($this->ratesList as $key => $value) {
70
            $rate = $rates[$key];
71
            $this->assertRate($rate, $date, $value);
72
        }
73
    }
74
75
76
    public function testGetRatesExchanges()
77
    {
78
        $provider = $this->getProvider();
79
80
        $date = new \DateTime();
81
        $date->setDate(2016, 4, 12);
82
        $rates = $provider->getRatesExchanges(['USD', 'PLN'], $date);
83
        foreach ($rates as $rate) {
84
            $pattern = $this->ratesList[$rate->getCharCode()];
85
            $this->assertRate($rate, $date, $pattern);
86
        }
87
88
        // Fail
89
        try {
90
            $provider->getRatesExchanges(['USD', 'HUI'], $date);
91
            $this->assertTrue(false);
92
        } catch (UndefinedCurrencyException $exc) {
93
            $this->assertTrue(true);
94
        }
95
    }
96
97
98
    public function testDenominationGetRateExchanges()
99
    {
100
        $provider = $this->getProvider();
101
102
        $date = new \DateTime();
103
        $date->setDate(2016, 7, 1);
104
        $rate = $provider->getRateExchange('USD', $date);
105
        $this->assertEquals($rate->getRate(), 2.0053);
106
107
    }
108
109
110
    public function testGetRateExchange()
111
    {
112
        $provider = $this->getProvider();
113
114
        $date = new \DateTime();
115
        $date->setDate(2016, 4, 12);
116
        $rate = $provider->getRateExchange('USD', $date);
117
        $this->assertRate($rate, $date, $this->ratesList['USD']);
118
119
        // Fail
120
        try {
121
            $provider->getRateExchange('HUI', $date);
122
            $this->assertTrue(false);
123
        } catch (UndefinedCurrencyException $exc) {
124
            $this->assertTrue(true);
125
        }
126
    }
127
128
129
    public function testGetRatesExchangesDynamic()
130
    {
131
        $provider = $this->getProvider();
132
133
        $start = new \DateTime();
134
        $start->setDate(2016, 4, 12);
135
136
        $end = new \DateTime();
137
        $end->setDate(2016, 4, 16);
138
139
        $code = 'USD';
140
        $rates = $provider->getRatesExchangesDynamic($code, $start, $end);
141
142
        foreach ($rates as $rate) {
143
            $date = $rate->getDate()->format('m/d/Y');
144
145
            $this->assertTrue(isset($this->dynamicRates[$code][$date]));
146
            $this->assertEquals($rate->getRate(), $this->dynamicRates[$code][$date]);
147
        }
148
149
150
    }
151
152
153
    private function assertRate(ExchangeRate $rate, \DateTime $date, $value)
154
    {
155
        $this->assertEquals($rate->getNumCode(), $value['num_code']);
156
        $this->assertEquals($rate->getScale(), $value['scale']);
157
        $this->assertEquals($rate->getName(), $value['name']);
158
        $this->assertEquals($rate->getRate(), $value['rate']);
159
        $this->assertEquals($rate->getCharCode(), $value['code']);
160
        $this->assertEquals($rate->getDate()->format('dmy'), $date->format('dmy'));
161
    }
162
163
}