How to use AtLeast class

Best Mockery code snippet using .AtLeast

Run Mockery automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

TransactionJournalFactoryTest.php

Source: TransactionJournalFactoryTest.php Github

copy
1<?php
2/**
3 * TransactionJournalFactoryTest.php
4 * Copyright (c) 2019 james@firefly-iii.org
5 *
6 * This file is part of Firefly III (https://github.com/firefly-iii).
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21
22declare(strict_types=1);
23
24namespace Tests\Unit\Factory;
25
26
27use Amount;
28use FireflyIII\Exceptions\FireflyException;
29use FireflyIII\Factory\AccountFactory;
30use FireflyIII\Factory\PiggyBankEventFactory;
31use FireflyIII\Factory\TagFactory;
32use FireflyIII\Factory\TransactionCurrencyFactory;
33use FireflyIII\Factory\TransactionFactory;
34use FireflyIII\Factory\TransactionJournalFactory;
35use FireflyIII\Factory\TransactionJournalMetaFactory;
36use FireflyIII\Models\PiggyBankEvent;
37use FireflyIII\Models\Transaction;
38use FireflyIII\Models\TransactionJournal;
39use FireflyIII\Models\TransactionType;
40use FireflyIII\Repositories\Account\AccountRepositoryInterface;
41use FireflyIII\Repositories\Bill\BillRepositoryInterface;
42use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
43use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
44use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
45use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
46use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface;
47use FireflyIII\Validation\AccountValidator;
48use Log;
49use Tests\TestCase;
50
51/**
52 * Class TransactionJournalFactoryTest
53 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
54 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
55 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
56 */
57class TransactionJournalFactoryTest extends TestCase
58{
59
60    /**
61     *
62     */
63    public function setUp(): void
64    {
65        parent::setUp();
66        Log::info(sprintf('Now in %s.', get_class($this)));
67    }
68
69    /**
70     * Submit empty array.
71     *
72     * @covers \FireflyIII\Factory\TransactionJournalFactory
73     * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
74     */
75    public function testCreate(): void
76    {
77        $billRepos          = $this->mock(BillRepositoryInterface::class);
78        $budgetRepos        = $this->mock(BudgetRepositoryInterface::class);
79        $catRepos           = $this->mock(CategoryRepositoryInterface::class);
80        $curRepos           = $this->mock(CurrencyRepositoryInterface::class);
81        $piggyRepos         = $this->mock(PiggyBankRepositoryInterface::class);
82        $tagFactory         = $this->mock(TagFactory::class);
83        $transactionFactory = $this->mock(TransactionFactory::class);
84        $accountRepos       = $this->mock(AccountRepositoryInterface::class);
85        $typeRepos          = $this->mock(TransactionTypeRepositoryInterface::class);
86        $eventFactory       = $this->mock(PiggyBankEventFactory::class);
87        $accountFactory     = $this->mock(AccountFactory::class);
88        $currencyFactory    = $this->mock(TransactionCurrencyFactory::class);
89        $metaFactory        = $this->mock(TransactionJournalMetaFactory::class);
90        $validator = $this->mock(AccountValidator::class);
91        $submission         = [];
92
93        // mock calls to all repositories
94        $curRepos->shouldReceive('setUser')->atLeast()->once();
95        $billRepos->shouldReceive('setUser')->atLeast()->once();
96        $budgetRepos->shouldReceive('setUser')->atLeast()->once();
97        $catRepos->shouldReceive('setUser')->atLeast()->once();
98        $piggyRepos->shouldReceive('setUser')->atLeast()->once();
99        $accountRepos->shouldReceive('setUser')->atLeast()->once();
100        $tagFactory->shouldReceive('setUser')->atLeast()->once();
101        $transactionFactory->shouldReceive('setUser')->atLeast()->once();
102
103
104        /** @var TransactionJournalFactory $factory */
105        $factory = app(TransactionJournalFactory::class);
106        $factory->setUser($this->user());
107        Log::warning('The following error is part of a test.');
108        try {
109            $collection = $factory->create($submission);
110        } catch (FireflyException $e) {
111            $this->assertTrue(false, $e->getMessage());
112
113            return;
114        }
115        $this->assertCount(0, $collection);
116    }
117
118    /**
119     * Submit minimal array for a withdrawal.
120     * @covers \FireflyIII\Factory\TransactionJournalFactory
121     * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
122     */
123    public function testCreateWithdrawal(): void
124    {
125        $billRepos          = $this->mock(BillRepositoryInterface::class);
126        $budgetRepos        = $this->mock(BudgetRepositoryInterface::class);
127        $catRepos           = $this->mock(CategoryRepositoryInterface::class);
128        $curRepos           = $this->mock(CurrencyRepositoryInterface::class);
129        $piggyRepos         = $this->mock(PiggyBankRepositoryInterface::class);
130        $tagFactory         = $this->mock(TagFactory::class);
131        $accountRepos       = $this->mock(AccountRepositoryInterface::class);
132        $typeRepos          = $this->mock(TransactionTypeRepositoryInterface::class);
133        $eventFactory       = $this->mock(PiggyBankEventFactory::class);
134        $accountFactory     = $this->mock(AccountFactory::class);
135        $currencyFactory    = $this->mock(TransactionCurrencyFactory::class);
136        $metaFactory        = $this->mock(TransactionJournalMetaFactory::class);
137        $transactionFactory = $this->mock(TransactionFactory::class);
138        $validator = $this->mock(AccountValidator::class);
139
140
141        $validator->shouldReceive('setUser')->atLeast()->once();
142        $validator->shouldReceive('setTransactionType')->atLeast()->once()->withArgs(['withdrawal']);
143        $validator->shouldReceive('validateSource')->atLeast()->once()->andReturnTrue();
144        $validator->shouldReceive('validateDestination')->atLeast()->once()->andReturnTrue();
145
146
147
148        // data
149        $withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
150        $asset      = $this->getRandomAsset();
151        $expense    = $this->getRandomExpense();
152        $euro       = $this->getEuro();
153        $submission = [
154            'transactions' => [
155                [
156                    'type'           => 'withdrawal',
157                    'amount'         => '10',
158                    'description'    => sprintf('I am a test #%d', $this->randomInt()),
159                    'source_id'      => $asset->id,
160                    'destination_id' => $expense->id,
161                ],
162            ],
163        ];
164
165
166        // mock calls to all repositories
167        $curRepos->shouldReceive('setUser')->atLeast()->once();
168        $billRepos->shouldReceive('setUser')->atLeast()->once();
169        $budgetRepos->shouldReceive('setUser')->atLeast()->once();
170        $catRepos->shouldReceive('setUser')->atLeast()->once();
171        $piggyRepos->shouldReceive('setUser')->atLeast()->once();
172        $accountRepos->shouldReceive('setUser')->atLeast()->once();
173        $tagFactory->shouldReceive('setUser')->atLeast()->once();
174        $transactionFactory->shouldReceive('setUser')->atLeast()->once();
175
176        $transactionFactory->shouldReceive('setJournal')->atLeast()->once();
177        $transactionFactory->shouldReceive('setAccount')->atLeast()->once();
178        $transactionFactory->shouldReceive('setCurrency')->atLeast()->once();
179        $transactionFactory->shouldReceive('setForeignCurrency')->atLeast()->once();
180        $transactionFactory->shouldReceive('setReconciled')->atLeast()->once();
181        $transactionFactory->shouldReceive('createNegative')->atLeast()->once()->andReturn(new Transaction);
182        $transactionFactory->shouldReceive('createPositive')->atLeast()->once()->andReturn(new Transaction);
183
184        $typeRepos->shouldReceive('findTransactionType')->withArgs([null, 'withdrawal'])->atLeast()->once()->andReturn($withdrawal);
185        $curRepos->shouldReceive('findCurrency')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
186        $curRepos->shouldReceive('findCurrencyNull')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
187        $billRepos->shouldReceive('findBill')->withArgs([0, null])->atLeast()->once()->andReturnNull();
188        $accountRepos->shouldReceive('findNull')->withArgs([$asset->id])->atLeast()->once()->andReturn($asset);
189        $accountRepos->shouldReceive('findNull')->withArgs([$expense->id])->atLeast()->once()->andReturn($expense);
190        $accountRepos->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro);
191        $budgetRepos->shouldReceive('findBudget')->withArgs([0, null])->atLeast()->once()->andReturnNull();
192        $catRepos->shouldReceive('findCategory')->withArgs([0, null])->atLeast()->once()->andReturnNull();
193        $metaFactory->shouldReceive('updateOrCreate')->atLeast()->once();
194
195
196        /** @var TransactionJournalFactory $factory */
197        $factory = app(TransactionJournalFactory::class);
198        $factory->setUser($this->user());
199
200        try {
201            $collection = $factory->create($submission);
202        } catch (FireflyException $e) {
203            $this->assertTrue(false, $e->getMessage());
204
205            return;
206        }
207        $this->assertCount(1, $collection);
208        /** @var TransactionJournal $first */
209        $first = $collection->first();
210        $this->assertEquals($first->description, $submission['transactions'][0]['description']);
211        $first->forceDelete();
212    }
213
214
215    /**
216     * Submit minimal array for a deposit.
217     * @covers \FireflyIII\Factory\TransactionJournalFactory
218     * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
219     */
220    public function testCreateDeposit(): void
221    {
222        $billRepos          = $this->mock(BillRepositoryInterface::class);
223        $budgetRepos        = $this->mock(BudgetRepositoryInterface::class);
224        $catRepos           = $this->mock(CategoryRepositoryInterface::class);
225        $curRepos           = $this->mock(CurrencyRepositoryInterface::class);
226        $piggyRepos         = $this->mock(PiggyBankRepositoryInterface::class);
227        $tagFactory         = $this->mock(TagFactory::class);
228        $accountRepos       = $this->mock(AccountRepositoryInterface::class);
229        $typeRepos          = $this->mock(TransactionTypeRepositoryInterface::class);
230        $eventFactory       = $this->mock(PiggyBankEventFactory::class);
231        $accountFactory     = $this->mock(AccountFactory::class);
232        $currencyFactory    = $this->mock(TransactionCurrencyFactory::class);
233        $metaFactory        = $this->mock(TransactionJournalMetaFactory::class);
234        $transactionFactory = $this->mock(TransactionFactory::class);
235        $validator = $this->mock(AccountValidator::class);
236
237        $validator->shouldReceive('setUser')->atLeast()->once();
238        $validator->shouldReceive('setTransactionType')->atLeast()->once()->withArgs(['deposit']);
239        $validator->shouldReceive('validateSource')->atLeast()->once()->andReturnTrue();
240        $validator->shouldReceive('validateDestination')->atLeast()->once()->andReturnTrue();
241
242        // data
243        $deposit = TransactionType::where('type', TransactionType::DEPOSIT)->first();
244        $asset      = $this->getRandomAsset();
245        $revenue = $this->getRandomRevenue();
246        $euro       = $this->getEuro();
247        $submission = [
248            'transactions' => [
249                [
250                    'type'           => 'deposit',
251                    'amount'         => '10',
252                    'description'    => sprintf('I am a test #%d', $this->randomInt()),
253                    'source_id'      => $revenue->id,
254                    'destination_id' => $asset->id,
255                ],
256            ],
257        ];
258
259
260        // mock calls to all repositories
261        $curRepos->shouldReceive('setUser')->atLeast()->once();
262        $billRepos->shouldReceive('setUser')->atLeast()->once();
263        $budgetRepos->shouldReceive('setUser')->atLeast()->once();
264        $catRepos->shouldReceive('setUser')->atLeast()->once();
265        $piggyRepos->shouldReceive('setUser')->atLeast()->once();
266        $accountRepos->shouldReceive('setUser')->atLeast()->once();
267        $tagFactory->shouldReceive('setUser')->atLeast()->once();
268        $transactionFactory->shouldReceive('setUser')->atLeast()->once();
269
270        $transactionFactory->shouldReceive('setJournal')->atLeast()->once();
271        $transactionFactory->shouldReceive('setAccount')->atLeast()->once();
272        $transactionFactory->shouldReceive('setCurrency')->atLeast()->once();
273        $transactionFactory->shouldReceive('setForeignCurrency')->atLeast()->once();
274        $transactionFactory->shouldReceive('setReconciled')->atLeast()->once();
275        $transactionFactory->shouldReceive('createNegative')->atLeast()->once()->andReturn(new Transaction);
276        $transactionFactory->shouldReceive('createPositive')->atLeast()->once()->andReturn(new Transaction);
277
278        $typeRepos->shouldReceive('findTransactionType')->withArgs([null, 'deposit'])->atLeast()->once()->andReturn($deposit);
279        $curRepos->shouldReceive('findCurrency')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
280        $curRepos->shouldReceive('findCurrencyNull')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
281        $billRepos->shouldReceive('findBill')->withArgs([0, null])->atLeast()->once()->andReturnNull();
282        $accountRepos->shouldReceive('findNull')->withArgs([$revenue->id])->atLeast()->once()->andReturn($revenue);
283        $accountRepos->shouldReceive('findNull')->withArgs([$asset->id])->atLeast()->once()->andReturn($asset);
284        $accountRepos->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro);
285        $catRepos->shouldReceive('findCategory')->withArgs([0, null])->atLeast()->once()->andReturnNull();
286        $metaFactory->shouldReceive('updateOrCreate')->atLeast()->once();
287
288
289        /** @var TransactionJournalFactory $factory */
290        $factory = app(TransactionJournalFactory::class);
291        $factory->setUser($this->user());
292
293        try {
294            $collection = $factory->create($submission);
295        } catch (FireflyException $e) {
296            $this->assertTrue(false, $e->getMessage());
297
298            return;
299        }
300        $this->assertCount(1, $collection);
301        /** @var TransactionJournal $first */
302        $first = $collection->first();
303        $this->assertEquals($first->description, $submission['transactions'][0]['description']);
304        $first->forceDelete();
305    }
306
307
308    /**
309     * Submit array for a withdrawal. Include tag info.
310     *
311     * @covers \FireflyIII\Factory\TransactionJournalFactory
312     * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
313     */
314    public function testCreateWithdrawalTags(): void
315    {
316        $billRepos          = $this->mock(BillRepositoryInterface::class);
317        $budgetRepos        = $this->mock(BudgetRepositoryInterface::class);
318        $catRepos           = $this->mock(CategoryRepositoryInterface::class);
319        $curRepos           = $this->mock(CurrencyRepositoryInterface::class);
320        $piggyRepos         = $this->mock(PiggyBankRepositoryInterface::class);
321        $tagFactory         = $this->mock(TagFactory::class);
322        $accountRepos       = $this->mock(AccountRepositoryInterface::class);
323        $typeRepos          = $this->mock(TransactionTypeRepositoryInterface::class);
324        $eventFactory       = $this->mock(PiggyBankEventFactory::class);
325        $accountFactory     = $this->mock(AccountFactory::class);
326        $currencyFactory    = $this->mock(TransactionCurrencyFactory::class);
327        $metaFactory        = $this->mock(TransactionJournalMetaFactory::class);
328        $transactionFactory = $this->mock(TransactionFactory::class);
329        $validator = $this->mock(AccountValidator::class);
330
331        $validator->shouldReceive('setUser')->atLeast()->once();
332        $validator->shouldReceive('setTransactionType')->atLeast()->once()->withArgs(['withdrawal']);
333        $validator->shouldReceive('validateSource')->atLeast()->once()->andReturnTrue();
334        $validator->shouldReceive('validateDestination')->atLeast()->once()->andReturnTrue();
335
336        // data
337        $withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
338        $asset      = $this->getRandomAsset();
339        $tag = $this->user()->tags()->inRandomOrder()->first();
340        $expense    = $this->getRandomExpense();
341        $euro       = $this->getEuro();
342        $submission = [
343            'transactions' => [
344                [
345                    'type'           => 'withdrawal',
346                    'amount'         => '10',
347                    'description'    => sprintf('I am a test #%d', $this->randomInt()),
348                    'source_id'      => $asset->id,
349                    'destination_id' => $expense->id,
350                    'tags'           => ['a'],
351                ],
352            ],
353        ];
354
355
356        // mock calls to all repositories
357        $curRepos->shouldReceive('setUser')->atLeast()->once();
358        $billRepos->shouldReceive('setUser')->atLeast()->once();
359        $budgetRepos->shouldReceive('setUser')->atLeast()->once();
360        $catRepos->shouldReceive('setUser')->atLeast()->once();
361        $piggyRepos->shouldReceive('setUser')->atLeast()->once();
362        $accountRepos->shouldReceive('setUser')->atLeast()->once();
363        $tagFactory->shouldReceive('setUser')->atLeast()->once();
364        $transactionFactory->shouldReceive('setUser')->atLeast()->once();
365
366        $transactionFactory->shouldReceive('setJournal')->atLeast()->once();
367        $transactionFactory->shouldReceive('setAccount')->atLeast()->once();
368        $transactionFactory->shouldReceive('setCurrency')->atLeast()->once();
369        $transactionFactory->shouldReceive('setForeignCurrency')->atLeast()->once();
370        $transactionFactory->shouldReceive('setReconciled')->atLeast()->once();
371        $transactionFactory->shouldReceive('createNegative')->atLeast()->once()->andReturn(new Transaction);
372        $transactionFactory->shouldReceive('createPositive')->atLeast()->once()->andReturn(new Transaction);
373
374        $typeRepos->shouldReceive('findTransactionType')->withArgs([null, 'withdrawal'])->atLeast()->once()->andReturn($withdrawal);
375        $curRepos->shouldReceive('findCurrency')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
376        $curRepos->shouldReceive('findCurrencyNull')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
377        $billRepos->shouldReceive('findBill')->withArgs([0, null])->atLeast()->once()->andReturnNull();
378        $accountRepos->shouldReceive('findNull')->withArgs([$asset->id])->atLeast()->once()->andReturn($asset);
379        $accountRepos->shouldReceive('findNull')->withArgs([$expense->id])->atLeast()->once()->andReturn($expense);
380        $accountRepos->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro);
381        $budgetRepos->shouldReceive('findBudget')->withArgs([0, null])->atLeast()->once()->andReturnNull();
382        $catRepos->shouldReceive('findCategory')->withArgs([0, null])->atLeast()->once()->andReturnNull();
383        $metaFactory->shouldReceive('updateOrCreate')->atLeast()->once();
384        $tagFactory->shouldReceive('findOrCreate')->atLeast()->once()->andReturn($tag);
385
386
387        /** @var TransactionJournalFactory $factory */
388        $factory = app(TransactionJournalFactory::class);
389        $factory->setUser($this->user());
390
391        try {
392            $collection = $factory->create($submission);
393        } catch (FireflyException $e) {
394            $this->assertTrue(false, $e->getMessage());
395
396            return;
397        }
398        $this->assertCount(1, $collection);
399        /** @var TransactionJournal $first */
400        $first = $collection->first();
401        $this->assertEquals($first->description, $submission['transactions'][0]['description']);
402        $first->forceDelete();
403    }
404
405
406    /**
407     * Submit minimal array for a withdrawal.
408     * @covers \FireflyIII\Factory\TransactionJournalFactory
409     * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
410     */
411    public function testCreateWithdrawalNote(): void
412    {
413        $billRepos          = $this->mock(BillRepositoryInterface::class);
414        $budgetRepos        = $this->mock(BudgetRepositoryInterface::class);
415        $catRepos           = $this->mock(CategoryRepositoryInterface::class);
416        $curRepos           = $this->mock(CurrencyRepositoryInterface::class);
417        $piggyRepos         = $this->mock(PiggyBankRepositoryInterface::class);
418        $tagFactory         = $this->mock(TagFactory::class);
419        $accountRepos       = $this->mock(AccountRepositoryInterface::class);
420        $typeRepos          = $this->mock(TransactionTypeRepositoryInterface::class);
421        $eventFactory       = $this->mock(PiggyBankEventFactory::class);
422        $accountFactory     = $this->mock(AccountFactory::class);
423        $currencyFactory    = $this->mock(TransactionCurrencyFactory::class);
424        $metaFactory        = $this->mock(TransactionJournalMetaFactory::class);
425        $transactionFactory = $this->mock(TransactionFactory::class);
426        $validator = $this->mock(AccountValidator::class);
427
428        $validator->shouldReceive('setUser')->atLeast()->once();
429        $validator->shouldReceive('setTransactionType')->atLeast()->once()->withArgs(['withdrawal']);
430        $validator->shouldReceive('validateSource')->atLeast()->once()->andReturnTrue();
431        $validator->shouldReceive('validateDestination')->atLeast()->once()->andReturnTrue();
432
433        // data
434        $withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
435        $asset      = $this->getRandomAsset();
436        $expense    = $this->getRandomExpense();
437        $euro       = $this->getEuro();
438        $submission = [
439            'transactions' => [
440                [
441                    'type'           => 'withdrawal',
442                    'amount'         => '10',
443                    'description'    => sprintf('I am a test #%d', $this->randomInt()),
444                    'source_id'      => $asset->id,
445                    'destination_id' => $expense->id,
446                    'notes'          => 'I am a note',
447                ],
448            ],
449        ];
450
451
452        // mock calls to all repositories
453        $curRepos->shouldReceive('setUser')->atLeast()->once();
454        $billRepos->shouldReceive('setUser')->atLeast()->once();
455        $budgetRepos->shouldReceive('setUser')->atLeast()->once();
456        $catRepos->shouldReceive('setUser')->atLeast()->once();
457        $piggyRepos->shouldReceive('setUser')->atLeast()->once();
458        $accountRepos->shouldReceive('setUser')->atLeast()->once();
459        $tagFactory->shouldReceive('setUser')->atLeast()->once();
460        $transactionFactory->shouldReceive('setUser')->atLeast()->once();
461
462        $transactionFactory->shouldReceive('setJournal')->atLeast()->once();
463        $transactionFactory->shouldReceive('setAccount')->atLeast()->once();
464        $transactionFactory->shouldReceive('setCurrency')->atLeast()->once();
465        $transactionFactory->shouldReceive('setForeignCurrency')->atLeast()->once();
466        $transactionFactory->shouldReceive('setReconciled')->atLeast()->once();
467        $transactionFactory->shouldReceive('createNegative')->atLeast()->once()->andReturn(new Transaction);
468        $transactionFactory->shouldReceive('createPositive')->atLeast()->once()->andReturn(new Transaction);
469
470        $typeRepos->shouldReceive('findTransactionType')->withArgs([null, 'withdrawal'])->atLeast()->once()->andReturn($withdrawal);
471        $curRepos->shouldReceive('findCurrency')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
472        $curRepos->shouldReceive('findCurrencyNull')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
473        $billRepos->shouldReceive('findBill')->withArgs([0, null])->atLeast()->once()->andReturnNull();
474        $accountRepos->shouldReceive('findNull')->withArgs([$asset->id])->atLeast()->once()->andReturn($asset);
475        $accountRepos->shouldReceive('findNull')->withArgs([$expense->id])->atLeast()->once()->andReturn($expense);
476        $accountRepos->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro);
477        $budgetRepos->shouldReceive('findBudget')->withArgs([0, null])->atLeast()->once()->andReturnNull();
478        $catRepos->shouldReceive('findCategory')->withArgs([0, null])->atLeast()->once()->andReturnNull();
479        $metaFactory->shouldReceive('updateOrCreate')->atLeast()->once();
480
481
482        /** @var TransactionJournalFactory $factory */
483        $factory = app(TransactionJournalFactory::class);
484        $factory->setUser($this->user());
485
486        try {
487            $collection = $factory->create($submission);
488        } catch (FireflyException $e) {
489            $this->assertTrue(false, $e->getMessage());
490
491            return;
492        }
493        $this->assertCount(1, $collection);
494        /** @var TransactionJournal $first */
495        $first = $collection->first();
496        $this->assertEquals($first->description, $submission['transactions'][0]['description']);
497        $this->assertCount(1, $first->notes()->get());
498        $first->forceDelete();
499    }
500
501    /**
502     * Submit array for a withdrawal.
503     *
504     * Include budget, category.
505     *
506     * @covers \FireflyIII\Factory\TransactionJournalFactory
507     * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
508     */
509    public function testCreateWithdrawalMeta(): void
510    {
511        $billRepos          = $this->mock(BillRepositoryInterface::class);
512        $budgetRepos        = $this->mock(BudgetRepositoryInterface::class);
513        $catRepos           = $this->mock(CategoryRepositoryInterface::class);
514        $curRepos           = $this->mock(CurrencyRepositoryInterface::class);
515        $piggyRepos         = $this->mock(PiggyBankRepositoryInterface::class);
516        $tagFactory         = $this->mock(TagFactory::class);
517        $accountRepos       = $this->mock(AccountRepositoryInterface::class);
518        $typeRepos          = $this->mock(TransactionTypeRepositoryInterface::class);
519        $eventFactory       = $this->mock(PiggyBankEventFactory::class);
520        $accountFactory     = $this->mock(AccountFactory::class);
521        $currencyFactory    = $this->mock(TransactionCurrencyFactory::class);
522        $metaFactory        = $this->mock(TransactionJournalMetaFactory::class);
523        $transactionFactory = $this->mock(TransactionFactory::class);
524        $validator = $this->mock(AccountValidator::class);
525
526        $validator->shouldReceive('setUser')->atLeast()->once();
527        $validator->shouldReceive('setTransactionType')->atLeast()->once()->withArgs(['withdrawal']);
528        $validator->shouldReceive('validateSource')->atLeast()->once()->andReturnTrue();
529        $validator->shouldReceive('validateDestination')->atLeast()->once()->andReturnTrue();
530
531        // data
532        $withdrawal = TransactionType::where('type', TransactionType::WITHDRAWAL)->first();
533        $asset      = $this->getRandomAsset();
534        $expense    = $this->getRandomExpense();
535        $budget     = $this->user()->budgets()->inRandomOrder()->first();
536        $category   = $this->user()->categories()->inRandomOrder()->first();
537        $euro       = $this->getEuro();
538        $submission = [
539            'transactions' => [
540                [
541                    'type'           => 'withdrawal',
542                    'amount'         => '10',
543                    'description'    => sprintf('I am a test #%d', $this->randomInt()),
544                    'source_id'      => $asset->id,
545                    'budget_id'      => $budget->id,
546                    'category_id'    => $category->id,
547                    'destination_id' => $expense->id,
548                ],
549            ],
550        ];
551
552
553        // mock calls to all repositories
554        $curRepos->shouldReceive('setUser')->atLeast()->once();
555        $billRepos->shouldReceive('setUser')->atLeast()->once();
556        $budgetRepos->shouldReceive('setUser')->atLeast()->once();
557        $catRepos->shouldReceive('setUser')->atLeast()->once();
558        $piggyRepos->shouldReceive('setUser')->atLeast()->once();
559        $accountRepos->shouldReceive('setUser')->atLeast()->once();
560        $tagFactory->shouldReceive('setUser')->atLeast()->once();
561        $transactionFactory->shouldReceive('setUser')->atLeast()->once();
562
563        $transactionFactory->shouldReceive('setJournal')->atLeast()->once();
564        $transactionFactory->shouldReceive('setAccount')->atLeast()->once();
565        $transactionFactory->shouldReceive('setCurrency')->atLeast()->once();
566        $transactionFactory->shouldReceive('setForeignCurrency')->atLeast()->once();
567        $transactionFactory->shouldReceive('setReconciled')->atLeast()->once();
568        $transactionFactory->shouldReceive('createNegative')->atLeast()->once()->andReturn(new Transaction);
569        $transactionFactory->shouldReceive('createPositive')->atLeast()->once()->andReturn(new Transaction);
570
571        $typeRepos->shouldReceive('findTransactionType')->withArgs([null, 'withdrawal'])->atLeast()->once()->andReturn($withdrawal);
572        $curRepos->shouldReceive('findCurrency')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
573        $curRepos->shouldReceive('findCurrencyNull')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
574        $billRepos->shouldReceive('findBill')->withArgs([0, null])->atLeast()->once()->andReturnNull();
575        $accountRepos->shouldReceive('findNull')->withArgs([$asset->id])->atLeast()->once()->andReturn($asset);
576        $accountRepos->shouldReceive('findNull')->withArgs([$expense->id])->atLeast()->once()->andReturn($expense);
577        $accountRepos->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro);
578        $budgetRepos->shouldReceive('findBudget')->withArgs([$budget->id, null])->atLeast()->once()->andReturn($budget);
579        $catRepos->shouldReceive('findCategory')->withArgs([$category->id, null])->atLeast()->once()->andReturn($category);
580        $metaFactory->shouldReceive('updateOrCreate')->atLeast()->once();
581
582
583        /** @var TransactionJournalFactory $factory */
584        $factory = app(TransactionJournalFactory::class);
585        $factory->setUser($this->user());
586
587        try {
588            $collection = $factory->create($submission);
589        } catch (FireflyException $e) {
590            $this->assertTrue(false, $e->getMessage());
591
592            return;
593        }
594        $this->assertCount(1, $collection);
595        /** @var TransactionJournal $first */
596        $first = $collection->first();
597        $this->assertEquals($first->description, $submission['transactions'][0]['description']);
598        $first->forceDelete();
599    }
600
601    /**
602     * Submit minimal array for a withdrawal.
603     * Includes piggy bank data, but the piggy bank is invalid.
604     *
605     * @covers \FireflyIII\Factory\TransactionJournalFactory
606     * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
607     */
608    public function testCreateTransferInvalidPiggie(): void
609    {
610        $billRepos          = $this->mock(BillRepositoryInterface::class);
611        $budgetRepos        = $this->mock(BudgetRepositoryInterface::class);
612        $catRepos           = $this->mock(CategoryRepositoryInterface::class);
613        $curRepos           = $this->mock(CurrencyRepositoryInterface::class);
614        $piggyRepos         = $this->mock(PiggyBankRepositoryInterface::class);
615        $tagFactory         = $this->mock(TagFactory::class);
616        $accountRepos       = $this->mock(AccountRepositoryInterface::class);
617        $typeRepos          = $this->mock(TransactionTypeRepositoryInterface::class);
618        $eventFactory       = $this->mock(PiggyBankEventFactory::class);
619        $accountFactory     = $this->mock(AccountFactory::class);
620        $currencyFactory    = $this->mock(TransactionCurrencyFactory::class);
621        $metaFactory        = $this->mock(TransactionJournalMetaFactory::class);
622        $transactionFactory = $this->mock(TransactionFactory::class);
623        $validator = $this->mock(AccountValidator::class);
624
625        $validator->shouldReceive('setUser')->atLeast()->once();
626        $validator->shouldReceive('setTransactionType')->atLeast()->once()->withArgs(['transfer']);
627        $validator->shouldReceive('validateSource')->atLeast()->once()->andReturnTrue();
628        $validator->shouldReceive('validateDestination')->atLeast()->once()->andReturnTrue();
629
630        // data
631        $transfer   = TransactionType::where('type', TransactionType::TRANSFER)->first();
632        $asset      = $this->getRandomAsset();
633        $otherAsset = $this->getRandomAsset($asset->id);
634        $euro       = $this->getEuro();
635        $piggy      = $this->user()->piggyBanks()->inRandomOrder()->first();
636        $submission = [
637            'transactions' => [
638                [
639                    'type'           => 'transfer',
640                    'amount'         => '10',
641                    'description'    => sprintf('I am a test #%d', $this->randomInt()),
642                    'source_id'      => $asset->id,
643                    'destination_id' => $otherAsset->id,
644                    'piggy_bank_id'  => $piggy->id,
645                ],
646            ],
647        ];
648
649
650        // mock calls to all repositories
651        $curRepos->shouldReceive('setUser')->atLeast()->once();
652        $billRepos->shouldReceive('setUser')->atLeast()->once();
653        $budgetRepos->shouldReceive('setUser')->atLeast()->once();
654        $catRepos->shouldReceive('setUser')->atLeast()->once();
655        $piggyRepos->shouldReceive('setUser')->atLeast()->once();
656        $accountRepos->shouldReceive('setUser')->atLeast()->once();
657        $tagFactory->shouldReceive('setUser')->atLeast()->once();
658        $transactionFactory->shouldReceive('setUser')->atLeast()->once();
659
660        $transactionFactory->shouldReceive('setJournal')->atLeast()->once();
661        $transactionFactory->shouldReceive('setAccount')->atLeast()->once();
662        $transactionFactory->shouldReceive('setCurrency')->atLeast()->once();
663        $transactionFactory->shouldReceive('setForeignCurrency')->atLeast()->once();
664        $transactionFactory->shouldReceive('setReconciled')->atLeast()->once();
665        $transactionFactory->shouldReceive('createNegative')->atLeast()->once()->andReturn(new Transaction);
666        $transactionFactory->shouldReceive('createPositive')->atLeast()->once()->andReturn(new Transaction);
667
668        $typeRepos->shouldReceive('findTransactionType')->withArgs([null, 'transfer'])->atLeast()->once()->andReturn($transfer);
669        $curRepos->shouldReceive('findCurrency')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
670        $curRepos->shouldReceive('findCurrencyNull')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
671        $billRepos->shouldReceive('findBill')->withArgs([0, null])->atLeast()->once()->andReturnNull();
672        $accountRepos->shouldReceive('findNull')->withArgs([$asset->id])->atLeast()->once()->andReturn($asset);
673        $accountRepos->shouldReceive('findNull')->withArgs([$otherAsset->id])->atLeast()->once()->andReturn($otherAsset);
674        $accountRepos->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro);
675        $catRepos->shouldReceive('findCategory')->withArgs([0, null])->atLeast()->once()->andReturnNull();
676        $metaFactory->shouldReceive('updateOrCreate')->atLeast()->once();
677        $piggyRepos->shouldReceive('findPiggyBank')->withArgs([$piggy->id, null])->atLeast()->once()->andReturn(null);
678
679
680        /** @var TransactionJournalFactory $factory */
681        $factory = app(TransactionJournalFactory::class);
682        $factory->setUser($this->user());
683
684        try {
685            $collection = $factory->create($submission);
686        } catch (FireflyException $e) {
687            $this->assertTrue(false, $e->getMessage());
688
689            return;
690        }
691        $this->assertCount(1, $collection);
692        /** @var TransactionJournal $first */
693        $first = $collection->first();
694        $this->assertEquals($first->description, $submission['transactions'][0]['description']);
695        $first->forceDelete();
696    }
697
698
699    /**
700     * Submit minimal array for a withdrawal.
701     * Includes piggy bank data.
702     *
703     * @covers \FireflyIII\Factory\TransactionJournalFactory
704     * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
705     */
706    public function testCreateTransfer(): void
707    {
708        $billRepos          = $this->mock(BillRepositoryInterface::class);
709        $budgetRepos        = $this->mock(BudgetRepositoryInterface::class);
710        $catRepos           = $this->mock(CategoryRepositoryInterface::class);
711        $curRepos           = $this->mock(CurrencyRepositoryInterface::class);
712        $piggyRepos         = $this->mock(PiggyBankRepositoryInterface::class);
713        $tagFactory         = $this->mock(TagFactory::class);
714        $accountRepos       = $this->mock(AccountRepositoryInterface::class);
715        $typeRepos          = $this->mock(TransactionTypeRepositoryInterface::class);
716        $eventFactory       = $this->mock(PiggyBankEventFactory::class);
717        $accountFactory     = $this->mock(AccountFactory::class);
718        $currencyFactory    = $this->mock(TransactionCurrencyFactory::class);
719        $metaFactory        = $this->mock(TransactionJournalMetaFactory::class);
720        $transactionFactory = $this->mock(TransactionFactory::class);
721        $validator = $this->mock(AccountValidator::class);
722
723        $validator->shouldReceive('setUser')->atLeast()->once();
724        $validator->shouldReceive('setTransactionType')->atLeast()->once()->withArgs(['transfer']);
725        $validator->shouldReceive('validateSource')->atLeast()->once()->andReturnTrue();
726        $validator->shouldReceive('validateDestination')->atLeast()->once()->andReturnTrue();
727
728        // data
729        $transfer   = TransactionType::where('type', TransactionType::TRANSFER)->first();
730        $asset      = $this->getRandomAsset();
731        $otherAsset = $this->getRandomAsset($asset->id);
732        $euro       = $this->getEuro();
733        $piggy      = $this->user()->piggyBanks()->inRandomOrder()->first();
734        $submission = [
735            'transactions' => [
736                [
737                    'type'           => 'transfer',
738                    'amount'         => '10',
739                    'description'    => sprintf('I am a test #%d', $this->randomInt()),
740                    'source_id'      => $asset->id,
741                    'destination_id' => $otherAsset->id,
742                    'piggy_bank_id'  => $piggy->id,
743                ],
744            ],
745        ];
746
747
748        // mock calls to all repositories
749        $curRepos->shouldReceive('setUser')->atLeast()->once();
750        $billRepos->shouldReceive('setUser')->atLeast()->once();
751        $budgetRepos->shouldReceive('setUser')->atLeast()->once();
752        $catRepos->shouldReceive('setUser')->atLeast()->once();
753        $piggyRepos->shouldReceive('setUser')->atLeast()->once();
754        $accountRepos->shouldReceive('setUser')->atLeast()->once();
755        $tagFactory->shouldReceive('setUser')->atLeast()->once();
756        $transactionFactory->shouldReceive('setUser')->atLeast()->once();
757
758        $transactionFactory->shouldReceive('setJournal')->atLeast()->once();
759        $transactionFactory->shouldReceive('setAccount')->atLeast()->once();
760        $transactionFactory->shouldReceive('setCurrency')->atLeast()->once();
761        $transactionFactory->shouldReceive('setForeignCurrency')->atLeast()->once();
762        $transactionFactory->shouldReceive('setReconciled')->atLeast()->once();
763        $transactionFactory->shouldReceive('createNegative')->atLeast()->once()->andReturn(new Transaction);
764        $transactionFactory->shouldReceive('createPositive')->atLeast()->once()->andReturn(new Transaction);
765
766        $typeRepos->shouldReceive('findTransactionType')->withArgs([null, 'transfer'])->atLeast()->once()->andReturn($transfer);
767        $curRepos->shouldReceive('findCurrency')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
768        $curRepos->shouldReceive('findCurrencyNull')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
769        $billRepos->shouldReceive('findBill')->withArgs([0, null])->atLeast()->once()->andReturnNull();
770        $accountRepos->shouldReceive('findNull')->withArgs([$asset->id])->atLeast()->once()->andReturn($asset);
771        $accountRepos->shouldReceive('findNull')->withArgs([$otherAsset->id])->atLeast()->once()->andReturn($otherAsset);
772        $accountRepos->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro);
773        $catRepos->shouldReceive('findCategory')->withArgs([0, null])->atLeast()->once()->andReturnNull();
774        $metaFactory->shouldReceive('updateOrCreate')->atLeast()->once();
775        $piggyRepos->shouldReceive('findPiggyBank')->withArgs([$piggy->id, null])->atLeast()->once()->andReturn($piggy);
776        $eventFactory->shouldReceive('create')->atLeast()->once()->andReturn(new PiggyBankEvent);
777
778
779        /** @var TransactionJournalFactory $factory */
780        $factory = app(TransactionJournalFactory::class);
781        $factory->setUser($this->user());
782
783        try {
784            $collection = $factory->create($submission);
785        } catch (FireflyException $e) {
786            $this->assertTrue(false, $e->getMessage());
787
788            return;
789        }
790        $this->assertCount(1, $collection);
791        /** @var TransactionJournal $first */
792        $first = $collection->first();
793        $this->assertEquals($first->description, $submission['transactions'][0]['description']);
794        $first->forceDelete();
795    }
796
797
798    /**
799     * Submit minimal array for a withdrawal.
800     * Includes piggy bank data.
801     * Includes foreign amounts but foreign currency is missing or invalid.
802     * Will be solved by getting users default currency.
803     *
804     * @covers \FireflyIII\Factory\TransactionJournalFactory
805     * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait
806     */
807    public function testCreateTransferForeign(): void
808    {
809        $billRepos          = $this->mock(BillRepositoryInterface::class);
810        $budgetRepos        = $this->mock(BudgetRepositoryInterface::class);
811        $catRepos           = $this->mock(CategoryRepositoryInterface::class);
812        $curRepos           = $this->mock(CurrencyRepositoryInterface::class);
813        $piggyRepos         = $this->mock(PiggyBankRepositoryInterface::class);
814        $tagFactory         = $this->mock(TagFactory::class);
815        $accountRepos       = $this->mock(AccountRepositoryInterface::class);
816        $typeRepos          = $this->mock(TransactionTypeRepositoryInterface::class);
817        $eventFactory       = $this->mock(PiggyBankEventFactory::class);
818        $accountFactory     = $this->mock(AccountFactory::class);
819        $currencyFactory    = $this->mock(TransactionCurrencyFactory::class);
820        $metaFactory        = $this->mock(TransactionJournalMetaFactory::class);
821        $transactionFactory = $this->mock(TransactionFactory::class);
822        $validator = $this->mock(AccountValidator::class);
823
824        $validator->shouldReceive('setUser')->atLeast()->once();
825        $validator->shouldReceive('setTransactionType')->atLeast()->once()->withArgs(['transfer']);
826        $validator->shouldReceive('validateSource')->atLeast()->once()->andReturnTrue();
827        $validator->shouldReceive('validateDestination')->atLeast()->once()->andReturnTrue();
828
829        // data
830        $transfer   = TransactionType::where('type', TransactionType::TRANSFER)->first();
831        $asset      = $this->getRandomAsset();
832        $otherAsset = $this->getRandomAsset($asset->id);
833        $euro       = $this->getEuro();
834        $piggy      = $this->user()->piggyBanks()->inRandomOrder()->first();
835        $submission = [
836            'transactions' => [
837                [
838                    'type'           => 'transfer',
839                    'amount'         => '10',
840                    'foreign_amount' => '10',
841                    'description'    => sprintf('I am a test #%d', $this->randomInt()),
842                    'source_id'      => $asset->id,
843                    'destination_id' => $otherAsset->id,
844                    'piggy_bank_id'  => $piggy->id,
845                ],
846            ],
847        ];
848
849
850        // mock calls to all repositories
851        $curRepos->shouldReceive('setUser')->atLeast()->once();
852        $billRepos->shouldReceive('setUser')->atLeast()->once();
853        $budgetRepos->shouldReceive('setUser')->atLeast()->once();
854        $catRepos->shouldReceive('setUser')->atLeast()->once();
855        $piggyRepos->shouldReceive('setUser')->atLeast()->once();
856        $accountRepos->shouldReceive('setUser')->atLeast()->once();
857        $tagFactory->shouldReceive('setUser')->atLeast()->once();
858        $transactionFactory->shouldReceive('setUser')->atLeast()->once();
859
860        Amount::shouldReceive('getDefaultCurrencyByUser')->atLeast()->once()->andReturn($euro);
861
862        $transactionFactory->shouldReceive('setJournal')->atLeast()->once();
863        $transactionFactory->shouldReceive('setAccount')->atLeast()->once();
864        $transactionFactory->shouldReceive('setCurrency')->atLeast()->once();
865        $transactionFactory->shouldReceive('setForeignCurrency')->atLeast()->once();
866        $transactionFactory->shouldReceive('setReconciled')->atLeast()->once();
867        $transactionFactory->shouldReceive('createNegative')->atLeast()->once()->andReturn(new Transaction);
868        $transactionFactory->shouldReceive('createPositive')->atLeast()->once()->andReturn(new Transaction);
869
870        $typeRepos->shouldReceive('findTransactionType')->withArgs([null, 'transfer'])->atLeast()->once()->andReturn($transfer);
871        $curRepos->shouldReceive('findCurrency')->atLeast()->once()->withArgs([0, null])->andReturn($euro);
872        $curRepos->shouldReceive('findCurrencyNull')->atLeast()->once()->withArgs([0, null])->andReturnNull();
873        $billRepos->shouldReceive('findBill')->withArgs([0, null])->atLeast()->once()->andReturnNull();
874        $accountRepos->shouldReceive('findNull')->withArgs([$asset->id])->atLeast()->once()->andReturn($asset);
875        $accountRepos->shouldReceive('findNull')->withArgs([$otherAsset->id])->atLeast()->once()->andReturn($otherAsset);
876        $accountRepos->shouldReceive('getAccountCurrency')->atLeast()->once()->andReturn($euro, null);
877        $catRepos->shouldReceive('findCategory')->withArgs([0, null])->atLeast()->once()->andReturnNull();
878        $metaFactory->shouldReceive('updateOrCreate')->atLeast()->once();
879        $piggyRepos->shouldReceive('findPiggyBank')->withArgs([$piggy->id, null])->atLeast()->once()->andReturn($piggy);
880        $eventFactory->shouldReceive('create')->atLeast()->once()->andReturn(new PiggyBankEvent);
881
882
883        /** @var TransactionJournalFactory $factory */
884        $factory = app(TransactionJournalFactory::class);
885        $factory->setUser($this->user());
886
887        try {
888            $collection = $factory->create($submission);
889        } catch (FireflyException $e) {
890            $this->assertTrue(false, $e->getMessage());
891
892            return;
893        }
894        $this->assertCount(1, $collection);
895        /** @var TransactionJournal $first */
896        $first = $collection->first();
897        $this->assertEquals($first->description, $submission['transactions'][0]['description']);
898        $first->forceDelete();
899    }
900}
901
Full Screen

