Passed
Push — master ( 445708...0db764 )
by Steffen
08:17
created

CurrencyConverterTest::testConversion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 5
nop 6
1
<?php
2
/*
3
 * This file is part of the unicorn project
4
 *
5
 * (c) Philipp Braeutigam <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Xynnn\Unicorn\Tests\Converter;
11
12
use SteffenBrand\CurrCurr\Client\EcbClientMock;
13
use SteffenBrand\CurrCurr\Model\Currency;
14
use Xynnn\Unicorn\Converter\CurrencyConverter;
15
use Xynnn\Unicorn\Model\ConvertibleValue;
16
use Xynnn\Unicorn\Model\Unit;
17
18
class CurrencyConverterTest extends AbstractConverterTest
19
{
20
    public function testIsInstantiable()
21
    {
22
        $converter = $this->getConverter();
23
24
        $this->assertInstanceOf(CurrencyConverter::class, $converter);
25
    }
26
27
    public function testGetName()
28
    {
29
        $converter = $this->getConverter();
30
31
        $this->assertEquals('unicorn.converter.currency', $converter->getName());
32
    }
33
34
    /**
35
     * @expectedException \Xynnn\Unicorn\Exception\UnsupportedUnitException
36
     * @expectedExceptionMessage The conversion of "eur" is not possible. Make sure to add it to the converters units array first.
37
     */
38
    public function testWrongTypePassed()
39
    {
40
        $converter = $this->getConverter();
41
        $converter->convert(new ConvertibleValue('10', $converter::$eur), new Unit('eur', '€', '1'));
42
    }
43
44 View Code Duplication
    public function testOwnTypePassed()
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...
45
    {
46
        $converter = $this->getConverter();
47
        $converter->addUnit(new Unit('myCurrency', 'mc', '5'));
48
        $result = $converter->convert(new ConvertibleValue('1', $converter::$eur), new Unit('myCurrency', 'mc', '5'));
49
50
        $this->assertEquals('5', $result->getValue());
51
        $this->assertEquals(new Unit('myCurrency', 'mc', '5'), $result->getUnit());
52
        $this->assertEquals('mc', $result->getUnit()->getAbbreviation());
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function dataProvider()
59
    {
60
        $converter = $this->getConverter();
61
        $converter->loadExchangeRates();
62
63
        return [
64
            [$converter, new ConvertibleValue('10', $converter::$eur), $converter::$usd, bcmul('10', $converter::$usd->getFactor(), $converter::MAX_DECIMALS), Currency::USD, $converter::$usd->getAbbreviation()],
65
            [$converter, new ConvertibleValue(bcmul('10', $converter::$usd->getFactor(), $converter::MAX_DECIMALS), $converter::$usd), $converter::$eur, '10', 'EUR', '€']
66
        ];
67
    }
68
69
    public function testNoRoundingErrorDuringAdditionAndSubtractionAndConversion()
70
    {
71
        $converter = $this->getConverter();
72
73
        // sum up two different units
74
        $addition = $converter->add(
75
            new ConvertibleValue('10.12345678908745', $converter::$eur),
76
            new ConvertibleValue('100.77', $converter::$usd)
77
        );
78
79
        // convert them to jpy
80
        $jpy = $converter->convert($addition, $converter::$jpy);
81
82
        // subtract eur from jpy
83
        $subtraction = $converter->sub(
84
            $jpy,
85
            new ConvertibleValue('10.12345678908745', $converter::$eur)
86
        );
87
88
        // convert back to usd
89
        $usd = $converter->convert($subtraction, $converter::$usd);
90
91
        // make sure no rounding error occured
92
        $this->assertEquals('100.77', $usd->getValue(), 'unexpected rounding error occured');
93
    }
94
95
    public function testGetFloatValTrimsTrailingZeros()
96
    {
97
        $converter = $this->getConverter();
98
        $cv = new ConvertibleValue('10.12345678900000', $converter::$eur);
99
        $this->assertEquals(10.123456789, $cv->getFloatValue());
100
    }
101
102
    /**
103
     * @return CurrencyConverter
104
     */
105
    private function getConverter() : CurrencyConverter
106
    {
107
        return new CurrencyConverter(new EcbClientMock(EcbClientMock::VALID_RESPONSE));
108
    }
109
}
110