. */ declare(strict_types=1); namespace Tests\Unit\Import\Object; use FireflyIII\Import\Object\ImportAccount; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use FireflyIII\Repositories\Account\AccountRepositoryInterface; use Illuminate\Support\Collection; use Mockery; use Tests\TestCase; /** * Class ImportAccountTest */ class ImportAccountTest extends TestCase { /** * Should error because it requires a default asset account. * * @covers \FireflyIII\Import\Object\ImportAccount */ public function testBasic() { // mock stuff $repository = $this->mock(AccountRepositoryInterface::class); $accountType = AccountType::where('type', AccountType::ASSET)->first(); $account = Account::find(1); // mock calls: $repository->shouldReceive('setUser')->once()->withArgs([Mockery::any()]); $repository->shouldReceive('getAccountType')->twice()->withArgs([AccountType::ASSET])->andReturn($accountType); $repository->shouldReceive('getAccountsByType')->twice()->withArgs([[AccountType::ASSET]])->andReturn(new Collection()); $repository->shouldReceive('find')->once()->withArgs([1])->andReturn($account); // create import account. $importAccount = new ImportAccount; $importAccount->setUser($this->user()); $importAccount->setDefaultAccountId(1); $found = $importAccount->getAccount(); $this->assertEquals(1, $found->id); } /** * Should error because it requires a default asset account. * * @covers \FireflyIII\Import\Object\ImportAccount */ public function testEmptyMappingAccountId() { // mock stuff $repository = $this->mock(AccountRepositoryInterface::class); $accountType = AccountType::where('type', AccountType::ASSET)->first(); $account = Account::find(1); // mock calls: $repository->shouldReceive('setUser')->once()->withArgs([Mockery::any()]); $repository->shouldReceive('getAccountType')->once()->withArgs([AccountType::ASSET])->andReturn($accountType); //$repository->shouldReceive('getAccountsByType')->once()->withArgs([[AccountType::ASSET]])->andReturn(new Collection()); //$repository->shouldReceive('find')->once()->withArgs([1])->andReturn($account); // create import account. $importAccount = new ImportAccount; $importAccount->setUser($this->user()); $importAccount->setDefaultAccountId(1); // add an account id: $accountId = [ 'role' => 'account-id', 'mapped' => null, 'value' => 2, ]; $importAccount->setAccountId($accountId); $found = $importAccount->getAccount(); $this->assertEquals(2, $found->id); } /** * @covers \FireflyIII\Import\Object\ImportAccount * @expectedException \FireflyIII\Exceptions\FireflyException * @expectedExceptionMessage ImportAccount cannot continue without a default account to fall back on. */ public function testNoAccount() { $repository = $this->mock(AccountRepositoryInterface::class); $repository->shouldReceive('setUser')->once()->withArgs([Mockery::any()]); $importAccount = new ImportAccount; $importAccount->setUser($this->user()); $importAccount->getAccount(); } /** * @covers \FireflyIII\Import\Object\ImportAccount * @expectedException \FireflyIII\Exceptions\FireflyException * @expectedExceptionMessage ImportAccount cannot continue without user. */ public function testNoUser() { $this->mock(AccountRepositoryInterface::class); $importAccount = new ImportAccount; $importAccount->getAccount(); } }