First code for liabilities and some tests.

This commit is contained in:
James Cole
2018-08-03 16:35:55 +02:00
parent ae85876965
commit 2290fcde22
10 changed files with 282 additions and 54 deletions

View File

@@ -148,6 +148,38 @@ class AvailableBudgetControllerTest extends TestCase
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* Store new available budget but the budget currency is invalid.
*
* @covers \FireflyIII\Api\V1\Controllers\AvailableBudgetController
* @covers \FireflyIII\Api\V1\Requests\AvailableBudgetRequest
*/
public function testStoreInvalidCurrency(): void
{
// mock stuff:
$repository = $this->mock(BudgetRepositoryInterface::class);
$currencyRepository = $this->mock(CurrencyRepositoryInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$currencyRepository->shouldReceive('findNull')->andReturn(null);
// data to submit
$data = [
'transaction_currency_id' => '1',
'amount' => '100',
'start_date' => '2018-01-01',
'end_date' => '2018-01-31',
];
// test API
$response = $this->post('/api/v1/available_budgets', $data,['Accept' => 'application/json']);
$response->assertStatus(500);
$response->assertSee('Could not find the indicated currency.');
$response->assertHeader('Content-Type', 'application/json');
}
/**
* Update available budget.
*

View File

@@ -109,7 +109,7 @@ class ConfigurationControllerTest extends TestCase
*/
public function testUpdate(): void
{
$data = [
$data = [
'name' => 'permission_update_check',
'value' => 1,
@@ -135,7 +135,57 @@ class ConfigurationControllerTest extends TestCase
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check'])->andReturn($permConfig)->once();
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check'])->andReturn($lastConfig)->once();
FireflyConfig::shouldReceive('get')->withArgs(['single_user_mode'])->andReturn($singleConfig)->once();
FireflyConfig::shouldReceive('set')->once()->withArgs(['permission_update_check',1]);
FireflyConfig::shouldReceive('set')->once()->withArgs(['permission_update_check', 1]);
$expected = [
'data' => [
'is_demo_site' => false,
'permission_update_check' => -1,
'last_update_check' => 123456789,
'single_user_mode' => true,
],
];
$response = $this->post('/api/v1/configuration', $data);
$response->assertStatus(200);
$response->assertExactJson($expected);
}
/**
* Set configuration variables.
*
* @covers \FireflyIII\Api\V1\Controllers\ConfigurationController
*/
public function testUpdateBoolean(): void
{
$data = [
'name' => 'single_user_mode',
'value' => 'true',
];
$demoConfig = new Configuration;
$demoConfig->name = 'is_demo_site';
$demoConfig->data = false;
$permConfig = new Configuration;
$permConfig->name = 'permission_update_check';
$permConfig->data = -1;
$lastConfig = new Configuration;
$lastConfig->name = 'last_update_check';
$lastConfig->data = 123456789;
$singleConfig = new Configuration;
$singleConfig->name = 'single_user_mode';
$singleConfig->data = true;
FireflyConfig::shouldReceive('get')->withArgs(['is_demo_site'])->andReturn($demoConfig)->once();
FireflyConfig::shouldReceive('get')->withArgs(['permission_update_check'])->andReturn($permConfig)->once();
FireflyConfig::shouldReceive('get')->withArgs(['last_update_check'])->andReturn($lastConfig)->once();
FireflyConfig::shouldReceive('get')->withArgs(['single_user_mode'])->andReturn($singleConfig)->once();
FireflyConfig::shouldReceive('set')->once()->withArgs(['single_user_mode', true]);
$expected = [

View File

@@ -102,4 +102,72 @@ class CurrencyExchangeRateControllerTest extends TestCase
);
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyExchangeRateController
*/
public function testIndexBadSource(): void
{
// mock repository
$repository = $this->mock(CurrencyRepositoryInterface::class);
$service = $this->mock(ExchangeRateInterface::class);
$rate = new CurrencyExchangeRate();
$rate->date = new Carbon();
$rate->updated_at = new Carbon();
$rate->created_at = new Carbon();
$rate->rate = '0.5';
$rate->to_currency_id = 1;
$rate->from_currency_id = 2;
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn(null)->once();
$repository->shouldReceive('findByCodeNull')->withArgs(['USD'])->andReturn(TransactionCurrency::whereCode('USD')->first())->once();
// test API
$params = [
'from' => 'EUR',
'to' => 'USD',
'date' => '2018-01-01',
];
$response = $this->get('/api/v1/cer?' . http_build_query($params), ['Accept' => 'application/json']);
$response->assertStatus(500);
$response->assertSee('Unknown source currency.');
$response->assertHeader('Content-Type', 'application/json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\CurrencyExchangeRateController
*/
public function testIndexBadDestination(): void
{
// mock repository
$repository = $this->mock(CurrencyRepositoryInterface::class);
$service = $this->mock(ExchangeRateInterface::class);
$rate = new CurrencyExchangeRate();
$rate->date = new Carbon();
$rate->updated_at = new Carbon();
$rate->created_at = new Carbon();
$rate->rate = '0.5';
$rate->to_currency_id = 1;
$rate->from_currency_id = 2;
// mock calls:
$repository->shouldReceive('setUser')->once();
$repository->shouldReceive('findByCodeNull')->withArgs(['EUR'])->andReturn(TransactionCurrency::whereCode('USD')->first())->once();
$repository->shouldReceive('findByCodeNull')->withArgs(['USD'])->andReturn(null)->once();
// test API
$params = [
'from' => 'EUR',
'to' => 'USD',
'date' => '2018-01-01',
];
$response = $this->get('/api/v1/cer?' . http_build_query($params), ['Accept' => 'application/json']);
$response->assertStatus(500);
$response->assertSee('Unknown destination currency.');
$response->assertHeader('Content-Type', 'application/json');
}
}

View File

@@ -188,6 +188,53 @@ class JournalLinkControllerTest extends TestCase
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\JournalLinkController
* @covers \FireflyIII\Api\V1\Requests\JournalLinkRequest
*/
public function testStoreWithNull(): void
{
$journalLink = TransactionJournalLink::first();
$journal = $this->user()->transactionJournals()->find(1);
$transaction = Transaction::first();
$transaction->date = new Carbon;
$transaction->transaction_type_type = 'Withdrawal';
// mock stuff:
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
// mock calls:
$repository->shouldReceive('setUser')->once();
$journalRepos->shouldReceive('setUser')->once();
$collector->shouldReceive('setUser')->withAnyArgs();
$collector->shouldReceive('setUser')->withAnyArgs();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('setJournals')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
$journalRepos->shouldReceive('findNull')->andReturn(null);
// data to submit
$data = [
'link_type_id' => '1',
'inward_id' => '1',
'outward_id' => '2',
'notes' => 'Some notes',
];
// test API
$response = $this->post('/api/v1/journal_links', $data, ['Accept' => 'application/json']);
$response->assertStatus(500);
$response->assertSee('Source or destination is NULL.'); // error message
$response->assertHeader('Content-Type', 'application/json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\JournalLinkController
* @covers \FireflyIII\Api\V1\Requests\JournalLinkRequest
@@ -236,4 +283,53 @@ class JournalLinkControllerTest extends TestCase
$response->assertSee($journalLink->created_at->toAtomString()); // the creation moment.
$response->assertHeader('Content-Type', 'application/vnd.api+json');
}
/**
* @covers \FireflyIII\Api\V1\Controllers\JournalLinkController
* @covers \FireflyIII\Api\V1\Requests\JournalLinkRequest
*/
public function testUpdateWithNull(): void
{
// mock repositories
$repository = $this->mock(LinkTypeRepositoryInterface::class);
$journalRepos = $this->mock(JournalRepositoryInterface::class);
$collector = $this->mock(JournalCollectorInterface::class);
$journalLink = TransactionJournalLink::first();
$journal = $this->user()->transactionJournals()->find(1);
$transaction = Transaction::first();
$transaction->date = new Carbon;
$transaction->transaction_type_type = 'Withdrawal';
// mock calls:
$repository->shouldReceive('setUser');
$journalRepos->shouldReceive('setUser')->once();
$collector->shouldReceive('setUser')->withAnyArgs();
$collector->shouldReceive('setUser')->withAnyArgs();
$collector->shouldReceive('withOpposingAccount')->andReturnSelf();
$collector->shouldReceive('withCategoryInformation')->andReturnSelf();
$collector->shouldReceive('withBudgetInformation')->andReturnSelf();
$collector->shouldReceive('setJournals')->andReturnSelf();
$collector->shouldReceive('getJournals')->andReturn(new Collection([$transaction]));
$journalRepos->shouldReceive('findNull')->andReturn(null);
$repository->shouldReceive('updateLink')->once()->andReturn($journalLink);
// data to submit
$data = [
'link_type_id' => '1',
'inward_id' => '1',
'outward_id' => '2',
'notes' => 'Some notes',
];
// test API
$response = $this->put('/api/v1/journal_links/' . $journalLink->id, $data, ['Accept' => 'application/json']);
$response->assertStatus(500);
$response->assertSee('Source or destination is NULL.'); // the creation moment.
$response->assertHeader('Content-Type', 'application/json');
}
}