ApplyRulesTest.php

Source: ApplyRulesTest.php Github

copy
1<?php
2/**
3 * ApplyRulesTest.php
4 * Copyright (c) 2019 james@firefly-iii.org
5 *
6 * This file is part of Firefly III (https://github.com/firefly-iii).
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21
22declare(strict_types=1);
23
24namespace Tests\Unit\Console\Commands\Tools;
25
26
27use FireflyIII\Helpers\Collector\GroupCollectorInterface;
28use FireflyIII\Models\Preference;
29use FireflyIII\Repositories\Account\AccountRepositoryInterface;
30use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
31use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
32use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
33use FireflyIII\Repositories\User\UserRepositoryInterface;
34use FireflyIII\TransactionRules\Engine\RuleEngine;
35use Illuminate\Support\Collection;
36use Log;
37use Mockery;
38use Preferences;
39use Tests\TestCase;
40
41/**
42 * Class ApplyRulesTest
43 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
44 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
45 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
46 */
47class ApplyRulesTest extends TestCase
48{
49    /**
50     *
51     */
52    public function setUp(): void
53    {
54        parent::setUp();
55        Log::info(sprintf('Now in %s.', get_class($this)));
56    }
57
58    /**
59     * Basic call with everything perfect (and ALL rules).
60     *
61     * @covers \FireflyIII\Console\Commands\Tools\ApplyRules
62     */
63    public function testHandle(): void
64    {
65        $ruleRepos      = $this->mock(RuleRepositoryInterface::class);
66        $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
67        $journalRepos   = $this->mock(JournalRepositoryInterface::class);
68        $collector      = $this->mock(GroupCollectorInterface::class);
69        $accountRepos   = $this->mock(AccountRepositoryInterface::class);
70        $ruleEngine     = $this->mock(RuleEngine::class);
71        $userRepos      = $this->mock(UserRepositoryInterface::class);
72
73        // data
74        $asset   = $this->getRandomAsset();
75        $journal = $this->getRandomWithdrawal();
76        $group   = $this->user()->ruleGroups()->first();
77        $rule    = $this->user()->rules()->first();
78        $groups  = new Collection([$group]);
79        $rules   = new Collection([$rule]);
80
81        // expected calls:
82        $userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($this->user());
83        $ruleRepos->shouldReceive('setUser')->atLeast()->once();
84        $ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
85        $accountRepos->shouldReceive('setUser')->atLeast()->once();
86        $journalRepos->shouldReceive('setUser')->atLeast()->once();
87        $accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([1])->andReturn($asset);
88        $journalRepos->shouldReceive('firstNull')->atLeast()->once()->andReturn($journal);
89
90        $ruleGroupRepos->shouldReceive('getActiveGroups')->atLeast()->once()->andReturn($groups);
91        $ruleGroupRepos->shouldReceive('getActiveStoreRules')->atLeast()->once()->andReturn($rules);
92
93
94        $collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
95        $collector->shouldReceive('setAccounts')->atLeast()->once()->andReturnSelf();
96        $collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
97        $collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([[], [], []]);
98
99        $ruleEngine->shouldReceive('setUser')->atLeast()->once();
100        $ruleEngine->shouldReceive('setRulesToApply')->atLeast()->once();
101        $ruleEngine->shouldReceive('processJournalArray')->times(3);
102        $ruleEngine->shouldReceive('setTriggerMode')->atLeast()->once()->withArgs([RuleEngine::TRIGGER_STORE]);
103
104        $parameters = [
105            '--user=1',
106            '--token=token',
107            '--accounts=1',
108            '--all_rules',
109        ];
110
111        // mock Preferences Facade:
112        $pref = new Preference;
113        $pref->data = 'token';
114        Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token',null])->atLeast()->once()->andReturn($pref);
115
116        $this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
117             ->expectsOutput('Will apply 1 rule(s) to 3 transaction(s).')
118             ->expectsOutput('Done!')
119             ->assertExitCode(0);
120
121        // this method changes no objects so there is nothing to verify.
122    }
123
124    /**
125     * Basic call with everything perfect (and ALL rules), but no rules will be selected.
126     *
127     * @covers \FireflyIII\Console\Commands\Tools\ApplyRules
128     */
129    public function testHandEmpty(): void
130    {
131        $ruleRepos      = $this->mock(RuleRepositoryInterface::class);
132        $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
133        $journalRepos   = $this->mock(JournalRepositoryInterface::class);
134        $collector      = $this->mock(GroupCollectorInterface::class);
135        $accountRepos   = $this->mock(AccountRepositoryInterface::class);
136        $ruleEngine     = $this->mock(RuleEngine::class);
137        $userRepos      = $this->mock(UserRepositoryInterface::class);
138
139        // data
140        $asset   = $this->getRandomAsset();
141        $journal = $this->getRandomWithdrawal();
142        $group   = $this->user()->ruleGroups()->first();
143        $groups  = new Collection([$group]);
144
145        // expected calls:
146        $userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($this->user());
147        $ruleRepos->shouldReceive('setUser')->atLeast()->once();
148        $ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
149        $accountRepos->shouldReceive('setUser')->atLeast()->once();
150        $journalRepos->shouldReceive('setUser')->atLeast()->once();
151        $accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([1])->andReturn($asset);
152        $journalRepos->shouldReceive('firstNull')->atLeast()->once()->andReturn($journal);
153
154        $ruleGroupRepos->shouldReceive('getActiveGroups')->atLeast()->once()->andReturn($groups);
155        $ruleGroupRepos->shouldReceive('getActiveStoreRules')->atLeast()->once()->andReturn(new Collection);
156
157
158        $collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
159        $collector->shouldReceive('setAccounts')->atLeast()->once()->andReturnSelf();
160        $collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
161        $collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([[], [], []]);
162
163        $ruleEngine->shouldReceive('setUser')->atLeast()->once();
164        $ruleEngine->shouldReceive('setRulesToApply')->atLeast()->once();
165        $ruleEngine->shouldReceive('processJournalArray')->times(3);
166        $ruleEngine->shouldReceive('setTriggerMode')->atLeast()->once()->withArgs([RuleEngine::TRIGGER_STORE]);
167
168        $parameters = [
169            '--user=1',
170            '--token=token',
171            '--accounts=1',
172            '--all_rules',
173        ];
174
175        // mock Preferences Facade:
176        $pref = new Preference;
177        $pref->data = 'token';
178        Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token',null])->atLeast()->once()->andReturn($pref);
179
180        $this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
181             ->expectsOutput('No rules or rule groups have been included.')
182             ->expectsOutput('Done!')
183             ->assertExitCode(0);
184    }
185
186
187    /**
188     * Basic call with everything perfect (and ALL rules) and dates.
189     *
190     * @covers \FireflyIII\Console\Commands\Tools\ApplyRules
191     */
192    public function testHandleDate(): void
193    {
194        $ruleRepos      = $this->mock(RuleRepositoryInterface::class);
195        $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
196        $this->mock(JournalRepositoryInterface::class);
197        $collector    = $this->mock(GroupCollectorInterface::class);
198        $accountRepos = $this->mock(AccountRepositoryInterface::class);
199        $ruleEngine   = $this->mock(RuleEngine::class);
200        $userRepos    = $this->mock(UserRepositoryInterface::class);
201
202        // data
203        $asset  = $this->getRandomAsset();
204        $group  = $this->user()->ruleGroups()->first();
205        $rule   = $this->user()->rules()->first();
206        $groups = new Collection([$group]);
207        $rules  = new Collection([$rule]);
208
209        // expected calls:
210        $userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($this->user());
211        $ruleRepos->shouldReceive('setUser')->atLeast()->once();
212        $ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
213        $accountRepos->shouldReceive('setUser')->atLeast()->once();
214        $accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([1])->andReturn($asset);
215
216        $ruleGroupRepos->shouldReceive('getActiveGroups')->atLeast()->once()->andReturn($groups);
217        $ruleGroupRepos->shouldReceive('getActiveStoreRules')->atLeast()->once()->andReturn($rules);
218
219
220        $collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
221        $collector->shouldReceive('setAccounts')->atLeast()->once()->andReturnSelf();
222        $collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
223        $collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([[], [], []]);
224
225        $ruleEngine->shouldReceive('setUser')->atLeast()->once();
226        $ruleEngine->shouldReceive('setRulesToApply')->atLeast()->once();
227        $ruleEngine->shouldReceive('processJournalArray')->times(3);
228        $ruleEngine->shouldReceive('setTriggerMode')->atLeast()->once()->withArgs([RuleEngine::TRIGGER_STORE]);
229
230        $parameters = [
231            '--user=1',
232            '--token=token',
233            '--accounts=1',
234            '--all_rules',
235            '--start_date=2019-01-31',
236            '--end_date=2019-01-01',
237        ];
238
239        // mock Preferences Facade:
240        $pref = new Preference;
241        $pref->data = 'token';
242        Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token',null])->atLeast()->once()->andReturn($pref);
243
244        $this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
245             ->expectsOutput('Will apply 1 rule(s) to 3 transaction(s).')
246             ->expectsOutput('Done!')
247             ->assertExitCode(0);
248    }
249
250    /**
251     * Will submit some rules to apply.
252     *
253     * @covers \FireflyIII\Console\Commands\Tools\ApplyRules
254     */
255    public function testHandleRules(): void
256    {
257        $ruleRepos      = $this->mock(RuleRepositoryInterface::class);
258        $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
259        $journalRepos   = $this->mock(JournalRepositoryInterface::class);
260        $collector      = $this->mock(GroupCollectorInterface::class);
261        $accountRepos   = $this->mock(AccountRepositoryInterface::class);
262        $ruleEngine     = $this->mock(RuleEngine::class);
263        $userRepos      = $this->mock(UserRepositoryInterface::class);
264
265        // data
266        $asset        = $this->getRandomAsset();
267        $journal      = $this->getRandomWithdrawal();
268        $group        = $this->user()->ruleGroups()->first();
269        $groups       = new Collection([$group]);
270        $activeRule   = $this->user()->rules()->where('active', 1)->inRandomOrder()->first();
271        $inactiveRule = $this->user()->rules()->where('active', 0)->inRandomOrder()->first();
272        $rules        = new Collection([$activeRule]);
273
274        // expected calls:
275        $userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($this->user());
276        $ruleRepos->shouldReceive('setUser')->atLeast()->once();
277        $ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
278        $accountRepos->shouldReceive('setUser')->atLeast()->once();
279        $journalRepos->shouldReceive('setUser')->atLeast()->once();
280        $accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([1])->andReturn($asset);
281        $journalRepos->shouldReceive('firstNull')->atLeast()->once()->andReturn($journal);
282
283        $ruleRepos->shouldReceive('find')->atLeast()->once()->withArgs([$activeRule->id])->andReturn($activeRule);
284        $ruleRepos->shouldReceive('find')->atLeast()->once()->withArgs([$inactiveRule->id])->andReturn($inactiveRule);
285
286        $ruleGroupRepos->shouldReceive('getActiveGroups')->atLeast()->once()->andReturn($groups);
287        $ruleGroupRepos->shouldReceive('getActiveStoreRules')->atLeast()->once()->andReturn($rules);
288
289
290        $collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
291        $collector->shouldReceive('setAccounts')->atLeast()->once()->andReturnSelf();
292        $collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
293        $collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([[], [], []]);
294
295        $ruleEngine->shouldReceive('setUser')->atLeast()->once();
296        $ruleEngine->shouldReceive('setRulesToApply')->atLeast()->once();
297        $ruleEngine->shouldReceive('processJournalArray')->times(3);
298        $ruleEngine->shouldReceive('setTriggerMode')->atLeast()->once()->withArgs([RuleEngine::TRIGGER_STORE]);
299
300        $parameters = [
301            '--user=1',
302            '--token=token',
303            '--accounts=1',
304            sprintf('--rules=%d,%d', $activeRule->id, $inactiveRule->id),
305        ];
306
307        // mock Preferences Facade:
308        $pref = new Preference;
309        $pref->data = 'token';
310        Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token',null])->atLeast()->once()->andReturn($pref);
311
312        $this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
313             ->expectsOutput('Will apply 1 rule(s) to 3 transaction(s).')
314             ->expectsOutput('Done!')
315             ->assertExitCode(0);
316    }
317
318    /**
319     * Basic call with two rule groups. One active, one inactive.
320     *
321     * @covers \FireflyIII\Console\Commands\Tools\ApplyRules
322     */
323    public function testHandleRuleGroups(): void
324    {
325        $ruleRepos      = $this->mock(RuleRepositoryInterface::class);
326        $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
327        $journalRepos   = $this->mock(JournalRepositoryInterface::class);
328        $collector      = $this->mock(GroupCollectorInterface::class);
329        $accountRepos   = $this->mock(AccountRepositoryInterface::class);
330        $ruleEngine     = $this->mock(RuleEngine::class);
331        $userRepos      = $this->mock(UserRepositoryInterface::class);
332
333        $activeGroup   = $this->user()->ruleGroups()->where('active', 1)->inRandomOrder()->first();
334        $inactiveGroup = $this->user()->ruleGroups()->where('active', 0)->inRandomOrder()->first();
335
336        // data
337        $asset   = $this->getRandomAsset();
338        $journal = $this->getRandomWithdrawal();
339        $rule    = $this->user()->rules()->first();
340        $groups  = new Collection([$activeGroup]);
341        $rules   = new Collection([$rule]);
342
343        // expected calls:
344        $userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($this->user());
345        $ruleRepos->shouldReceive('setUser')->atLeast()->once();
346        $ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
347        $accountRepos->shouldReceive('setUser')->atLeast()->once();
348        $journalRepos->shouldReceive('setUser')->atLeast()->once();
349        $accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([1])->andReturn($asset);
350        $journalRepos->shouldReceive('firstNull')->atLeast()->once()->andReturn($journal);
351
352        $ruleGroupRepos->shouldReceive('getActiveGroups')->atLeast()->once()->andReturn($groups);
353        $ruleGroupRepos->shouldReceive('getActiveStoreRules')->atLeast()->once()->andReturn($rules);
354        $ruleGroupRepos->shouldReceive('find')->atLeast()->once()->withArgs([$activeGroup->id])->andReturn($activeGroup);
355        $ruleGroupRepos->shouldReceive('find')->atLeast()->once()->withArgs([$inactiveGroup->id])->andReturn($inactiveGroup);
356
357        $collector->shouldReceive('setUser')->atLeast()->once()->andReturnSelf();
358        $collector->shouldReceive('setAccounts')->atLeast()->once()->andReturnSelf();
359        $collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
360        $collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn([[], [], []]);
361
362        $ruleEngine->shouldReceive('setUser')->atLeast()->once();
363        $ruleEngine->shouldReceive('setRulesToApply')->atLeast()->once();
364        $ruleEngine->shouldReceive('processJournalArray')->times(3);
365        $ruleEngine->shouldReceive('setTriggerMode')->atLeast()->once()->withArgs([RuleEngine::TRIGGER_STORE]);
366
367        $parameters = [
368            '--user=1',
369            '--token=token',
370            '--accounts=1',
371            sprintf('--rule_groups=%d,%d', $activeGroup->id, $inactiveGroup->id),
372        ];
373
374        // mock Preferences Facade:
375        $pref = new Preference;
376        $pref->data = 'token';
377        Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token',null])->atLeast()->once()->andReturn($pref);
378
379        $this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
380             ->expectsOutput(sprintf('Will ignore inactive rule group #%d ("%s")', $inactiveGroup->id, $inactiveGroup->title))
381            // one rule out of 2 groups:
382             ->expectsOutput('Will apply 1 rule(s) to 3 transaction(s).')
383             ->expectsOutput('Done!')
384             ->assertExitCode(0);
385    }
386
387    /**
388     * Basic call but no accounts submitted.
389     *
390     * @covers \FireflyIII\Console\Commands\Tools\ApplyRules
391     */
392    public function testHandleNoAccounts(): void
393    {
394        $ruleRepos      = $this->mock(RuleRepositoryInterface::class);
395        $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
396        $this->mock(JournalRepositoryInterface::class);
397        $this->mock(GroupCollectorInterface::class);
398        $this->mock(AccountRepositoryInterface::class);
399        $this->mock(RuleEngine::class);
400        $userRepos = $this->mock(UserRepositoryInterface::class);
401
402        // expected calls:
403        $userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($this->user());
404        $ruleRepos->shouldReceive('setUser')->atLeast()->once();
405        $ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
406
407
408        $parameters = [
409            '--user=1',
410            '--token=token',
411            '--accounts=',
412            '--all_rules',
413        ];
414
415        // mock Preferences Facade:
416        $pref = new Preference;
417        $pref->data = 'token';
418        Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token',null])->atLeast()->once()->andReturn($pref);
419
420        $this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
421             ->expectsOutput('Please use the --accounts option to indicate the accounts to apply rules to.')
422             ->assertExitCode(1);
423    }
424
425    /**
426     * Basic call but only one expense account submitted
427     *
428     * @covers \FireflyIII\Console\Commands\Tools\ApplyRules
429     */
430    public function testHandleExpenseAccounts(): void
431    {
432        $ruleRepos      = $this->mock(RuleRepositoryInterface::class);
433        $ruleGroupRepos = $this->mock(RuleGroupRepositoryInterface::class);
434        $accountRepos   = $this->mock(AccountRepositoryInterface::class);
435        $userRepos      = $this->mock(UserRepositoryInterface::class);
436        $this->mock(RuleEngine::class);
437        $this->mock(JournalRepositoryInterface::class);
438        $this->mock(GroupCollectorInterface::class);
439
440        // data
441        $expense = $this->getRandomExpense();
442
443        // expected calls:
444        $userRepos->shouldReceive('findNull')->atLeast()->once()->andReturn($this->user());
445        $ruleRepos->shouldReceive('setUser')->atLeast()->once();
446        $ruleGroupRepos->shouldReceive('setUser')->atLeast()->once();
447        $accountRepos->shouldReceive('setUser')->atLeast()->once();
448        $accountRepos->shouldReceive('findNull')->atLeast()->once()->withArgs([$expense->id])->andReturn($expense);
449
450
451        $parameters = [
452            '--user=1',
453            '--token=token',
454            '--accounts=' . $expense->id,
455            '--all_rules',
456        ];
457
458        // mock Preferences Facade:
459        $pref = new Preference;
460        $pref->data = 'token';
461        Preferences::shouldReceive('getForUser')->withArgs([Mockery::any(), 'access_token',null])->atLeast()->once()->andReturn($pref);
462
463        $this->artisan('firefly-iii:apply-rules ' . implode(' ', $parameters))
464             ->expectsOutput('Please make sure all accounts in --accounts are asset accounts or liabilities.')
465             ->assertExitCode(1);
466    }
467
468}
469
Full Screen

