Passed
Push — master ( b4f1ba...dc5416 )
by Alessandro
02:37 queued 22s
created

testConvertWithNotValidResponseReturn0()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Tests;
4
5
use CurrencyConverter\ApiCaller;
6
use CurrencyConverter\CurrencyConverter;
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
    public function testConvertWithinValidCurrencyProvided()
20
    {
21
        $fromCurrency = 'EUR';
22
        $toCurrency = 'USD';
23
        $expected = 100;
24
25
        $apiCaller = $this->prophesize(ApiCaller::class);
26
        $apiCaller->convert($fromCurrency, $toCurrency)->willReturn($expected);
27
        $apiCaller->isLastCallEmpty()->willReturn(false);
28
        $apiCaller->getLastResponse()->willReturn(json_encode([$fromCurrency . '_' . $toCurrency => $expected]));
29
30
        $currencyConverter = new CurrencyConverter('apiKey');
31
        $currencyConverter->setApiCaller($apiCaller->reveal());
32
        $result = $currencyConverter->convert($fromCurrency, $toCurrency, random_int(1, 999999));
33
34
        $this->assertNotEquals($expected, $result);
35
    }
36
37
    public function testConvertWithNotValidResponseReturn0()
38
    {
39
        $fromCurrency = 'EUR';
40
        $toCurrency = 'USD';
41
        $value = 100;
42
43
        $apiCaller = $this->prophesize(ApiCaller::class);
44
        $apiCaller->convert($fromCurrency, $toCurrency)->willReturn($value);
45
        $apiCaller->isLastCallEmpty()->willReturn(false);
46
        $apiCaller->getLastResponse()->willReturn(json_encode(['notEsistKey' => $value]));
47
48
        $currencyConverter = new CurrencyConverter('apiKey');
49
        $currencyConverter->setApiCaller($apiCaller->reveal());
50
        $result = $currencyConverter->convert($fromCurrency, $toCurrency, random_int(1, 999999));
51
52
        $this->assertEquals(0, $result);
53
    }
54
}
55