|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Feature\Api; |
|
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
|
|
|
* A basic test example. |
|
18
|
|
|
* |
|
19
|
|
|
* @test |
|
20
|
|
|
* |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
|
|
public function it_can_get_all_transactions() |
|
24
|
|
|
{ |
|
25
|
|
|
// Arrange |
|
26
|
|
|
$url = '/api/v1/transactions'; |
|
27
|
|
|
$transaction = factory(Transaction::class)->create(['user_id' => $this->user->id]); |
|
28
|
|
|
|
|
29
|
|
|
// Act |
|
30
|
|
|
$this->ensureAuthenticated(); |
|
31
|
|
|
$response = $this->get($url); |
|
32
|
|
|
|
|
33
|
|
|
// Assert |
|
34
|
|
|
$response->assertStatus(200) |
|
35
|
|
|
->assertJsonFragment([ |
|
36
|
|
|
'id' => $transaction->id, |
|
37
|
|
|
'status' => [ |
|
38
|
|
|
'id' => $transaction->status->id, |
|
39
|
|
|
'name' => 'mmex.'.$transaction->status->name, |
|
40
|
|
|
'slug' => $transaction->status->slug, |
|
41
|
|
|
], |
|
42
|
|
|
'type' => [ |
|
43
|
|
|
'id' => $transaction->type->id, |
|
44
|
|
|
'name' => 'mmex.'.$transaction->type->name, |
|
45
|
|
|
'slug' => $transaction->type->slug, |
|
46
|
|
|
], |
|
47
|
|
|
'account_name' => $transaction->account_name, |
|
48
|
|
|
'to_account_name' => $transaction->to_account_name, |
|
49
|
|
|
'payee_name' => $transaction->payee_name, |
|
50
|
|
|
'category_name' => $transaction->category_name, |
|
51
|
|
|
'sub_category_name' => $transaction->sub_category_name, |
|
52
|
|
|
'amount' => round($transaction->amount, 2), |
|
53
|
|
|
'notes' => $transaction->notes, |
|
54
|
|
|
'created_at' => $transaction->created_at->toIso8601String(), |
|
55
|
|
|
'updated_at' => $transaction->updated_at->toIso8601String(), |
|
56
|
|
|
]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @test |
|
61
|
|
|
*/ |
|
62
|
|
|
public function it_can_create_new_transaction_with_a_new_payee() |
|
63
|
|
|
{ |
|
64
|
|
|
// Arrange |
|
65
|
|
|
$knownDate = Carbon::create(2017, 07, 16); |
|
66
|
|
|
Carbon::setTestNow($knownDate); |
|
67
|
|
|
|
|
68
|
|
|
$url = '/api/v1/transactions'; |
|
69
|
|
|
|
|
70
|
|
|
$status = factory(TransactionStatus::class)->create(); |
|
71
|
|
|
$type = factory(TransactionType::class)->create(); |
|
72
|
|
|
$account = factory(Account::class)->create(['user_id' => $this->user->id]); |
|
73
|
|
|
|
|
74
|
|
|
$category = factory(Category::class)->create(['user_id' => $this->user->id]); |
|
75
|
|
|
$subcategory = factory(Category::class)->create(['user_id' => $this->user->id, 'parent_id' => $category->id]); |
|
76
|
|
|
|
|
77
|
|
|
$data = [ |
|
78
|
|
|
'transaction_date' => $knownDate->toIso8601String(), |
|
79
|
|
|
'transaction_status' => $status->name, |
|
80
|
|
|
'transaction_type' => $type->name, |
|
81
|
|
|
'account' => $account->name, |
|
82
|
|
|
'payee' => 'a new payee', |
|
83
|
|
|
'category' => $category->name, |
|
84
|
|
|
'sub_category' => $subcategory->name, |
|
85
|
|
|
'amount' => 13.37, |
|
86
|
|
|
'notes' => 'Created from API', |
|
87
|
|
|
]; |
|
88
|
|
|
//dd($data); |
|
89
|
|
|
|
|
90
|
|
|
// Act |
|
91
|
|
|
$this->ensureAuthenticated(); |
|
92
|
|
|
$response = $this->json('POST', $url, $data); |
|
93
|
|
|
|
|
94
|
|
|
// Assert |
|
95
|
|
|
$response->assertStatus(201); |
|
96
|
|
|
$this->assertDatabaseHas('payees', [ |
|
97
|
|
|
'user_id' => $this->user->id, |
|
98
|
|
|
'name' => 'a new payee', |
|
99
|
|
|
'last_used_at' => null, |
|
100
|
|
|
'last_category_id' => null, |
|
101
|
|
|
]); |
|
102
|
|
|
|
|
103
|
|
|
$this->assertDatabaseHas('transactions', [ |
|
104
|
|
|
'user_id' => $this->user->id, |
|
105
|
|
|
'transaction_date' => Carbon::create(2017, 07, 16, 0, 0, 0)->toDateTimeString(), |
|
106
|
|
|
'status_id' => $status->id, |
|
107
|
|
|
'type_id' => $type->id, |
|
108
|
|
|
'account_name' => $account->name, |
|
109
|
|
|
'to_account_name' => null, |
|
110
|
|
|
'payee_name' => 'a new payee', |
|
111
|
|
|
'sub_category_name' => $subcategory->name, |
|
112
|
|
|
'amount' => 13.37, |
|
113
|
|
|
'notes' => 'Created from API', |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @test |
|
119
|
|
|
*/ |
|
120
|
|
|
public function it_can_create_a_new_transaction_with_a_existing_payee() |
|
121
|
|
|
{ |
|
122
|
|
|
// Arrange |
|
123
|
|
|
$knownDate = Carbon::create(2017, 07, 16); |
|
124
|
|
|
Carbon::setTestNow($knownDate); |
|
125
|
|
|
|
|
126
|
|
|
$url = '/api/v1/transactions'; |
|
127
|
|
|
|
|
128
|
|
|
$status = factory(TransactionStatus::class)->create(); |
|
129
|
|
|
$type = factory(TransactionType::class)->create(); |
|
130
|
|
|
$account = factory(Account::class)->create(['user_id' => $this->user->id]); |
|
131
|
|
|
$payee = factory(Payee::class)->create(['user_id' => $this->user->id]); |
|
132
|
|
|
$category = factory(Category::class)->create(['user_id' => $this->user->id]); |
|
133
|
|
|
$subcategory = factory(Category::class)->create(['user_id' => $this->user->id, 'parent_id' => $category->id]); |
|
134
|
|
|
|
|
135
|
|
|
$data = [ |
|
136
|
|
|
'transaction_date' => $knownDate->toIso8601String(), |
|
137
|
|
|
'transaction_status' => $status->name, |
|
138
|
|
|
'transaction_type' => $type->name, |
|
139
|
|
|
'account' => $account->name, |
|
140
|
|
|
'payee' => $payee->name, |
|
141
|
|
|
'category' => $category->name, |
|
142
|
|
|
'sub_category' => $subcategory->name, |
|
143
|
|
|
'amount' => 13.37, |
|
144
|
|
|
'notes' => 'Created from API', |
|
145
|
|
|
]; |
|
146
|
|
|
|
|
147
|
|
|
// Act |
|
148
|
|
|
$this->ensureAuthenticated(); |
|
149
|
|
|
$response = $this->json('POST', $url, $data); |
|
150
|
|
|
|
|
151
|
|
|
// Assert |
|
152
|
|
|
$response->assertStatus(201); |
|
153
|
|
|
$this->assertDatabaseHas('payees', [ |
|
154
|
|
|
'user_id' => $this->user->id, |
|
155
|
|
|
'name' => $payee->name, |
|
156
|
|
|
'last_used_at' => $payee->last_user_at, |
|
157
|
|
|
'last_category_id' => $payee->last_category_id, |
|
158
|
|
|
]); |
|
159
|
|
|
|
|
160
|
|
|
$this->assertDatabaseHas('transactions', [ |
|
161
|
|
|
'user_id' => $this->user->id, |
|
162
|
|
|
'transaction_date' => Carbon::create(2017, 07, 16, 0, 0, 0)->toDateTimeString(), |
|
163
|
|
|
'status_id' => $status->id, |
|
164
|
|
|
'type_id' => $type->id, |
|
165
|
|
|
'account_name' => $account->name, |
|
166
|
|
|
'to_account_name' => null, |
|
167
|
|
|
'payee_name' => $payee->name, |
|
168
|
|
|
'sub_category_name' => $subcategory->name, |
|
169
|
|
|
'amount' => 13.37, |
|
170
|
|
|
'notes' => 'Created from API', |
|
171
|
|
|
]); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|