mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 02:36:28 +00:00 
			
		
		
		
	Expand factory tests.
This commit is contained in:
		
							
								
								
									
										396
									
								
								tests/Unit/Factory/AccountFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										396
									
								
								tests/Unit/Factory/AccountFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,396 @@ | ||||
| <?php | ||||
| /** | ||||
|  * AccountFactoryTest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Tests\Unit\Factory; | ||||
|  | ||||
|  | ||||
| use Carbon\Carbon; | ||||
| use FireflyIII\Factory\AccountFactory; | ||||
| use FireflyIII\Models\AccountType; | ||||
| use Tests\TestCase; | ||||
|  | ||||
| /** | ||||
|  * Class AccountFactoryTest | ||||
|  */ | ||||
| class AccountFactoryTest extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * Test minimal set of data to make factory work (asset account). | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\AccountFactory | ||||
|      * @covers \FireflyIII\Factory\AccountMetaFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasic() | ||||
|     { | ||||
|  | ||||
|         $data = [ | ||||
|             'account_type_id' => null, | ||||
|             'accountType'     => 'asset', | ||||
|             'iban'            => null, | ||||
|             'name'            => 'Basic asset account #' . rand(1, 1000), | ||||
|             'virtualBalance'  => null, | ||||
|             'active'          => true, | ||||
|             'accountRole'     => 'defaultAsset', | ||||
|         ]; | ||||
|  | ||||
|         /** @var AccountFactory $factory */ | ||||
|         $factory = app(AccountFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $account = $factory->create($data); | ||||
|  | ||||
|         // assert stuff about account: | ||||
|         $this->assertEquals($account->name, $data['name']); | ||||
|         $this->assertEquals(AccountType::ASSET, $account->accountType->type); | ||||
|         $this->assertEquals('', $account->iban); | ||||
|         $this->assertTrue($account->active); | ||||
|         $this->assertEquals('0', $account->virtual_balance); | ||||
|         $this->assertEquals('defaultAsset', $account->getMeta('accountRole')); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Test creation of CC asset. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\AccountFactory | ||||
|      * @covers \FireflyIII\Factory\AccountMetaFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasicCC() | ||||
|     { | ||||
|  | ||||
|         $data = [ | ||||
|             'account_type_id'      => null, | ||||
|             'accountType'          => 'asset', | ||||
|             'iban'                 => null, | ||||
|             'name'                 => 'Basic CC account #' . rand(1, 1000), | ||||
|             'virtualBalance'       => null, | ||||
|             'active'               => true, | ||||
|             'accountRole'          => 'ccAsset', | ||||
|             'ccMonthlyPaymentDate' => '2018-01-01', | ||||
|         ]; | ||||
|  | ||||
|         /** @var AccountFactory $factory */ | ||||
|         $factory = app(AccountFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $account = $factory->create($data); | ||||
|  | ||||
|         // assert stuff about account: | ||||
|         $this->assertEquals($account->name, $data['name']); | ||||
|         $this->assertEquals(AccountType::ASSET, $account->accountType->type); | ||||
|         $this->assertEquals('', $account->iban); | ||||
|         $this->assertTrue($account->active); | ||||
|         $this->assertEquals('0', $account->virtual_balance); | ||||
|         $this->assertEquals('ccAsset', $account->getMeta('accountRole')); | ||||
|         $this->assertEquals('2018-01-01', $account->getMeta('ccMonthlyPaymentDate')); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Create an expense account. This overrules the virtual balance. | ||||
|      * Role should not be set. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\AccountFactory | ||||
|      * @covers \FireflyIII\Factory\AccountMetaFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasicExpense() | ||||
|     { | ||||
|  | ||||
|         $data = [ | ||||
|             'account_type_id' => null, | ||||
|             'accountType'     => 'expense', | ||||
|             'iban'            => null, | ||||
|             'name'            => 'Basic expense account #' . rand(1, 1000), | ||||
|             'virtualBalance'  => '1243', | ||||
|             'active'          => true, | ||||
|             'accountRole'     => 'defaultAsset', | ||||
|         ]; | ||||
|  | ||||
|         /** @var AccountFactory $factory */ | ||||
|         $factory = app(AccountFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $account = $factory->create($data); | ||||
|  | ||||
|         // assert stuff about account: | ||||
|         $this->assertEquals($account->name, $data['name']); | ||||
|         $this->assertEquals(AccountType::EXPENSE, $account->accountType->type); | ||||
|         $this->assertEquals('', $account->iban); | ||||
|         $this->assertTrue($account->active); | ||||
|         $this->assertEquals('0', $account->virtual_balance); | ||||
|         $this->assertEquals('', $account->getMeta('accountRole')); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Submit IB data for asset account. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\AccountFactory | ||||
|      * @covers \FireflyIII\Factory\AccountMetaFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasicIB() | ||||
|     { | ||||
|  | ||||
|         $data = [ | ||||
|             'account_type_id'    => null, | ||||
|             'accountType'        => 'asset', | ||||
|             'iban'               => null, | ||||
|             'name'               => 'Basic asset account #' . rand(1, 1000), | ||||
|             'virtualBalance'     => null, | ||||
|             'active'             => true, | ||||
|             'accountRole'        => 'defaultAsset', | ||||
|             'openingBalance'     => '100', | ||||
|             'openingBalanceDate' => new Carbon('2018-01-01'), | ||||
|             'currency_id'        => 1, | ||||
|         ]; | ||||
|  | ||||
|         /** @var AccountFactory $factory */ | ||||
|         $factory = app(AccountFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $account = $factory->create($data); | ||||
|  | ||||
|         // assert stuff about account: | ||||
|         $this->assertEquals($account->name, $data['name']); | ||||
|         $this->assertEquals(AccountType::ASSET, $account->accountType->type); | ||||
|         $this->assertEquals('', $account->iban); | ||||
|         $this->assertTrue($account->active); | ||||
|         $this->assertEquals('0', $account->virtual_balance); | ||||
|         $this->assertEquals('defaultAsset', $account->getMeta('accountRole')); | ||||
|  | ||||
|         // find opening balance: | ||||
|         $this->assertEquals(1, $account->transactions()->count()); | ||||
|         $this->assertEquals(100, floatval($account->transactions()->first()->amount)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Submit empty (amount = 0) IB data for asset account. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\AccountFactory | ||||
|      * @covers \FireflyIII\Factory\AccountMetaFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasicIBZero() | ||||
|     { | ||||
|  | ||||
|         $data = [ | ||||
|             'account_type_id'    => null, | ||||
|             'accountType'        => 'asset', | ||||
|             'iban'               => null, | ||||
|             'name'               => 'Basic asset account #' . rand(1, 1000), | ||||
|             'virtualBalance'     => null, | ||||
|             'active'             => true, | ||||
|             'accountRole'        => 'defaultAsset', | ||||
|             'openingBalance'     => '0.0', | ||||
|             'openingBalanceDate' => new Carbon('2018-01-01'), | ||||
|             'currency_id'        => 1, | ||||
|         ]; | ||||
|  | ||||
|         /** @var AccountFactory $factory */ | ||||
|         $factory = app(AccountFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $account = $factory->create($data); | ||||
|  | ||||
|         // assert stuff about account: | ||||
|         $this->assertEquals($account->name, $data['name']); | ||||
|         $this->assertEquals(AccountType::ASSET, $account->accountType->type); | ||||
|         $this->assertEquals('', $account->iban); | ||||
|         $this->assertTrue($account->active); | ||||
|         $this->assertEquals('0', $account->virtual_balance); | ||||
|         $this->assertEquals('defaultAsset', $account->getMeta('accountRole')); | ||||
|  | ||||
|         // find opening balance: | ||||
|         $this->assertEquals(0, $account->transactions()->count()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Add valid IBAN. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\AccountFactory | ||||
|      * @covers \FireflyIII\Factory\AccountMetaFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasicIban() | ||||
|     { | ||||
|  | ||||
|         $data = [ | ||||
|             'account_type_id' => null, | ||||
|             'accountType'     => 'asset', | ||||
|             'iban'            => 'NL18RABO0326747238', | ||||
|             'name'            => 'Basic asset account #' . rand(1, 1000), | ||||
|             'virtualBalance'  => null, | ||||
|             'active'          => true, | ||||
|             'accountRole'     => 'defaultAsset', | ||||
|         ]; | ||||
|  | ||||
|         /** @var AccountFactory $factory */ | ||||
|         $factory = app(AccountFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $account = $factory->create($data); | ||||
|  | ||||
|         // assert stuff about account: | ||||
|         $this->assertEquals($account->name, $data['name']); | ||||
|         $this->assertEquals(AccountType::ASSET, $account->accountType->type); | ||||
|         $this->assertEquals('NL18RABO0326747238', $account->iban); | ||||
|         $this->assertTrue($account->active); | ||||
|         $this->assertEquals('0', $account->virtual_balance); | ||||
|         $this->assertEquals('defaultAsset', $account->getMeta('accountRole')); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Add invalid IBAN. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\AccountFactory | ||||
|      * @covers \FireflyIII\Factory\AccountMetaFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasicInvalidIban() | ||||
|     { | ||||
|  | ||||
|         $data = [ | ||||
|             'account_type_id' => null, | ||||
|             'accountType'     => 'asset', | ||||
|             'iban'            => 'NL1XRABO032674X238', | ||||
|             'name'            => 'Basic asset account #' . rand(1, 1000), | ||||
|             'virtualBalance'  => null, | ||||
|             'active'          => true, | ||||
|             'accountRole'     => 'defaultAsset', | ||||
|         ]; | ||||
|  | ||||
|         /** @var AccountFactory $factory */ | ||||
|         $factory = app(AccountFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $account = $factory->create($data); | ||||
|  | ||||
|         // assert stuff about account: | ||||
|         $this->assertEquals($account->name, $data['name']); | ||||
|         $this->assertEquals(AccountType::ASSET, $account->accountType->type); | ||||
|         $this->assertEquals('', $account->iban); | ||||
|         $this->assertTrue($account->active); | ||||
|         $this->assertEquals('0', $account->virtual_balance); | ||||
|         $this->assertEquals('defaultAsset', $account->getMeta('accountRole')); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Submit IB data for asset account. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\AccountFactory | ||||
|      * @covers \FireflyIII\Factory\AccountMetaFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasicNegativeIB() | ||||
|     { | ||||
|  | ||||
|         $data = [ | ||||
|             'account_type_id'    => null, | ||||
|             'accountType'        => 'asset', | ||||
|             'iban'               => null, | ||||
|             'name'               => 'Basic asset account #' . rand(1, 1000), | ||||
|             'virtualBalance'     => null, | ||||
|             'active'             => true, | ||||
|             'accountRole'        => 'defaultAsset', | ||||
|             'openingBalance'     => '-100', | ||||
|             'openingBalanceDate' => new Carbon('2018-01-01'), | ||||
|             'currency_id'        => 1, | ||||
|         ]; | ||||
|  | ||||
|         /** @var AccountFactory $factory */ | ||||
|         $factory = app(AccountFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $account = $factory->create($data); | ||||
|  | ||||
|         // assert stuff about account: | ||||
|         $this->assertEquals($account->name, $data['name']); | ||||
|         $this->assertEquals(AccountType::ASSET, $account->accountType->type); | ||||
|         $this->assertEquals('', $account->iban); | ||||
|         $this->assertTrue($account->active); | ||||
|         $this->assertEquals('0', $account->virtual_balance); | ||||
|         $this->assertEquals('defaultAsset', $account->getMeta('accountRole')); | ||||
|  | ||||
|         // find opening balance: | ||||
|         $this->assertEquals(1, $account->transactions()->count()); | ||||
|         $this->assertEquals(-100, floatval($account->transactions()->first()->amount)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Add some notes to asset account. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\AccountFactory | ||||
|      * @covers \FireflyIII\Factory\AccountMetaFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\AccountServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasicNotes() | ||||
|     { | ||||
|  | ||||
|         $data = [ | ||||
|             'account_type_id' => null, | ||||
|             'accountType'     => 'asset', | ||||
|             'iban'            => null, | ||||
|             'name'            => 'Basic asset account #' . rand(1, 1000), | ||||
|             'virtualBalance'  => null, | ||||
|             'active'          => true, | ||||
|             'accountRole'     => 'defaultAsset', | ||||
|             'notes'           => 'Hello!', | ||||
|         ]; | ||||
|  | ||||
|         /** @var AccountFactory $factory */ | ||||
|         $factory = app(AccountFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $account = $factory->create($data); | ||||
|  | ||||
|         // assert stuff about account: | ||||
|         $this->assertEquals($account->name, $data['name']); | ||||
|         $this->assertEquals(AccountType::ASSET, $account->accountType->type); | ||||
|         $this->assertEquals('', $account->iban); | ||||
|         $this->assertTrue($account->active); | ||||
|         $this->assertEquals('0', $account->virtual_balance); | ||||
|         $this->assertEquals('defaultAsset', $account->getMeta('accountRole')); | ||||
|         $note = $account->notes()->first(); | ||||
|         $this->assertEquals('Hello!', $note->text); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Should return existing account. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\AccountFactory | ||||
|      */ | ||||
|     public function testCreateExisting() | ||||
|     { | ||||
|         $existing = $this->user()->accounts()->where('account_type_id', 3)->first(); | ||||
|         $data     = [ | ||||
|             'account_type_id' => null, | ||||
|             'accountType'     => 'asset', | ||||
|             'name'            => $existing->name, | ||||
|             'virtualBalance'  => null, | ||||
|             'iban'            => null, | ||||
|             'active'          => true, | ||||
|             'accountRole'     => 'defaultAsset', | ||||
|         ]; | ||||
|  | ||||
|         /** @var AccountFactory $factory */ | ||||
|         $factory = app(AccountFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $account = $factory->create($data); | ||||
|  | ||||
|         // assert stuff about account: | ||||
|         $this->assertEquals($account->id, $existing->id); | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										134
									
								
								tests/Unit/Factory/BillFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										134
									
								
								tests/Unit/Factory/BillFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,134 @@ | ||||
| <?php | ||||
| /** | ||||
|  * BillFactoryTest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Tests\Unit\Factory; | ||||
|  | ||||
|  | ||||
| use FireflyIII\Factory\BillFactory; | ||||
| use Tests\TestCase; | ||||
|  | ||||
| /** | ||||
|  * Class BillFactoryTest | ||||
|  */ | ||||
| class BillFactoryTest extends TestCase | ||||
| { | ||||
|  | ||||
|     /** | ||||
|      * Create basic bill with minimum data. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\BillFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\BillServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasic() | ||||
|     { | ||||
|         $data = [ | ||||
|             'name'        => 'Some new bill #' . rand(1, 1000), | ||||
|             'match'       => 'i,am,word' . rand(1, 1000), | ||||
|             'amount_min'  => '5', | ||||
|             'amount_max'  => '10', | ||||
|             'date'        => '2018-01-01', | ||||
|             'repeat_freq' => 'monthly', | ||||
|             'skip'        => 0, | ||||
|             'automatch'   => true, | ||||
|             'active'      => true, | ||||
|             'notes'       => 'Hello!', | ||||
|         ]; | ||||
|  | ||||
|         /** @var BillFactory $factory */ | ||||
|         $factory = app(BillFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $bill = $factory->create($data); | ||||
|  | ||||
|         $this->assertEquals($data['name'], $bill->name); | ||||
|         $this->assertEquals($data['match'], $bill->match); | ||||
|         $this->assertEquals($data['amount_min'], $bill->amount_min); | ||||
|         $this->assertEquals($data['repeat_freq'], $bill->repeat_freq); | ||||
|         $note = $bill->notes()->first(); | ||||
|         $this->assertEquals($data['notes'], $note->text); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Find by ID | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\BillFactory | ||||
|      * | ||||
|      */ | ||||
|     public function testFindById() | ||||
|     { | ||||
|         $existing = $this->user()->piggyBanks()->first(); | ||||
|         /** @var BillFactory $factory */ | ||||
|         $factory = app(BillFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $piggy = $factory->find($existing->id, null); | ||||
|         $this->assertEquals($existing->id, $piggy->id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Find by name | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\BillFactory | ||||
|      * | ||||
|      */ | ||||
|     public function testFindByName() | ||||
|     { | ||||
|         $existing = $this->user()->bills()->first(); | ||||
|         /** @var BillFactory $factory */ | ||||
|         $factory = app(BillFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $piggy = $factory->find(null, $existing->name); | ||||
|  | ||||
|         $this->assertEquals($existing->id, $piggy->id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Find by unknown name | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\BillFactory | ||||
|      * | ||||
|      */ | ||||
|     public function testFindByUnknownName() | ||||
|     { | ||||
|         /** @var BillFactory $factory */ | ||||
|         $factory = app(BillFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $piggy = $factory->find(null, 'I dont exist' . rand(1, 1000)); | ||||
|  | ||||
|         $this->assertNull($piggy); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Find NULL | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\BillFactory | ||||
|      * | ||||
|      */ | ||||
|     public function testFindNull() | ||||
|     { | ||||
|         /** @var BillFactory $factory */ | ||||
|         $factory = app(BillFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $this->assertNull($factory->find(null, null)); | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										97
									
								
								tests/Unit/Factory/BudgetFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										97
									
								
								tests/Unit/Factory/BudgetFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,97 @@ | ||||
| <?php | ||||
| /** | ||||
|  * BudgetFactoryTest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Tests\Unit\Factory; | ||||
|  | ||||
|  | ||||
| use FireflyIII\Factory\BudgetFactory; | ||||
| use Tests\TestCase; | ||||
|  | ||||
| /** | ||||
|  * Class BudgetFactoryTest | ||||
|  */ | ||||
| class BudgetFactoryTest extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * Put in ID, return it. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\BudgetFactory | ||||
|      */ | ||||
|     public function testFindById() | ||||
|     { | ||||
|         $existing = $this->user()->budgets()->first(); | ||||
|         /** @var BudgetFactory $factory */ | ||||
|         $factory = app(BudgetFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|  | ||||
|         $budget = $factory->find($existing->id, null); | ||||
|         $this->assertEquals($existing->id, $budget->id); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Put in name, return it. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\BudgetFactory | ||||
|      */ | ||||
|     public function testFindByName() | ||||
|     { | ||||
|         $existing = $this->user()->budgets()->first(); | ||||
|         /** @var BudgetFactory $factory */ | ||||
|         $factory = app(BudgetFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|  | ||||
|         $budget = $factory->find(null, $existing->name); | ||||
|         $this->assertEquals($existing->id, $budget->id); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Put in unknown, get NULL | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\BudgetFactory | ||||
|      */ | ||||
|     public function testFindUnknown() | ||||
|     { | ||||
|         /** @var BudgetFactory $factory */ | ||||
|         $factory = app(BudgetFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $this->assertNull($factory->find(null, 'I dont exist.'.rand(1,000))); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Put in NULL, will find NULL. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\BudgetFactory | ||||
|      */ | ||||
|     public function testFindNull() | ||||
|     { | ||||
|         /** @var BudgetFactory $factory */ | ||||
|         $factory = app(BudgetFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|  | ||||
|         $this->assertNull($factory->find(null, null)); | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										103
									
								
								tests/Unit/Factory/CategoryFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								tests/Unit/Factory/CategoryFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,103 @@ | ||||
| <?php | ||||
| /** | ||||
|  * CategoryFactoryTest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Tests\Unit\Factory; | ||||
|  | ||||
| use FireflyIII\Factory\CategoryFactory; | ||||
| use Tests\TestCase; | ||||
|  | ||||
| /** | ||||
|  * Class CategoryFactoryTest | ||||
|  */ | ||||
| class CategoryFactoryTest extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\CategoryFactory | ||||
|      */ | ||||
|     public function testFindOrCreateExistingID() | ||||
|     { | ||||
|         $existing = $this->user()->categories()->first(); | ||||
|  | ||||
|         /** @var CategoryFactory $factory */ | ||||
|         $factory = app(CategoryFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $category = $factory->findOrCreate($existing->id, null); | ||||
|         $this->assertEquals($existing->id, $category->id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\CategoryFactory | ||||
|      */ | ||||
|     public function testFindOrCreateExistingName() | ||||
|     { | ||||
|         $existing = $this->user()->categories()->first(); | ||||
|  | ||||
|         /** @var CategoryFactory $factory */ | ||||
|         $factory = app(CategoryFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $category = $factory->findOrCreate(null, $existing->name); | ||||
|         $this->assertEquals($existing->id, $category->id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * You can force a NULL result by presenting an invalid ID and no valid name. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\CategoryFactory | ||||
|      */ | ||||
|     public function testFindOrCreateInvalidID() | ||||
|     { | ||||
|         $existing = $this->user()->categories()->max('id'); | ||||
|         $existing = $existing + 4; | ||||
|  | ||||
|         /** @var CategoryFactory $factory */ | ||||
|         $factory = app(CategoryFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $this->assertNull($factory->findOrCreate($existing, '')); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\CategoryFactory | ||||
|      */ | ||||
|     public function testFindOrCreateNewName() | ||||
|     { | ||||
|         $name = 'Some new category #' . rand(1, 1000); | ||||
|  | ||||
|         /** @var CategoryFactory $factory */ | ||||
|         $factory = app(CategoryFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $category = $factory->findOrCreate(null, $name); | ||||
|         $this->assertEquals($name, $category->name); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\CategoryFactory | ||||
|      */ | ||||
|     public function testFindOrCreateNull() | ||||
|     { | ||||
|         /** @var CategoryFactory $factory */ | ||||
|         $factory = app(CategoryFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $this->assertNull($factory->findOrCreate(null, null)); | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										135
									
								
								tests/Unit/Factory/PiggyBankEventFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										135
									
								
								tests/Unit/Factory/PiggyBankEventFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,135 @@ | ||||
| <?php | ||||
| /** | ||||
|  * PiggyBankEventFactoryTest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Tests\Unit\Factory; | ||||
|  | ||||
|  | ||||
| use FireflyIII\Factory\PiggyBankEventFactory; | ||||
| use FireflyIII\Models\PiggyBankEvent; | ||||
| use FireflyIII\Models\PiggyBankRepetition; | ||||
| use FireflyIII\Models\TransactionJournal; | ||||
| use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface; | ||||
| use Tests\TestCase; | ||||
|  | ||||
| /** | ||||
|  * Class PiggyBankEventFactoryTest | ||||
|  */ | ||||
| class PiggyBankEventFactoryTest extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\PiggyBankEventFactory | ||||
|      */ | ||||
|     public function testCreateAmountZero() | ||||
|     { | ||||
|         /** @var TransactionJournal $transfer */ | ||||
|         $transfer   = $this->user()->transactionJournals()->where('transaction_type_id', 3)->first(); | ||||
|         $piggy      = $this->user()->piggyBanks()->first(); | ||||
|         $repetition = PiggyBankRepetition::first(); | ||||
|         $repos      = $this->mock(PiggyBankRepositoryInterface::class); | ||||
|         /** @var PiggyBankEventFactory $factory */ | ||||
|         $factory = app(PiggyBankEventFactory::class); | ||||
|  | ||||
|         // mock: | ||||
|         $repos->shouldReceive('setUser'); | ||||
|         $repos->shouldReceive('getRepetition')->andReturn($repetition); | ||||
|         $repos->shouldReceive('getExactAmount')->andReturn('0'); | ||||
|  | ||||
|         $this->assertNull($factory->create($transfer, $piggy)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\PiggyBankEventFactory | ||||
|      */ | ||||
|     public function testCreateNoPiggy() | ||||
|     { | ||||
|         /** @var TransactionJournal $transfer */ | ||||
|         $transfer = $this->user()->transactionJournals()->where('transaction_type_id', 3)->first(); | ||||
|  | ||||
|         /** @var PiggyBankEventFactory $factory */ | ||||
|         $factory = app(PiggyBankEventFactory::class); | ||||
|  | ||||
|         $this->assertNull($factory->create($transfer, null)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\PiggyBankEventFactory | ||||
|      */ | ||||
|     public function testCreateNoRep() | ||||
|     { | ||||
|         /** @var TransactionJournal $transfer */ | ||||
|         $transfer   = $this->user()->transactionJournals()->where('transaction_type_id', 3)->first(); | ||||
|         $piggy      = $this->user()->piggyBanks()->first(); | ||||
|         $repetition = new PiggyBankRepetition; | ||||
|         $repos      = $this->mock(PiggyBankRepositoryInterface::class); | ||||
|         /** @var PiggyBankEventFactory $factory */ | ||||
|         $factory = app(PiggyBankEventFactory::class); | ||||
|  | ||||
|         // mock: | ||||
|         $repos->shouldReceive('setUser'); | ||||
|         $repos->shouldReceive('getRepetition')->andReturn($repetition); | ||||
|         $repos->shouldReceive('getExactAmount')->andReturn('0'); | ||||
|  | ||||
|         $this->assertNull($factory->create($transfer, $piggy)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\PiggyBankEventFactory | ||||
|      */ | ||||
|     public function testCreateNotTransfer() | ||||
|     { | ||||
|         /** @var TransactionJournal $deposit */ | ||||
|         $deposit = $this->user()->transactionJournals()->where('transaction_type_id', 2)->first(); | ||||
|         $piggy   = $this->user()->piggyBanks()->first(); | ||||
|         /** @var PiggyBankEventFactory $factory */ | ||||
|         $factory = app(PiggyBankEventFactory::class); | ||||
|  | ||||
|         $this->assertNull($factory->create($deposit, $piggy)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\PiggyBankEventFactory | ||||
|      */ | ||||
|     public function testCreateSuccess() | ||||
|     { | ||||
|         /** @var TransactionJournal $transfer */ | ||||
|         $transfer   = $this->user()->transactionJournals()->where('transaction_type_id', 3)->first(); | ||||
|         $piggy      = $this->user()->piggyBanks()->first(); | ||||
|         $repetition = PiggyBankRepetition::first(); | ||||
|         $event      = PiggyBankEvent::first(); | ||||
|         $repos      = $this->mock(PiggyBankRepositoryInterface::class); | ||||
|         /** @var PiggyBankEventFactory $factory */ | ||||
|         $factory = app(PiggyBankEventFactory::class); | ||||
|  | ||||
|         // mock: | ||||
|         $repos->shouldReceive('setUser'); | ||||
|         $repos->shouldReceive('getRepetition')->andReturn($repetition); | ||||
|         $repos->shouldReceive('getExactAmount')->andReturn('5'); | ||||
|         $repos->shouldReceive('addAmountToRepetition')->once(); | ||||
|         $repos->shouldReceive('createEventWithJournal')->once()->andReturn($event); | ||||
|  | ||||
|         $result = $factory->create($transfer, $piggy); | ||||
|         $this->assertEquals($result->id, $event->id); | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										96
									
								
								tests/Unit/Factory/PiggyBankFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								tests/Unit/Factory/PiggyBankFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,96 @@ | ||||
| <?php | ||||
| /** | ||||
|  * PiggyBankFactoryTest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Tests\Unit\Factory; | ||||
|  | ||||
|  | ||||
| use FireflyIII\Factory\PiggyBankFactory; | ||||
| use Tests\TestCase; | ||||
|  | ||||
| /** | ||||
|  * Class PiggyBankFactoryTest | ||||
|  */ | ||||
| class PiggyBankFactoryTest extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * Put in ID, return it. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\PiggyBankFactory | ||||
|      */ | ||||
|     public function testFindById() | ||||
|     { | ||||
|         $existing = $this->user()->piggyBanks()->first(); | ||||
|         /** @var PiggyBankFactory $factory */ | ||||
|         $factory = app(PiggyBankFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|  | ||||
|         $piggyBank = $factory->find($existing->id, null); | ||||
|         $this->assertEquals($existing->id, $piggyBank->id); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Put in name, return it. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\PiggyBankFactory | ||||
|      */ | ||||
|     public function testFindByName() | ||||
|     { | ||||
|         $existing = $this->user()->piggyBanks()->first(); | ||||
|         /** @var PiggyBankFactory $factory */ | ||||
|         $factory = app(PiggyBankFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|  | ||||
|         $piggyBank = $factory->find(null, $existing->name); | ||||
|         $this->assertEquals($existing->id, $piggyBank->id); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Put in NULL, will find NULL. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\PiggyBankFactory | ||||
|      */ | ||||
|     public function testFindNull() | ||||
|     { | ||||
|         /** @var PiggyBankFactory $factory */ | ||||
|         $factory = app(PiggyBankFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|  | ||||
|         $this->assertNull($factory->find(null, null)); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Put in unknown, get NULL | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\PiggyBankFactory | ||||
|      */ | ||||
|     public function testFindUnknown() | ||||
|     { | ||||
|         /** @var PiggyBankFactory $factory */ | ||||
|         $factory = app(PiggyBankFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $this->assertNull($factory->find(null, 'I dont exist.' . rand(1, 000))); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										63
									
								
								tests/Unit/Factory/TagFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								tests/Unit/Factory/TagFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | ||||
| <?php | ||||
| /** | ||||
|  * TagFactoryTest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Tests\Unit\Factory; | ||||
|  | ||||
|  | ||||
| use FireflyIII\Factory\TagFactory; | ||||
| use Tests\TestCase; | ||||
|  | ||||
| /** | ||||
|  * Class TagFactoryTest | ||||
|  */ | ||||
| class TagFactoryTest extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TagFactory | ||||
|      */ | ||||
|     public function testFindOrCreateExisting() | ||||
|     { | ||||
|         $tag = $this->user()->tags()->first(); | ||||
|         /** @var TagFactory $factory */ | ||||
|         $factory = app(TagFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|  | ||||
|         $result = $factory->findOrCreate($tag->tag); | ||||
|         $this->assertEquals($tag->id, $result->id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TagFactory | ||||
|      */ | ||||
|     public function testFindOrCreateNew() | ||||
|     { | ||||
|         $tag = 'Some new tag#' . rand(1, 1000); | ||||
|         /** @var TagFactory $factory */ | ||||
|         $factory = app(TagFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|  | ||||
|         $result = $factory->findOrCreate($tag); | ||||
|         $this->assertEquals($tag, $result->tag); | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										81
									
								
								tests/Unit/Factory/TransactionCurrencyFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								tests/Unit/Factory/TransactionCurrencyFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,81 @@ | ||||
| <?php | ||||
| /** | ||||
|  * TransactionCurrencyFactoryTest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Tests\Unit\Factory; | ||||
|  | ||||
|  | ||||
| use FireflyIII\Factory\TransactionCurrencyFactory; | ||||
| use FireflyIII\Models\TransactionCurrency; | ||||
| use Tests\TestCase; | ||||
|  | ||||
| /** | ||||
|  * Class TransactionCurrencyFactoryTest | ||||
|  */ | ||||
| class TransactionCurrencyFactoryTest extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionCurrencyFactory | ||||
|      */ | ||||
|     public function testFindByBadCode() | ||||
|     { | ||||
|         /** @var TransactionCurrencyFactory $factory */ | ||||
|         $factory = app(TransactionCurrencyFactory::class); | ||||
|         $this->assertNull($factory->find(null, 'BAD CODE')); | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionCurrencyFactory | ||||
|      */ | ||||
|     public function testFindByCode() | ||||
|     { | ||||
|         $currency = TransactionCurrency::find(1); | ||||
|         /** @var TransactionCurrencyFactory $factory */ | ||||
|         $factory = app(TransactionCurrencyFactory::class); | ||||
|         $result  = $factory->find(null, $currency->code); | ||||
|         $this->assertEquals($currency->id, $result->id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionCurrencyFactory | ||||
|      */ | ||||
|     public function testFindByID() | ||||
|     { | ||||
|         $currency = TransactionCurrency::find(1); | ||||
|         /** @var TransactionCurrencyFactory $factory */ | ||||
|         $factory = app(TransactionCurrencyFactory::class); | ||||
|         $result  = $factory->find($currency->id, null); | ||||
|         $this->assertEquals($currency->id, $result->id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionCurrencyFactory | ||||
|      */ | ||||
|     public function testFindNull() | ||||
|     { | ||||
|         /** @var TransactionCurrencyFactory $factory */ | ||||
|         $factory = app(TransactionCurrencyFactory::class); | ||||
|         $this->assertNull($factory->find(null, null)); | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										823
									
								
								tests/Unit/Factory/TransactionFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										823
									
								
								tests/Unit/Factory/TransactionFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,823 @@ | ||||
| <?php | ||||
| /** | ||||
|  * TransactionFactoryTest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Tests\Unit\Factory; | ||||
|  | ||||
|  | ||||
| use FireflyIII\Factory\AccountFactory; | ||||
| use FireflyIII\Factory\BudgetFactory; | ||||
| use FireflyIII\Factory\CategoryFactory; | ||||
| use FireflyIII\Factory\TransactionCurrencyFactory; | ||||
| use FireflyIII\Factory\TransactionFactory; | ||||
| use FireflyIII\Models\AccountType; | ||||
| use FireflyIII\Models\Transaction; | ||||
| use FireflyIII\Models\TransactionCurrency; | ||||
| use FireflyIII\Models\TransactionJournal; | ||||
| use FireflyIII\Repositories\Account\AccountRepositoryInterface; | ||||
| use Tests\TestCase; | ||||
|  | ||||
| /** | ||||
|  * Class TransactionFactoryTest | ||||
|  */ | ||||
| class TransactionFactoryTest extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait | ||||
|      */ | ||||
|     public function testCreatePairBasic() | ||||
|     { | ||||
|         // objects: | ||||
|         $asset   = $this->user()->accounts()->where('account_type_id', 3)->first(); | ||||
|         $expense = $this->user()->accounts()->where('account_type_id', 4)->first(); | ||||
|         $euro    = TransactionCurrency::first(); | ||||
|  | ||||
|         // mocked classes | ||||
|         $accountRepos    = $this->mock(AccountRepositoryInterface::class); | ||||
|         $budgetFactory   = $this->mock(BudgetFactory::class); | ||||
|         $categoryFactory = $this->mock(CategoryFactory::class); | ||||
|         $currencyFactory = $this->mock(TransactionCurrencyFactory::class); | ||||
|  | ||||
|         $data = [ | ||||
|             'currency_id'           => 1, | ||||
|             'currency_code'         => null, | ||||
|             'description'           => null, | ||||
|             'source_id'             => $asset->id, | ||||
|             'source_name'           => null, | ||||
|             'destination_id'        => $expense->id, | ||||
|             'destination_name'      => null, | ||||
|             'amount'                => '10', | ||||
|             'reconciled'            => false, | ||||
|             'identifier'            => 0, | ||||
|             'foreign_currency_id'   => null, | ||||
|             'foreign_currency_code' => null, | ||||
|             'foreign_amount'        => null, | ||||
|             'budget_id'             => null, | ||||
|             'budget_name'           => null, | ||||
|             'category_id'           => null, | ||||
|             'category_name'         => null, | ||||
|         ]; | ||||
|  | ||||
|         // mock: | ||||
|         $accountRepos->shouldReceive('setUser'); | ||||
|         $budgetFactory->shouldReceive('setUser'); | ||||
|         $categoryFactory->shouldReceive('setUser'); | ||||
|         // first search action is for the asset account, second is for expense account. | ||||
|         $accountRepos->shouldReceive('findNull')->andReturn($asset, $expense); | ||||
|  | ||||
|         // factories return various stuff: | ||||
|         $budgetFactory->shouldReceive('find')->andReturn(null); | ||||
|         $categoryFactory->shouldReceive('findOrCreate')->andReturn(null); | ||||
|         $currencyFactory->shouldReceive('find')->andReturn($euro, null); | ||||
|  | ||||
|         /** @var TransactionJournal $withdrawal */ | ||||
|         $withdrawal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first(); | ||||
|         $count      = $withdrawal->transactions()->count(); | ||||
|  | ||||
|         /** @var TransactionFactory $factory */ | ||||
|         $factory = app(TransactionFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $collection = $factory->createPair($withdrawal, $data); | ||||
|  | ||||
|         $newCount = $withdrawal->transactions()->count(); | ||||
|  | ||||
|         $this->assertCount(2, $collection); | ||||
|         $this->assertEquals($count + 2, $newCount); | ||||
|         // find stuff in transaction #1 (should suffice): | ||||
|         /** @var Transaction $first */ | ||||
|         $first = $collection->first(); | ||||
|         $this->assertEquals($withdrawal->id, $first->transaction_journal_id); | ||||
|         $this->assertEquals(-10, $first->amount); | ||||
|         $this->assertEquals(false, $first->reconciled); | ||||
|         $this->assertEquals(0, $first->identifier); | ||||
|         $this->assertEquals($euro->id, $first->transaction_currency_id); | ||||
|         $this->assertNull($first->foreign_amount); | ||||
|         $this->assertNull($first->foreign_currency_id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait | ||||
|      */ | ||||
|     public function testCreatePairBasicByName() | ||||
|     { | ||||
|         // objects: | ||||
|         $asset   = $this->user()->accounts()->where('account_type_id', 3)->first(); | ||||
|         $expense = $this->user()->accounts()->where('account_type_id', 4)->first(); | ||||
|         $euro    = TransactionCurrency::first(); | ||||
|  | ||||
|         // mocked classes | ||||
|         $accountRepos    = $this->mock(AccountRepositoryInterface::class); | ||||
|         $budgetFactory   = $this->mock(BudgetFactory::class); | ||||
|         $categoryFactory = $this->mock(CategoryFactory::class); | ||||
|         $currencyFactory = $this->mock(TransactionCurrencyFactory::class); | ||||
|         $accountFactory  = $this->mock(AccountFactory::class); | ||||
|  | ||||
|         $data = [ | ||||
|             'currency_id'           => 1, | ||||
|             'currency_code'         => null, | ||||
|             'description'           => null, | ||||
|             'source_id'             => null, | ||||
|             'source_name'           => $asset->name, | ||||
|             'destination_id'        => null, | ||||
|             'destination_name'      => $expense->name, | ||||
|             'amount'                => '10', | ||||
|             'reconciled'            => false, | ||||
|             'identifier'            => 0, | ||||
|             'foreign_currency_id'   => null, | ||||
|             'foreign_currency_code' => null, | ||||
|             'foreign_amount'        => null, | ||||
|             'budget_id'             => null, | ||||
|             'budget_name'           => null, | ||||
|             'category_id'           => null, | ||||
|             'category_name'         => null, | ||||
|         ]; | ||||
|  | ||||
|         // mock: | ||||
|         $accountRepos->shouldReceive('setUser'); | ||||
|         $budgetFactory->shouldReceive('setUser'); | ||||
|         $categoryFactory->shouldReceive('setUser'); | ||||
|         $accountFactory->shouldReceive('setUser'); | ||||
|         // first search action is for the asset account | ||||
|         $accountRepos->shouldReceive('findByName')->withArgs([$asset->name, [AccountType::ASSET]])->andReturn($asset); | ||||
|         // second is for expense account. | ||||
|         $accountFactory->shouldReceive('findOrCreate')->andReturn($expense); | ||||
|  | ||||
|         // factories return various stuff: | ||||
|         $budgetFactory->shouldReceive('find')->andReturn(null); | ||||
|         $categoryFactory->shouldReceive('findOrCreate')->andReturn(null); | ||||
|         $currencyFactory->shouldReceive('find')->andReturn($euro, null); | ||||
|  | ||||
|         /** @var TransactionJournal $withdrawal */ | ||||
|         $withdrawal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first(); | ||||
|         $count      = $withdrawal->transactions()->count(); | ||||
|  | ||||
|         /** @var TransactionFactory $factory */ | ||||
|         $factory = app(TransactionFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $collection = $factory->createPair($withdrawal, $data); | ||||
|  | ||||
|         $newCount = $withdrawal->transactions()->count(); | ||||
|  | ||||
|         $this->assertCount(2, $collection); | ||||
|         $this->assertEquals($count + 2, $newCount); | ||||
|         // find stuff in transaction #1 (should suffice): | ||||
|         /** @var Transaction $first */ | ||||
|         $first = $collection->first(); | ||||
|         $this->assertEquals($withdrawal->id, $first->transaction_journal_id); | ||||
|         $this->assertEquals(-10, $first->amount); | ||||
|         $this->assertEquals(false, $first->reconciled); | ||||
|         $this->assertEquals(0, $first->identifier); | ||||
|         $this->assertEquals($euro->id, $first->transaction_currency_id); | ||||
|         $this->assertNull($first->foreign_amount); | ||||
|         $this->assertNull($first->foreign_currency_id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait | ||||
|      */ | ||||
|     public function testCreatePairBasicIntoCash() | ||||
|     { | ||||
|         // objects: | ||||
|         $asset   = $this->user()->accounts()->where('account_type_id', 3)->first(); | ||||
|         $expense = $this->user()->accounts()->where('account_type_id', 4)->first(); | ||||
|         $euro    = TransactionCurrency::first(); | ||||
|  | ||||
|         // mocked classes | ||||
|         $accountRepos    = $this->mock(AccountRepositoryInterface::class); | ||||
|         $budgetFactory   = $this->mock(BudgetFactory::class); | ||||
|         $categoryFactory = $this->mock(CategoryFactory::class); | ||||
|         $currencyFactory = $this->mock(TransactionCurrencyFactory::class); | ||||
|         $accountFactory  = $this->mock(AccountFactory::class); | ||||
|  | ||||
|         $data = [ | ||||
|             'currency_id'           => 1, | ||||
|             'currency_code'         => null, | ||||
|             'description'           => null, | ||||
|             'source_id'             => null, | ||||
|             'source_name'           => $asset->name, | ||||
|             'destination_id'        => null, | ||||
|             'destination_name'      => '', | ||||
|             'amount'                => '10', | ||||
|             'reconciled'            => false, | ||||
|             'identifier'            => 0, | ||||
|             'foreign_currency_id'   => null, | ||||
|             'foreign_currency_code' => null, | ||||
|             'foreign_amount'        => null, | ||||
|             'budget_id'             => null, | ||||
|             'budget_name'           => null, | ||||
|             'category_id'           => null, | ||||
|             'category_name'         => null, | ||||
|         ]; | ||||
|  | ||||
|         // mock: | ||||
|         $accountRepos->shouldReceive('setUser'); | ||||
|         $budgetFactory->shouldReceive('setUser'); | ||||
|         $categoryFactory->shouldReceive('setUser'); | ||||
|         $accountFactory->shouldReceive('setUser'); | ||||
|         // first search action is for the asset account | ||||
|         $accountRepos->shouldReceive('findByName')->withArgs([$asset->name, [AccountType::ASSET]])->andReturn($asset); | ||||
|         // second is for expense account (cash) | ||||
|         $accountRepos->shouldReceive('getCashAccount')->andReturn($expense); | ||||
|  | ||||
|         // factories return various stuff: | ||||
|         $budgetFactory->shouldReceive('find')->andReturn(null); | ||||
|         $categoryFactory->shouldReceive('findOrCreate')->andReturn(null); | ||||
|         $currencyFactory->shouldReceive('find')->andReturn($euro, null); | ||||
|  | ||||
|         /** @var TransactionJournal $withdrawal */ | ||||
|         $withdrawal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first(); | ||||
|         $count      = $withdrawal->transactions()->count(); | ||||
|  | ||||
|         /** @var TransactionFactory $factory */ | ||||
|         $factory = app(TransactionFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $collection = $factory->createPair($withdrawal, $data); | ||||
|  | ||||
|         $newCount = $withdrawal->transactions()->count(); | ||||
|  | ||||
|         $this->assertCount(2, $collection); | ||||
|         $this->assertEquals($count + 2, $newCount); | ||||
|         // find stuff in transaction #1 (should suffice): | ||||
|         /** @var Transaction $first */ | ||||
|         $first = $collection->first(); | ||||
|         $this->assertEquals($withdrawal->id, $first->transaction_journal_id); | ||||
|         $this->assertEquals(-10, $first->amount); | ||||
|         $this->assertEquals(false, $first->reconciled); | ||||
|         $this->assertEquals(0, $first->identifier); | ||||
|         $this->assertEquals($euro->id, $first->transaction_currency_id); | ||||
|         $this->assertNull($first->foreign_amount); | ||||
|         $this->assertNull($first->foreign_currency_id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Add budget and category data. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\TransactionFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait | ||||
|      */ | ||||
|     public function testCreatePairBasicMeta() | ||||
|     { | ||||
|         // objects: | ||||
|         $asset    = $this->user()->accounts()->where('account_type_id', 3)->first(); | ||||
|         $expense  = $this->user()->accounts()->where('account_type_id', 4)->first(); | ||||
|         $budget   = $this->user()->budgets()->first(); | ||||
|         $category = $this->user()->categories()->first(); | ||||
|         $euro     = TransactionCurrency::first(); | ||||
|  | ||||
|         // mocked classes | ||||
|         $accountRepos    = $this->mock(AccountRepositoryInterface::class); | ||||
|         $budgetFactory   = $this->mock(BudgetFactory::class); | ||||
|         $categoryFactory = $this->mock(CategoryFactory::class); | ||||
|         $currencyFactory = $this->mock(TransactionCurrencyFactory::class); | ||||
|  | ||||
|         $data = [ | ||||
|             'currency_id'           => 1, | ||||
|             'currency_code'         => null, | ||||
|             'description'           => null, | ||||
|             'source_id'             => $asset->id, | ||||
|             'source_name'           => null, | ||||
|             'destination_id'        => $expense->id, | ||||
|             'destination_name'      => null, | ||||
|             'amount'                => '10', | ||||
|             'reconciled'            => false, | ||||
|             'identifier'            => 0, | ||||
|             'foreign_currency_id'   => null, | ||||
|             'foreign_currency_code' => null, | ||||
|             'foreign_amount'        => null, | ||||
|             'budget_id'             => $budget->id, | ||||
|             'budget_name'           => null, | ||||
|             'category_id'           => $category->id, | ||||
|             'category_name'         => null, | ||||
|         ]; | ||||
|  | ||||
|         // mock: | ||||
|         $accountRepos->shouldReceive('setUser'); | ||||
|         $budgetFactory->shouldReceive('setUser'); | ||||
|         $categoryFactory->shouldReceive('setUser'); | ||||
|         // first search action is for the asset account, second is for expense account. | ||||
|         $accountRepos->shouldReceive('findNull')->andReturn($asset, $expense); | ||||
|  | ||||
|         // factories return various stuff: | ||||
|         $budgetFactory->shouldReceive('find')->andReturn($budget); | ||||
|         $categoryFactory->shouldReceive('findOrCreate')->andReturn($category); | ||||
|         $currencyFactory->shouldReceive('find')->andReturn($euro, null); | ||||
|  | ||||
|         /** @var TransactionJournal $withdrawal */ | ||||
|         $withdrawal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first(); | ||||
|         $count      = $withdrawal->transactions()->count(); | ||||
|  | ||||
|         /** @var TransactionFactory $factory */ | ||||
|         $factory = app(TransactionFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $collection = $factory->createPair($withdrawal, $data); | ||||
|  | ||||
|         $newCount = $withdrawal->transactions()->count(); | ||||
|  | ||||
|         $this->assertCount(2, $collection); | ||||
|         $this->assertEquals($count + 2, $newCount); | ||||
|         // find stuff in transaction #1 (should suffice): | ||||
|         /** @var Transaction $first */ | ||||
|         $first = $collection->first(); | ||||
|         $this->assertEquals($withdrawal->id, $first->transaction_journal_id); | ||||
|         $this->assertEquals(-10, $first->amount); | ||||
|         $this->assertEquals(false, $first->reconciled); | ||||
|         $this->assertEquals(0, $first->identifier); | ||||
|         $this->assertEquals($euro->id, $first->transaction_currency_id); | ||||
|         $this->assertNull($first->foreign_amount); | ||||
|         $this->assertNull($first->foreign_currency_id); | ||||
|         $this->assertEquals($budget->name, $first->budgets()->first()->name); | ||||
|         $this->assertEquals($category->name, $first->categories()->first()->name); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Create deposit using minimal data. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\TransactionFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait | ||||
|      */ | ||||
|     public function testCreatePairDeposit() | ||||
|     { | ||||
|         // objects: | ||||
|         $asset   = $this->user()->accounts()->where('account_type_id', 3)->first(); | ||||
|         $revenue = $this->user()->accounts()->where('account_type_id', 5)->first(); | ||||
|         $euro    = TransactionCurrency::first(); | ||||
|         $foreign = TransactionCurrency::where('id', '!=', $euro->id)->first(); | ||||
|  | ||||
|         // mocked classes | ||||
|         $accountRepos    = $this->mock(AccountRepositoryInterface::class); | ||||
|         $budgetFactory   = $this->mock(BudgetFactory::class); | ||||
|         $categoryFactory = $this->mock(CategoryFactory::class); | ||||
|         $currencyFactory = $this->mock(TransactionCurrencyFactory::class); | ||||
|  | ||||
|         $data = [ | ||||
|             'currency_id'           => 1, | ||||
|             'currency_code'         => null, | ||||
|             'description'           => null, | ||||
|             'source_id'             => $revenue->id, | ||||
|             'source_name'           => null, | ||||
|             'destination_id'        => $asset->id, | ||||
|             'destination_name'      => null, | ||||
|             'amount'                => '10', | ||||
|             'reconciled'            => false, | ||||
|             'identifier'            => 0, | ||||
|             'foreign_currency_id'   => null, | ||||
|             'foreign_currency_code' => null, | ||||
|             'foreign_amount'        => null, | ||||
|             'budget_id'             => null, | ||||
|             'budget_name'           => null, | ||||
|             'category_id'           => null, | ||||
|             'category_name'         => null, | ||||
|         ]; | ||||
|  | ||||
|         // mock: | ||||
|         $accountRepos->shouldReceive('setUser'); | ||||
|         $budgetFactory->shouldReceive('setUser'); | ||||
|         $categoryFactory->shouldReceive('setUser'); | ||||
|         // first search action is for the asset account, second is for expense account. | ||||
|         $accountRepos->shouldReceive('findNull')->andReturn($revenue, $asset); | ||||
|  | ||||
|         // factories return various stuff: | ||||
|         $budgetFactory->shouldReceive('find')->andReturn(null); | ||||
|         $categoryFactory->shouldReceive('findOrCreate')->andReturn(null); | ||||
|         $currencyFactory->shouldReceive('find')->andReturn($euro, null); | ||||
|  | ||||
|         /** @var TransactionJournal $deposit */ | ||||
|         $deposit = $this->user()->transactionJournals()->where('transaction_type_id', 2)->first(); | ||||
|         $count   = $deposit->transactions()->count(); | ||||
|  | ||||
|         /** @var TransactionFactory $factory */ | ||||
|         $factory = app(TransactionFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $collection = $factory->createPair($deposit, $data); | ||||
|  | ||||
|         $newCount = $deposit->transactions()->count(); | ||||
|  | ||||
|         $this->assertCount(2, $collection); | ||||
|         $this->assertEquals($count + 2, $newCount); | ||||
|         // find stuff in transaction #1 (should suffice): | ||||
|         /** @var Transaction $first */ | ||||
|         $first = $collection->first(); | ||||
|         $this->assertEquals($deposit->id, $first->transaction_journal_id); | ||||
|         $this->assertEquals(-10, $first->amount); | ||||
|         $this->assertEquals(false, $first->reconciled); | ||||
|         $this->assertEquals(0, $first->identifier); | ||||
|         $this->assertEquals($euro->id, $first->transaction_currency_id); | ||||
|         $this->assertNull($first->foreign_amount); | ||||
|         $this->assertNull($first->foreign_currency_id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Create deposit using minimal data. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\TransactionFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait | ||||
|      */ | ||||
|     public function testCreatePairDepositByName() | ||||
|     { | ||||
|         // objects: | ||||
|         $asset   = $this->user()->accounts()->where('account_type_id', 3)->first(); | ||||
|         $revenue = $this->user()->accounts()->where('account_type_id', 5)->first(); | ||||
|         $euro    = TransactionCurrency::first(); | ||||
|         $foreign = TransactionCurrency::where('id', '!=', $euro->id)->first(); | ||||
|  | ||||
|         // mocked classes | ||||
|         $accountRepos    = $this->mock(AccountRepositoryInterface::class); | ||||
|         $budgetFactory   = $this->mock(BudgetFactory::class); | ||||
|         $categoryFactory = $this->mock(CategoryFactory::class); | ||||
|         $currencyFactory = $this->mock(TransactionCurrencyFactory::class); | ||||
|         $accountFactory  = $this->mock(AccountFactory::class); | ||||
|  | ||||
|         $data = [ | ||||
|             'currency_id'           => 1, | ||||
|             'currency_code'         => null, | ||||
|             'description'           => null, | ||||
|             'source_id'             => null, | ||||
|             'source_name'           => $revenue->name, | ||||
|             'destination_id'        => $asset->id, | ||||
|             'destination_name'      => null, | ||||
|             'amount'                => '10', | ||||
|             'reconciled'            => false, | ||||
|             'identifier'            => 0, | ||||
|             'foreign_currency_id'   => null, | ||||
|             'foreign_currency_code' => null, | ||||
|             'foreign_amount'        => null, | ||||
|             'budget_id'             => null, | ||||
|             'budget_name'           => null, | ||||
|             'category_id'           => null, | ||||
|             'category_name'         => null, | ||||
|         ]; | ||||
|  | ||||
|         // mock: | ||||
|         $accountRepos->shouldReceive('setUser'); | ||||
|         $budgetFactory->shouldReceive('setUser'); | ||||
|         $categoryFactory->shouldReceive('setUser'); | ||||
|         $accountFactory->shouldReceive('setUser'); | ||||
|         // first search action is for the asset account | ||||
|         $accountRepos->shouldReceive('findNull')->andReturn($asset); | ||||
|         // second is for revenue account. | ||||
|         $accountFactory->shouldReceive('findOrCreate')->andReturn($revenue); | ||||
|  | ||||
|         // factories return various stuff: | ||||
|         $budgetFactory->shouldReceive('find')->andReturn(null); | ||||
|         $categoryFactory->shouldReceive('findOrCreate')->andReturn(null); | ||||
|         $currencyFactory->shouldReceive('find')->andReturn($euro, null); | ||||
|  | ||||
|         /** @var TransactionJournal $deposit */ | ||||
|         $deposit = $this->user()->transactionJournals()->where('transaction_type_id', 2)->first(); | ||||
|         $count   = $deposit->transactions()->count(); | ||||
|  | ||||
|         /** @var TransactionFactory $factory */ | ||||
|         $factory = app(TransactionFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $collection = $factory->createPair($deposit, $data); | ||||
|  | ||||
|         $newCount = $deposit->transactions()->count(); | ||||
|  | ||||
|         $this->assertCount(2, $collection); | ||||
|         $this->assertEquals($count + 2, $newCount); | ||||
|         // find stuff in transaction #1 (should suffice): | ||||
|         /** @var Transaction $first */ | ||||
|         $first = $collection->first(); | ||||
|         $this->assertEquals($deposit->id, $first->transaction_journal_id); | ||||
|         $this->assertEquals(-10, $first->amount); | ||||
|         $this->assertEquals(false, $first->reconciled); | ||||
|         $this->assertEquals(0, $first->identifier); | ||||
|         $this->assertEquals($euro->id, $first->transaction_currency_id); | ||||
|         $this->assertNull($first->foreign_amount); | ||||
|         $this->assertNull($first->foreign_currency_id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Create deposit using minimal data. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\TransactionFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait | ||||
|      */ | ||||
|     public function testCreatePairDepositCash() | ||||
|     { | ||||
|         // objects: | ||||
|         $asset   = $this->user()->accounts()->where('account_type_id', 3)->first(); | ||||
|         $revenue = $this->user()->accounts()->where('account_type_id', 5)->first(); | ||||
|         $euro    = TransactionCurrency::first(); | ||||
|         $foreign = TransactionCurrency::where('id', '!=', $euro->id)->first(); | ||||
|  | ||||
|         // mocked classes | ||||
|         $accountRepos    = $this->mock(AccountRepositoryInterface::class); | ||||
|         $budgetFactory   = $this->mock(BudgetFactory::class); | ||||
|         $categoryFactory = $this->mock(CategoryFactory::class); | ||||
|         $currencyFactory = $this->mock(TransactionCurrencyFactory::class); | ||||
|         $accountFactory  = $this->mock(AccountFactory::class); | ||||
|  | ||||
|         $data = [ | ||||
|             'currency_id'           => 1, | ||||
|             'currency_code'         => null, | ||||
|             'description'           => null, | ||||
|             'source_id'             => null, | ||||
|             'source_name'           => '', | ||||
|             'destination_id'        => $asset->id, | ||||
|             'destination_name'      => null, | ||||
|             'amount'                => '10', | ||||
|             'reconciled'            => false, | ||||
|             'identifier'            => 0, | ||||
|             'foreign_currency_id'   => null, | ||||
|             'foreign_currency_code' => null, | ||||
|             'foreign_amount'        => null, | ||||
|             'budget_id'             => null, | ||||
|             'budget_name'           => null, | ||||
|             'category_id'           => null, | ||||
|             'category_name'         => null, | ||||
|         ]; | ||||
|  | ||||
|         // mock: | ||||
|         $accountRepos->shouldReceive('setUser'); | ||||
|         $budgetFactory->shouldReceive('setUser'); | ||||
|         $categoryFactory->shouldReceive('setUser'); | ||||
|         $accountFactory->shouldReceive('setUser'); | ||||
|         // first search action is for the asset account | ||||
|         $accountRepos->shouldReceive('findNull')->andReturn($asset); | ||||
|  | ||||
|         // second is for revenue account. | ||||
|         $accountRepos->shouldReceive('getCashAccount')->andReturn($revenue)->once(); | ||||
|  | ||||
|         // factories return various stuff: | ||||
|         $budgetFactory->shouldReceive('find')->andReturn(null); | ||||
|         $categoryFactory->shouldReceive('findOrCreate')->andReturn(null); | ||||
|         $currencyFactory->shouldReceive('find')->andReturn($euro, null); | ||||
|  | ||||
|         /** @var TransactionJournal $deposit */ | ||||
|         $deposit = $this->user()->transactionJournals()->where('transaction_type_id', 2)->first(); | ||||
|         $count   = $deposit->transactions()->count(); | ||||
|  | ||||
|         /** @var TransactionFactory $factory */ | ||||
|         $factory = app(TransactionFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $collection = $factory->createPair($deposit, $data); | ||||
|  | ||||
|         $newCount = $deposit->transactions()->count(); | ||||
|  | ||||
|         $this->assertCount(2, $collection); | ||||
|         $this->assertEquals($count + 2, $newCount); | ||||
|         // find stuff in transaction #1 (should suffice): | ||||
|         /** @var Transaction $first */ | ||||
|         $first = $collection->first(); | ||||
|         $this->assertEquals($deposit->id, $first->transaction_journal_id); | ||||
|         $this->assertEquals(-10, $first->amount); | ||||
|         $this->assertEquals(false, $first->reconciled); | ||||
|         $this->assertEquals(0, $first->identifier); | ||||
|         $this->assertEquals($euro->id, $first->transaction_currency_id); | ||||
|         $this->assertNull($first->foreign_amount); | ||||
|         $this->assertNull($first->foreign_currency_id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait | ||||
|      */ | ||||
|     public function testCreatePairForeign() | ||||
|     { | ||||
|         // objects: | ||||
|         $asset   = $this->user()->accounts()->where('account_type_id', 3)->first(); | ||||
|         $expense = $this->user()->accounts()->where('account_type_id', 4)->first(); | ||||
|         $euro    = TransactionCurrency::first(); | ||||
|         $foreign = TransactionCurrency::where('id', '!=', $euro->id)->first(); | ||||
|  | ||||
|         // mocked classes | ||||
|         $accountRepos    = $this->mock(AccountRepositoryInterface::class); | ||||
|         $budgetFactory   = $this->mock(BudgetFactory::class); | ||||
|         $categoryFactory = $this->mock(CategoryFactory::class); | ||||
|         $currencyFactory = $this->mock(TransactionCurrencyFactory::class); | ||||
|  | ||||
|         $data = [ | ||||
|             'currency_id'           => $euro->id, | ||||
|             'currency_code'         => null, | ||||
|             'description'           => null, | ||||
|             'source_id'             => $asset->id, | ||||
|             'source_name'           => null, | ||||
|             'destination_id'        => $expense->id, | ||||
|             'destination_name'      => null, | ||||
|             'amount'                => '10', | ||||
|             'reconciled'            => false, | ||||
|             'identifier'            => 0, | ||||
|             'foreign_currency_id'   => $foreign->id, | ||||
|             'foreign_currency_code' => null, | ||||
|             'foreign_amount'        => '10', | ||||
|             'budget_id'             => null, | ||||
|             'budget_name'           => null, | ||||
|             'category_id'           => null, | ||||
|             'category_name'         => null, | ||||
|         ]; | ||||
|  | ||||
|         // mock: | ||||
|         $accountRepos->shouldReceive('setUser'); | ||||
|         $budgetFactory->shouldReceive('setUser'); | ||||
|         $categoryFactory->shouldReceive('setUser'); | ||||
|         // first search action is for the asset account, second is for expense account. | ||||
|         $accountRepos->shouldReceive('findNull')->andReturn($asset, $expense); | ||||
|  | ||||
|         // factories return various stuff: | ||||
|         $budgetFactory->shouldReceive('find')->andReturn(null); | ||||
|         $categoryFactory->shouldReceive('findOrCreate')->andReturn(null); | ||||
|         $currencyFactory->shouldReceive('find')->andReturn($euro, $foreign); | ||||
|  | ||||
|         /** @var TransactionJournal $withdrawal */ | ||||
|         $withdrawal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first(); | ||||
|         $count      = $withdrawal->transactions()->count(); | ||||
|  | ||||
|         /** @var TransactionFactory $factory */ | ||||
|         $factory = app(TransactionFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $collection = $factory->createPair($withdrawal, $data); | ||||
|  | ||||
|         $newCount = $withdrawal->transactions()->count(); | ||||
|  | ||||
|         $this->assertCount(2, $collection); | ||||
|         $this->assertEquals($count + 2, $newCount); | ||||
|         // find stuff in transaction #1 (should suffice): | ||||
|         /** @var Transaction $first */ | ||||
|         $first = $collection->first(); | ||||
|         $this->assertEquals($withdrawal->id, $first->transaction_journal_id); | ||||
|         $this->assertEquals(-10, $first->amount); | ||||
|         $this->assertEquals(-10, $first->foreign_amount); | ||||
|         $this->assertEquals(false, $first->reconciled); | ||||
|         $this->assertEquals(0, $first->identifier); | ||||
|         $this->assertEquals($euro->id, $first->transaction_currency_id); | ||||
|         $this->assertEquals($foreign->id, $first->foreign_currency_id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Create transfer using minimal data. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\TransactionFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait | ||||
|      */ | ||||
|     public function testCreatePairReconciliation() | ||||
|     { | ||||
|         // objects: | ||||
|         $asset    = $this->user()->accounts()->where('account_type_id', 3)->first(); | ||||
|         $opposing = $this->user()->accounts()->where('id', '!=', $asset->id)->where('account_type_id', 3)->first(); | ||||
|         $euro     = TransactionCurrency::first(); | ||||
|         $foreign  = TransactionCurrency::where('id', '!=', $euro->id)->first(); | ||||
|  | ||||
|         // mocked classes | ||||
|         $accountRepos    = $this->mock(AccountRepositoryInterface::class); | ||||
|         $budgetFactory   = $this->mock(BudgetFactory::class); | ||||
|         $categoryFactory = $this->mock(CategoryFactory::class); | ||||
|         $currencyFactory = $this->mock(TransactionCurrencyFactory::class); | ||||
|  | ||||
|         $data = [ | ||||
|             'currency_id'           => 1, | ||||
|             'currency_code'         => null, | ||||
|             'description'           => null, | ||||
|             'source_id'             => $asset->id, | ||||
|             'source_name'           => null, | ||||
|             'destination_id'        => $opposing->id, | ||||
|             'destination_name'      => null, | ||||
|             'amount'                => '10', | ||||
|             'reconciled'            => false, | ||||
|             'identifier'            => 0, | ||||
|             'foreign_currency_id'   => null, | ||||
|             'foreign_currency_code' => null, | ||||
|             'foreign_amount'        => null, | ||||
|             'budget_id'             => null, | ||||
|             'budget_name'           => null, | ||||
|             'category_id'           => null, | ||||
|             'category_name'         => null, | ||||
|         ]; | ||||
|  | ||||
|         // mock: | ||||
|         $accountRepos->shouldReceive('setUser'); | ||||
|         $budgetFactory->shouldReceive('setUser'); | ||||
|         $categoryFactory->shouldReceive('setUser'); | ||||
|         // first search action is for the asset account, second is for expense account. | ||||
|         $accountRepos->shouldReceive('findNull')->andReturn($asset, $opposing); | ||||
|  | ||||
|         // factories return various stuff: | ||||
|         $budgetFactory->shouldReceive('find')->andReturn(null); | ||||
|         $categoryFactory->shouldReceive('findOrCreate')->andReturn(null); | ||||
|         $currencyFactory->shouldReceive('find')->andReturn($euro, null); | ||||
|  | ||||
|         /** @var TransactionJournal $recon */ | ||||
|         $recon = $this->user()->transactionJournals()->where('transaction_type_id', 5)->first(); | ||||
|         $count = $recon->transactions()->count(); | ||||
|  | ||||
|         /** @var TransactionFactory $factory */ | ||||
|         $factory = app(TransactionFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $collection = $factory->createPair($recon, $data); | ||||
|  | ||||
|         $newCount = $recon->transactions()->count(); | ||||
|  | ||||
|         $this->assertCount(2, $collection); | ||||
|         $this->assertEquals($count + 2, $newCount); | ||||
|         // find stuff in transaction #1 (should suffice): | ||||
|         /** @var Transaction $first */ | ||||
|         $first = $collection->first(); | ||||
|         $this->assertEquals($recon->id, $first->transaction_journal_id); | ||||
|         $this->assertEquals(-10, $first->amount); | ||||
|         $this->assertEquals(false, $first->reconciled); | ||||
|         $this->assertEquals(0, $first->identifier); | ||||
|         $this->assertEquals($euro->id, $first->transaction_currency_id); | ||||
|         $this->assertNull($first->foreign_amount); | ||||
|         $this->assertNull($first->foreign_currency_id); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Create reconciliation using minimal (bad) data. | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\TransactionFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\TransactionServiceTrait | ||||
|      */ | ||||
|     public function testCreatePairTransfer() | ||||
|     { | ||||
|         // objects: | ||||
|         $asset    = $this->user()->accounts()->where('account_type_id', 3)->first(); | ||||
|         $opposing = $this->user()->accounts()->where('id', '!=', $asset->id)->where('account_type_id', 3)->first(); | ||||
|         $euro     = TransactionCurrency::first(); | ||||
|         $foreign  = TransactionCurrency::where('id', '!=', $euro->id)->first(); | ||||
|  | ||||
|         // mocked classes | ||||
|         $accountRepos    = $this->mock(AccountRepositoryInterface::class); | ||||
|         $budgetFactory   = $this->mock(BudgetFactory::class); | ||||
|         $categoryFactory = $this->mock(CategoryFactory::class); | ||||
|         $currencyFactory = $this->mock(TransactionCurrencyFactory::class); | ||||
|  | ||||
|         $data = [ | ||||
|             'currency_id'           => 1, | ||||
|             'currency_code'         => null, | ||||
|             'description'           => null, | ||||
|             'source_id'             => $asset->id, | ||||
|             'source_name'           => null, | ||||
|             'destination_id'        => $opposing->id, | ||||
|             'destination_name'      => null, | ||||
|             'amount'                => '10', | ||||
|             'reconciled'            => false, | ||||
|             'identifier'            => 0, | ||||
|             'foreign_currency_id'   => null, | ||||
|             'foreign_currency_code' => null, | ||||
|             'foreign_amount'        => null, | ||||
|             'budget_id'             => null, | ||||
|             'budget_name'           => null, | ||||
|             'category_id'           => null, | ||||
|             'category_name'         => null, | ||||
|         ]; | ||||
|  | ||||
|         // mock: | ||||
|         $accountRepos->shouldReceive('setUser'); | ||||
|         $budgetFactory->shouldReceive('setUser'); | ||||
|         $categoryFactory->shouldReceive('setUser'); | ||||
|         // first search action is for the asset account, second is for expense account. | ||||
|         $accountRepos->shouldReceive('findNull')->andReturn($asset, $opposing); | ||||
|  | ||||
|         // factories return various stuff: | ||||
|         $budgetFactory->shouldReceive('find')->andReturn(null); | ||||
|         $categoryFactory->shouldReceive('findOrCreate')->andReturn(null); | ||||
|         $currencyFactory->shouldReceive('find')->andReturn($euro, null); | ||||
|  | ||||
|         /** @var TransactionJournal $transfer */ | ||||
|         $transfer = $this->user()->transactionJournals()->where('transaction_type_id', 3)->first(); | ||||
|         $count    = $transfer->transactions()->count(); | ||||
|  | ||||
|         /** @var TransactionFactory $factory */ | ||||
|         $factory = app(TransactionFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         $collection = $factory->createPair($transfer, $data); | ||||
|  | ||||
|         $newCount = $transfer->transactions()->count(); | ||||
|  | ||||
|         $this->assertCount(2, $collection); | ||||
|         $this->assertEquals($count + 2, $newCount); | ||||
|         // find stuff in transaction #1 (should suffice): | ||||
|         /** @var Transaction $first */ | ||||
|         $first = $collection->first(); | ||||
|         $this->assertEquals($transfer->id, $first->transaction_journal_id); | ||||
|         $this->assertEquals(-10, $first->amount); | ||||
|         $this->assertEquals(false, $first->reconciled); | ||||
|         $this->assertEquals(0, $first->identifier); | ||||
|         $this->assertEquals($euro->id, $first->transaction_currency_id); | ||||
|         $this->assertNull($first->foreign_amount); | ||||
|         $this->assertNull($first->foreign_currency_id); | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										177
									
								
								tests/Unit/Factory/TransactionJournalFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										177
									
								
								tests/Unit/Factory/TransactionJournalFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,177 @@ | ||||
| <?php | ||||
| /** | ||||
|  * TransactionJournalFactoryTest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Tests\Unit\Factory; | ||||
|  | ||||
|  | ||||
| use Carbon\Carbon; | ||||
| use FireflyIII\Exceptions\FireflyException; | ||||
| use FireflyIII\Factory\BillFactory; | ||||
| use FireflyIII\Factory\PiggyBankEventFactory; | ||||
| use FireflyIII\Factory\PiggyBankFactory; | ||||
| use FireflyIII\Factory\TagFactory; | ||||
| use FireflyIII\Factory\TransactionFactory; | ||||
| use FireflyIII\Factory\TransactionJournalFactory; | ||||
| use FireflyIII\Factory\TransactionJournalMetaFactory; | ||||
| use FireflyIII\Factory\TransactionTypeFactory; | ||||
| use FireflyIII\Models\TransactionCurrency; | ||||
| use FireflyIII\Models\TransactionType; | ||||
| use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; | ||||
| use Tests\TestCase; | ||||
|  | ||||
| /** | ||||
|  * Class TransactionJournalFactoryTest | ||||
|  */ | ||||
| class TransactionJournalFactoryTest extends TestCase | ||||
| { | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionJournalFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasic() | ||||
|     { | ||||
|         // mock used classes: | ||||
|         $type               = TransactionType::find(1); | ||||
|         $euro               = TransactionCurrency::find(1); | ||||
|         $billFactory        = $this->mock(BillFactory::class); | ||||
|         $tagFactory         = $this->mock(TagFactory::class); | ||||
|         $metaFactory        = $this->mock(TransactionJournalMetaFactory::class); | ||||
|         $typeFactory        = $this->mock(TransactionTypeFactory::class); | ||||
|         $transactionFactory = $this->mock(TransactionFactory::class); | ||||
|         $piggyFactory       = $this->mock(PiggyBankFactory::class); | ||||
|         $eventFactory       = $this->mock(PiggyBankEventFactory::class); | ||||
|         $currencyRepos      = $this->mock(CurrencyRepositoryInterface::class); | ||||
|  | ||||
|         // mock stuff: | ||||
|         $typeFactory->shouldReceive('find')->andReturn($type); | ||||
|         $currencyRepos->shouldReceive('find')->andReturn($euro); | ||||
|  | ||||
|         // mock factories: | ||||
|         $transactionFactory->shouldReceive('setUser')->once(); | ||||
|         $billFactory->shouldReceive('setUser')->once(); | ||||
|         $piggyFactory->shouldReceive('setUser')->once(); | ||||
|         $tagFactory->shouldReceive('setUser')->once(); | ||||
|  | ||||
|         $transactionFactory->shouldReceive('createPair')->once(); | ||||
|         $billFactory->shouldReceive('find')->andReturn(null); | ||||
|         $piggyFactory->shouldReceive('find')->andReturn(null); | ||||
|         $data = [ | ||||
|             'type'            => 'withdrawal', | ||||
|             'user'            => $this->user()->id, | ||||
|             'description'     => 'I are journal', | ||||
|             'date'            => new Carbon('2018-01-01'), | ||||
|             'bill_id'         => null, | ||||
|             'bill_name'       => null, | ||||
|             'piggy_bank_id'   => null, | ||||
|             'piggy_bank_name' => null, | ||||
|             'notes'           => 'Hello', | ||||
|             'tags'            => [], | ||||
|             'transactions'    => [[]], | ||||
|         ]; | ||||
|  | ||||
|         /** @var TransactionJournalFactory $factory */ | ||||
|         $factory = app(TransactionJournalFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         try { | ||||
|             $journal = $factory->create($data); | ||||
|         } catch (FireflyException $e) { | ||||
|             $this->assertTrue(false, $e->getMessage()); | ||||
|         } | ||||
|         $this->assertEquals($data['description'], $journal->description); | ||||
|         $this->assertEquals('2018-01-01', $journal->date->format('Y-m-d')); | ||||
|         $this->assertEquals(1, $journal->notes()->count()); | ||||
|  | ||||
|     } | ||||
|  | ||||
|  | ||||
|     /** | ||||
|      * Same but with added meta data | ||||
|      * | ||||
|      * @covers \FireflyIII\Factory\TransactionJournalFactory | ||||
|      * @covers \FireflyIII\Services\Internal\Support\JournalServiceTrait | ||||
|      */ | ||||
|     public function testCreateBasicMeta() | ||||
|     { | ||||
|         // mock used classes: | ||||
|         $type               = TransactionType::find(1); | ||||
|         $euro               = TransactionCurrency::find(1); | ||||
|         $piggy              = $this->user()->piggyBanks()->first(); | ||||
|         $bill               = $this->user()->bills()->first(); | ||||
|         $tag                = $this->user()->tags()->first(); | ||||
|         $billFactory        = $this->mock(BillFactory::class); | ||||
|         $tagFactory         = $this->mock(TagFactory::class); | ||||
|         $metaFactory        = $this->mock(TransactionJournalMetaFactory::class); | ||||
|         $typeFactory        = $this->mock(TransactionTypeFactory::class); | ||||
|         $transactionFactory = $this->mock(TransactionFactory::class); | ||||
|         $piggyFactory       = $this->mock(PiggyBankFactory::class); | ||||
|         $eventFactory       = $this->mock(PiggyBankEventFactory::class); | ||||
|         $currencyRepos      = $this->mock(CurrencyRepositoryInterface::class); | ||||
|  | ||||
|         // mock stuff: | ||||
|         $typeFactory->shouldReceive('find')->andReturn($type); | ||||
|         $currencyRepos->shouldReceive('find')->andReturn($euro); | ||||
|  | ||||
|         // mock factories: | ||||
|         $transactionFactory->shouldReceive('setUser')->once(); | ||||
|         $billFactory->shouldReceive('setUser')->once(); | ||||
|         $piggyFactory->shouldReceive('setUser')->once(); | ||||
|         $tagFactory->shouldReceive('setUser')->once(); | ||||
|  | ||||
|         $transactionFactory->shouldReceive('createPair')->once(); | ||||
|         $billFactory->shouldReceive('find')->andReturn($bill); | ||||
|         $piggyFactory->shouldReceive('find')->andReturn($piggy); | ||||
|         $eventFactory->shouldReceive('create')->once(); | ||||
|         $tagFactory->shouldReceive('findOrCreate')->andReturn($tag); | ||||
|         $metaFactory->shouldReceive('updateOrCreate'); | ||||
|  | ||||
|         $data = [ | ||||
|             'type'            => 'withdrawal', | ||||
|             'user'            => $this->user()->id, | ||||
|             'description'     => 'I are journal', | ||||
|             'date'            => new Carbon('2018-01-01'), | ||||
|             'bill_id'         => $bill->id, | ||||
|             'bill_name'       => null, | ||||
|             'piggy_bank_id'   => $piggy->id, | ||||
|             'piggy_bank_name' => null, | ||||
|             'notes'           => '', | ||||
|             'tags'            => ['a', 'b', 'c'], | ||||
|             'transactions'    => [[]], | ||||
|             'interest_date'   => '2018-01-01', | ||||
|         ]; | ||||
|  | ||||
|         /** @var TransactionJournalFactory $factory */ | ||||
|         $factory = app(TransactionJournalFactory::class); | ||||
|         $factory->setUser($this->user()); | ||||
|         try { | ||||
|             $journal = $factory->create($data); | ||||
|         } catch (FireflyException $e) { | ||||
|             $this->assertTrue(false, $e->getMessage()); | ||||
|         } | ||||
|         $this->assertEquals($data['description'], $journal->description); | ||||
|         $this->assertEquals('2018-01-01', $journal->date->format('Y-m-d')); | ||||
|         $this->assertEquals(0, $journal->notes()->count()); | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
							
								
								
									
										105
									
								
								tests/Unit/Factory/TransactionJournalMetaFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										105
									
								
								tests/Unit/Factory/TransactionJournalMetaFactoryTest.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,105 @@ | ||||
| <?php | ||||
| /** | ||||
|  * TransactionJournalMetaFactoryTest.php | ||||
|  * Copyright (c) 2018 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This file is part of Firefly III. | ||||
|  * | ||||
|  * Firefly III is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU General Public License as published by | ||||
|  * the Free Software Foundation, either version 3 of the License, or | ||||
|  * (at your option) any later version. | ||||
|  * | ||||
|  * Firefly III is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU General Public License | ||||
|  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Tests\Unit\Factory; | ||||
|  | ||||
| use Carbon\Carbon; | ||||
| use FireflyIII\Factory\TransactionJournalMetaFactory; | ||||
| use FireflyIII\Models\TransactionJournal; | ||||
| use FireflyIII\Models\TransactionJournalMeta; | ||||
| use Tests\TestCase; | ||||
|  | ||||
| /** | ||||
|  * Class TransactionJournalMetaFactoryTest | ||||
|  */ | ||||
| class TransactionJournalMetaFactoryTest extends TestCase | ||||
| { | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionJournalMetaFactory | ||||
|      */ | ||||
|     public function testUpdateOrCreateBasic() | ||||
|     { | ||||
|         /** @var TransactionJournal $journal */ | ||||
|         $journal = $this->user()->transactionJournals()->where('transaction_type_id', 1)->first(); | ||||
|         $set     = [ | ||||
|             'journal' => $journal, | ||||
|             'name'    => 'hello', | ||||
|             'data'    => 'bye!', | ||||
|         ]; | ||||
|         /** @var TransactionJournalMetaFactory $factory */ | ||||
|         $factory = app(TransactionJournalMetaFactory::class); | ||||
|         $result  = $factory->updateOrCreate($set); | ||||
|  | ||||
|         $this->assertEquals(1, $journal->transactionJournalMeta()->count()); | ||||
|         $this->assertEquals($set['data'], $result->data); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionJournalMetaFactory | ||||
|      */ | ||||
|     public function testUpdateOrCreateDate() | ||||
|     { | ||||
|         /** @var TransactionJournal $journal */ | ||||
|         $journal = $this->user()->transactionJournals()->where('transaction_type_id', 2)->first(); | ||||
|         $set     = [ | ||||
|             'journal' => $journal, | ||||
|             'name'    => 'hello', | ||||
|             'data'    => new Carbon('2012-01-01'), | ||||
|         ]; | ||||
|         /** @var TransactionJournalMetaFactory $factory */ | ||||
|         $factory = app(TransactionJournalMetaFactory::class); | ||||
|         $result  = $factory->updateOrCreate($set); | ||||
|  | ||||
|         $this->assertEquals(1, $journal->transactionJournalMeta()->count()); | ||||
|         $this->assertEquals($set['data']->toW3cString(), $result->data); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @covers \FireflyIII\Factory\TransactionJournalMetaFactory | ||||
|      */ | ||||
|     public function testUpdateOrCreateDeleteExisting() | ||||
|     { | ||||
|         /** @var TransactionJournal $journal */ | ||||
|         $journal = $this->user()->transactionJournals()->where('transaction_type_id', 3)->first(); | ||||
|         $meta    = TransactionJournalMeta::create( | ||||
|             [ | ||||
|                 'transaction_journal_id' => $journal->id, | ||||
|                 'name'                   => 'hello', | ||||
|                 'data'                   => 'bye!', | ||||
|             ] | ||||
|         ); | ||||
|  | ||||
|         $set = [ | ||||
|             'journal' => $journal, | ||||
|             'name'    => 'hello', | ||||
|             'data'    => null, | ||||
|         ]; | ||||
|         /** @var TransactionJournalMetaFactory $factory */ | ||||
|         $factory = app(TransactionJournalMetaFactory::class); | ||||
|         $factory->updateOrCreate($set); | ||||
|  | ||||
|         $this->assertEquals(0, $journal->transactionJournalMeta()->count()); | ||||
|     } | ||||
|  | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user