RunningBalanceTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A setId() 0 7 1
A testId() 0 6 1
A testSetGetUser() 0 8 1
A testSetGetBalance() 0 6 1
A testAddBalance() 0 6 1
A addBalanceProvider() 0 10 1
A testSubtractBalance() 0 6 1
A subtractBalanceProvider() 0 10 1
A testJsonSerializeWithDefaultValuesThrowsException() 0 5 1
A testJsonSerializeWithModifiedValues() 0 20 1
1
<?php
2
3
namespace JhFlexiTimeTest\Entity;
4
5
use JhFlexiTime\Entity\RunningBalance;
6
use JhUser\Entity\User;
7
use ReflectionClass;
8
use DateTime;
9
10
class RunningBalanceTest extends \PHPUnit_Framework_TestCase
11
{
12
13
    /**
14
     * @var RunningBalance
15
     */
16
    protected $runningBalance;
17
18
    /**
19
     * SetUp
20
     */
21
    public function setUp()
22
    {
23
        $this->runningBalance = new RunningBalance();
24
    }
25
26
    /**
27
     * @param RunningBalance $runningBalance
28
     * @param $id
29
     */
30
    public function setId(RunningBalance $runningBalance, $id)
31
    {
32
        $reflector = new ReflectionClass($runningBalance);
33
        $property  = $reflector->getProperty('id');
34
        $property->setAccessible(true);
35
        $property->setValue($runningBalance, $id);
36
    }
37
38
    public function testId()
39
    {
40
        $this->assertNull($this->runningBalance->getId());
41
        $this->setId($this->runningBalance, 1);
42
        $this->assertEquals(1, $this->runningBalance->getId());
43
    }
44
45
    public function testSetGetUser()
46
    {
47
        $user = $this->getMock('ZfcUser\Entity\UserInterface');
48
49
        $this->assertNull($this->runningBalance->getUser());
50
        $this->runningBalance->setUser($user);
51
        $this->assertSame($user, $this->runningBalance->getUser());
52
    }
53
54
    public function testSetGetBalance()
55
    {
56
        $this->assertEquals(0, $this->runningBalance->getBalance());
57
        $this->runningBalance->setBalance(10);
58
        $this->assertSame(10, $this->runningBalance->getBalance());
59
    }
60
61
    /**
62
     * @param float $originalBalance
63
     * @param float $addition
64
     * @param float $expected
65
     *
66
     * @dataProvider addBalanceProvider
67
     */
68
    public function testAddBalance($originalBalance, $addition, $expected)
69
    {
70
        $this->runningBalance->setBalance($originalBalance);
71
        $this->runningBalance->addBalance($addition);
72
        $this->assertEquals($expected, $this->runningBalance->getBalance());
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function addBalanceProvider()
79
    {
80
        return [
81
            [0,    2,      2],
82
            [100,  10,     110],
83
            [230,  -10,    220],
84
            [-20,  20,     0],
85
            [-20,  -20,    -40],
86
        ];
87
    }
88
89
    /**
90
     * @param float $originalBalance
91
     * @param float $subtraction
92
     * @param float $expected
93
     *
94
     * @dataProvider subtractBalanceProvider
95
     */
96
    public function testSubtractBalance($originalBalance, $subtraction, $expected)
97
    {
98
        $this->runningBalance->setBalance($originalBalance);
99
        $this->runningBalance->subtractBalance($subtraction);
100
        $this->assertEquals($expected, $this->runningBalance->getBalance());
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    public function subtractBalanceProvider()
107
    {
108
        return [
109
            [0,    2,      -2],
110
            [100,  10,     90],
111
            [230,  -10,    240],
112
            [-20,  20,     -40],
113
            [-20,  -20,    0],
114
        ];
115
    }
116
117
118
    public function testJsonSerializeWithDefaultValuesThrowsException()
119
    {
120
        $this->setExpectedException('Exception', 'User Must be an instance of \ZfcUser\Entity\UserInterface');
121
        $this->runningBalance->jsonSerialize();
122
    }
123
124
    public function testJsonSerializeWithModifiedValues()
125
    {
126
        $user = $this->getMock('ZfcUser\Entity\UserInterface');
127
        $user->expects($this->once())
128
             ->method('getId')
129
             ->will($this->returnValue(1));
130
131
        $expected = [
132
            'id'        => 1,
133
            'user'      => 1,
134
            'balance'   => 2,
135
        ];
136
137
        $this->setId($this->runningBalance, 1);
138
        $this->runningBalance
139
            ->setUser($user)
140
            ->setBalance(2);
141
142
        $this->assertEquals($expected, $this->runningBalance->jsonSerialize());
143
    }
144
}
145