1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Feature; |
4
|
|
|
|
5
|
|
|
use App\Models\Account; |
6
|
|
|
use App\Models\Category; |
7
|
|
|
use App\Models\Payee; |
8
|
|
|
use App\Models\Transaction; |
9
|
|
|
use App\Models\TransactionStatus; |
10
|
|
|
use App\Models\TransactionType; |
11
|
|
|
use Carbon\Carbon; |
12
|
|
|
use Tests\Features\FeatureTestCase; |
13
|
|
|
|
14
|
|
|
class TransactionControllerTest extends FeatureTestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @test |
18
|
|
|
*/ |
19
|
|
|
public function it_can_create_a_transaction_with_all_properties() |
20
|
|
|
{ |
21
|
|
|
// Arrange |
22
|
|
|
$status = factory(TransactionStatus::class)->create(); |
23
|
|
|
$type = factory(TransactionType::class)->create(); |
24
|
|
|
$account = factory(Account::class)->create(['user_id' => $this->user->id]); |
25
|
|
|
$payee = factory(Payee::class)->create(['user_id' => $this->user->id]); |
26
|
|
|
$category = factory(Category::class)->create(['user_id' => $this->user->id]); |
27
|
|
|
$subcategory = factory(Category::class)->create(['user_id' => $this->user->id, 'parent_id' => $category->id]); |
28
|
|
|
|
29
|
|
|
// TODO: Test fileupload |
30
|
|
|
//Storage::fake('media'); |
|
|
|
|
31
|
|
|
$data = [ |
32
|
|
|
'transaction_date' => '12/31/2017', |
33
|
|
|
'transaction_status' => $status->id, |
34
|
|
|
'transaction_type' => $type->id, |
35
|
|
|
'account' => $account->id, |
36
|
|
|
'payee' => $payee->id, |
37
|
|
|
'category' => $category->id, |
38
|
|
|
'subcategory' => $subcategory->id, |
39
|
|
|
'amount' => 13.37, |
40
|
|
|
'notes' => 'Some notes', |
41
|
|
|
//'attachments' => [UploadedFile::fake()->image('receipt.jpg')] |
|
|
|
|
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
// Act |
45
|
|
|
$this->ensureAuthenticated(); |
46
|
|
|
$response = $this->post('/transactions', $data); |
47
|
|
|
|
48
|
|
|
// Assert |
49
|
|
|
$response->assertRedirect('/'); |
50
|
|
|
|
51
|
|
|
$this->assertDatabaseHas('transactions', [ |
52
|
|
|
'user_id' => $this->user->id, |
53
|
|
|
'transaction_date' => Carbon::create(2017, 12, 31, 0, 0, 0)->toDateTimeString(), |
54
|
|
|
'status_id' => $status->id, |
55
|
|
|
'type_id' => $type->id, |
56
|
|
|
'account_name' => $account->name, |
57
|
|
|
'to_account_name' => null, |
58
|
|
|
'payee_name' => $payee->name, |
59
|
|
|
'sub_category_name' => $subcategory->name, |
60
|
|
|
'amount' => 13.37, |
61
|
|
|
'notes' => 'Some notes', |
62
|
|
|
]); |
63
|
|
|
|
64
|
|
|
//$lastTransaction = Transaction::latest()->get()->first(); |
|
|
|
|
65
|
|
|
//$filename = 'Transaction_'.$lastTransaction->id.'_receipt.png'; |
|
|
|
|
66
|
|
|
//Storage::disk('media')->assertExists($lastTransaction->id.'/'.$filename); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @test |
71
|
|
|
*/ |
72
|
|
|
public function it_can_edit_an_existing_transaction() |
73
|
|
|
{ |
74
|
|
|
// Arrange |
75
|
|
|
$transaction = factory(Transaction::class)->create(['user_id' => $this->user->id]); |
76
|
|
|
|
77
|
|
|
$status = factory(TransactionStatus::class)->create(); |
78
|
|
|
$type = factory(TransactionType::class)->create(); |
79
|
|
|
$account = factory(Account::class)->create(['user_id' => $this->user->id]); |
80
|
|
|
$payee = factory(Payee::class)->create(['user_id' => $this->user->id]); |
81
|
|
|
$category = factory(Category::class)->create(['user_id' => $this->user->id]); |
82
|
|
|
$subcategory = factory(Category::class)->create(['user_id' => $this->user->id, 'parent_id' => $category->id]); |
83
|
|
|
|
84
|
|
|
// TODO: Test fileupload |
85
|
|
|
//Storage::fake('media'); |
|
|
|
|
86
|
|
|
$data = [ |
87
|
|
|
'transaction_date' => '12/31/2017', |
88
|
|
|
'transaction_status' => $status->id, |
89
|
|
|
'transaction_type' => $type->id, |
90
|
|
|
'account' => $account->id, |
91
|
|
|
'payee' => $payee->id, |
92
|
|
|
'category' => $category->id, |
93
|
|
|
'subcategory' => $subcategory->id, |
94
|
|
|
'amount' => 13.37, |
95
|
|
|
'notes' => 'Some notes', |
96
|
|
|
//'attachments' => [UploadedFile::fake()->image('receipt.jpg')] |
|
|
|
|
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
// Act |
100
|
|
|
$this->ensureAuthenticated(); |
101
|
|
|
$response = $this->put('/transactions/'.$transaction->id, $data); |
102
|
|
|
|
103
|
|
|
// Assert |
104
|
|
|
$response->assertRedirect('/'); |
105
|
|
|
|
106
|
|
|
$this->assertDatabaseHas('transactions', [ |
107
|
|
|
'id' => $transaction->id, |
108
|
|
|
'user_id' => $this->user->id, |
109
|
|
|
'transaction_date' => Carbon::create(2017, 12, 31, 0, 0, 0)->toDateTimeString(), |
110
|
|
|
'status_id' => $status->id, |
111
|
|
|
'type_id' => $type->id, |
112
|
|
|
'account_name' => $account->name, |
113
|
|
|
'to_account_name' => null, |
114
|
|
|
'payee_name' => $payee->name, |
115
|
|
|
'category_name' => $category->name, |
116
|
|
|
'sub_category_name' => $subcategory->name, |
117
|
|
|
'amount' => 13.37, |
118
|
|
|
'notes' => 'Some notes', |
119
|
|
|
]); |
120
|
|
|
|
121
|
|
|
//$lastTransaction = Transaction::latest()->get()->first(); |
|
|
|
|
122
|
|
|
//$filename = 'Transaction_'.$lastTransaction->id.'_receipt.png'; |
|
|
|
|
123
|
|
|
//Storage::disk('media')->assertExists($lastTransaction->id.'/'.$filename); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @test |
128
|
|
|
*/ |
129
|
|
|
public function it_stores_last_used_category_by_payee_in_payees_table_on_create() |
130
|
|
|
{ |
131
|
|
|
// Arrange |
132
|
|
|
$type = factory(TransactionType::class)->create(); |
133
|
|
|
$account = factory(Account::class)->create(['user_id' => $this->user->id]); |
134
|
|
|
$payee = factory(Payee::class)->create(['user_id' => $this->user->id]); |
135
|
|
|
$category = factory(Category::class)->create(['user_id' => $this->user->id]); |
136
|
|
|
|
137
|
|
|
$data = [ |
138
|
|
|
'transaction_type' => $type->id, |
139
|
|
|
'account' => $account->id, |
140
|
|
|
'payee' => $payee->id, |
141
|
|
|
'category' => $category->id, |
142
|
|
|
'amount' => 13.37, |
143
|
|
|
]; |
144
|
|
|
|
145
|
|
|
// Act |
146
|
|
|
$this->ensureAuthenticated(); |
147
|
|
|
$response = $this->post('/transactions', $data); |
148
|
|
|
|
149
|
|
|
// Assert |
150
|
|
|
$response->assertRedirect('/'); |
151
|
|
|
$this->assertDatabaseHas('payees', ['user_id' => $this->user->id, 'id' => $payee->id, 'last_category_id' => $category->id]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @test |
156
|
|
|
*/ |
157
|
|
|
public function it_stores_last_used_category_by_payee_in_payees_table_on_update() |
158
|
|
|
{ |
159
|
|
|
// Arrange |
160
|
|
|
$transaction = factory(Transaction::class)->create(['user_id' => $this->user->id]); |
161
|
|
|
|
162
|
|
|
$type = factory(TransactionType::class)->create(); |
163
|
|
|
$account = factory(Account::class)->create(['user_id' => $this->user->id]); |
164
|
|
|
$payee = factory(Payee::class)->create(['user_id' => $this->user->id]); |
165
|
|
|
$category = factory(Category::class)->create(['user_id' => $this->user->id]); |
166
|
|
|
|
167
|
|
|
$data = [ |
168
|
|
|
'transaction_type' => $type->id, |
169
|
|
|
'account' => $account->id, |
170
|
|
|
'payee' => $payee->id, |
171
|
|
|
'category' => $category->id, |
172
|
|
|
'amount' => 13.37, |
173
|
|
|
]; |
174
|
|
|
|
175
|
|
|
// Act |
176
|
|
|
$this->ensureAuthenticated(); |
177
|
|
|
$response = $this->put('/transactions/'.$transaction->id, $data); |
178
|
|
|
|
179
|
|
|
// Assert |
180
|
|
|
$response->assertRedirect('/'); |
181
|
|
|
$this->assertDatabaseHas('payees', ['user_id' => $this->user->id, 'id' => $payee->id, 'last_category_id' => $category->id]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @test |
186
|
|
|
*/ |
187
|
|
|
public function it_stores_last_used_subcategory_by_payee_in_payees_table_on_create() |
188
|
|
|
{ |
189
|
|
|
// Arrange |
190
|
|
|
$type = factory(TransactionType::class)->create(); |
191
|
|
|
$account = factory(Account::class)->create(['user_id' => $this->user->id]); |
192
|
|
|
$payee = factory(Payee::class)->create(['user_id' => $this->user->id]); |
193
|
|
|
$category = factory(Category::class)->create(['user_id' => $this->user->id]); |
194
|
|
|
$subcategory = factory(Category::class)->create(['user_id' => $this->user->id, 'parent_id' => $category->id]); |
195
|
|
|
|
196
|
|
|
$data = [ |
197
|
|
|
'transaction_type' => $type->id, |
198
|
|
|
'account' => $account->id, |
199
|
|
|
'payee' => $payee->id, |
200
|
|
|
'category' => $category->id, |
201
|
|
|
'subcategory' => $subcategory->id, |
202
|
|
|
'amount' => 13.37, |
203
|
|
|
]; |
204
|
|
|
|
205
|
|
|
// Act |
206
|
|
|
$this->ensureAuthenticated(); |
207
|
|
|
$response = $this->post('/transactions', $data); |
208
|
|
|
|
209
|
|
|
// Assert |
210
|
|
|
$response->assertRedirect('/'); |
211
|
|
|
$this->assertDatabaseHas('payees', ['user_id' => $this->user->id, 'id' => $payee->id, 'last_category_id' => $subcategory->id]); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @test |
216
|
|
|
*/ |
217
|
|
|
public function it_stores_last_used_subcategory_by_payee_in_payees_table_on_update() |
218
|
|
|
{ |
219
|
|
|
// Arrange |
220
|
|
|
$transaction = factory(Transaction::class)->create(['user_id' => $this->user->id]); |
221
|
|
|
|
222
|
|
|
$type = factory(TransactionType::class)->create(); |
223
|
|
|
$account = factory(Account::class)->create(['user_id' => $this->user->id]); |
224
|
|
|
$payee = factory(Payee::class)->create(['user_id' => $this->user->id]); |
225
|
|
|
$category = factory(Category::class)->create(['user_id' => $this->user->id]); |
226
|
|
|
$subcategory = factory(Category::class)->create(['user_id' => $this->user->id, 'parent_id' => $category->id]); |
227
|
|
|
|
228
|
|
|
$data = [ |
229
|
|
|
'transaction_type' => $type->id, |
230
|
|
|
'account' => $account->id, |
231
|
|
|
'payee' => $payee->id, |
232
|
|
|
'category' => $category->id, |
233
|
|
|
'subcategory' => $subcategory->id, |
234
|
|
|
'amount' => 13.37, |
235
|
|
|
]; |
236
|
|
|
|
237
|
|
|
// Act |
238
|
|
|
$this->ensureAuthenticated(); |
239
|
|
|
$response = $this->put('/transactions/'.$transaction->id, $data); |
240
|
|
|
|
241
|
|
|
// Assert |
242
|
|
|
$response->assertRedirect('/'); |
243
|
|
|
$this->assertDatabaseHas('payees', ['user_id' => $this->user->id, 'id' => $payee->id, 'last_category_id' => $subcategory->id]); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @test |
248
|
|
|
*/ |
249
|
|
|
public function it_stores_account_to_on_transfer_transactions() |
250
|
|
|
{ |
251
|
|
|
// Arrange |
252
|
|
|
$type = factory(TransactionType::class)->create(); |
253
|
|
|
$account = factory(Account::class)->create(['user_id' => $this->user->id]); |
254
|
|
|
$toaccount = factory(Account::class)->create(['user_id' => $this->user->id]); |
255
|
|
|
$payee = factory(Payee::class)->create(['user_id' => $this->user->id]); |
256
|
|
|
$category = factory(Category::class)->create(['user_id' => $this->user->id]); |
257
|
|
|
$subcategory = factory(Category::class)->create(['user_id' => $this->user->id, 'parent_id' => $category->id]); |
258
|
|
|
|
259
|
|
|
$data = [ |
260
|
|
|
'transaction_type' => $type->id, |
261
|
|
|
'account' => $account->id, |
262
|
|
|
'to_account' => $toaccount->id, |
263
|
|
|
'payee' => $payee->id, |
264
|
|
|
'category' => $category->id, |
265
|
|
|
'subcategory' => $subcategory->id, |
266
|
|
|
'amount' => 13.37, |
267
|
|
|
]; |
268
|
|
|
|
269
|
|
|
// Act |
270
|
|
|
$this->ensureAuthenticated(); |
271
|
|
|
$response = $this->post('/transactions', $data); |
272
|
|
|
|
273
|
|
|
// Assert |
274
|
|
|
$response->assertRedirect('/'); |
275
|
|
|
$this->assertDatabaseHas('transactions', [ |
276
|
|
|
'user_id' => $this->user->id, |
277
|
|
|
'type_id' => $type->id, |
278
|
|
|
'account_name' => $account->name, |
279
|
|
|
'to_account_name' => $toaccount->name, |
280
|
|
|
'payee_name' => $payee->name, |
281
|
|
|
'category_name' => $category->name, |
282
|
|
|
'sub_category_name' => $subcategory->name, |
283
|
|
|
]); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @test |
288
|
|
|
*/ |
289
|
|
|
public function it_stores_last_used_date_by_payee_in_payees_table_on_create() |
290
|
|
|
{ |
291
|
|
|
// Arrange |
292
|
|
|
$knownDate = Carbon::create(2017, 07, 16, 12); |
293
|
|
|
Carbon::setTestNow($knownDate); |
294
|
|
|
|
295
|
|
|
$type = factory(TransactionType::class)->create(); |
296
|
|
|
$account = factory(Account::class)->create(['user_id' => $this->user->id]); |
297
|
|
|
$payee = factory(Payee::class)->create(['user_id' => $this->user->id]); |
298
|
|
|
$category = factory(Category::class)->create(['user_id' => $this->user->id]); |
299
|
|
|
$subcategory = factory(Category::class)->create(['user_id' => $this->user->id, 'parent_id' => $category->id]); |
300
|
|
|
|
301
|
|
|
$data = [ |
302
|
|
|
'transaction_type' => $type->id, |
303
|
|
|
'account' => $account->id, |
304
|
|
|
'payee' => $payee->id, |
305
|
|
|
'category' => $category->id, |
306
|
|
|
'subcategory' => $subcategory->id, |
307
|
|
|
'amount' => 13.37, |
308
|
|
|
]; |
309
|
|
|
|
310
|
|
|
// Act |
311
|
|
|
$this->ensureAuthenticated(); |
312
|
|
|
$response = $this->post('/transactions', $data); |
313
|
|
|
|
314
|
|
|
// Assert |
315
|
|
|
$response->assertRedirect('/'); |
316
|
|
|
$this->assertDatabaseHas('payees', ['user_id' => $this->user->id, 'id' => $payee->id, 'last_used_at' => Carbon::now()->toDateTimeString()]); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @test |
321
|
|
|
*/ |
322
|
|
|
public function it_stores_last_used_date_by_payee_in_payees_table_on_update() |
323
|
|
|
{ |
324
|
|
|
// Arrange |
325
|
|
|
$knownDate = Carbon::create(2017, 07, 16, 12); |
326
|
|
|
Carbon::setTestNow($knownDate); |
327
|
|
|
|
328
|
|
|
$transaction = factory(Transaction::class)->create(['user_id' => $this->user->id]); |
329
|
|
|
|
330
|
|
|
$type = factory(TransactionType::class)->create(); |
331
|
|
|
$account = factory(Account::class)->create(['user_id' => $this->user->id]); |
332
|
|
|
$payee = factory(Payee::class)->create(['user_id' => $this->user->id]); |
333
|
|
|
$category = factory(Category::class)->create(['user_id' => $this->user->id]); |
334
|
|
|
$subcategory = factory(Category::class)->create(['user_id' => $this->user->id, 'parent_id' => $category->id]); |
335
|
|
|
|
336
|
|
|
$data = [ |
337
|
|
|
'transaction_type' => $type->id, |
338
|
|
|
'account' => $account->id, |
339
|
|
|
'payee' => $payee->id, |
340
|
|
|
'category' => $category->id, |
341
|
|
|
'subcategory' => $subcategory->id, |
342
|
|
|
'amount' => 13.37, |
343
|
|
|
]; |
344
|
|
|
|
345
|
|
|
// Act |
346
|
|
|
$this->ensureAuthenticated(); |
347
|
|
|
$response = $this->put('/transactions/'.$transaction->id, $data); |
348
|
|
|
|
349
|
|
|
// Assert |
350
|
|
|
$response->assertRedirect('/'); |
351
|
|
|
$this->assertDatabaseHas('payees', ['user_id' => $this->user->id, 'id' => $payee->id, 'last_used_at' => Carbon::now()->toDateTimeString()]); |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.