Passed
Branch master (e17e02)
by Jacques
02:12
created

InvestmentTest::testParseInvestmentsXML()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 84
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 69
c 1
b 0
f 0
dl 0
loc 84
rs 8.6763
cc 2
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace OfxParser\Parsers;
4
5
use PHPUnit\Framework\TestCase;
6
use OfxParser\Parsers\Investment as InvestmentParser;
7
8
/**
9
 * @covers OfxParser\Parsers\Investment
10
 */
11
class InvestmentTest extends TestCase
12
{
13
    public function testParseInvestmentsXML()
14
    {
15
        $parser = new InvestmentParser();
16
        $ofx = $parser->loadFromFile(__DIR__ . '/../../fixtures/ofxdata-investments-xml.ofx');
17
18
        $account = reset($ofx->bankAccounts);
19
        self::assertSame('TEST-UID-1', $account->transactionUid);
20
        self::assertSame('vanguard.com', $account->brokerId);
21
        self::assertSame('1234567890', $account->accountNumber);
22
23
        // Check some transactions:
24
        $expected = array(
25
            '100100' => array(
26
                'tradeDate' => new \DateTime('2010-01-01'),
27
                'settlementDate' => new \DateTime('2010-01-02'),
28
                'securityId' => '122022322',
29
                'securityIdType' => 'CUSIP',
30
                'units' => '31.25',
31
                'unitPrice' => '32.0',
32
                'total' => '-1000.0',
33
                'buyType' => 'BUY',
34
                'actionCode' => 'BUYMF',
35
            ),
36
            '100200' => array(
37
                'tradeDate' => new \DateTime('2011-02-01'),
38
                'settlementDate' => new \DateTime('2011-02-03'),
39
                'securityId' => '355055155',
40
                'securityIdType' => 'CUSIP',
41
                'units' => '3.0',
42
                'unitPrice' => '181.96',
43
                'total' => '-545.88',
44
                'buyType' => 'BUY',
45
                'actionCode' => 'BUYSTOCK',
46
            ),
47
            '100300' => array(
48
                'tradeDate' => new \DateTime('2010-01-01'),
49
                'settlementDate' => new \DateTime('2010-01-01'),
50
                'securityId' => '122022322',
51
                'securityIdType' => 'CUSIP',
52
                'units' => '-1000.0',
53
                'unitPrice' => '1.0',
54
                'total' => '1000.0',
55
                'sellType' => 'SELL',
56
                'actionCode' => 'SELLMF',
57
            ),
58
            '200100' => array(
59
                'tradeDate' => new \DateTime('2011-02-01'),
60
                'settlementDate' => new \DateTime('2011-02-01'),
61
                'securityId' => '822722622',
62
                'securityIdType' => 'CUSIP',
63
                'units' => '',
64
                'unitPrice' => '',
65
                'total' => '12.59',
66
                'incomeType' => 'DIV',
67
                'subAccountSec' => 'CASH',
68
                'subAccountFund' => 'CASH',
69
                'actionCode' => 'INCOME',
70
            ),
71
            '200200' => array(
72
                'tradeDate' => new \DateTime('2011-02-01'),
73
                'settlementDate' => new \DateTime('2011-02-01'),
74
                'securityId' => '355055155',
75
                'securityIdType' => 'CUSIP',
76
                'units' => '0.037',
77
                'unitPrice' => '187.9894',
78
                'total' => '-6.97',
79
                'incomeType' => 'DIV',
80
                'subAccountSec' => 'CASH',
81
                'subAccountFund' => '',
82
                'actionCode' => 'REINVEST',
83
            ),
84
            '300100' => array(
85
                'date' => new \DateTime('2010-01-15'),
86
                'type' => 'OTHER',
87
                'amount' => 1234.56,
88
                'actionCode' => 'INVBANKTRAN',
89
            ),
90
        );
91
92
        if (count($expected)) {
93
            self::assertTrue(count($account->statement->transactions) > 0);
94
        }
95
96
        $this->validateTransactions($account->statement->transactions, $expected);
97
    }
98
99
    public function testParseInvestmentsXMLOneLine()
100
    {
101
        $parser = new InvestmentParser();
102
        $ofx = $parser->loadFromFile(__DIR__ . '/../../fixtures/ofxdata-investments-oneline-xml.ofx');
103
104
        $account = reset($ofx->bankAccounts);
105
        self::assertSame('TEST-UID-1', $account->transactionUid);
106
        self::assertSame('vanguard.com', $account->brokerId);
107
        self::assertSame('1234567890', $account->accountNumber);
108
109
        // Check some transactions:
110
        $expected = array(
111
            '100200' => array(
112
                'tradeDate' => new \DateTime('2011-02-01'),
113
                'settlementDate' => new \DateTime('2011-02-03'),
114
                'securityId' => '355055155',
115
                'securityIdType' => 'CUSIP',
116
                'units' => '3.0',
117
                'unitPrice' => '181.96',
118
                'total' => '-545.88',
119
                'buyType' => 'BUY',
120
                'actionCode' => 'BUYSTOCK',
121
            ),
122
        );
123
124
        if (count($expected)) {
125
            self::assertTrue(count($account->statement->transactions) > 0);
126
        }
127
128
        $this->validateTransactions($account->statement->transactions, $expected);
129
    }
130
131
    public function testParseInvestmentsXMLMultipleAccounts()
132
    {
133
        $parser = new InvestmentParser();
134
        $ofx = $parser->loadFromFile(__DIR__ . '/../../fixtures/ofxdata-investments-multiple-accounts-xml.ofx');
135
136
        // Check some transactions:
137
        $expected = array(
138
            '1234567890' => array(
139
                '100200' => array(
140
                    'tradeDate' => new \DateTime('2011-02-01'),
141
                    'settlementDate' => new \DateTime('2011-02-03'),
142
                    'securityId' => '355055155',
143
                    'securityIdType' => 'CUSIP',
144
                    'units' => '3.0',
145
                    'unitPrice' => '181.96',
146
                    'total' => '-545.88',
147
                    'buyType' => 'BUY',
148
                    'actionCode' => 'BUYSTOCK',
149
                ),
150
            ),
151
            '987654321' => array(
152
                '200200' => array(
153
                    'tradeDate' => new \DateTime('2011-02-01'),
154
                    'settlementDate' => new \DateTime('2011-02-01'),
155
                    'securityId' => '355055155',
156
                    'securityIdType' => 'CUSIP',
157
                    'units' => '0.037',
158
                    'unitPrice' => '187.9894',
159
                    'total' => '-6.97',
160
                    'incomeType' => 'DIV',
161
                    'subAccountSec' => 'CASH',
162
                    'subAccountFund' => '',
163
                    'actionCode' => 'REINVEST',
164
                ),
165
            ),
166
        );
167
168
        if (count($expected)) {
169
            self::assertEquals(count($ofx->bankAccounts), count($expected), 'Account count mismatch');
170
        }
171
172
        foreach ($ofx->bankAccounts as $account) {
173
            $myExpected = isset($expected[$account->accountNumber])
174
                ? $expected[$account->accountNumber]
175
                : array();
176
177
            $this->validateTransactions($account->statement->transactions, $myExpected);
178
        }
179
    }
180
181
    public function testGoogleFinanceInvestments()
182
    {
183
        $parser = new InvestmentParser();
184
        $ofx = $parser->loadFromFile(__DIR__ . '/../../fixtures/ofxdata-google.ofx');
185
186
        $account = reset($ofx->bankAccounts);
187
        self::assertSame('1001', $account->transactionUid);
188
        self::assertSame('google.com', $account->brokerId);
189
        self::assertSame('StockersTest', $account->accountNumber);
190
191
        // Check some transactions:
192
        $expected = array(
193
            '1' => array(
194
                'tradeDate' => new \DateTime('2010-04-01'),
195
                'securityId' => 'TSE:T',
196
                'securityIdType' => 'TICKER',
197
                'units' => '5',
198
                'unitPrice' => '20',
199
                'total' => '-100',
200
                'buyType' => 'BUY',
201
                'actionCode' => 'BUYSTOCK',
202
            ),
203
        );
204
205
        if (count($expected)) {
206
            self::assertTrue(count($account->statement->transactions) > 0);
207
        }
208
209
        $this->validateTransactions($account->statement->transactions, $expected);
210
    }
211
212
    protected function validateTransactions($transactions, $expected)
213
    {
214
        foreach ($transactions as $t) {
215
            if (isset($expected[$t->uniqueId])) {
216
                $data = $expected[$t->uniqueId];
217
                foreach ($data as $prop => $val) {
218
                    // TEMP:
219
                    if ($prop == 'actionCode') {
220
                        continue;
221
                    }
222
223
                    if ($val instanceof \DateTimeInterface) {
224
                        self::assertSame(
225
                            $val->format('Y-m-d'),
226
                            $t->{$prop}->format('Y-m-d'),
227
                            'Failed comparison for "' . $prop .'"'
228
                        );
229
                    } else {
230
                        self::assertSame(
231
                            $val,
232
                            $t->{$prop},
233
                            'Failed comparison for "' . $prop .'"'
234
                        );
235
                    }
236
                }
237
            }
238
        }
239
    }
240
}
241