InvestmentTest::testGoogleFinanceInvestments()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 29
rs 9.6333
cc 2
nc 2
nop 0
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
final class InvestmentTest extends TestCase
12
{
13
    public function testParseInvestmentsXML(): void
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 = [
25
            '100100' => [
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' => [
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' => [
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' => [
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' => [
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' => [
85
                'date' => new \DateTime('2010-01-15'),
86
                'type' => 'OTHER',
87
                'amount' => 1234.56,
88
                'actionCode' => 'INVBANKTRAN',
89
            ],
90
        ];
91
92
        self::assertGreaterThan(0, count($account->statement->transactions));
93
94
        $this->validateTransactions($account->statement->transactions, $expected);
95
    }
96
97
    public function testParseInvestmentsXMLOneLine(): void
98
    {
99
        $parser = new InvestmentParser();
100
        $ofx = $parser->loadFromFile(__DIR__ . '/../../fixtures/ofxdata-investments-oneline-xml.ofx');
101
102
        $account = reset($ofx->bankAccounts);
103
        self::assertSame('TEST-UID-1', $account->transactionUid);
104
        self::assertSame('vanguard.com', $account->brokerId);
105
        self::assertSame('1234567890', $account->accountNumber);
106
107
        // Check some transactions:
108
        $expected = array(
109
            '100200' => array(
110
                'tradeDate' => new \DateTime('2011-02-01'),
111
                'settlementDate' => new \DateTime('2011-02-03'),
112
                'securityId' => '355055155',
113
                'securityIdType' => 'CUSIP',
114
                'units' => '3.0',
115
                'unitPrice' => '181.96',
116
                'total' => '-545.88',
117
                'buyType' => 'BUY',
118
                'actionCode' => 'BUYSTOCK',
119
            ),
120
        );
121
122
        if (count($expected)) {
123
            self::assertTrue(count($account->statement->transactions) > 0);
124
        }
125
126
        $this->validateTransactions($account->statement->transactions, $expected);
127
    }
128
129
    public function testParseInvestmentsXMLMultipleAccounts(): void
130
    {
131
        $parser = new InvestmentParser();
132
        $ofx = $parser->loadFromFile(__DIR__ . '/../../fixtures/ofxdata-investments-multiple-accounts-xml.ofx');
133
134
        // Check some transactions:
135
        $expected = array(
136
            '1234567890' => array(
137
                '100200' => array(
138
                    'tradeDate' => new \DateTime('2011-02-01'),
139
                    'settlementDate' => new \DateTime('2011-02-03'),
140
                    'securityId' => '355055155',
141
                    'securityIdType' => 'CUSIP',
142
                    'units' => '3.0',
143
                    'unitPrice' => '181.96',
144
                    'total' => '-545.88',
145
                    'buyType' => 'BUY',
146
                    'actionCode' => 'BUYSTOCK',
147
                ),
148
            ),
149
            '987654321' => array(
150
                '200200' => array(
151
                    'tradeDate' => new \DateTime('2011-02-01'),
152
                    'settlementDate' => new \DateTime('2011-02-01'),
153
                    'securityId' => '355055155',
154
                    'securityIdType' => 'CUSIP',
155
                    'units' => '0.037',
156
                    'unitPrice' => '187.9894',
157
                    'total' => '-6.97',
158
                    'incomeType' => 'DIV',
159
                    'subAccountSec' => 'CASH',
160
                    'subAccountFund' => '',
161
                    'actionCode' => 'REINVEST',
162
                ),
163
            ),
164
        );
165
166
        if (count($expected)) {
167
            self::assertEquals(count($ofx->bankAccounts), count($expected), 'Account count mismatch');
168
        }
169
170
        foreach ($ofx->bankAccounts as $account) {
171
            $myExpected = isset($expected[$account->accountNumber])
172
                ? $expected[$account->accountNumber]
173
                : array();
174
175
            $this->validateTransactions($account->statement->transactions, $myExpected);
176
        }
177
    }
178
179
    public function testGoogleFinanceInvestments(): void
180
    {
181
        $parser = new InvestmentParser();
182
        $ofx = $parser->loadFromFile(__DIR__ . '/../../fixtures/ofxdata-google.ofx');
183
184
        $account = reset($ofx->bankAccounts);
185
        self::assertSame('1001', $account->transactionUid);
186
        self::assertSame('google.com', $account->brokerId);
187
        self::assertSame('StockersTest', $account->accountNumber);
188
189
        // Check some transactions:
190
        $expected = array(
191
            '1' => array(
192
                'tradeDate' => new \DateTime('2010-04-01'),
193
                'securityId' => 'TSE:T',
194
                'securityIdType' => 'TICKER',
195
                'units' => '5',
196
                'unitPrice' => '20',
197
                'total' => '-100',
198
                'buyType' => 'BUY',
199
                'actionCode' => 'BUYSTOCK',
200
            ),
201
        );
202
203
        if (count($expected)) {
204
            self::assertTrue(count($account->statement->transactions) > 0);
205
        }
206
207
        $this->validateTransactions($account->statement->transactions, $expected);
208
    }
209
210
    protected function validateTransactions($transactions, $expected)
211
    {
212
        foreach ($transactions as $t) {
213
            if (isset($expected[$t->uniqueId])) {
214
                $data = $expected[$t->uniqueId];
215
                foreach ($data as $prop => $val) {
216
                    // TEMP:
217
                    if ($prop == 'actionCode') {
218
                        continue;
219
                    }
220
221
                    if ($val instanceof \DateTimeInterface) {
222
                        self::assertSame(
223
                            $val->format('Y-m-d'),
224
                            $t->{$prop}->format('Y-m-d'),
225
                            'Failed comparison for "' . $prop .'"'
226
                        );
227
                    } else {
228
                        self::assertSame(
229
                            $val,
230
                            $t->{$prop},
231
                            'Failed comparison for "' . $prop .'"'
232
                        );
233
                    }
234
                }
235
            }
236
        }
237
    }
238
}
239