IndexControllerTest.php

Source: IndexControllerTest.php Github

copy
1<?php
2/**
3 * IndexControllerTest.php
4 * Copyright (c) 2019 james@firefly-iii.org
5 *
6 * This file is part of Firefly III (https://github.com/firefly-iii).
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21
22declare(strict_types=1);
23
24namespace Tests\Feature\Controllers\Transaction;
25
26
27use Amount;
28use FireflyIII\Helpers\Collector\GroupCollectorInterface;
29use FireflyIII\Models\Preference;
30use FireflyIII\Repositories\User\UserRepositoryInterface;
31use Illuminate\Pagination\LengthAwarePaginator;
32use Log;
33use Mockery;
34use Preferences;
35use Tests\TestCase;
36
37/**
38 * Class IndexControllerTest
39 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
40 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
41 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
42 */
43class IndexControllerTest extends TestCase
44{
45
46    /**
47     *
48     */
49    public function setUp(): void
50    {
51        parent::setUp();
52        Log::info(sprintf('Now in %s.', get_class($this)));
53    }
54
55    /**
56     * @covers \FireflyIII\Http\Controllers\Transaction\IndexController
57     */
58    public function testIndex(): void
59    {
60        $this->mockDefaultSession();
61        $group     = $this->getRandomWithdrawalGroup();
62        $userRepos = $this->mock(UserRepositoryInterface::class);
63        $collector = $this->mock(GroupCollectorInterface::class);
64
65        // generic set for the info blocks:
66        $groupArray = [
67            $this->getRandomWithdrawalAsArray(),
68            $this->getRandomDepositAsArray(),
69            $this->getRandomTransferAsArray(),
70        ];
71
72        // role?
73        $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true);
74
75        // make paginator.
76        $paginator = new LengthAwarePaginator([$group], 1, 40, 1);
77        Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('10');
78
79        $collector->shouldReceive('setTypes')->atLeast()->once()->andReturnSelf();
80        $collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
81        $collector->shouldReceive('setLimit')->atLeast()->once()->andReturnSelf();
82        $collector->shouldReceive('setPage')->atLeast()->once()->andReturnSelf();
83        $collector->shouldReceive('withBudgetInformation')->atLeast()->once()->andReturnSelf();
84        $collector->shouldReceive('withCategoryInformation')->atLeast()->once()->andReturnSelf();
85        $collector->shouldReceive('withAccountInformation')->atLeast()->once()->andReturnSelf();
86        $collector->shouldReceive('getPaginatedGroups')->atLeast()->once()->andReturn($paginator);
87        $collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn($groupArray);
88
89
90        $pref       = new Preference;
91        $pref->data = 50;
92        Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
93        Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
94
95        $this->be($this->user());
96        $response = $this->get(route('transactions.index', ['withdrawal']));
97        $response->assertStatus(200);
98    }
99
100    /**
101     * @covers \FireflyIII\Http\Controllers\Transaction\IndexController
102     */
103    public function testIndexDeposit(): void
104    {
105        $this->mockDefaultSession();
106        $group     = $this->getRandomWithdrawalGroup();
107        $userRepos = $this->mock(UserRepositoryInterface::class);
108        $collector = $this->mock(GroupCollectorInterface::class);
109
110        // generic set for the info blocks:
111        $groupArray = [
112            $this->getRandomWithdrawalAsArray(),
113            $this->getRandomDepositAsArray(),
114            $this->getRandomTransferAsArray(),
115        ];
116
117        // role?
118        $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true);
119
120        // make paginator.
121        $paginator = new LengthAwarePaginator([$group], 1, 40, 1);
122        Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('10');
123
124        $collector->shouldReceive('setTypes')->atLeast()->once()->andReturnSelf();
125        $collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
126        $collector->shouldReceive('setLimit')->atLeast()->once()->andReturnSelf();
127        $collector->shouldReceive('setPage')->atLeast()->once()->andReturnSelf();
128        $collector->shouldReceive('withBudgetInformation')->atLeast()->once()->andReturnSelf();
129        $collector->shouldReceive('withCategoryInformation')->atLeast()->once()->andReturnSelf();
130        $collector->shouldReceive('withAccountInformation')->atLeast()->once()->andReturnSelf();
131        $collector->shouldReceive('getPaginatedGroups')->atLeast()->once()->andReturn($paginator);
132        $collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn($groupArray);
133
134
135        $pref       = new Preference;
136        $pref->data = 50;
137        Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
138        Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
139
140        $this->be($this->user());
141        $response = $this->get(route('transactions.index', ['deposit']));
142        $response->assertStatus(200);
143    }
144
145    /**
146     * @covers \FireflyIII\Http\Controllers\Transaction\IndexController
147     */
148    public function testIndexTransfers(): void
149    {
150        $this->mockDefaultSession();
151        $group     = $this->getRandomWithdrawalGroup();
152        $userRepos = $this->mock(UserRepositoryInterface::class);
153        $collector = $this->mock(GroupCollectorInterface::class);
154
155        // generic set for the info blocks:
156        $groupArray = [
157            $this->getRandomWithdrawalAsArray(),
158            $this->getRandomDepositAsArray(),
159            $this->getRandomTransferAsArray(),
160        ];
161
162        // role?
163        $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true);
164
165        // make paginator.
166        $paginator = new LengthAwarePaginator([$group], 1, 40, 1);
167        Amount::shouldReceive('formatAnything')->atLeast()->once()->andReturn('10');
168
169        $collector->shouldReceive('setTypes')->atLeast()->once()->andReturnSelf();
170        $collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
171        $collector->shouldReceive('setLimit')->atLeast()->once()->andReturnSelf();
172        $collector->shouldReceive('setPage')->atLeast()->once()->andReturnSelf();
173        $collector->shouldReceive('withBudgetInformation')->atLeast()->once()->andReturnSelf();
174        $collector->shouldReceive('withCategoryInformation')->atLeast()->once()->andReturnSelf();
175        $collector->shouldReceive('withAccountInformation')->atLeast()->once()->andReturnSelf();
176        $collector->shouldReceive('getPaginatedGroups')->atLeast()->once()->andReturn($paginator);
177        $collector->shouldReceive('getExtractedJournals')->atLeast()->once()->andReturn($groupArray);
178
179
180        $pref       = new Preference;
181        $pref->data = 50;
182        Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
183        Preferences::shouldReceive('lastActivity')->atLeast()->once()->andReturn('md512345');
184
185        $this->be($this->user());
186        $response = $this->get(route('transactions.index', ['transfers']));
187        $response->assertStatus(200);
188    }
189
190
191    /**
192     * @covers \FireflyIII\Http\Controllers\Transaction\IndexController
193     */
194    public function testIndexAll(): void
195    {
196        $this->mockDefaultSession();
197        $group     = $this->getRandomWithdrawalGroup();
198        $userRepos = $this->mock(UserRepositoryInterface::class);
199        $collector = $this->mock(GroupCollectorInterface::class);
200
201        // role?
202        $userRepos->shouldReceive('hasRole')->withArgs([Mockery::any(), 'owner'])->andReturn(true);
203
204        // make paginator.
205        $paginator = new LengthAwarePaginator([$group], 1, 40, 1);
206
207        $collector->shouldReceive('setTypes')->atLeast()->once()->andReturnSelf();
208        $collector->shouldReceive('setRange')->atLeast()->once()->andReturnSelf();
209        $collector->shouldReceive('setLimit')->atLeast()->once()->andReturnSelf();
210        $collector->shouldReceive('setPage')->atLeast()->once()->andReturnSelf();
211        $collector->shouldReceive('withBudgetInformation')->atLeast()->once()->andReturnSelf();
212        $collector->shouldReceive('withCategoryInformation')->atLeast()->once()->andReturnSelf();
213        $collector->shouldReceive('withAccountInformation')->atLeast()->once()->andReturnSelf();
214        $collector->shouldReceive('getPaginatedGroups')->atLeast()->once()->andReturn($paginator);
215
216        $pref       = new Preference;
217        $pref->data = 50;
218        Preferences::shouldReceive('get')->withArgs(['listPageSize', 50])->atLeast()->once()->andReturn($pref);
219
220        $this->be($this->user());
221        $response = $this->get(route('transactions.index.all', ['withdrawal']));
222        $response->assertStatus(200);
223    }
224
225}
226
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in AtLeast

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)