Completed
Push — master ( 2eabb1...48c775 )
by Alessandro
01:50
created

RatesTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 0
loc 54
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testReturnsZeroWheneverApiProvideEmptyResponse() 0 21 1
A testReturnRates() 0 29 1
1
<?php
2
3
namespace Tests;
4
5
use CurrencyConverter\Rates;
6
7
class RatesTest extends \PHPUnit\Framework\TestCase
8
{
9
    public function testReturnsZeroWheneverApiProvideEmptyResponse()
10
    {
11
        $this->apiCaller = $this
0 ignored issues
show
Bug introduced by
The property apiCaller does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
12
            ->getMockBuilder('CurrencyConverter\ApiCaller')
13
            ->disableOriginalConstructor()
14
            ->getMock();
15
16
        $this->rates = new Rates($this->apiCaller);
0 ignored issues
show
Bug introduced by
The property rates does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
18
        $this->apiCaller->expects($this->once())
19
            ->method('convert')
20
            ->with('FOO', 'BAR');
21
22
        $this->apiCaller->expects($this->once())
23
            ->method('isLastCallEmpty')
24
            ->willReturn(true);
25
26
        $result = $this->rates->getRates('FOO', 'BAR');
27
28
        $this->assertEquals(0, $result);
29
    }
30
31
    public function testReturnRates()
32
    {
33
        $from = 'FOO';
34
        $to = 'BAR';
35
        $expected = 100;
36
37
        $this->apiCaller = $this
38
            ->getMockBuilder('CurrencyConverter\ApiCaller')
39
            ->disableOriginalConstructor()
40
            ->getMock();
41
42
        $this->rates = new Rates($this->apiCaller);
43
44
        $this->apiCaller->expects($this->once())
45
            ->method('convert')
46
            ->with('FOO', 'BAR');
47
48
        $this->apiCaller->expects($this->once())
49
            ->method('isLastCallEmpty')
50
            ->willReturn(false);
51
52
        $this->apiCaller->expects($this->once())
53
             ->method('getLastResponse')
54
             ->willReturn(json_encode([$from . '_' . $to => $expected]));
55
56
        $result = $this->rates->getRates($from, $to);
57
58
        $this->assertEquals($expected, $result);
59
    }
60
}
61