1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* MmexControllerTest.php, laravel-money-manager-ex. |
4
|
|
|
* |
5
|
|
|
* This File belongs to to Project laravel-money-manager-ex |
6
|
|
|
* |
7
|
|
|
* @author Oliver Kaufmann <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @version 1.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tests\Feature\MmexClient; |
13
|
|
|
|
14
|
|
|
use App\Models\Transaction; |
15
|
|
|
use App\Services\TransactionService; |
16
|
|
|
|
17
|
|
|
class TransactionTest extends MmexTestCase |
18
|
|
|
{ |
19
|
|
|
public function testEmptyResponseWhenNoTransactionsExists() |
20
|
|
|
{ |
21
|
|
|
// Arrange |
22
|
|
|
$url = $this->buildUrl(['download_transaction' => 'true']); |
23
|
|
|
|
24
|
|
|
// Act |
25
|
|
|
$response = $this->get($url); |
26
|
|
|
|
27
|
|
|
// Assert |
28
|
|
|
$response->assertStatus(200); |
29
|
|
|
$this->assertEquals($response->getContent(), ''); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testDeleteTransactions() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
// Arrange |
35
|
|
|
$transaction = factory(Transaction::class)->create(['user_id' => $this->user->id]); |
36
|
|
|
$url = $this->buildUrl(['delete_group' => $transaction->id]); |
37
|
|
|
|
38
|
|
|
// Act |
39
|
|
|
$response = $this->get($url); |
40
|
|
|
|
41
|
|
|
// Assert |
42
|
|
|
$this->assertSeeMmexSuccess($response); |
43
|
|
|
$this->assertIsSoftDeletedInDatabase('transactions', ['user_id' => $this->user->id, 'id' => $transaction->id]); // must not be deleted! just soft deleted. |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testDownloadTransactions() |
47
|
|
|
{ |
48
|
|
|
// Arrange |
49
|
|
|
/** @var Transaction $transaction */ |
50
|
|
|
$transaction = factory(Transaction::class)->create(['user_id' => $this->user->id]); |
51
|
|
|
$this->addReceiptsToTransaction($transaction); |
52
|
|
|
$url = $this->buildUrl(['download_transaction' => 'true']); |
53
|
|
|
|
54
|
|
|
// Act |
55
|
|
|
$response = $this->get($url); |
56
|
|
|
|
57
|
|
|
// Assert |
58
|
|
|
$response->assertStatus(200) |
59
|
|
|
->assertJsonFragment( |
60
|
|
|
[ |
61
|
|
|
'ID' => (string) $transaction->id, |
62
|
|
|
'Date' => $transaction->transaction_date->toDateString(), |
63
|
|
|
'Account' => $transaction->account_name, |
64
|
|
|
'ToAccount' => $transaction->to_account_name, |
65
|
|
|
'Status' => $transaction->status->slug, |
66
|
|
|
'Type' => $transaction->type->name, |
67
|
|
|
'Payee' => $transaction->payee_name, |
68
|
|
|
'Category' => $transaction->category_name, |
69
|
|
|
'SubCategory' => $transaction->sub_category_name, |
70
|
|
|
'Amount' => (string) $transaction->amount, |
71
|
|
|
'Notes' => $transaction->notes, |
72
|
|
|
'Attachments' => 'Transaction_'.$transaction->id.'_test-receipt.png;Transaction_'.$transaction->id |
73
|
|
|
.'_test-receipt-2.png;Transaction_'.$transaction->id.'_test-receipt-3.png', |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Attachment file name will be provided as comma separated list in the transaction download. |
80
|
|
|
*/ |
81
|
|
|
public function testDownloadAttachment() |
82
|
|
|
{ |
83
|
|
|
// Arrange |
84
|
|
|
/** @var Transaction $transaction */ |
85
|
|
|
$transaction = factory(Transaction::class)->create(['user_id' => $this->user->id]); |
86
|
|
|
$this->addReceiptsToTransaction($transaction); |
87
|
|
|
$fileName = 'Transaction_'.$transaction->id.'_test-receipt-3.png'; |
88
|
|
|
$url = $this->buildUrl(['download_attachment' => $fileName]); |
89
|
|
|
|
90
|
|
|
// Act |
91
|
|
|
$response = $this->get($url); |
92
|
|
|
|
93
|
|
|
// Assert |
94
|
|
|
$response->assertStatus(200) |
95
|
|
|
->assertHeader('Content-Type', '') |
96
|
|
|
->assertHeader('Cache-Control', 'public') |
97
|
|
|
->assertHeader('Content-Description', 'File Transfer') |
98
|
|
|
->assertHeader('Content-Disposition', 'attachment; filename= '.$fileName) |
99
|
|
|
->assertHeader('Content-Transfer-Encoding', 'binary'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $transaction |
104
|
|
|
*/ |
105
|
|
|
protected function addReceiptsToTransaction(Transaction $transaction) |
106
|
|
|
{ |
107
|
|
|
$transactionService = resolve(TransactionService::class); |
108
|
|
|
|
109
|
|
|
$transactionService->addAttachment($transaction, base_path('tests/data/test-receipt.png'), true); |
110
|
|
|
$transactionService->addAttachment($transaction, base_path('tests/data/test-receipt-2.png'), true); |
111
|
|
|
$transactionService->addAttachment($transaction, base_path('tests/data/test-receipt-3.png'), true); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.