Completed
Push — master ( d208aa...25518b )
by Lars
12:37
created

AccountTest::testSavePrimoSaldo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
require_once 'Intraface/modules/accounting/Account.php';
3
4
class FakeAccountYear
5
{
6
    public $kernel;
7
    function __construct()
8
    {
9
        $this->kernel = new Stub_Kernel;
10
        $this->kernel->setting->set('intranet', 'vatpercent', 25);
11
    }
12
    function get()
13
    {
14
        return 1;
15
    }
16
17
    function getSetting()
18
    {
19
        return 1;
20
    }
21
}
22
23
class AccountTest extends PHPUnit_Framework_TestCase
24
{
25
    private $delta = 0.001;
26
    protected $db;
27
28
    function setUp()
29
    {
30
        $this->db = MDB2::singleton(DB_DSN);
31
    }
32
33
    function tearDown()
34
    {
35
        $this->db->exec('TRUNCATE accounting_account');
36
    }
37
38
    function createAccount($id = 0)
39
    {
40
        return new Account(new FakeAccountYear, $id);
41
    }
42
43
    /////////////////////////////////////////////////////////////////////////////////////
44
45
    function testVatCalculationReturnsCorrectValues()
46
    {
47
        $this->assertEquals((80 + Account::calculateVat(100, 25)), 100);
48
        $this->assertEquals((100 + Account::calculateVat(110, 10)), 110);
49
        $this->assertEquals(round((93.40 + Account::calculateVat(100.41, 7.5)), 2), 100.41);
50
    }
51
52
    function testConstructionOfAnAccountObjectReturnsAValidObject()
53
    {
54
        $account = $this->createAccount();
55
        $this->assertTrue(is_object($account));
56
        $this->assertEquals('Account', get_class($account));
57
    }
58
59 View Code Duplication
    function testSaveReturnsAnIntegerLargerThanZero()
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...
60
    {
61
        $account = $this->createAccount();
62
        $account_number = rand(1, 1000000);
63
        $data = array(
64
            'number' => $account_number,
65
            'name' => 'test',
66
            'type_key' => 1,
67
            'use_key' => 1,
68
            'vat_key' => 1,
69
            'vat_percent' => 25
70
        );
71
        $id = $account->save($data);
72
73
        $this->assertTrue(($id > 0));
74
    }
75
76
    function testSavePrimoSaldo()
77
    {
78
        $account = $this->createAccount();
79
        $account_number = rand(1, 1000000);
80
        $data = array(
81
            'number' => $account_number,
82
            'name' => 'test',
83
            'type_key' => 1,
84
            'use_key' => 1,
85
            'vat_key' => 1,
86
            'vat_percent' => 25
87
        );
88
        $id = $account->save($data);
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
90
        $debet = '380071,97';
91
        $credit = '0';
92
        $this->assertTrue($account->savePrimoSaldo($debet, $credit));
93
94
        $saldo = $account->getPrimoSaldo();
95
96
        $this->assertEquals('380071.97', $saldo['debet'], '', $this->delta);
97
        $this->assertEquals($credit, $saldo['credit'], '', $this->delta);
98
    }
99
100
    function testUpdatePrimoSaldo()
101
    {
102
        $account = $this->createAccount();
103
        $account_number = rand(1, 1000000);
104
        $data = array(
105
            'number' => $account_number,
106
            'name' => 'test',
107
            'type_key' => 1,
108
            'use_key' => 1,
109
            'vat_key' => 1,
110
            'vat_percent' => 25
111
        );
112
        $id = $account->save($data);
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
113
114
        // ATTENTION: test will fail if primosaldo is not a double (not float)
115
        $debet = '380071,97';
116
        $credit = '0';
117
        $this->assertTrue($account->savePrimoSaldo($debet, $credit));
118
119
        $debet_new = '380071,96';
120
        $credit_new = '0';
121
122
        $this->assertTrue($account->savePrimoSaldo($debet_new, $credit_new));
123
124
        $saldo = $account->getPrimoSaldo();
125
126
        $this->assertEquals('380071.96', $saldo['debet'], '', $this->delta);
127
    }
128
129
    function testDeleteActuallyDeletesTheAccount()
130
    {
131
        $account = $this->createAccount();
132
        $this->assertTrue($account->delete());
133
        $this->assertEquals(0, $account->get('active'));
134
    }
135
136
    function testGetSaldo()
137
    {
138
        $account = $this->createAccount();
139
        // @todo this is strange behaviour
140
        $this->assertTrue($account->getSaldo());
141
    }
142
143
    function testAnyPostsReturnsZeroWhenNoPostsAreFound()
144
    {
145
        $account = $this->createAccount();
146
        $this->assertEquals(0, $account->anyPosts());
147
    }
148
149
    function testGetPostReturnsAnArray()
150
    {
151
        $account = $this->createAccount();
152
        $this->assertTrue(is_array($account->getPosts()));
153
    }
154
155
    function testAnyAccountsReturnsZeroWhenNoAccountIsSaved()
156
    {
157
        $account = $this->createAccount();
158
        $this->assertTrue($account->anyAccounts() == 0);
159
    }
160
161
162 View Code Duplication
    function testAnyAccountsReturnsAnIntegerGreaterThanZeroWhenOneAccountIsSaved()
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...
163
    {
164
        $account = $this->createAccount();
165
        $account_number = rand(1, 1000000);
166
        $data = array(
167
            'number' => $account_number,
168
            'name' => 'test',
169
            'type_key' => 1,
170
            'use_key' => 1,
171
            'vat_key' => 1,
172
            'vat_percent' => 25
173
        );
174
        $this->assertTrue($account->save($data) > 0);
175
176
        $this->assertTrue($account->anyAccounts() > 0);
177
    }
178
179
    function testVatPercentIsTransferredWhenSavingAnAccountFromTheValuesOfAnotherAccount()
180
    {
181
        $account = $this->createAccount();
182
        $account_number = rand(1, 1000000);
183
        $data = array(
184
            'number' => $account_number,
185
            'name' => 'test',
186
            'type_key' => 1,
187
            'use_key' => 1,
188
            'vat_key' => 1,
189
            'vat_percent' => 25
190
        );
191
        $this->assertTrue($account->save($data) > 0);
192
        $this->assertEquals(25, $account->get('vat_percent'));
193
194
        $account_new = $this->createAccount();
195
        $data = $account->get();
196
        $data['number'] = rand(1, 1000000);
197
        ;
198
        $account_new->save($data);
199
        $this->assertEquals(25, $account_new->get('vat_percent'));
200
    }
201
202
    function testFactory()
203
    {
204
        $account = $this->createAccount();
205
        $factory = Account::factory(new FakeAccountYear, $account->getNumber());
0 ignored issues
show
Deprecated Code introduced by
The method Account::factory() has been deprecated.

This method has been deprecated.

Loading history...
206
        $this->assertTrue(is_object($factory));
207
        $this->assertEquals($account->getNumber(), $factory->getNumber());
208
    }
209
210
    function testValidForStateReturnsFalseIfTypeKeyIsNotSet()
211
    {
212
        $account = $this->createAccount();
213
        $res = $account->validForState();
214
        $this->assertFalse($res);
215
    }
216
217
    function testSaveReturnsTrueWhenTypeKeyIsSet()
218
    {
219
        $account = $this->createAccount();
220
        $account_number = rand(1, 1000000);
221
        $data = array(
222
            'number' => $account_number,
223
            'name' => 'test',
224
            'type_key' => 2,
225
            'use_key' => 1,
226
            'vat_key' => 1,
227
            'vat_percent' => 25
228
        );
229
        $id = $account->save($data);
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
230
        $res = $account->validForState();
231
        $this->assertEquals('operating', $account->getType());
232
        $this->assertTrue($res);
233
    }
234
}
235