1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kingsquare\Banking; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
class StatementTest extends \PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
public function testBankAssesor() |
11
|
|
|
{ |
12
|
|
|
$expected = 'ABN'; |
13
|
|
|
$statement = new Statement(); |
14
|
|
|
$statement->setBank($expected); |
15
|
|
|
|
16
|
|
|
$this->assertEquals($expected, $statement->getBank()); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testAccountAssesor() |
20
|
|
|
{ |
21
|
|
|
$expected = '62.90.64.393'; |
22
|
|
|
$statement = new Statement(); |
23
|
|
|
$statement->setAccount($expected); |
24
|
|
|
|
25
|
|
|
$this->assertEquals($expected, $statement->getAccount()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testTransactionsAssesor() |
29
|
|
|
{ |
30
|
|
|
$expected = [ |
31
|
|
|
new Transaction(), |
32
|
|
|
new Transaction(), |
33
|
|
|
]; |
34
|
|
|
$statement = new Statement(); |
35
|
|
|
$statement->setTransactions($expected); |
36
|
|
|
|
37
|
|
|
$this->assertEquals($expected, $statement->getTransactions()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testStartPriceAssesor() |
41
|
|
|
{ |
42
|
|
|
$expected = '6250'; |
43
|
|
|
$statement = new Statement(); |
44
|
|
|
$statement->setStartPrice($expected); |
45
|
|
|
|
46
|
|
|
$this->assertEquals($expected, $statement->getStartPrice()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testEndPriceAssesor() |
50
|
|
|
{ |
51
|
|
|
$expected = '16250'; |
52
|
|
|
$statement = new Statement(); |
53
|
|
|
$statement->setEndPrice($expected); |
54
|
|
|
|
55
|
|
|
$this->assertEquals($expected, $statement->getEndPrice()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @expectedException PHPUnit_Framework_Error_Deprecated |
60
|
|
|
* @expectedExceptionMessage Deprecated in favor of splitting the start and end timestamps for a statement. Please use setStartTimestamp($format) or setEndTimestamp($format) instead. setTimestamp is now setStartTimestamp |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function testDeprecatedTimestampAssesor() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$expected = time(); |
65
|
|
|
$statement = new Statement(); |
66
|
|
|
$statement->setTimestamp($expected); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$this->assertEquals($expected, $statement->getTimestamp()); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testTimestampAssesor() |
72
|
|
|
{ |
73
|
|
|
$time = time(); |
74
|
|
|
$expectedStart = $time - 1440; |
75
|
|
|
$expectedEnd = $time; |
76
|
|
|
$statement = new Statement(); |
77
|
|
|
$statement->setStartTimestamp($expectedStart); |
78
|
|
|
$statement->setEndTimestamp($expectedEnd); |
79
|
|
|
|
80
|
|
|
$this->assertEquals($expectedStart, $statement->getStartTimestamp()); |
81
|
|
|
$this->assertEquals($expectedEnd, $statement->getEndTimestamp()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testNumberAssesor() |
85
|
|
|
{ |
86
|
|
|
$expected = '2665487AAF'; |
87
|
|
|
$statement = new Statement(); |
88
|
|
|
$statement->setNumber($expected); |
89
|
|
|
|
90
|
|
|
$this->assertEquals($expected, $statement->getNumber()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @depends testStartPriceAssesor |
95
|
|
|
* @depends testEndPriceAssesor |
96
|
|
|
*/ |
97
|
|
|
public function testDeltaPrice() |
98
|
|
|
{ |
99
|
|
|
$expected = '10000'; |
100
|
|
|
$statement = new Statement(); |
101
|
|
|
$statement->setStartPrice('16250'); |
102
|
|
|
$statement->setEndPrice('6250'); |
103
|
|
|
|
104
|
|
|
$this->assertEquals($expected, $statement->getDeltaPrice()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @depends testTransactionsAssesor |
109
|
|
|
*/ |
110
|
|
|
public function testAddTransaction() |
111
|
|
|
{ |
112
|
|
|
$statement = new Statement(); |
113
|
|
|
$statement->setTransactions([ |
114
|
|
|
new Transaction(), |
115
|
|
|
new Transaction(), |
116
|
|
|
]); |
117
|
|
|
$statement->addTransaction(new Transaction()); |
118
|
|
|
|
119
|
|
|
$this->assertCount(3, $statement->getTransactions()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @depends testTimestampAssesor |
124
|
|
|
* @expectedException PHPUnit_Framework_Error_Deprecated |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
public function testGetTimestampWithFormat() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$expected = '2012-01-01 12:00'; |
129
|
|
|
$statement = new Statement(); |
130
|
|
|
$statement->setTimestamp(strtotime($expected)); |
|
|
|
|
131
|
|
|
|
132
|
|
|
$this->assertEquals($expected, $statement->getTimestamp('Y-m-d H:i')); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function testJsonSerialization() |
136
|
|
|
{ |
137
|
|
|
$expected = '{"bank":"ABN","account":"62.90.64.393","transactions":[],'. |
138
|
|
|
'"startPrice":16250,"endPrice":6250,"startTimestamp":123,"endTimestamp":0,"number":"2665487AAF"}'; |
139
|
|
|
$params = [ |
140
|
|
|
'bank' => 'ABN', |
141
|
|
|
'account' => '62.90.64.393', |
142
|
|
|
'transactions' => [], |
143
|
|
|
'startPrice' => 16250, |
144
|
|
|
'endPrice' => 6250, |
145
|
|
|
'startTimestamp' => 123, |
146
|
|
|
'number' => '2665487AAF', |
147
|
|
|
]; |
148
|
|
|
$statement = new Statement(); |
149
|
|
|
foreach ($params as $key => $value) { |
150
|
|
|
$statement->{'set'.$key}($value); |
151
|
|
|
} |
152
|
|
|
$this->assertSame($expected, json_encode($statement)); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @depends testJsonSerialization |
157
|
|
|
* @expectedException PHPUnit_Framework_Error_Deprecated |
158
|
|
|
*/ |
159
|
|
|
public function testJsonSerializationWithTransactions() |
160
|
|
|
{ |
161
|
|
|
$expected = '{"bank":"ABN","account":"62.90.64.393","transactions":[{"account":"123123","accountName":'. |
162
|
|
|
'"Kingsquare BV","price":110,"debitcredit":"D","description":"test","valueTimestamp":1231,"entryTimestamp"'. |
163
|
|
|
':1234,"transactionCode":"13G"},{"account":"123123","accountName":"Kingsquare BV","price":110,"debitcredit"'. |
164
|
|
|
':"D","description":"test","valueTimestamp":1231,"entryTimestamp":1234,"transactionCode":"13G"}],'. |
165
|
|
|
'"startPrice":16250,"endPrice":6250,"number":"2665487AAF"}'; |
166
|
|
|
$params = [ |
167
|
|
|
'bank' => 'ABN', |
168
|
|
|
'account' => '62.90.64.393', |
169
|
|
|
'transactions' => [ |
170
|
|
|
[ |
171
|
|
|
'account' => '123123', |
172
|
|
|
'accountName' => 'Kingsquare BV', |
173
|
|
|
'price' => 110.0, |
174
|
|
|
'debitcredit' => Transaction::DEBIT, |
175
|
|
|
'description' => 'test', |
176
|
|
|
'valueTimestamp' => 1231, |
177
|
|
|
'entryTimestamp' => 1234, |
178
|
|
|
'transactionCode' => '13G', |
179
|
|
|
], |
180
|
|
|
[ |
181
|
|
|
'account' => '123123', |
182
|
|
|
'accountName' => 'Kingsquare BV', |
183
|
|
|
'price' => 110.0, |
184
|
|
|
'debitcredit' => Transaction::DEBIT, |
185
|
|
|
'description' => 'test', |
186
|
|
|
'valueTimestamp' => 1231, |
187
|
|
|
'entryTimestamp' => 1234, |
188
|
|
|
'transactionCode' => '13G', |
189
|
|
|
], |
190
|
|
|
], |
191
|
|
|
'startPrice' => 16250, |
192
|
|
|
'endPrice' => 6250, |
193
|
|
|
'timestamp' => 123, |
194
|
|
|
'number' => '2665487AAF', |
195
|
|
|
]; |
196
|
|
|
$statement = new Statement(); |
197
|
|
|
foreach ($params as $key => $value) { |
198
|
|
|
$statement->{'set'.$key}($value); |
199
|
|
|
} |
200
|
|
|
$this->assertSame($expected, json_encode($statement)); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
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.